#include <WiFi.h>
#include "time.h"
#include <Wire.h> // for I2C with RTC module
#include "RTClib.h" //to show time
RTC_DS1307 RTC;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
int hou, minu, sec, wkday, yea = 0;
byte d, wk;
byte mont;
const char* ntpServer = "hu.pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
int count = 0;
uint32_t autorestart_debounce_counter = 20;
uint8_t timeSyncNeeded = 1;
uint32_t timeSyncFailedDebounceCounter = 0;
uint8_t timeValidFlag = 0;
unsigned long getTime(void)
{
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println("Failed to obtain time");
return(0);
}
time(&now);
return now;
}
void setTimezone(String timezone)
{
Serial.printf(" Setting Timezone to %s\n",timezone.c_str());
setenv("TZ",timezone.c_str(),1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
tzset();
}
void printLocalTime()
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
while (1);
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
hou = timeinfo.tm_hour;
minu = timeinfo.tm_min;
sec = timeinfo.tm_sec;
d = timeinfo.tm_mday;
mont = timeinfo.tm_mon + 1;
yea = timeinfo.tm_year + 1900;
}
void SyncTime_NTPtoESP(void)
{
configTime(3600, 3600, "hu.pool.ntp.org");
setTimezone("CET-1CEST,M3.5.0,M10.5.0/3");
}
void SyncTime_ESPtoDS1307(void)
{
DS1307_RTC.adjust(getTime());
}
void SyncTime_DS1307toESP(void)
{
ESP32Time rtc(0);
DateTime now = DS1307_RTC.now(); //Get DS1307 time
rtc.setTime(now.unixtime());
}
void ConnectLastWifi(void)
{
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
for (size_t i = 0; i < 5; i++)
{
if(WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
//AutoRestartHandler();
delay(500);
}
}
//AutoRestartTimerReset();
if(WiFi.status() == WL_CONNECTED)
{
Serial.println(" ");
Serial.print("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());
}
}
void setup()
{
Serial.begin(115200);
//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
wfbegin:
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
// if (RTC.lostPower())
// {
// Serial.println("RTC power failure, resetting the time!");
// //rtc.adjust(DateTime(F(_DATE), F(TIME_)));
if(count<5)
{
count++;
goto wfbegin;
}
if (! RTC.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
RTC.begin();
// The following lines can be uncommented to set the date and time
RTC.adjust(DateTime(yea, mont, d, hou, minu, sec)); // Set Day-of-Week to SUNDAY
// rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014
//rtc.adjust(DateTime(_DATE, __TIME_));
}
void loop()
{
delay(1000);
// printLocalTime();
DateTime now = RTC.now();
Serial.print(now.year());
Serial.print(".");
Serial.print(now.month());
Serial.print(".");
Serial.print(now.day());
Serial.print(".");
Serial.print(now.hour() -1);
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.print(now.second());
Serial.println();
if (1 == timeSyncNeeded)
{
ConnectLastWifi();
if(WiFi.status() == WL_CONNECTED)
{
SyncTime_NTPtoESP();
SyncTime_ESPtoDS1307();
timeSyncNeeded = 0;
timeSyncFailedDebounceCounter = TIME_SYNC_DEBOUNCE_COUNTER;
timeValidFlag = 1;
Serial.println(" ");
Serial.println("NTP -> ESP -> DS1307 time sync finished!");
}
else
{
Serial.println(" ");
Serial.println("NTP -> ESP -> DS1307 time sync failed!");
if(timeSyncFailedDebounceCounter > 0)
{
timeSyncFailedDebounceCounter--;
}
else if (0 == timeSyncFailedDebounceCounter)
{
timeValidFlag = 0;
}
SyncTime_DS1307toESP();
Serial.println("No WiFi -> Offline mode!");
}
timeSyncNeeded = 0;
}
}