#define BLYNK_TEMPLATE_ID "TMPL6HfTWDzlJ"
#define BLYNK_TEMPLATE_NAME "weather monitoring system iot mark 2"
#define BLYNK_AUTH_TOKEN "snXL5qfZhW5E3P-1XXCsnLYYxEKEbvQ_"
#define BLYNK_PRINT Serial
#include <Wire.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Define pins for sensors
#define LDR_PIN 5
#define DHTPIN 15
#define RAINPIN 34
// Initialize the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize the DHT22 sensor
DHT dht(DHTPIN, DHT22);
BlynkTimer timer;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Constants for the light sensor calculation
const float GAMMA = 0.7;
const float RL10 = 50.0;
void setup() {
// Debug console
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
// Initialize LCD
lcd.init();
lcd.backlight();
// Set pin modes
pinMode(LDR_PIN, INPUT);
pinMode(RAINPIN, INPUT);
pinMode(DHTPIN, INPUT);
// Configure analog read resolution
analogReadResolution(12);
// Display startup message on LCD
lcd.setCursor(0, 0);
lcd.print("Weather Monitor");
lcd.setCursor(4, 1);
lcd.print("System");
delay(4000);
lcd.clear();
// Connect to WiFi
connectToWiFi();
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Set up Blynk timer to send sensor data
timer.setInterval(1000L, sendSensorData);
// Initial sensor readings display
updateDisplay();
}
void connectToWiFi() {
WiFi.begin(ssid, pass);
Serial.print("Connecting to WiFi...");
while (WiFi.status()!= WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println(" Connected to WiFi!");
}
void updateDisplay() {
displayTemperature();
delay(2000);
displayHumidity();
delay(2000);
displayRain();
delay(2000);
displayLight();
delay(2000);
}
void displayTemperature() {
float t = dht.readTemperature();
if (isnan(t)) {
Serial.println("Failed to read temperature from DHT sensor!");
lcd.setCursor(0, 0);
lcd.print("Temp: Error");
return;
}
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" °C");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature:");
lcd.setCursor(0, 1);
lcd.print(t);
lcd.print(" C");
}
void displayHumidity() {
float h = dht.readHumidity();
if (isnan(h)) {
Serial.println("Failed to read humidity from DHT sensor!");
lcd.setCursor(0, 0);
lcd.print("Humidity: Error");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity:");
lcd.setCursor(0, 1);
lcd.print(h);
lcd.print(" %");
}
void displayRain() {
int Rvalue = analogRead(RAINPIN);
Rvalue = map(Rvalue, 0, 4095, 0, 100);
Rvalue = (Rvalue * -1) + 100;
Serial.print("Rain: ");
Serial.print(Rvalue);
Serial.println(" %");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rain Level:");
lcd.setCursor(0, 1);
lcd.print(Rvalue);
lcd.print(" %");
delay(2000); // Delay to allow reading
// Rain conditions and messages
lcd.clear();
if (Rvalue <= 16) {
lcd.print("Dry Weather");
Serial.println("Dry Weather");
} else if (Rvalue <= 32) {
lcd.print("Dew or Heavy Mist");
Serial.println("Dew or Heavy Mist");
} else if (Rvalue <= 51) {
lcd.print("Light Rain");
Serial.println("Light Rain");
} else if (Rvalue <= 70) {
lcd.print("Moderate Rain");
Serial.println("Moderate Rain");
} else if (Rvalue <= 85) {
lcd.print("Heavy Rain");
Serial.println("Heavy Rain");
} else {
lcd.print("Very Heavy Rain");
Serial.println("Very Heavy Rain");
}
}
void displayLight() {
int analogValue = analogRead(LDR_PIN);
Serial.print("Raw analog value: ");
Serial.println(analogValue);
float voltage = analogValue / 1024.0 * 5; // Assuming 3.3V reference
Serial.print("Voltage: ");
Serial.println(voltage);
float resistance = voltage / (1 - voltage / 5);
Serial.print("Resistance: ");
Serial.println(resistance);
float lux = pow(RL10 * 1000 * pow(10.0, GAMMA) / resistance, (1 / GAMMA));
Serial.print("Lux: ");
Serial.println(lux);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Light Intensity:");
delay(2000); // Delay to allow reading
// Light intensity conditions and messages
lcd.clear();
if (lux <= 100) {
lcd.print("It's Dark");
Serial.println("It's Dark");
} else if (lux <= 300) {
lcd.print("Comfortable Light");
Serial.println("Comfortable Light");
} else if (lux <= 600) {
lcd.print("Noon Brightness");
Serial.println("Noon Brightness");
} else if (lux <= 900) {
lcd.print("A Bit Too Bright");
Serial.println("A Bit Too Bright");
} else if (lux <= 1500) {
lcd.print("Way Too Bright");
Serial.println("Way Too Bright");
} else {
lcd.print("My Eyes Are Burning");
Serial.println("My Eyes Are Burning");
}
}
void loop() {
// Continuously update each sensor reading in sequence
updateDisplay();
Blynk.run();
timer.run();
}
// Send sensor data to Blynk app
void sendSensorData() {
float t = dht.readTemperature();
float h = dht.readHumidity();
int Rvalue = analogRead(RAINPIN);
int Lvalue = analogRead(LDR_PIN);
// Send temperature and humidity to Blynk
if (!isnan(t)) Blynk.virtualWrite(V1, t);
if (!isnan(h)) Blynk.virtualWrite(V2, h);
// Convert and send rain data
Rvalue = map(Rvalue, 0, 4095, 0, 100);
Rvalue = (Rvalue * -1) + 100;
Blynk.virtualWrite(V3, Rvalue);
// Convert the light sensor analog value to lux
float voltage = Lvalue / 1024.0 * 5; // Assuming 3.3V reference
float resistance = voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1000 * pow(10.0, GAMMA) / resistance, (1 / GAMMA));
// Send light intensity (in lux) to Blynk
Blynk.virtualWrite(V4, lux);
// Debug output to Serial for troubleshooting
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
Serial.print("Rain Level: ");
Serial.print(Rvalue);
Serial.println(" %");
Serial.print("Light Intensity: ");
Serial.print(lux);
Serial.println(" lux");
}