#include <LiquidCrystal_I2C.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address and dimensions
#define DHTPIN 11
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
byte degree_symbol[8] = {
0b00111,
0b00101,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
void setup()
{
lcd.init();
lcd.backlight();
lcd.print("Weather Station");
delay(3000);
lcd.clear();
lcd.print("Data Fecting....");
delay(2000);
lcd.clear();
lcd.print("Temp = ");
lcd.setCursor(0, 1);
lcd.print("Humidity = ");
lcd.createChar(1, degree_symbol);
lcd.setCursor(9, 0);
lcd.write(1);
lcd.print("C");
lcd.setCursor(13, 1);
lcd.print("%");
dht.begin();
}
void loop()
{
delay(1000);
const int numReadings = 5;
float tempSum = 0;
float humSum = 0;
int validReadings = 0;
for (int i = 0; i < numReadings; i++) {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
tempSum += temperature;
humSum += humidity;
validReadings++;
}
delay(100);
}
if (validReadings > 0)
{
float avgTemp = tempSum / validReadings;
float avgHum = humSum / validReadings;
//For Temperature
lcd.setCursor(7, 0);
lcd.print(" ");
lcd.setCursor(7, 0);
lcd.print(avgTemp, 1);
lcd.print(" C");
//For Humanity
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(avgHum, 1);
lcd.print("%");
}
else
{
lcd.setCursor(7, 0);
lcd.print("Err ");
lcd.setCursor(11, 1);
lcd.print("Err ");
}
}