#include "DHTesp.h"
#define BLYNK_TEMPLATE_ID "TMPL3w0JRYaGj"
#define BLYNK_TEMPLATE_NAME "Home Automation"
#define BLYNK_AUTH_TOKEN "rPHXOBWYaA0Agrs8yNvpTKiVd94ykDAf"
#include <Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define DHTPIN 2 // Define the GPIO pin connected to the DHT22 sensor.
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Servo myservo;
int servoPin = 14; // The GPIO pin connected to the servo.
int motorPin = 15; // The GPIO pin connected to the motor.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
void setup() {
Blynk.begin(ssid, pass);
dht.begin();
myservo.attach(servoPin);
pinMode(motorPin, OUTPUT);
}
void loop() {
Blynk.run();
delay(2000); // Wait a few seconds between measurements.
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Blynk.virtualWrite(V3, temperature); // Send temperature data to virtual pin V1
Blynk.virtualWrite(V4, humidity); // Send humidity data to virtual pin V2
// Control the servo based on temperature
if (temperature > 25) {
myservo.write(90); // Rotate the servo to 90 degrees.
} else {
myservo.write(0); // Rotate the servo to 0 degrees.
}
// Control the DC motor based on humidity
if (humidity > 60) {
digitalWrite(motorPin, HIGH); // Turn on the motor.
} else {
digitalWrite(motorPin, LOW); // Turn off the motor.
}
}