Skip to content

Commit 8ba8037

Browse files
authored
Merge pull request #340 from pennam/esp-time
ESP TimeService: remove delays introduced by NTP requests
2 parents 1edf3f0 + 9facaec commit 8ba8037

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,5 @@ OTA is supported by the following boards:
8989
Support for ESP boards is obtained through a third-party core with some differences and limitations compared to Arduino boards.
9090

9191
- **Authentication scheme**: Board authentication is done through `DEVICE_LOGIN_NAME` and `DEVICE_KEY`, both values are included in the `thingProperties.h` file.
92-
- **RTC**: RTC support is not included thus each `ArduinoCould.update()` call will lead to an NTP request introducing delay in your `loop()` function. The scheduler widget will not work correctly if connection is lost after configuration.
9392
- **Watchdog**: Watchdog support is not included.
9493
- **OTA**: OTA support is not included

src/property/Property.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
#undef min
2222
#include <algorithm>
2323

24-
#if !defined ARDUINO_ARCH_SAMD && !defined ARDUINO_ARCH_MBED
25-
#pragma message "No RTC available on this architecture - ArduinoIoTCloud will not keep track of local change timestamps ."
26-
#endif
27-
2824
/******************************************************************************
2925
CTOR/DTOR
3026
******************************************************************************/

src/utility/time/TimeService.cpp

+37-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ time_t cvt_time(char const * time);
4343
* CONSTANTS
4444
**************************************************************************************/
4545

46+
#ifdef ARDUINO_ARCH_ESP8266
47+
static unsigned long const AIOT_TIMESERVICE_ESP8266_NTP_SYNC_TIMEOUT_ms = 86400000;
48+
#endif
4649
static time_t const EPOCH_AT_COMPILE_TIME = cvt_time(__DATE__);
4750
static time_t const EPOCH = 0;
4851

@@ -52,12 +55,15 @@ static time_t const EPOCH = 0;
5255

5356
TimeService::TimeService()
5457
: _con_hdl(nullptr)
55-
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
5658
, _is_rtc_configured(false)
57-
#endif
5859
, _is_tz_configured(false)
5960
, _timezone_offset(0)
6061
, _timezone_dst_until(0)
62+
#ifdef ARDUINO_ARCH_ESP8266
63+
, _last_ntp_sync_tick(0)
64+
, _last_rtc_update_tick(0)
65+
, _rtc(0)
66+
#endif
6167
{
6268

6369
}
@@ -100,6 +106,35 @@ unsigned long TimeService::getTime()
100106
return utc;
101107
}
102108
return time(NULL);
109+
#elif ARDUINO_ARCH_ESP32
110+
if(!_is_rtc_configured)
111+
{
112+
configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
113+
_is_rtc_configured = true;
114+
}
115+
return time(NULL);
116+
#elif ARDUINO_ARCH_ESP8266
117+
unsigned long const now = millis();
118+
bool const is_ntp_sync_timeout = (now - _last_ntp_sync_tick) > AIOT_TIMESERVICE_ESP8266_NTP_SYNC_TIMEOUT_ms;
119+
if(!_is_rtc_configured || is_ntp_sync_timeout)
120+
{
121+
_is_rtc_configured = false;
122+
unsigned long utc = getRemoteTime();
123+
if(EPOCH_AT_COMPILE_TIME != utc)
124+
{
125+
_rtc = utc;
126+
_last_ntp_sync_tick = now;
127+
_last_rtc_update_tick = now;
128+
_is_rtc_configured = true;
129+
}
130+
return utc;
131+
}
132+
unsigned long const elapsed_s = (now - _last_rtc_update_tick) / 1000;
133+
if(elapsed_s) {
134+
_rtc += elapsed_s;
135+
_last_rtc_update_tick = now;
136+
}
137+
return _rtc;
103138
#else
104139
return getRemoteTime();
105140
#endif

src/utility/time/TimeService.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ class TimeService
5656
private:
5757

5858
ConnectionHandler * _con_hdl;
59-
#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED)
6059
bool _is_rtc_configured;
61-
#endif
6260
bool _is_tz_configured;
6361
long _timezone_offset;
6462
unsigned long _timezone_dst_until;
63+
#ifdef ARDUINO_ARCH_ESP8266
64+
unsigned long _last_ntp_sync_tick;
65+
unsigned long _last_rtc_update_tick;
66+
unsigned long _rtc;
67+
#endif
6568

6669
unsigned long getRemoteTime();
6770
bool connected();

0 commit comments

Comments
 (0)