#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <WiFi.h>
// PIR Motion Sensor
#define PIR_PIN 13
// DHT22 Temperature Sensor
#define DHTPIN 14
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize the Serial Monitor
Serial.begin(115200);
// Initialize WiFi
WiFi.mode(WIFI_STA);
WiFi.begin("Wokwi-GUEST",""); // Connect to WiFi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected");
// Initialize the PIR Motion Sensor pin
pinMode(PIR_PIN, INPUT);
// Initialize the DHT22 sensor
dht.begin();
Serial.println("TESTTTTT");
// Initialize the LCD
lcd.init();
//lcd.begin(16, 2);
lcd.backlight();
// Display a startup message
lcd.setCursor(0, 0);
lcd.print("Motion Temp Mon");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void loop() {
// Read the PIR sensor
int motion = digitalRead(PIR_PIN);
if (motion == HIGH) {
// If motion is detected, read the temperature
float temp = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
lcd.setCursor(0, 0);
lcd.print("Error reading temp");
lcd.setCursor(0, 1);
lcd.print("Check sensor");
delay(2000); // Wait for 2 seconds before trying again
return;
}
// Print the temperature to the Serial Monitor
Serial.print("Motion detected! Temp: ");
Serial.print(temp);
Serial.println(" C");
// Display the temperature on the LCD
lcd.clear(); // Clear the display before updating
lcd.setCursor(0, 0);
lcd.print("Motion detected!");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print(" C");
} else {
// If no motion is detected, clear the second line of the LCD
lcd.clear(); // Clear the display before updating
lcd.setCursor(0, 0);
lcd.print("No motion");
lcd.setCursor(0, 1);
lcd.print(" ");
}
// Small delay to avoid flickering
delay(1000);
}