#include <DHTesp.h>
#define BLYNK_TEMPLATE_ID "TMPL6wWb6IXN8"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "Bb8VhNhD9dpKcisPsMHwwy-_2tkjOaOm"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
const int DHTPIN = 15; // Pin where you connect the output of the DHT22 sensor
DHTesp dhtSensor;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD address and number of columns and rows
#define RELAY_PIN_2 25 // Pin for Relay 2
bool fanStatus = false; // variable to store the fan status
char ssid[] = "Wokwi-GUEST"; // replace xxx with your wifi ssid
char pass[] = ""; // replace xxx with your wifi password
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHTPIN, DHTesp::DHT22);
lcd.init(); // Initialize LCD
lcd.backlight();
pinMode(RELAY_PIN_2, OUTPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(V2); // Request the current state of the button (V2) from the server
}
BLYNK_WRITE(V2) {
int buttonState = param.asInt();
fanStatus = buttonState;
if (fanStatus == 1) {
controlRelays(0);
}
}
BLYNK_WRITE(V3) {
Serial.println("Historical Graph Updated");
}
void loop() {
Blynk.run();
delay(2000); // Delay before reading the sensor
float temperature = dhtSensor.getTemperature(); // Read temperature from the DHT22 sensor
float humidity = dhtSensor.getHumidity(); // Read humidity from the DHT22 sensor
if (!isnan(temperature) && !isnan(humidity)) {
Blynk.virtualWrite(V0, temperature);
Blynk.virtualWrite(V1, humidity);
Blynk.virtualWrite(V3, temperature);
Blynk.virtualWrite(V3, humidity);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C | Humidity: ");
Serial.print(humidity);
Serial.print(" % | Fan Status: ");
displayTemperature(temperature, humidity);
controlRelays(temperature);
}
delay(2000); // Delay before reading the sensor again
}
void displayTemperature(float temperature, float humidity) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humid: ");
lcd.print(humidity);
lcd.print(" %");
delay(2000);
}
void controlRelays(float temperature) {
if (temperature >= 25.0 || fanStatus) {
lcd.clear();
digitalWrite(RELAY_PIN_2, HIGH);
lcd.setCursor(0, 1);
lcd.print("FAN ON");
Blynk.virtualWrite(V2, 1);
Serial.println("ON");
} else {
digitalWrite(RELAY_PIN_2, LOW);
delay(3000);
Blynk.virtualWrite(V2, 0);
Serial.println("OFF");
}
}