#include "RTClib.h"
#include "DHT.h"
#include <LiquidCrystal.h>
RTC_DS1307 rtc;
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Define LCD pins
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
delay(100);
// Initialize the LCD
lcd.begin(16, 2);
if (!rtc.begin()) {
lcd.clear();
lcd.print("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
lcd.clear();
lcd.print("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
dht.begin();
// Display current date on LCD
DateTime now = rtc.now();
lcd.clear();
lcd.print("Date: ");
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.setCursor(0, 1);
lcd.print("Hello Everyone");
delay(3000);
lcd.clear();
}
void loop() {
DateTime now = rtc.now();
int h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
lcd.clear();
lcd.print("Failed to read");
lcd.setCursor(0, 1);
lcd.print("DHT sensor data!");
delay(1000);
lcd.clear();
return;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.setCursor(0, 2);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
delay(1000);
}