#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#define DHTPIN A0
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 RTC;
void setup() {
// put your setup code here, to run once:
dht.begin();
lcd.init();
lcd.backlight();
//lcd.setCursor(3,0);
//lcd.print("Test DHT22");
Serial.begin(115200);
//Serial.println("Test DHT22");
RTC.begin();
}
void loop() {
// put your main code here, to run repeatedly:
char stmp[17];
DateTime now = RTC.now();
char op = now.second()%2;
lcd.setCursor(0,0);
if(op==0) {
/*
lcd.print("DATE: ");
lcd.print(now.year()); lcd.print("/");
snprintf(stmp2, 15, "%02d", now.month());
lcd.print(stmp2);
lcd.print("/");
snprintf(stmp2, 15, "%02d", now.day());
lcd.print(stmp2);
*/
snprintf(stmp, 17, "DATE: %4d/%02d/%02d", now.year(), now.month(), now.day());
}
else {
/*
lcd.print("TIME: ");
snprintf(stmp2, 15, "%02d", now.hour());
lcd.print(stmp2);
lcd.print(":");
snprintf(stmp2, 15, "%02d", now.minute());
lcd.print(stmp2);
lcd.print(":");
snprintf(stmp2, 15, "%02d", now.second());
lcd.print(stmp2);
*/
snprintf(stmp, 17, "TIME: %02d/%02d/%02d ", now.hour(), now.minute(), now.second());
}
lcd.print(stmp);
stmp[17] = 0;
//Serial.println(stmp);
float T = dht.readTemperature();
float H = dht.readHumidity();
//Serial.println(String("T:")+String(T,1)+String("C"));
//Serial.println(String("H:")+String(H,1)+String("%"));
// Check if any reads failed and exit early (to try again).
if (isnan(T) || isnan(H)) {
Serial.println(F("Failed to read from DHT sensor!"));
}
else {
/*
lcd.setCursor(0,1);
lcd.print("T:");
lcd.print(String(T,1));
lcd.print("C");
lcd.print(", ");
lcd.print("H:");
lcd.print(String(H,1));
lcd.print("%");
*/
//char stmp[17];
snprintf(stmp, 17, "T:%sC, H:%s%%", String(T,1).c_str(), String(H,1).c_str());
lcd.setCursor(0,1);
lcd.print(stmp);
//Serial.println(stmp);
}
delay(500);
}