Improved NTP handling on boot

This commit is contained in:
M-Factory
2025-11-17 18:25:53 +09:00
parent 2a6ab2d1be
commit 378f79c60e
2 changed files with 23 additions and 53 deletions

View File

@@ -551,32 +551,17 @@ void setupTime() {
Serial.println(F("[TIME] Starting NTP sync"));
}
bool serverOk = false;
IPAddress resolvedIP;
// Try first server if it's not empty
if (strlen(ntpServer1) > 0 && WiFi.hostByName(ntpServer1, resolvedIP) == 1) {
serverOk = true;
}
// Try second server if first failed
else if (strlen(ntpServer2) > 0 && WiFi.hostByName(ntpServer2, resolvedIP) == 1) {
serverOk = true;
}
if (serverOk) {
configTime(0, 0, ntpServer1, ntpServer2); // safe to call now
setenv("TZ", ianaToPosix(timeZone), 1);
tzset();
ntpState = NTP_SYNCING;
ntpStartTime = millis();
ntpRetryCount = 0;
ntpSyncSuccessful = false;
} else {
Serial.println(F("[TIME] NTP server lookup failed — retry sync in 30 seconds"));
ntpSyncSuccessful = false;
ntpState = NTP_SYNCING; // instead of NTP_IDLE
ntpStartTime = millis(); // start the failed timer (so retry delay counts from now)
}
configTime(0, 0, ntpServer1, ntpServer2);
// Set the Time Zone
setenv("TZ", ianaToPosix(timeZone), 1);
tzset();
// Initialize state flags to begin synchronization tracking
ntpState = NTP_SYNCING;
ntpStartTime = millis();
ntpRetryCount = 0;
ntpSyncSuccessful = false;
}

View File

@@ -543,37 +543,22 @@ void clearWiFiCredentialsInConfig() {
// Time / NTP Functions
// -----------------------------------------------------------------------------
void setupTime() {
sntp_stop();
sntp_stop();
if (!isAPMode) {
Serial.println(F("[TIME] Starting NTP sync"));
}
bool serverOk = false;
IPAddress resolvedIP;
// Try first server if it's not empty
if (strlen(ntpServer1) > 0 && WiFi.hostByName(ntpServer1, resolvedIP) == 1) {
serverOk = true;
}
// Try second server if first failed
else if (strlen(ntpServer2) > 0 && WiFi.hostByName(ntpServer2, resolvedIP) == 1) {
serverOk = true;
}
if (serverOk) {
configTime(0, 0, ntpServer1, ntpServer2); // safe to call now
setenv("TZ", ianaToPosix(timeZone), 1);
tzset();
ntpState = NTP_SYNCING;
ntpStartTime = millis();
ntpRetryCount = 0;
ntpSyncSuccessful = false;
} else {
Serial.println(F("[TIME] NTP server lookup failed — retry sync in 30 seconds"));
ntpSyncSuccessful = false;
ntpState = NTP_SYNCING; // instead of NTP_IDLE
ntpStartTime = millis(); // start the failed timer (so retry delay counts from now)
}
configTime(0, 0, ntpServer1, ntpServer2);
// Set the Time Zone
setenv("TZ", ianaToPosix(timeZone), 1);
tzset();
// Initialize state flags (essential for your loop logic to handle retries)
ntpState = NTP_SYNCING;
ntpStartTime = millis();
ntpRetryCount = 0;
ntpSyncSuccessful = false;
}