#define BLYNK_TEMPLATE_ID "TMPL6vqvArQQZ"
#define BLYNK_TEMPLATE_NAME "test2"
#define BLYNK_AUTH_TOKEN "Q7mnaHCFxp3-kqHuVXbu33jdDuXuEgwv"
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// #include <ESP32Servo.h>
// Define pins and constants
#define DHTPIN 4
#define SERVO_PIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo;
char auth[] = "Q7mnaHCFxp3-kqHuVXbu33jdDuXuEgwv";
char ssid[] = "YourWiFiSSID";
char pass[] = "YourWiFiPassword";
void setup() {
Serial.begin(9600);
dht.begin();
lcd.begin();
lcd.backlight();
servo.attach(SERVO_PIN);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
if (temp > 27 || humidity > 30) {
servo.write(90); // Move servo to 90 degrees (turn on fan)
} else {
servo.write(0); // Move servo to 0 degrees (turn off fan)
}
}
// Blynk button widget to control the servo manually
BLYNK_WRITE(V1) {
int servoState = param.asInt();
if (servoState == 1) {
servo.write(90); // Move servo to 90 degrees
} else {
servo.write(0); // Move servo to 0 degrees
}
}