Added delay to NTP to let WiFi catch up before time request
This commit is contained in:
@@ -152,9 +152,9 @@ void Timekeeper::syncTimeWithNTP() {
|
|||||||
// Configure NTP with settings from config
|
// Configure NTP with settings from config
|
||||||
configTime(timeConfig.gmtOffsetSec, timeConfig.daylightOffsetSec, timeConfig.ntpServer.c_str());
|
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;
|
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
|
// Success! Update RTC with synchronized time
|
||||||
rtc.adjust(DateTime(timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
|
rtc.adjust(DateTime(timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday,
|
||||||
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec));
|
timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec));
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ BellEngine bellEngine(player, configManager, telemetry, outputManager); // 🔥
|
|||||||
|
|
||||||
TaskHandle_t bellEngineHandle = NULL; // Legacy - will be removed
|
TaskHandle_t bellEngineHandle = NULL; // Legacy - will be removed
|
||||||
TimerHandle_t schedulerTimer;
|
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()
|
void setup()
|
||||||
@@ -357,15 +366,25 @@ void setup()
|
|||||||
// Track if AsyncWebServer has been started to prevent duplicates
|
// Track if AsyncWebServer has been started to prevent duplicates
|
||||||
static bool webServerStarted = false;
|
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
|
// Set up network callbacks
|
||||||
networking.setNetworkCallbacks(
|
networking.setNetworkCallbacks(
|
||||||
[&webServerStarted]() {
|
[&webServerStarted]() {
|
||||||
communication.onNetworkConnected();
|
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)
|
// Skip NTP sync in AP mode (no internet connection)
|
||||||
if (!networking.isInAPMode()) {
|
if (!networking.isInAPMode() && ntpSyncTimer) {
|
||||||
timekeeper.syncTimeWithNTP();
|
LOG_DEBUG("Network connected - scheduling NTP sync after 3s stabilization (non-blocking)");
|
||||||
|
xTimerStart(ntpSyncTimer, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start AsyncWebServer when network becomes available (only once!)
|
// Start AsyncWebServer when network becomes available (only once!)
|
||||||
@@ -384,10 +403,11 @@ void setup()
|
|||||||
LOG_INFO("Network already connected - initializing services");
|
LOG_INFO("Network already connected - initializing services");
|
||||||
communication.onNetworkConnected();
|
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)
|
// Skip NTP sync in AP mode (no internet connection)
|
||||||
if (!networking.isInAPMode()) {
|
if (!networking.isInAPMode() && ntpSyncTimer) {
|
||||||
timekeeper.syncTimeWithNTP();
|
LOG_DEBUG("Network already connected - scheduling NTP sync after 3s stabilization (non-blocking)");
|
||||||
|
xTimerStart(ntpSyncTimer, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 🔥 CRITICAL: Start AsyncWebServer ONLY when network is ready
|
// 🔥 CRITICAL: Start AsyncWebServer ONLY when network is ready
|
||||||
|
|||||||
Reference in New Issue
Block a user