33class DS3231RTC :
public Clock {
40 SUPLA_LOG_DEBUG(
"Unable to find RTC");
42 struct tm timeinfo {};
44 RTCLostPower = (rtc.lostPower()) ? true :
false;
45 DateTime now = rtc.now();
47 timeinfo.tm_year = now.year() - 1900;
48 timeinfo.tm_mon = now.month() - 1;
49 timeinfo.tm_mday = now.day();
50 timeinfo.tm_hour = now.hour();
51 timeinfo.tm_min = now.minute();
52 timeinfo.tm_sec = now.second();
54 localtime = mktime(&timeinfo);
56#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
57 timeval tv = {localtime, 0};
58 settimeofday(&tv,
nullptr);
59#elif defined(ARDUINO_ARCH_AVR)
60 set_system_time(mktime(&timeinfo));
62 printCurrentTime(
"from RTC:");
64 if (getYear() >= 2023) {
67 SUPLA_LOG_DEBUG(
"Clock is not ready");
72 bool rtcIsReady()
const {
76 bool getRTCLostPowerFlag()
const {
80 void resetRTCLostPowerFlag() {
84 void parseLocaltimeFromServer(TSDC_UserLocalTimeResult *result) {
85 struct tm timeinfo {};
87 printCurrentTime(
"current");
89 timeinfo.tm_year = result->year - 1900;
90 timeinfo.tm_mon = result->month - 1;
91 timeinfo.tm_mday = result->day;
92 timeinfo.tm_hour = result->hour;
93 timeinfo.tm_min = result->min;
94 timeinfo.tm_sec = result->sec;
96 localtime = mktime(&timeinfo);
98#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
99 timeval tv = {localtime, 0};
100 settimeofday(&tv,
nullptr);
101#elif defined(ARDUINO_ARCH_AVR)
102 set_system_time(mktime(&timeinfo));
105 printCurrentTime(
"new");
109 DateTime now = rtc.now();
110 if ((now.year() != getYear()) || (now.month() != getMonth()) ||
111 (now.day() != getDay()) || (now.hour() != getHour()) ||
112 (now.minute() != getMin()) || (now.second() - getSec() > 5) ||
113 (now.second() - getSec() < -5)) {
114 rtc.adjust(DateTime(getTimeStamp()));
115 SUPLA_LOG_DEBUG(
"Update RTC time from server");
122 bool RTCLostPower =
false;
123 bool isRTCReady =
false;