#include <WiFi.h>
#include <ThingSpeak.h>
#include "DHT.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include <math.h>
#define POWER_PIN 32
#define DO_PIN 13
#define DHT_SENSOR_PIN 21
#define BUZZER_PIN 25
#define LIGHT_SENSOR_PIN 36
const char* ssid = "Wokwi-GUEST";
const char* password = "";
unsigned long myChannelNumber = 22572773;
const char* myWriteAPIKey = "I55RDO222F3PYSMS";
WiFiClient client;
LiquidCrystal_I2C lcd(0x27, 20, 4);
DHT dht_sensor(DHT_SENSOR_PIN, DHT22);
float GAMMA = 0.7;
float RL10 = 85;
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(3000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
ThingSpeak.begin(client);
pinMode(POWER_PIN, OUTPUT);
pinMode(DO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
dht_sensor.begin();
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weather Monitoring");
lcd.setCursor(0, 1);
lcd.print("System");
delay(3000);
lcd.clear();
}
void loop() {
float humi = dht_sensor.readHumidity();
float tempC = dht_sensor.readTemperature();
if (isnan(humi) || isnan(tempC)) {
Serial.println("Failed to read from DHT sensor!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("DHT Sensor Error");
delay(3000);
return;
}
digitalWrite(POWER_PIN, HIGH);
delay(10);
int rain_state = digitalRead(DO_PIN);
digitalWrite(POWER_PIN, LOW);
String weather = rain_state == LOW ? "Clear" : "Rainy";
digitalWrite(BUZZER_PIN, weather == "Rainy" ? HIGH : LOW);
int analog_value = analogRead(LIGHT_SENSOR_PIN);
float voltage = analog_value / 4095.0 * 3.3;
float resistance = 5000 * voltage / (1 - voltage / 3.3);
float lux = pow(RL10 * 1000 * pow(10, GAMMA) / resistance, (1 / GAMMA));
String lightCategory;
if (lux > 0.1 && lux <= 1) {
lightCategory = "Dark";
} else if (lux > 1 && lux <= 10) {
lightCategory = "Dim";
} else if (lux > 10 && lux <= 100) {
lightCategory = "Dull";
} else if (lux > 100 && lux <= 1000) {
lightCategory = "Drab";
} else if (lux > 1000 && lux <= 5000) {
lightCategory = "Gloomy";
} else if (lux > 5000 && lux <= 10000) {
lightCategory = "Glowing";
} else if (lux > 10000 && lux <= 25000) {
lightCategory = "Shining";
} else if (lux > 25000 && lux <= 50000) {
lightCategory = "Bright";
} else if (lux > 50000) {
lightCategory = "Luminous";
} else {
lightCategory = "Out of range";
}
Serial.print("Temperature (C): ");
Serial.print(tempC);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humi);
Serial.println(" %");
Serial.print("Weather: ");
Serial.println(weather);
Serial.print("Lux: ");
Serial.println(lux);
Serial.print("Brightness: ");
Serial.println(lightCategory);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(tempC);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humi);
lcd.print("%");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weather: ");
lcd.print(weather);
lcd.setCursor(0, 1);
lcd.print("Lux: ");
lcd.print(lux);
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Light: ");
lcd.print(lightCategory);
delay(3000);
ThingSpeak.setField(1, tempC);
ThingSpeak.setField(2, humi);
ThingSpeak.setField(3, weather == "Clear" ? 0 : 1);
ThingSpeak.setField(4, lux);
ThingSpeak.setField(5, lightCategory);
int statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (statusCode == 200) {
Serial.println("Channel update successful.");
}
else {
Serial.println("Problem Writing data. HTTP error code :" + String(statusCode));
}
delay(2000);
}