#include <LiquidCrystal_I2C.h>
#include <DHTesp.h>
#define LED_PIN 32
#define DHT_PIN 15
#define MOTION_PIN 12
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
int motion_detec = 0;
DHTesp dhtSensor;
void setup() {
//config LED_PIN output
pinMode(LED_PIN, OUTPUT);
pinMode(DHT_PIN, INPUT);
pinMode(MOTION_PIN, INPUT);
//setup for serial communication
Serial.begin(9600);
Serial.println("ESP32 collecting sensors data");
//Setup DHT to according PIN
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("ESP32 collecting data ...");
delay(1000);
}
void loop() {
lcd.clear();
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float temp = data.temperature;
float humid = data.humidity;
lcd.setCursor(0,0);
lcd.print("Temp: " + String(temp));
lcd.setCursor(0,1);
lcd.print("Humidity: " + String(humid) +"%");
delay(1000);
//Turn on led if motion sensor return true
motion_detec = digitalRead(MOTION_PIN);
if(motion_detec == 1){
digitalWrite(LED_PIN, true);
Serial.println("Motion detected!");
}
else{
digitalWrite(LED_PIN, false);
}
}