#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
#define LDR_PIN 2
byte sensorPin = 4;
const int tempPin = 14;
LiquidCrystal_I2C lcd(0x27, 20, 4);
DynamicJsonDocument doc(1024);
String JsonData;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(sensorPin,INPUT);
pinMode(LDR_PIN, INPUT);
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
lcd.clear();
int sensorValue=digitalRead(sensorPin);
//Serial.println(sensorValue);
lcd.setCursor(1,0);
if(sensorValue==HIGH)
{
lcd.print("Motion detected");
doc["Motion"]="Detected";
}
else{
lcd.println("Motion not detected");
doc["Motion"]="Not Detected";
}
delay(1000);
int adc = analogRead(tempPin);
const float BETA = 3950.0; // should match the Beta Coefficient of the thermistor
float temp = 1 / (log(1 / (4096.0 / adc - 1)) / BETA + 1.0 / 298.15) - 273.15;
lcd.setCursor(1,1);
lcd.print("Temp is : "+String(temp));
delay(1000);
lcd.setCursor(1,2);
if(digitalRead(LDR_PIN)==HIGH)
{
lcd.print("Room : Light");
doc["Room"]="Light";
}
else
{
lcd.print("Room : Dark");
doc["Room"]="Dark";
}
doc["Temperature"]=temp;
serializeJson(doc,JsonData);
Serial.println(JsonData);
delay(1000);
}