#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 2 // Pin where your DHT sensor is connected.
#define DHTTYPE DHT22 // Type of DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
const float TEMPERATURE_THRESHOLD = 25.0; // in Celsius
const float HUMIDITY_THRESHOLD = 60.0; // in percentage
void setup() {
Wire.begin(); // Initialize the I2C communication for the LCD
dht.begin();
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");;
}
void loop() {
delay(5000); // Delay for 2 seconds to prevent rapid readings
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read data from DHT sensor!");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor Error");
return;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature, 1); // Display temperature with one decimal place
lcd.print(" ");
lcd.write(0xDF); // Display the degree symbol
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity, 1); // Display humidity with one decimal place
lcd.print(" %");
delay(5000);
// Check if temperature or humidity exceeds the thresholds
if (temperature > TEMPERATURE_THRESHOLD) {
lcd.setCursor(0, 0);
lcd.print("Temp HIGH! ");
// Add code here to trigger alarms or notifications
}
if (humidity > HUMIDITY_THRESHOLD) {
lcd.setCursor(0, 1);
lcd.print("Humidity HIGH!");
// Add code here to trigger alarms or notifications
}
delay(2000); // Delay for 2 seconds before taking the next reading
}