#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
int ledPin1 = 5;
int ledPin2 = 6;
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println(F("DHT22 example!"));
lcd.init();
dht.begin();
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
if (temperature >= 25)
{
digitalWrite(ledPin1, LOW);
}
else{
digitalWrite(ledPin1, HIGH);
}
if (temperature <= 25)
{
digitalWrite(ledPin2, LOW);
}
else{
digitalWrite(ledPin2, HIGH);
}
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(F("Humi: "));
lcd.print(humidity);
lcd.print(F("%"));
lcd.setCursor(0,1);
lcd.print(F("Temp: "));
lcd.print(temperature);
lcd.println(F(" C "));
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temperature);
Serial.println(F("°C "));
// Wait a few seconds between measurements.
delay(2000);
}