From fdd32f510d4b4e6c13a93e25c292cdc617d0caff Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 18 Oct 2022 14:49:07 +0200 Subject: [PATCH 1/5] ESP32: use core internal functions to configure and get time --- src/utility/time/TimeService.cpp | 9 +++++++-- src/utility/time/TimeService.h | 2 -- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 88c60a1a2..729206554 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -52,9 +52,7 @@ 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) @@ -100,6 +98,13 @@ unsigned long TimeService::getTime() return utc; } return time(NULL); +#elif ARDUINO_ARCH_ESP32 || ARDUINO_ARCH_ESP8266 + if(!_is_rtc_configured) + { + configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov"); + _is_rtc_configured = true; + } + return time(NULL); #else return getRemoteTime(); #endif diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index 8d8862287..adf8495ab 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -56,9 +56,7 @@ 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; From 8c7a8345e1c9972cceb06668420ffcd6f810f929 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 19 Oct 2022 12:03:45 +0200 Subject: [PATCH 2/5] ESP8266: add soft rtc --- src/utility/time/TimeService.cpp | 20 +++++++++++++++++++- src/utility/time/TimeService.h | 4 ++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 729206554..3a3f8e363 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -56,6 +56,10 @@ TimeService::TimeService() , _is_tz_configured(false) , _timezone_offset(0) , _timezone_dst_until(0) +#ifdef ARDUINO_ARCH_ESP8266 +, _soft_rtc_value(0) +, _soft_rtc_offset(0) +#endif { } @@ -98,13 +102,27 @@ unsigned long TimeService::getTime() return utc; } return time(NULL); -#elif ARDUINO_ARCH_ESP32 || ARDUINO_ARCH_ESP8266 +#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 + if(!_is_rtc_configured || millis() < _soft_rtc_offset) + { + _is_rtc_configured = false; + unsigned long utc = getRemoteTime(); + if(EPOCH_AT_COMPILE_TIME != utc) + { + _soft_rtc_value = utc; + _soft_rtc_offset = millis(); + _is_rtc_configured = true; + } + return utc; + } + return _soft_rtc_value + ((millis() - _soft_rtc_offset) / 1000); #else return getRemoteTime(); #endif diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index adf8495ab..56b8ac22f 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -60,6 +60,10 @@ class TimeService bool _is_tz_configured; long _timezone_offset; unsigned long _timezone_dst_until; +#ifdef ARDUINO_ARCH_ESP8266 + unsigned long _soft_rtc_value; + unsigned long _soft_rtc_offset; +#endif unsigned long getRemoteTime(); bool connected(); From 5c85ac161ba862abc23473c603393b99d560e00d Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Oct 2022 09:54:54 +0200 Subject: [PATCH 3/5] ESP8266: add NTP sync timeout --- src/utility/time/TimeService.cpp | 24 ++++++++++++++++++------ src/utility/time/TimeService.h | 5 +++-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 3a3f8e363..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; @@ -57,8 +60,9 @@ TimeService::TimeService() , _timezone_offset(0) , _timezone_dst_until(0) #ifdef ARDUINO_ARCH_ESP8266 -, _soft_rtc_value(0) -, _soft_rtc_offset(0) +, _last_ntp_sync_tick(0) +, _last_rtc_update_tick(0) +, _rtc(0) #endif { @@ -110,19 +114,27 @@ unsigned long TimeService::getTime() } return time(NULL); #elif ARDUINO_ARCH_ESP8266 - if(!_is_rtc_configured || millis() < _soft_rtc_offset) + 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) { - _soft_rtc_value = utc; - _soft_rtc_offset = millis(); + _rtc = utc; + _last_ntp_sync_tick = now; + _last_rtc_update_tick = now; _is_rtc_configured = true; } return utc; } - return _soft_rtc_value + ((millis() - _soft_rtc_offset) / 1000); + 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 56b8ac22f..bf8eef1a1 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -61,8 +61,9 @@ class TimeService long _timezone_offset; unsigned long _timezone_dst_until; #ifdef ARDUINO_ARCH_ESP8266 - unsigned long _soft_rtc_value; - unsigned long _soft_rtc_offset; + unsigned long _last_ntp_sync_tick; + unsigned long _last_rtc_update_tick; + unsigned long _rtc; #endif unsigned long getRemoteTime(); From 6879eb74728dfb2b8d20cad486c658692fd2784b Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 2 Nov 2022 12:40:54 +0100 Subject: [PATCH 4/5] ESP: remove note about RTC in the readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index cde81db0b..28915c6fd 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,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 From 9facaecb6d01c41ccd20257ed0bf820c58048916 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 1 Dec 2022 09:34:47 +0100 Subject: [PATCH 5/5] Remove No RTC available message --- src/property/Property.cpp | 4 ---- 1 file changed, 4 deletions(-) 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 ******************************************************************************/