fix esp32 time sync (RTC / NTP) (#11494)

This commit is contained in:
Manuel
2026-08-13 13:57:07 -04:00
committed by GitHub
parent a400143090
commit 778041ec06
2 changed files with 20 additions and 3 deletions
+14 -1
View File
@@ -140,6 +140,9 @@ RTCSetResult readFromRTC()
RTCQuality oldQuality = currentQuality;
timeStartMs64 = now;
zeroOffsetSecs = tv.tv_sec;
#if defined(ARCH_ESP32) || defined(ARCH_RP2040)
settimeofday(&tv, NULL);
#endif
currentQuality = RTCQualityDevice;
onTimeSourceQualityChanged(oldQuality, currentQuality);
}
@@ -186,6 +189,9 @@ RTCSetResult readFromRTC()
RTCQuality oldQuality = currentQuality;
timeStartMs64 = now;
zeroOffsetSecs = tv.tv_sec;
#if defined(ARCH_ESP32) || defined(ARCH_RP2040)
settimeofday(&tv, NULL);
#endif
currentQuality = RTCQualityDevice;
onTimeSourceQualityChanged(oldQuality, currentQuality);
}
@@ -222,6 +228,9 @@ RTCSetResult readFromRTC()
RTCQuality oldQuality = currentQuality;
timeStartMs64 = now;
zeroOffsetSecs = tv.tv_sec;
#if defined(ARCH_ESP32) || defined(ARCH_RP2040)
settimeofday(&tv, NULL);
#endif
currentQuality = RTCQualityDevice;
onTimeSourceQualityChanged(oldQuality, currentQuality);
}
@@ -389,7 +398,11 @@ RTCSetResult perhapsSetRTC(RTCQuality q, const struct timeval *tv, bool forceUpd
if (stm32wlRtcAvailable()) {
STM32RTC::getInstance().setEpoch(tv->tv_sec);
}
#elif defined(ARCH_ESP32) || defined(ARCH_RP2040)
#endif
// Keep the POSIX system clock in sync on platforms that support it so that
// any code using time() (e.g. the device-ui thread) sees the correct wall time
// even when a hardware RTC chip is also present and handled above.
#if defined(ARCH_ESP32) || defined(ARCH_RP2040)
settimeofday(tv, NULL);
#endif
+6 -2
View File
@@ -294,7 +294,7 @@ static int32_t reconnectWiFi()
#ifndef DISABLE_NTP
if (WiFi.isConnected() && (!Throttle::isWithinTimespanMs(lastrun_ntp, 43200000) || (lastrun_ntp == 0))) { // every 12 hours
LOG_DEBUG("Update NTP time from %s", config.network.ntp_server);
if (timeClient.update()) {
if (timeClient.forceUpdate()) {
LOG_DEBUG("NTP success - set RTCQualityNTP if needed");
struct timeval tv;
@@ -316,7 +316,11 @@ static int32_t reconnectWiFi()
return 1000; // check once per second
} else {
onNetworkConnected(); // will only do anything once (guarded by APStartupComplete)
return 300000; // every 5 minutes
#ifndef DISABLE_NTP
if (lastrun_ntp == 0)
return 5000; // NTP not yet synced, retry sooner
#endif
return 300000; // every 5 minutes
}
}