(Why is) getLocalTime() taking several seconds to complete successfully #8653
-
I noticed that 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;
}
In the above example it took ~6s for a "valid" timestamp to become available. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Most likely caused by rate-limiting or throttling on either the NTP server or one of the intermediary players (e.g. my ISP). |
Beta Was this translation helpful? Give feedback.
-
In my project I wait 30 seconds (I use Ticker.once() to schedule the event) for WiFi to connect and be settled before getting NTP time. I haven't noticed any delays. Once SNTP is started, it automatically re-queries NTP servers for time at a reasonable rate. I think it's once an hour, but could be every 6 hours. This is how I start SNTP:
|
Beta Was this translation helpful? Give feedback.
Most likely caused by rate-limiting or throttling on either the NTP server or one of the intermediary players (e.g. my ISP).
-> https://arduino.stackexchange.com/a/94447/48623