#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
const int DHT_PIN = 15;
const int LED_PIN = 13;
const int BUZZER_PIN = 14;
const int POT_PIN = 35;
const int LDR_PIN = 32;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2792928;
const char* myApiKey = "23IXA047L7QMXPP9";
const char* server = "api.thingspeak.com";
const int TEMP_THRESHOLD = 40;
const int HUMIDITY_THRESHOLD = 50;
DHTesp dhtSensor;
WiFiClient client;
LiquidCrystal_I2C lcd(0x27, 16, 4);
void setup() {
Serial.begin(2400);
lcd.init();
lcd.backlight();
lcd.print("Initializing ...\n");
delay(2000);
lcd.clear();
pinMode(POT_PIN, INPUT);
pinMode(LDR_PIN, INPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(LED_PIN, OUTPUT);
Serial.println("****************** WELCOME TO THE SMART IOT WEATHER STATION ******************");
delay(3000);
Serial.println("Wifi Status:\n");
Serial.println(">Connecting ...\n");
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_PIN, HIGH);
delay(280);
digitalWrite(LED_PIN, LOW);
delay(280);
}
delay(1000);
digitalWrite(LED_PIN, HIGH);
Serial.println(">Wifi connected !");
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
Serial.println("-------------------------------------------------------");
Serial.println("Monitoring Interface:\n");
}
void loop() {
lcd.clear();
float pot_value = 0;
float ldr_value = 0;
float data1;
pot_value = analogRead(POT_PIN);
pot_value = map(pot_value, 0, 4095, 0, 100);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
data1 = pot_value;
// Read LDR value and map it to light conditions
ldr_value = analogRead(LDR_PIN);
String lightCondition;
int lightCode;
if (ldr_value < 150) {
lightCondition = "Sunny";
lightCode=4;
} else if (ldr_value <= 220) {
lightCondition = "Partly Cloudy";
lightCode=3;
} else if (ldr_value <= 470) {
lightCondition = "Overcast";
lightCode=2;
} else if (ldr_value <= 1500) {
lightCondition = "Rainy/foggy";
lightCode=1;
} else {
lightCondition = "Dark/night)";
lightCode=0;
}
// Send data to ThingSpeak
ThingSpeak.setField(1, data.temperature);
ThingSpeak.setField(2, data.humidity);
ThingSpeak.setField(3, lightCode);
ThingSpeak.setField(4, data1); // New field for LDR values
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// Print data to Serial Monitor
Serial.println("Temperature Value: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity Value: " + String(data.humidity, 1) + "%");
Serial.println("Potentiometer Value: " + String(pot_value, 3) + "%");
Serial.println("LDR Value: " + String(ldr_value, 2) + " Ohms");
// Display data on LCD
lcd.setCursor(0, 0); // Row 0, Column 0
lcd.print("T:"); // Display "T:" for temperature
lcd.print(data.temperature); // Add temperature value
lcd.print("C H:"); // Add "H:" for humidity
lcd.print(data.humidity);
lcd.print("%");
lcd.setCursor(0, 1); // Row 1, Column 0
lcd.print("light:"); // Display "Lux:" for light
lcd.print(lightCondition); // Add LDR value
// Buzzer warning
if (data.temperature > TEMP_THRESHOLD || data.humidity > HUMIDITY_THRESHOLD) {
Serial.println("Buzzer sounding");
tone(BUZZER_PIN, 1000); // Activate buzzer
} else {
noTone(BUZZER_PIN); // Deactivate buzzer
}
// Data upload status
Serial.println("Data Upload Status:\n");
if (x == 200) {
Serial.println(">Uploaded successfully");
} else {
Serial.println(">Pending...");
}
Serial.println("-------------------------------------------------------");
delay(2000);
}