#define BLYNK_TEMPLATE_ID "TMPL6wxNsNN3g"
#define BLYNK_TEMPLATE_NAME "Smart Environmental Monitoring System"
#define BLYNK_AUTH_TOKEN "O8aX87BJkrqedwpNBaXLbw5tqeqEZjXM"
#include <DHT_U.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define LED 4
#define DHTPIN 17
#define SERVOPIN 21
#define PIRPIN 16
#define BUTTONPIN 15
int buttonState = 0;
int switchState = 0;
int ledStateBlynk = 0;
#define DHTTYPE DHT22 // Change this to the appropriate DHT sensor type
DHT_Unified dht(DHTPIN, DHTTYPE);
Servo servo;
BlynkTimer timer;
void setup() {
pinMode(LED, OUTPUT);
pinMode(PIRPIN, INPUT);
pinMode(BUTTONPIN, INPUT_PULLUP);
Serial.begin(9600);
dht.begin();
servo.attach(SERVOPIN, 500, 2400);
servo.write(0);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
buttonState = digitalRead(BUTTONPIN);
sensors_event_t event;
dht.temperature().getEvent(&event);
float temperature = event.temperature;
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
dht.humidity().getEvent(&event);
if (!isnan(event.relative_humidity)) {
float humidity = event.relative_humidity;
Serial.print(humidity);
Serial.println(" %");
// Send temperature and humidity values to Blynk gauges
Blynk.virtualWrite(V3, temperature);
Blynk.virtualWrite(V4, humidity);
if (switchState == HIGH && temperature > 30.0) {
Serial.println("Switch turned on and temperature over 30!");
servo.write(180);
} else {
servo.write(0);
}
const int PIRState = digitalRead(PIRPIN);
Serial.println(PIRState);
if (PIRState == 1) {
digitalWrite(LED, HIGH);
ledStateBlynk = 1;
} else {
digitalWrite(LED, LOW);
ledStateBlynk = 0;
}
Blynk.virtualWrite(V1, PIRState);
Blynk.virtualWrite(V2, ledStateBlynk);
Blynk.virtualWrite(V4, humidity);
} else {
Serial.println("Failed to read humidity from DHT sensor");
}
delay(1000);
}
BLYNK_WRITE(V0) {
switchState = param.asInt();
}
BLYNK_WRITE(V1) {
int ledState = param.asInt();
digitalWrite(LED, ledState);
}