#define BLYNK_PRINT Serial
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
char auth[] = "xxxxxxxxxxxxxxxxxxx"; // replace the xxx with your authentication key
char ssid[] = "xxxxx"; // replace xxx with your wifi ssid
char pass[] = "xxxxx"; // replace xxx with your wifi password
#define DHTPIN 2 // connect to pin GPIO 2
#define RELAY_PIN 14 // connect to the pin connected to the relay module (e.g., GPIO 14)
#define LED_PIN 13 // connect to the pin connected to the LED
// I2C LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2);
// uncomment the type of sensor you use
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// Define temperature thresholds and hysteresis
float temperatureThresholdActivate = 25.0;
float temperatureThresholdDeactivate = 22.0;
float hysteresis = 1.0;
void sendSensor() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V0, t); // Virtual pin 0
Blynk.virtualWrite(V1, h); // Virtual pin 1
// Control the simulated exhaust fan (LED) based on temperature thresholds
if (t > temperatureThresholdActivate) {
digitalWrite(RELAY_PIN, HIGH); // Activate the simulated fan (LED)
Serial.println("Exhaust fan activated.");
} else if (t < temperatureThresholdDeactivate - hysteresis) {
digitalWrite(RELAY_PIN, LOW); // Deactivate the simulated fan (LED)
Serial.println("Exhaust fan deactivated.");
}
// Display temperature and humidity on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print(" %");
}
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
dht.begin();
// Initialize LCD
lcd.begin(16, 2);
pinMode(RELAY_PIN, OUTPUT); // Set the relay pin as an output
pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
timer.setInterval(10000L, sendSensor); // Setup a function to be called every 10 seconds
}
void loop() {
Blynk.run();
timer.run();
}