#define BLYNK_TEMPLATE_ID "TMPL2jVKlarg0"
#define BLYNK_TEMPLATE_NAME "iot assignment"
#define BLYNK_AUTH_TOKEN "Rgm4tYJsshqBLS5v33i9caChWtC50kPf"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define DHT_PIN 15
#define LDR_PIN 36
#define LIGHT_PIN 5
DHTesp dht;
bool enableTempRead = true;
BlynkTimer timer;
void sendTemperature() {
if (enableTempRead) {
float temperature = dht.getTemperature();
if (isnan(temperature)) return;
Blynk.virtualWrite(V0, temperature);
if (temperature < 10 || temperature > 50) {
Blynk.notify("Temperature Alarm! Value: " + String(temperature));
}
}
}
void handleLight() {
int lightValue = analogRead(LDR_PIN);
String timeOfDay;
if (lightValue < 200) {
timeOfDay = "Morning";
} else if (lightValue < 700) {
timeOfDay = "Afternoon";
} else {
timeOfDay = "Evening";
}
Blynk.virtualWrite(V1, timeOfDay);
if (lightValue > 200) {
digitalWrite(LIGHT_PIN, HIGH);
} else {
digitalWrite(LIGHT_PIN, LOW);
}
}
BLYNK_WRITE(V2) {
enableTempRead = param.asInt();
}
void setup() {
Serial.begin(115200);
dht.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LIGHT_PIN, OUTPUT);
Blynk.begin(auth, ssid, pass);
timer.setInterval(2000L, sendTemperature);
timer.setInterval(2000L, handleLight);
}
void loop() {
Blynk.run();
timer.run();
}