From 8a16be1bea38e0b7bac150cf49065a0aa7bd3424 Mon Sep 17 00:00:00 2001 From: Petr Kracik Date: Wed, 4 Feb 2026 14:45:47 +0100 Subject: [PATCH] NTP: Fix nullptr if network is connected after setup() --- include/ntp_utils.h | 2 +- src/ntp_utils.cpp | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/ntp_utils.h b/include/ntp_utils.h index d016a23..59acde2 100644 --- a/include/ntp_utils.h +++ b/include/ntp_utils.h @@ -24,7 +24,7 @@ namespace NTP_Utils { - void setup(); + bool setup(); void update(); String getFormatedTime(); diff --git a/src/ntp_utils.cpp b/src/ntp_utils.cpp index bec0e80..1874501 100644 --- a/src/ntp_utils.cpp +++ b/src/ntp_utils.cpp @@ -27,26 +27,40 @@ extern Configuration Config; extern NetworkManager *networkManager; WiFiUDP ntpUDP; -NTPClient* timeClient; +NTPClient* timeClient = nullptr; namespace NTP_Utils { - void setup() { + bool setup() { if (networkManager->isConnected() && Config.digi.ecoMode == 0 && Config.callsign != "NOCALL-10") { int gmt = Config.ntp.gmtCorrection * 3600; + Serial.println("[NTP] Setting up, TZ offset: " + String(gmt) + " Server: " + Config.ntp.server); timeClient = new NTPClient(ntpUDP, Config.ntp.server.c_str(), gmt, 15 * 60 * 1000); // Update interval 15 min timeClient->begin(); + return true; } + return false; } void update() { - if (networkManager->isConnected() && Config.digi.ecoMode == 0 && Config.callsign != "NOCALL-10") timeClient->update(); + if (!networkManager->isConnected() || Config.digi.ecoMode != 0 || Config.callsign == "NOCALL-10") { + return; + } + if (timeClient == nullptr) { + if (!setup()) { + return; + } + } + + timeClient->update(); } String getFormatedTime() { - if (networkManager->isConnected() && Config.digi.ecoMode == 0) return timeClient->getFormattedTime(); + if (networkManager->isConnected() && Config.digi.ecoMode == 0 && timeClient != nullptr) { + return timeClient->getFormattedTime(); + } return "DigiEcoMode Active"; } -} \ No newline at end of file +}