From 7e279c6e4581c1956dd1f357679c19cfdd50bbd9 Mon Sep 17 00:00:00 2001 From: bonamin Date: Wed, 7 Jan 2026 21:46:26 +0200 Subject: [PATCH] Added delay to NTP to let WiFi catch up before time request --- vesper/src/TimeKeeper/TimeKeeper.cpp | 4 ++-- vesper/vesper.ino | 32 ++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/vesper/src/TimeKeeper/TimeKeeper.cpp b/vesper/src/TimeKeeper/TimeKeeper.cpp index 4f4b670..6816381 100644 --- a/vesper/src/TimeKeeper/TimeKeeper.cpp +++ b/vesper/src/TimeKeeper/TimeKeeper.cpp @@ -152,9 +152,9 @@ void Timekeeper::syncTimeWithNTP() { // Configure NTP with settings from config configTime(timeConfig.gmtOffsetSec, timeConfig.daylightOffsetSec, timeConfig.ntpServer.c_str()); - // 🔥 NON-BLOCKING: Try to get time immediately without waiting + // 🔥 NON-BLOCKING: Try to get time with reasonable timeout for network response struct tm timeInfo; - if (getLocalTime(&timeInfo, 100)) { // 100ms timeout instead of blocking + if (getLocalTime(&timeInfo, 5000)) { // 5 second timeout for NTP response // Success! Update RTC with synchronized time rtc.adjust(DateTime(timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday, timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec)); diff --git a/vesper/vesper.ino b/vesper/vesper.ino index 712119a..e1cbb01 100644 --- a/vesper/vesper.ino +++ b/vesper/vesper.ino @@ -184,6 +184,7 @@ BellEngine bellEngine(player, configManager, telemetry, outputManager); // 🔥 TaskHandle_t bellEngineHandle = NULL; // Legacy - will be removed TimerHandle_t schedulerTimer; +TimerHandle_t ntpSyncTimer; // Non-blocking delayed NTP sync timer @@ -194,6 +195,14 @@ void handleFactoryReset() { } } +// Non-blocking NTP sync timer callback +void ntpSyncTimerCallback(TimerHandle_t xTimer) { + LOG_DEBUG("Network stabilization complete - starting NTP sync"); + if (!networking.isInAPMode()) { + timekeeper.syncTimeWithNTP(); + } +} + void setup() @@ -357,15 +366,25 @@ void setup() // Track if AsyncWebServer has been started to prevent duplicates static bool webServerStarted = false; + // Create NTP sync timer (one-shot, 3 second delay for network stabilization) + ntpSyncTimer = xTimerCreate( + "NTPSync", // Timer name + pdMS_TO_TICKS(3000), // 3 second delay (network stabilization) + pdFALSE, // One-shot timer (not auto-reload) + NULL, // Timer ID (not used) + ntpSyncTimerCallback // Callback function + ); + // Set up network callbacks networking.setNetworkCallbacks( [&webServerStarted]() { communication.onNetworkConnected(); - // Non-blocking NTP sync (graceful without internet) + // Schedule non-blocking NTP sync after 3s network stabilization (like MQTT) // Skip NTP sync in AP mode (no internet connection) - if (!networking.isInAPMode()) { - timekeeper.syncTimeWithNTP(); + if (!networking.isInAPMode() && ntpSyncTimer) { + LOG_DEBUG("Network connected - scheduling NTP sync after 3s stabilization (non-blocking)"); + xTimerStart(ntpSyncTimer, 0); } // Start AsyncWebServer when network becomes available (only once!) @@ -384,10 +403,11 @@ void setup() LOG_INFO("Network already connected - initializing services"); communication.onNetworkConnected(); - // Non-blocking NTP sync (graceful without internet) + // Schedule non-blocking NTP sync after 3s network stabilization (like MQTT) // Skip NTP sync in AP mode (no internet connection) - if (!networking.isInAPMode()) { - timekeeper.syncTimeWithNTP(); + if (!networking.isInAPMode() && ntpSyncTimer) { + LOG_DEBUG("Network already connected - scheduling NTP sync after 3s stabilization (non-blocking)"); + xTimerStart(ntpSyncTimer, 0); } // 🔥 CRITICAL: Start AsyncWebServer ONLY when network is ready