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.