RTC ohne aktuellen NTP-Sync stuendlich neu einlesen

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
lukarioandClaude Sonnet 5.5 committed 2026-10-19 20:01:18 +02:00
1 parent deb5f3e500
commit 20fc9092e6
2 files changed
+62 -22

No files matched your search

+31 -11
View File
@@ -130,12 +130,15 @@ void loadSettings(){
configTime(tzString.c_str(), ntpServer.c_str()); configTime(tzString.c_str(), ntpServer.c_str());
} }
// Zeit aus der RTC uebernehmen (bis NTP erreichbar ist). Die RTC laeuft in UTC. // Zeit aus der RTC uebernehmen. Die RTC laeuft in UTC.
void initRtc(){ #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 #ifdef RTC_VORHANDEN
Wire.begin(RTC_SDA, RTC_SCL); lastRtcReadMs = millis();
if(!rtc.begin()) return;
rtcOk = true;
#if defined(RTC_MODUL_DS3231) #if defined(RTC_MODUL_DS3231)
bool valid = !rtc.lostPower(); bool valid = !rtc.lostPower();
#else #else
@@ -150,12 +153,29 @@ void initRtc(){
#endif #endif
} }
// Nach jeder NTP-Synchronisation die RTC nachstellen. void initRtc(){
void updateRtcFromNtp(){
#ifdef RTC_VORHANDEN #ifdef RTC_VORHANDEN
if(!ntpJustSynced) return; Wire.begin(RTC_SDA, RTC_SCL);
ntpJustSynced = false; if(!rtc.begin()) return;
if(rtcOk && timeSynced()) rtc.adjust(DateTime((uint32_t)time(nullptr))); 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 #endif
} }
@@ -511,7 +531,7 @@ void setup(){
void loop(){ void loop(){
server.handleClient(); server.handleClient();
updateRtcFromNtp(); updateRtc();
// Erst anzeigen, wenn die Zeit per NTP synchronisiert ist // Erst anzeigen, wenn die Zeit per NTP synchronisiert ist
if(!timeSynced()) return; if(!timeSynced()) return;
+31 -11
View File
@@ -130,12 +130,15 @@ void loadSettings(){
configTime(tzString.c_str(), ntpServer.c_str()); configTime(tzString.c_str(), ntpServer.c_str());
} }
// Zeit aus der RTC uebernehmen (bis NTP erreichbar ist). Die RTC laeuft in UTC. // Zeit aus der RTC uebernehmen. Die RTC laeuft in UTC.
void initRtc(){ #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 #ifdef RTC_VORHANDEN
Wire.begin(RTC_SDA, RTC_SCL); lastRtcReadMs = millis();
if(!rtc.begin()) return;
rtcOk = true;
#if defined(RTC_MODUL_DS3231) #if defined(RTC_MODUL_DS3231)
bool valid = !rtc.lostPower(); bool valid = !rtc.lostPower();
#else #else
@@ -150,12 +153,29 @@ void initRtc(){
#endif #endif
} }
// Nach jeder NTP-Synchronisation die RTC nachstellen. void initRtc(){
void updateRtcFromNtp(){
#ifdef RTC_VORHANDEN #ifdef RTC_VORHANDEN
if(!ntpJustSynced) return; Wire.begin(RTC_SDA, RTC_SCL);
ntpJustSynced = false; if(!rtc.begin()) return;
if(rtcOk && timeSynced()) rtc.adjust(DateTime((uint32_t)time(nullptr))); 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 #endif
} }
@@ -511,7 +531,7 @@ void setup(){
void loop(){ void loop(){
server.handleClient(); server.handleClient();
updateRtcFromNtp(); updateRtc();
// Erst anzeigen, wenn die Zeit per NTP synchronisiert ist // Erst anzeigen, wenn die Zeit per NTP synchronisiert ist
if(!timeSynced()) return; if(!timeSynced()) return;