#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x3F, 16 column and 2 rows
DHT dht(DHTPIN, DHTTYPE);
const int hot = 27;
const int cold = 22;
const int high = 61;
const int low = 39;
void setup()
{
dht.begin(); // initialize the sensor
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
}
void loop()
{
delay(2000); // wait a few seconds between measurements
float humi = dht.readHumidity(); // read humidity
float tempC = dht.readTemperature(); // read temperature
lcd.clear();
// check if any reads failed
if (isnan(humi) || isnan(tempC)) {
lcd.setCursor(0, 0);
lcd.print("Failed");
} else {
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Temp: ");
lcd.print(tempC); // print the temperature
lcd.print((char)223); // print ° character
lcd.print("C");
lcd.setCursor(0, 1); // start to print at the second row
lcd.print("Humi: ");
lcd.print(humi); // print the humidity
lcd.print("%");
}
delay(3000);
if (tempC < cold) {
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
lcd.setCursor(0, 0);
lcd.print(" TOO COLD ");
}
else if (tempC >= hot) {
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
lcd.setCursor(0, 0);
lcd.print(" TOO HOT ");
}
else {
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
lcd.setCursor(0, 0);
lcd.print(" MODERATE ");
}
if (humi >= high) {
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
lcd.setCursor(0, 1);
lcd.print(" TOO HUMID ");
}
else if (humi <= low) {
digitalWrite(10, HIGH);
digitalWrite(9, LOW);
lcd.setCursor(0, 1);
lcd.print(" TOO DRY ");
}
else {
digitalWrite(10, LOW);
digitalWrite(9, HIGH);
lcd.setCursor(0, 1);
lcd.print(" MODERATE ");
}
}