From 9f960fae17ee52d288b0eff0cc6e95c36ec1cbd3 Mon Sep 17 00:00:00 2001 From: MangaValk Date: Thu, 29 Oct 2026 00:08:29 +0100 Subject: [PATCH] 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 --- CLAUDE.md | 4 +++- .../uniclock/ESP32_UniClock2/ESP32_UniClock2.ino | 14 ++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 32e5dd9..f639bda 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -106,7 +106,7 @@ GPIO0 to GND, tap RST to GND, then release GPIO0. `wledDarkWithTubes` / `wledUseBrightness` live in `prmExt`, stored in EEPROM after `prm` with their own magic value: never add fields to `prm` (a `MAGIC_VALUE` bump factory-resets the clock, WiFi included). HA switch "SignalRGB". -7. Done (2026-10-28, not yet on the clock): new web UI. Source `firmware/uniclock/web/index.html` +7. Done (2026-10-29, on the clock): new web UI. Source `firmware/uniclock/web/index.html` (one file, no jQuery, controls generated from the `PAGES` table), gzipped by `web/gzip_web.py` to `data/index.html.gz` (12 KB, was 147 KB), served by one `serveStatic` with `no-cache`. Opened from disk it runs on sample data; check it with headless Edge (`msedge --headless=new @@ -114,4 +114,6 @@ GPIO0 to GND, tap RST to GND, then release GPIO0. new fields `ip`, `hasAlarm`, `hasRadar`, real `cathProtMin`; `/getCurrentInfos` has `currentSec` and `mqtt` (0 off / 1 not connected / 2 connected). Network settings save with a Save button and offer a restart. + NTP: the first attempt after boot usually fails; retries now back off 15 s, 30 s ... 5 min + (was a flat 5 min, so the clock showed 00:00 for 5 minutes after every restart). 8. Open: the user can create `secrets.h` (gitignored, never read it) for OTA and web passwords. diff --git a/firmware/uniclock/ESP32_UniClock2/ESP32_UniClock2.ino b/firmware/uniclock/ESP32_UniClock2/ESP32_UniClock2.ino index 1c13f53..52b3910 100644 --- a/firmware/uniclock/ESP32_UniClock2/ESP32_UniClock2.ino +++ b/firmware/uniclock/ESP32_UniClock2/ESP32_UniClock2.ino @@ -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)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) 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