0

This is a cross-post from https://github.com/espressif/arduino-esp32/discussions/8653 as it didn't yield any results there.

I noticed that getLocalTime() is consistently taking 5-10+ seconds to complete. So far I haven't been able to find the root cause.

I am testing on a ESP32-WROVER-B module. On startup the device first connects to WiFi and then tries to sync time.

boolean initTime() {
  struct tm timeinfo;

log_i("Synchronizing time."); // Connect to NTP server with 0 TZ offset, call setTimezone() later configTime(0, 0, "pool.ntp.org"); // getLocalTime uses a default timeout of 5s -> the loop takes at most 3*5s to complete for (int i = 0; i < 3; i++) { if (getLocalTime(&timeinfo)) { log_i("UTC time: %s.", getCurrentTimestamp(SYSTEM_TIMESTAMP_FORMAT).c_str()); return true; } }

log_e("Failed to obtain time."); return false; }

  ...
  10:12:28.118 > [  3634][I][connectivity.h:17] startWiFi(): ...done. IP: 192.168.0.57, WiFi RSSI: -55.
- 10:12:28.118 > [  3700][I][util.h:25] initTime(): Synchronizing time.
| 
->10:12:34.025 > [ 11572][I][util.h:37] initTime(): UTC time: 2023-09-19 08:12:34.
  10:12:34.037 > [ 11572][I][util.h:60] setTimezone(): Setting timezone to 'CET-1CEST,M3.5.0,M10.5.0/3'.
  10:12:34.042 > [ 11574][I][main.cpp:175] syncTime(): Current local time: 2023-09-19 10:12:34
  ...

In the above example it took ~6s for a "valid" timestamp to become available.

Marcel Stör
  • 125
  • 1
  • 9

1 Answers1

1

Based on the wording in the question, it appears you are requesting an NTP re-synchronizing event often. Perhaps as often as possible. Consider that most Arduino projects request an NTP re-synchronizing event once to set the local RTC. Then perhaps once a day to check if the RTC is accurate.

Keep in mind that many NTP servers defend them selves from attack by throttling the service from an IP address who utilize them excessively. This quote:

The NTP server may throttle traffic, especially for clients making frequent requests.

... is from this web site.


In case you are not aware, if time is important for your Arduino project, consider adding RTC hardware. This can be set to the the local time just after the NTP re-synchronizing. From that point forward, the local RTC can provide the time. Later, it may be prudent to check the local RTC against the NTP reported time. But this is usually not necessary for days at a time for the vast majority of Arduino projects.

st2000
  • 7,513
  • 2
  • 13
  • 19