Skip to content

Commit

Permalink
Merge pull request #340 from pennam/esp-time
Browse files Browse the repository at this point in the history
ESP TimeService: remove delays introduced by NTP requests
  • Loading branch information
pennam committed Dec 1, 2022
2 parents 1edf3f0 + 9facaec commit 8ba8037
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions src/property/Property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
#undef min
#include <algorithm>

#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
******************************************************************************/
Expand Down
39 changes: 37 additions & 2 deletions src/utility/time/TimeService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
{

}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/utility/time/TimeService.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 8ba8037

Please sign in to comment.