#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 7 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // Change to DHT22 if you're using that type of sensor
#define TEMP_THRESHOLD 25.0 // Temperature threshold for greenhouse (adjust as needed)
#define LED_PIN 4 // Digital pin connected to the LED
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize LCD screen with I2C address 0x27
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize DHT sensor
dht.begin();
// Initialize LCD screen
lcd.init();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Temperature ");
lcd.setCursor(0, 1);
lcd.print("Monitoring System");
// Initialize LED pin as an output
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Read temperature and humidity from DHT sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print temperature and humidity to serial monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
// Display temperature and humidity on LCD screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
// Check if temperature exceeds threshold
if (temperature > TEMP_THRESHOLD) {
// Display danger message on LCD
lcd.setCursor(0, 1);
lcd.print("Danger! Temp > ");
lcd.print(TEMP_THRESHOLD);
lcd.print(" C");
// Blink LED for emergency indication
blinkLED();
}
// Delay between readings
delay(5000); // Adjust delay time as needed
}
void blinkLED() {
// Blink LED for emergency indication
for (int i = 0; i < 3; i++) {
digitalWrite(LED_PIN, HIGH); // Turn LED on
delay(500); // Wait for 500 milliseconds
digitalWrite(LED_PIN, LOW); // Turn LED off
delay(500); // Wait for 500 milliseconds
}
}