#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int PUMP_RELAY_PIN = 18; // Relay for the pump
const int FAN_RELAY_PIN = 19; // Relay for the fan
float lower = 23; // Lower temperature threshold
const float upper = 27; // Upper temperature threshold
const float lowerHumidity = 40.0; // Lower humidity threshold
const float upperHumidity = 60.0; // Upper humidity threshold
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println(F("DHT22 !"));
dht.begin();
lcd.init();
lcd.backlight();
lcd.print("DHT22 Sensor");
pinMode(PUMP_RELAY_PIN, OUTPUT); // Set pump relay pin as output
pinMode(FAN_RELAY_PIN, OUTPUT); // Set fan relay pin as output
}
void loop() {
float temperature = dht.readTemperature(); // Read temperature from DHT22 sensor
float humidity = dht.readHumidity(); // Read humidity from DHT22 sensor
// Check if the sensor readings are valid
if (isnan(temperature) || isnan(humidity)) { // If the readings are not a number (NaN)
Serial.println(F("Failed to read from DHT sensor!")); // Print an error message
lcd.clear(); // Clear the LCD
lcd.print("Sensor Error"); // Display an error message on the LCD
} else {
// Display the temperature and humidity on the LCD
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0); // Set the cursor to the first row, first column
lcd.print("Temp: ");
lcd.print(temperature); // Display the temperature on the LCD
lcd.print(" C");
lcd.setCursor(0, 1); // Set the cursor to the second row, first column
lcd.print("Humidity: ");
lcd.print(humidity); // Display the humidity on the LCD
lcd.print(" %");
// Control the fan based on the temperature
if (temperature > upper) { // If the temperature is above the upper threshold (27°C)
Serial.println("Hott");
Serial.println("Turning on fan");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hott");
lcd.setCursor(0, 1);
lcd.print("Turning on fan");
digitalWrite(FAN_RELAY_PIN, HIGH); // Turn on the relay (activate fan or cooling system)
} else if (temperature < lower) { // If the temperature is below the lower threshold (23°C)
Serial.println("Cold");
Serial.println("Turning off fan");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Cold");
lcd.setCursor(0, 1);
lcd.print("Turning off fan");
digitalWrite(FAN_RELAY_PIN, LOW); // Turn off the relay (deactivate fan or cooling system)
}
// Control the pump based on the humidity
if (humidity > upperHumidity) { // If the humidity is above 60%
Serial.println("Humidity high");
Serial.println("Turning off pump");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity high");
lcd.setCursor(0, 1);
lcd.print("Turning off pump");
digitalWrite(PUMP_RELAY_PIN, LOW); // Turn off pump (deactivate water supply)
} else if (humidity < lowerHumidity) { // If the humidity is below 40%
Serial.println("Humidity low");
Serial.println("Turning on pump");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity low");
lcd.setCursor(0, 1);
lcd.print("Turning on pump");
digitalWrite(PUMP_RELAY_PIN, HIGH); // Turn on pump (activate water supply)
}
}
delay(3000); // Wait for 1 second before the next loop iteration
}