diff --git a/README.md b/README.md index 6440194e2..07151347c 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,5 @@ OTA is supported by the following boards: Support for ESP boards is obtained through a third-party core with some differences and limitations compared to Arduino boards. - **Authentication scheme**: Board authentication is done through `DEVICE_LOGIN_NAME` and `DEVICE_KEY`, both values are included in the `thingProperties.h` file. -- **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. - **Watchdog**: Watchdog support is not included. - **OTA**: OTA support is not included diff --git a/src/property/Property.cpp b/src/property/Property.cpp index 833d9c68d..c6c3db9e4 100644 --- a/src/property/Property.cpp +++ b/src/property/Property.cpp @@ -21,10 +21,6 @@ #undef min #include -#if !defined ARDUINO_ARCH_SAMD && !defined ARDUINO_ARCH_MBED - #pragma message "No RTC available on this architecture - ArduinoIoTCloud will not keep track of local change timestamps ." -#endif - /****************************************************************************** CTOR/DTOR ******************************************************************************/ diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 88c60a1a2..dc90048ad 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -43,6 +43,9 @@ time_t cvt_time(char const * time); * CONSTANTS **************************************************************************************/ +#ifdef ARDUINO_ARCH_ESP8266 +static unsigned long const AIOT_TIMESERVICE_ESP8266_NTP_SYNC_TIMEOUT_ms = 86400000; +#endif static time_t const EPOCH_AT_COMPILE_TIME = cvt_time(__DATE__); static time_t const EPOCH = 0; @@ -52,12 +55,15 @@ static time_t const EPOCH = 0; TimeService::TimeService() : _con_hdl(nullptr) -#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) , _is_rtc_configured(false) -#endif , _is_tz_configured(false) , _timezone_offset(0) , _timezone_dst_until(0) +#ifdef ARDUINO_ARCH_ESP8266 +, _last_ntp_sync_tick(0) +, _last_rtc_update_tick(0) +, _rtc(0) +#endif { } @@ -100,6 +106,35 @@ unsigned long TimeService::getTime() return utc; } return time(NULL); +#elif ARDUINO_ARCH_ESP32 + if(!_is_rtc_configured) + { + configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); + _is_rtc_configured = true; + } + return time(NULL); +#elif ARDUINO_ARCH_ESP8266 + unsigned long const now = millis(); + bool const is_ntp_sync_timeout = (now - _last_ntp_sync_tick) > AIOT_TIMESERVICE_ESP8266_NTP_SYNC_TIMEOUT_ms; + if(!_is_rtc_configured || is_ntp_sync_timeout) + { + _is_rtc_configured = false; + unsigned long utc = getRemoteTime(); + if(EPOCH_AT_COMPILE_TIME != utc) + { + _rtc = utc; + _last_ntp_sync_tick = now; + _last_rtc_update_tick = now; + _is_rtc_configured = true; + } + return utc; + } + unsigned long const elapsed_s = (now - _last_rtc_update_tick) / 1000; + if(elapsed_s) { + _rtc += elapsed_s; + _last_rtc_update_tick = now; + } + return _rtc; #else return getRemoteTime(); #endif diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index 8d8862287..bf8eef1a1 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -56,12 +56,15 @@ class TimeService private: ConnectionHandler * _con_hdl; -#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) bool _is_rtc_configured; -#endif bool _is_tz_configured; long _timezone_offset; unsigned long _timezone_dst_until; +#ifdef ARDUINO_ARCH_ESP8266 + unsigned long _last_ntp_sync_tick; + unsigned long _last_rtc_update_tick; + unsigned long _rtc; +#endif unsigned long getRemoteTime(); bool connected();