diff --git a/software/arduino/arduino.ino b/software/arduino/arduino.ino index 8e5f6a9..652c532 100644 --- a/software/arduino/arduino.ino +++ b/software/arduino/arduino.ino @@ -130,12 +130,15 @@ void loadSettings(){ configTime(tzString.c_str(), ntpServer.c_str()); } -// Zeit aus der RTC uebernehmen (bis NTP erreichbar ist). Die RTC laeuft in UTC. -void initRtc(){ +// Zeit aus der RTC uebernehmen. Die RTC laeuft in UTC. +#define RTC_INTERVAL_MS 3600000UL // ohne NTP: RTC stuendlich neu einlesen +#define NTP_GUELTIG_MS 7200000UL // NTP-Sync gilt 2 h als aktuell +unsigned long lastRtcReadMs = 0; +unsigned long lastNtpMs = 0; // 0 = noch kein NTP-Sync seit Start + +void readRtcToSystem(){ #ifdef RTC_VORHANDEN - Wire.begin(RTC_SDA, RTC_SCL); - if(!rtc.begin()) return; - rtcOk = true; + lastRtcReadMs = millis(); #if defined(RTC_MODUL_DS3231) bool valid = !rtc.lostPower(); #else @@ -150,12 +153,29 @@ void initRtc(){ #endif } -// Nach jeder NTP-Synchronisation die RTC nachstellen. -void updateRtcFromNtp(){ +void initRtc(){ #ifdef RTC_VORHANDEN - if(!ntpJustSynced) return; - ntpJustSynced = false; - if(rtcOk && timeSynced()) rtc.adjust(DateTime((uint32_t)time(nullptr))); + Wire.begin(RTC_SDA, RTC_SCL); + if(!rtc.begin()) return; + rtcOk = true; + readRtcToSystem(); +#endif +} + +// Nach jeder NTP-Synchronisation die RTC nachstellen; ohne aktuellen NTP-Sync +// stuendlich die Systemzeit aus der RTC auffrischen. +void updateRtc(){ +#ifdef RTC_VORHANDEN + if(!rtcOk) return; + if(ntpJustSynced){ + ntpJustSynced = false; + if(timeSynced()){ + lastNtpMs = millis() ? millis() : 1; + rtc.adjust(DateTime((uint32_t)time(nullptr))); + } + } + bool ntpAktuell = lastNtpMs != 0 && millis() - lastNtpMs < NTP_GUELTIG_MS; + if(!ntpAktuell && millis() - lastRtcReadMs >= RTC_INTERVAL_MS) readRtcToSystem(); #endif } @@ -511,7 +531,7 @@ void setup(){ void loop(){ server.handleClient(); - updateRtcFromNtp(); + updateRtc(); // Erst anzeigen, wenn die Zeit per NTP synchronisiert ist if(!timeSynced()) return; diff --git a/software/pio/src/main.cpp b/software/pio/src/main.cpp index 8e5f6a9..652c532 100644 --- a/software/pio/src/main.cpp +++ b/software/pio/src/main.cpp @@ -130,12 +130,15 @@ void loadSettings(){ configTime(tzString.c_str(), ntpServer.c_str()); } -// Zeit aus der RTC uebernehmen (bis NTP erreichbar ist). Die RTC laeuft in UTC. -void initRtc(){ +// Zeit aus der RTC uebernehmen. Die RTC laeuft in UTC. +#define RTC_INTERVAL_MS 3600000UL // ohne NTP: RTC stuendlich neu einlesen +#define NTP_GUELTIG_MS 7200000UL // NTP-Sync gilt 2 h als aktuell +unsigned long lastRtcReadMs = 0; +unsigned long lastNtpMs = 0; // 0 = noch kein NTP-Sync seit Start + +void readRtcToSystem(){ #ifdef RTC_VORHANDEN - Wire.begin(RTC_SDA, RTC_SCL); - if(!rtc.begin()) return; - rtcOk = true; + lastRtcReadMs = millis(); #if defined(RTC_MODUL_DS3231) bool valid = !rtc.lostPower(); #else @@ -150,12 +153,29 @@ void initRtc(){ #endif } -// Nach jeder NTP-Synchronisation die RTC nachstellen. -void updateRtcFromNtp(){ +void initRtc(){ #ifdef RTC_VORHANDEN - if(!ntpJustSynced) return; - ntpJustSynced = false; - if(rtcOk && timeSynced()) rtc.adjust(DateTime((uint32_t)time(nullptr))); + Wire.begin(RTC_SDA, RTC_SCL); + if(!rtc.begin()) return; + rtcOk = true; + readRtcToSystem(); +#endif +} + +// Nach jeder NTP-Synchronisation die RTC nachstellen; ohne aktuellen NTP-Sync +// stuendlich die Systemzeit aus der RTC auffrischen. +void updateRtc(){ +#ifdef RTC_VORHANDEN + if(!rtcOk) return; + if(ntpJustSynced){ + ntpJustSynced = false; + if(timeSynced()){ + lastNtpMs = millis() ? millis() : 1; + rtc.adjust(DateTime((uint32_t)time(nullptr))); + } + } + bool ntpAktuell = lastNtpMs != 0 && millis() - lastNtpMs < NTP_GUELTIG_MS; + if(!ntpAktuell && millis() - lastRtcReadMs >= RTC_INTERVAL_MS) readRtcToSystem(); #endif } @@ -511,7 +531,7 @@ void setup(){ void loop(){ server.handleClient(); - updateRtcFromNtp(); + updateRtc(); // Erst anzeigen, wenn die Zeit per NTP synchronisiert ist if(!timeSynced()) return;