#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
// Relay and buzzer pins
#define RELAY_PIN 5 // GPIO5 (D5)
#define BUZZER_PIN 18 // GPIO18 (D18)
// Threshold temperature
const float TEMP_THRESHOLD = 35.0;
const int DHT_PIN = 15;
DHTesp dhtSensor;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
void setup () {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
lcd.init();
lcd.backlight();
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Ensure relay is off initially
digitalWrite(BUZZER_PIN, LOW); // Ensure buzzer is off initially
}
void loop () {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 1) + " Celcius");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(data.temperature, 1) + "\xDF" + "C ");
// lcd.setCursor(0, 1);
// lcd.print("Humidity: " + String(data.humidity, 1) + "% ");
// Control relay and buzzer based on temperature
if (data.temperature > TEMP_THRESHOLD) {
digitalWrite(RELAY_PIN, HIGH); // Turn on relay (cooling system)
digitalWrite(BUZZER_PIN, HIGH); // Turn on buzzer (alert)
lcd.setCursor(0, 1);
lcd.print("Cooling ON "); // Clear the line by adding spaces
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off relay
digitalWrite(BUZZER_PIN, LOW); // Turn off buzzer
lcd.setCursor(0, 1);
lcd.print("Cooling OFF "); // Clear the line by adding spaces
}
delay(1000); // Delay outside the if-else block
}