Added delay to NTP to let WiFi catch up before time request

This commit is contained in:
2026-01-07 21:46:26 +02:00
parent eb6e0f0e5c
commit 7e279c6e45
2 changed files with 28 additions and 8 deletions

View File

@@ -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