Retry NTP sooner after a failed attempt
The first NTP attempt after boot usually fails and the retry waited a flat 5 minutes, so the clock showed 00:00 (1970) for 5 minutes after every restart or OTA update. Retries now back off 15 s, 30 s, 1 min ... up to 5 min. Measured after an OTA update: time set at 30 s uptime. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b40b5f680c
commit
9f960fae17
2 files changed
+13
-5
No files matched your search
@@ -878,21 +878,24 @@ void startWifiMode() {
|
||||
|
||||
|
||||
// Primary time source (see calcTime()/docs/DEVELOPER_GUIDE.md 4.4): refreshes the system clock from
|
||||
// NTP over WiFi, at most once every TIMESERVER_REFRESH (2h) - or every 5 minutes if the previous
|
||||
// attempt failed (lastTimeFailure backoff), retrying the NTP request itself up to 5 times per call.
|
||||
// NTP over WiFi, at most once every TIMESERVER_REFRESH (2h) - or, after a failed attempt, again after
|
||||
// 15 s, 30 s, 1 min ... up to 5 min (retryDelay), retrying the NTP request itself up to 5 times per call.
|
||||
// On success, rebuilds myTZ from the current prm.utc_offset/prm.enableDST (so a live timezone/DST
|
||||
// change from the web UI takes effect on the very next scheduled refresh) and calls setTime() with
|
||||
// the timezone-adjusted local time. Returns true only if this call actually refreshed the clock -
|
||||
// callers (calcTime()) use that to decide whether the RTC also needs updating this cycle.
|
||||
boolean updateTimefromTimeserver() { //true, if successful
|
||||
static unsigned long lastTimeFailure = 0;
|
||||
static unsigned long retryDelay = 15000; //after a failure: 15 s, then doubled each time, at most 5 min
|
||||
boolean res = false;
|
||||
int count = 1;
|
||||
|
||||
if (((millis()-lastTimeUpdate)<TIMESERVER_REFRESH) && (lastTimeUpdate!=0))
|
||||
return(res);
|
||||
|
||||
if ((lastTimeFailure>0) && ((millis()-lastTimeFailure)<300000)) return(res); //wait for 5min to retry, if no success
|
||||
// FIXED: was a flat 5 min, and the first attempt right after boot usually fails, so the clock showed
|
||||
// 00:00 (1970) for 5 minutes after every restart or OTA update.
|
||||
if ((lastTimeFailure>0) && ((millis()-lastTimeFailure)<retryDelay)) return(res);
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
while (true) {
|
||||
@@ -911,12 +914,15 @@ boolean updateTimefromTimeserver() { //true, if successful
|
||||
DPRINT(" time:"); DPRINT(hour()); DPRINT(":"); DPRINT(minute()); DPRINT(":"); DPRINTLN(second());
|
||||
DPRINTLN("Clock refreshed from timeserver.");
|
||||
lastTimeFailure = 0;
|
||||
retryDelay = 15000;
|
||||
}
|
||||
count ++;
|
||||
count ++;
|
||||
if (res) break; //success!!!
|
||||
Fdelay(1000);
|
||||
if (count > 5) { //failure, but stop trying
|
||||
if (lastTimeFailure > 0) retryDelay = min(retryDelay * 2, 300000ul);
|
||||
lastTimeFailure = millis();
|
||||
DPRINT("Timeserver failed, next try in "); DPRINT(retryDelay / 1000); DPRINTLN(" s");
|
||||
break;
|
||||
}
|
||||
} //end while
|
||||
|
||||
Reference in new issue
Block a user