#include <WiFi.h>
#include <HttpClient.h>
#include <ArduinoJson.h>
#include "DHTesp.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi password
// OpenWeatherMap API details
const char* apiKey = "81803ca3ebc0524787d3554cc0c7f1bd"; // Replace with your OpenWeatherMap API key
const char* city = "Indore"; // Replace with your city
const char* weatherApiUrl = "http://api.openweathermap.org/data/2.5/weather?q=Indore&appid=your_API_key&units=metric"; // API URL with city and API key
const int DHT_PIN = 15;
const int buz_PIN = 12;
DHTesp dhtSensor;
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define relay 2
const int pirPin = 13; // PIR sensor digital output pin
void setup()
{
// WiFi setup
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Set PIR sensor pin as input
pinMode(pirPin, INPUT);
pinMode(relay, OUTPUT);
pinMode(buz_PIN, OUTPUT);
Wire.begin(23, 22);
lcd.init();
lcd.backlight();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void DHT_sensor()
{
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(String(data.temperature, 2) + "°C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(String(data.humidity, 1) + "%");
if (data.temperature > 30)
{
digitalWrite(relay, HIGH);
digitalWrite(buz_PIN, HIGH);
}
else
{
digitalWrite(relay, LOW);
digitalWrite(buz_PIN, LOW);
}
delay(1000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}
void loop()
{
lcd.clear();
fetchWeatherData();
DHT_sensor();
motionSensor();
}
void fetchWeatherData()
{
if (WiFi.status() == WL_CONNECTED) {
HttpClient http;
String url = String(weatherApiUrl) + "&q=" + city + "&appid=" + apiKey + "&units=metric";
http.begin(url);
int httpResponseCode = http.GET(); // Make a GET request
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(response); // Debug response
// Parsing JSON response
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
float temperature = doc["main"]["temp"]; // Extract temperature
String weatherCondition = doc["weather"][0]["description"]; // Extract weather condition
// Print weather data to Serial Monitor
Serial.println("Weather Condition: " + weatherCondition);
Serial.println("Temperature: " + String(temperature) + "°C");
// Display on LCD
lcd.setCursor(0, 0);
lcd.print("Weather: ");
lcd.setCursor(0, 1);
lcd.print(weatherCondition);
delay(5000); // Update every 5 seconds
} else {
Serial.println("Error fetching weather data");
}
http.end();
}
}
int pirState = LOW;
void motionSensor()
{
int val = digitalRead(pirPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(relay, HIGH); // turn LED ON
digitalWrite(buz_PIN, HIGH);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
digitalWrite(relay, LOW); // turn LED OFF
digitalWrite(buz_PIN, LOW);
if (pirState == HIGH) {
// we have just turned off
Serial.println("Motion ended!");
pirState = LOW;
}
}
}