#define BLYNK_TEMPLATE_ID "TMPL3uigIMDW4"
#define BLYNK_TEMPLATE_NAME "iot mini project"
#define BLYNK_AUTH_TOKEN "sWRbJ3JejzBZYSFTcKMGz3saTBlH9yDT"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <string.h>
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// DHT define
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define Buzzer 33
#define DHTPIN 4 // DHT sensor data pin
#define DHTTYPE DHT22 // DHT sensor type (DHT11 or DHT22)
DHT dht(DHTPIN, DHTTYPE);
#define RED_LED 2 // Red LED connected to GPIO 2
#define GREEN_LED 15 // Green LED connected to GPIO 15
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(115200);
dht.begin();
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(WHITE);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(Buzzer, OUTPUT);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Blynk.begin(auth, ssid, pass);
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%, Temperature: ");
Serial.print(temperature);
Serial.println("°C");
if (humidity > 60 && temperature > 25) {
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Temperature:");
oled.println(temperature);
oled.print("\n");
oled.print("Humidity:");
oled.println(humidity);
oled.print("\n");
oled.println("Status: High");
oled.display();
tone(Buzzer, 100, 1000);
}
else {
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Temperature:");
oled.println(temperature);
oled.print("\n");
oled.print("Humidity:");
oled.println(humidity);
oled.print("\n");
oled.println("Status: Normal");
oled.display();
}
Blynk.virtualWrite(V1, humidity);
Blynk.virtualWrite(V2, temperature);
delay(3000);
}