#include <LiquidCrystal_I2C.h>
#include <DHTesp.h>

#define DHT_PIN 15
#define PIR_SENSOR 12
#define LED_PIN 32

DHTesp dhtSensor;
LiquidCrystal_I2C lcd(0x27, 20, 4); // set LCD address is 0x24 and 20x4 square

void setup() {
  //config LED_PIN output
  pinMode(LED_PIN, OUTPUT);

  //setup for serial communication
  Serial.begin(9600);
  Serial.println("ESP32 collecting sensors data");
  
  lcd.init(); // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("ESP32 collecting data...");

  //setup for dht sensor
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  //config PIR_SENSOR input
  pinMode(PIR_SENSOR, INPUT);
  delay(1000);
}

void loop() {
  TempAndHumidity data = dhtSensor.getTempAndHumidity();
  int temp = data.temperature;
  int humid = data.humidity;
  String stemp = String(temp) + "C";
  String shumid = String(humid) + "%";
  Serial.println("Temp: " + stemp);
  Serial.println("Humidity: " + shumid);
  Serial.println("---");
  int pir_value = digitalRead(PIR_SENSOR);
  if(pir_value == 1){
  Serial.println("Motion detected");
  delay(100);
  }
  else{
  Serial.println("Motion ended");
  delay(100);
  }

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: 30C");
  lcd.setCursor(0,1);
  lcd.print("Humidity: 76%");
  delay(1000);
  digitalWrite(LED_PIN, HIGH);
  Serial.println("LED ON");
  delay(500);
  digitalWrite(LED_PIN, LOW);
  Serial.println("LED OFF");
  delay(500);
}