//import library
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
//Attach pins to the sensors and LED
#define DHT_PIN 15
#define PIR_SENSOR 12
#define LED_PIN 32
DHTesp dhtSensor;
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
//config LED_PIN output
pinMode(LED_PIN, OUTPUT);
//setup for serial communication
Serial.begin(9600);
Serial.print("ESP32 collecting sensors data");
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Sensor : ESP32");
lcd.setCursor(0,1);
lcd.print("Collecting data");
delay(1000);
//setup for dht sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(PIR_SENSOR, INPUT);
}
void loop() {
//collecting DHT data and print to the LCD and serial
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("---");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: " + stemp);
lcd.setCursor(0,1);
lcd.print("Humidity: " + shumid);
delay(1000);
//collecting PIR data
int pir_value = digitalRead(PIR_SENSOR);
//Detect motion and turn on LED
if(pir_value == 1){
Serial.println("Motion detected");
digitalWrite(LED_PIN, HIGH);
Serial.println("LED ON");
delay(500);
}
//Stop motion and turn off LED
else{
Serial.println("Motion ended");
digitalWrite(LED_PIN, LOW);
Serial.println("LED OFF");
delay(500);
}
}