/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-dht22
*/
#include <DHT.h>
#include <LiquidCrystal.h>
#define DHT22_PIN 21 // ESP32 pin GPIO21 connected to DHT22 sensor
const int relay = 26;
DHT dht22(DHT22_PIN, DHT22);
LiquidCrystal lcd(19, 23, 18, 17, 16, 15);
void setup() {
Serial.begin(9600);
dht22.begin(); // initialize the DHT22 sensor
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Temp | Humidity:");
pinMode(relay, OUTPUT);
}
void loop() {
// read humidity
float humi = dht22.readHumidity();
// read temperature in Celsius
float tempC = dht22.readTemperature();
// read temperature in Fahrenheit
float tempF = dht22.readTemperature(true);
// check whether the reading is successful or not0
if ( isnan(tempC) || isnan(tempF) || isnan(humi)) {
Serial.println("Failed to read from DHT22 sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humi);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C ~ ");
Serial.print(tempF);
Serial.println("°F");
lcd.setCursor(0, 1);
lcd.print(tempC);
lcd.print(" | ");
lcd.println(humi);
if (tempC < 20){
digitalWrite(relay, LOW);
Serial.println("Current Flowing");
delay(1000);
} else {
// Normally Open configuration, send HIGH signal stop current flow
// (if you're usong Normally Closed configuration send LOW signal)
digitalWrite(relay, HIGH);
Serial.println("Current not Flowing");
}
}
// wait a 2 seconds between readings
delay(5000);
}