#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
DynamicJsonDocument doc(1024);
#define LDR_pin 2
LiquidCrystal_I2C lcd(0x27, 20, 4);
int PIR_pin = 14;
int led = 13;
int buzz = 12;
String roomState;
String motion;
String ledState;
String buzzState;
void setup() {
pinMode(LDR_pin, INPUT);
pinMode(PIR_pin, INPUT);
pinMode(led, OUTPUT);
pinMode(buzz, OUTPUT);
lcd.init();
lcd.backlight();
Serial.begin(115200);
Serial.println("Hello, ESP32!");
digitalWrite(led, LOW);
}
void loop() {
delay(1000);
lcd.clear();
int pir_value = digitalRead(PIR_pin);
String data;
if (digitalRead(LDR_pin) == LOW ){
lcd.setCursor(0, 0);
lcd.print("Room : Light");
digitalWrite(led, LOW);
lcd.setCursor(0, 2);
lcd.print("Led : OFF...");
roomState = "Light";
ledState = "OFF";
}
else {
lcd.setCursor(0, 0);
lcd.print("Room : Dark!");
digitalWrite(led, HIGH);
lcd.setCursor(0, 2);
lcd.print("Led : ON...");
roomState = "Dark";
ledState = "ON";
}
if (pir_value == HIGH) {
lcd.setCursor(0, 1);
lcd.print("motion Detected !!");
digitalWrite(buzz, HIGH);
lcd.setCursor(0, 3);
lcd.print("Buzzer :ON ");
motion = "Detected";
buzzState = "ON";
}
else {
lcd.setCursor(0, 1);
lcd.print("Motion undetected !!");
digitalWrite(buzz, LOW);
lcd.setCursor(0, 3);
lcd.print("Buzzer :OFF");
motion = "Undetected";
buzzState = "OFF";
}
doc["Room State"] = roomState;
doc["Motion"] = motion;
doc["LED State"] = ledState;
doc["BUzzer STate"] = buzzState;
serializeJson(doc, data);
Serial.println(data);
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}