Skip to content

Commit 8c7a834

Browse files
committed
ESP8266: add soft rtc
1 parent fdd32f5 commit 8c7a834

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/utility/time/TimeService.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ TimeService::TimeService()
5656
, _is_tz_configured(false)
5757
, _timezone_offset(0)
5858
, _timezone_dst_until(0)
59+
#ifdef ARDUINO_ARCH_ESP8266
60+
, _soft_rtc_value(0)
61+
, _soft_rtc_offset(0)
62+
#endif
5963
{
6064

6165
}
@@ -98,13 +102,27 @@ unsigned long TimeService::getTime()
98102
return utc;
99103
}
100104
return time(NULL);
101-
#elif ARDUINO_ARCH_ESP32 || ARDUINO_ARCH_ESP8266
105+
#elif ARDUINO_ARCH_ESP32
102106
if(!_is_rtc_configured)
103107
{
104108
configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
105109
_is_rtc_configured = true;
106110
}
107111
return time(NULL);
112+
#elif ARDUINO_ARCH_ESP8266
113+
if(!_is_rtc_configured || millis() < _soft_rtc_offset)
114+
{
115+
_is_rtc_configured = false;
116+
unsigned long utc = getRemoteTime();
117+
if(EPOCH_AT_COMPILE_TIME != utc)
118+
{
119+
_soft_rtc_value = utc;
120+
_soft_rtc_offset = millis();
121+
_is_rtc_configured = true;
122+
}
123+
return utc;
124+
}
125+
return _soft_rtc_value + ((millis() - _soft_rtc_offset) / 1000);
108126
#else
109127
return getRemoteTime();
110128
#endif

src/utility/time/TimeService.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class TimeService
6060
bool _is_tz_configured;
6161
long _timezone_offset;
6262
unsigned long _timezone_dst_until;
63+
#ifdef ARDUINO_ARCH_ESP8266
64+
unsigned long _soft_rtc_value;
65+
unsigned long _soft_rtc_offset;
66+
#endif
6367

6468
unsigned long getRemoteTime();
6569
bool connected();

0 commit comments

Comments
 (0)