#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"
String ds = "";
int cou = 0;
int tmpC =0;
short RTn = 0;
short DSs = 0;
short DSn = 0;
short DSh = 0;
short DSd = 0;
short DSm = 0;
short DSy = 0;
short cT = 0;
short tNow = 0;
char wkD[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char monD[12][12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
LiquidCrystal LCD(13, 12, 14, 27, 26, 25);
RTC_DS3231 rtc;
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 19600
#define UTC_OFFSET_DST 0
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%d/%m/%Y mafaa");
} //print LT
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello ESP32!");
// set up the LCD's number of columns and rows:
LCD.begin(8, 2);
LCD.clear();
// Print a message to the LCD.
LCD.print("hello, ESP32!");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
LCD.setCursor(0, 1);
LCD.print("No RTC");
//abort();
}
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
cou+1;
if(cou> 10) {break;}
LCD.setCursor(0, 1);
LCD.print("No internet");
delay(2000);
cou = 0;
}
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
delay(3000);
LCD.clear();
getRTC();
tNow = 1;
} //setup
void getRTC() {
if (! rtc.begin()) {
Serial.println("No RTC");
}else {
DateTime now = rtc.now();
//tNow = int(now.hour());
DSh = now.hour();
DSn = now.minute();
DSs = now.second();
RTn = DSn;
DSd = now.day();
DSm = now.month();
DSy = now.year();
LCDtim();
//if (hidx < 255) {LCDdat();}
Serial.println("RTC called");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(DSd);
Serial.print(" (");
//Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
//Serial.print(") ");
//Serial.print(now.hour(), DEC);
Serial.print(DSh, DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println(")");
Serial.print("Temp ");
Serial.print(rtc.getTemperature());
Serial.println(" C");
Serial.println();
}
LCDtim();
LCDdat();
} // getRTC
void LCDtim() {
//if((int((now.unixtime()) - wkD) > 5)) {wkD = int(now.unixtime());}
LCD.setCursor(0, 1);
if (DSh < 10) {LCD.print("0");}
LCD.print(DSh);
LCD.print(':');
if (DSn < 10) {LCD.print("0");}
LCD.print(DSn);
LCD.print(':');
if (DSs < 10) {LCD.print("0");}
LCD.print(DSs);
tmpC = rtc.getTemperature();
LCD.print(" ");
//LCD.print(tmpC);
LCD.print(cou);
//LCD.print("tNow");
//LCD.print(tNow);
LCD.setCursor(0, 10);
//LCD.print(wkD[(DSd + 1)]);
//Serial.println(wkD[(DSd + 1)]);
//Serial.println(wkD[(DSd + 1)]);
LCD.println(&timeinfo, "Www");
} //LCDtim
void LCDdat() {
LCD.setCursor(0, 0);
//LCD.print("Date: ");
if (DSd < 10) {LCD.print("0");}
LCD.print(DSd);
LCD.print("/");
if (DSm < 10) {LCD.print("0");}
LCD.print(DSm);
LCD.print("/");
int year = DSy % 100;
LCD.print(year);
//LCD.print(now.year());
//LCD.print(" ");
//LCD.print(wkD[now.dayOfTheWeek()]);
//LCD.print(" ");
//LCD.print(monD[(now.month() -1)]);
Serial.println("LCDdat called");
} //LCDdat
void loop() {
/* set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
LCD.setCursor(0, 0);
if (rtc.begin()) {DateTime now = rtc.now();
//LCD.print("Date: ");
//LCD.setCursor(6, 0);
if (now.day()< 10) {LCD.print("0");}
LCD.print(now.day());
LCD.print("/");
if (now.month()< 10) {LCD.print("0");}
LCD.print(now.month());
LCD.print("/");
int year = now.year() % 100;
LCD.print(year);
//LCD.print(now.year());
LCD.print(" ");
LCD.print(wkD[now.dayOfTheWeek()]);
LCD.print(" ");
LCD.print(monD[(now.month() -1)]);
LCD.setCursor(0, 1);
//LCD.print("Time: ");
if (now.hour()< 10) {LCD.print("0");}
LCD.print(now.hour());
LCD.print(':');
if (now.minute()< 10) {LCD.print("0");}
LCD.print(now.minute());
LCD.print(':');
if (now.second()< 10) {LCD.print("0");}
LCD.print(now.second());
tmpC = rtc.getTemperature();
LCD.print(" Tmp:");
LCD.print(tmpC);
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
//Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.printf("Tmp: %d ", tmpC);
Dh = now.hour();
Dm = now.minute();
Ds = now.second();
*/
/* cou 5 to 10 mint
RTC 10 mint
WIFI 1 hr
*/
if(cou > 300) {
//chktNow();
//if (booON) {FLED1();}
LCDdat();
cou = 0;
}
DSs+=1;
if(DSs ==60) {
DSs = 0;
DSn+=1;
if(DSn ==60) {
DSn = 0;
DSh+=1;
//if((DSh - Dph) == 100) {}
if(DSh ==24) {
DSh = 0;
LCDdat();
}
getRTC(); // update every hour
}
cT = ((DSh * 100)+DSn);
if (cT > tNow) {
Serial.println("cT > tNow");
/*initWiFi();
getRTC();
// Set RTC time
if (DSn !=RTn) {
Serial.println("RTC time adjust");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
} */
tNow = cT + 200;
}
}
//Serial.print(LCD.status());
Serial.printf("Dh %d", DSh);
Serial.printf(" Dm %d", DSn);
Serial.printf(" Ds %d", DSs);
Serial.printf(" cT %d", cT);
Serial.printf(" cou %d\n", cou);
LCDtim();
delay(1000);
cou+=1;
}