RTC ohne aktuellen NTP-Sync stuendlich neu einlesen
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
deb5f3e500
commit
20fc9092e6
2 files changed
+60
-20
No files matched your search
@@ -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;
|
||||
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(rtcOk && timeSynced()) rtc.adjust(DateTime((uint32_t)time(nullptr)));
|
||||
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;
|
||||
|
||||
+30
-10
@@ -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;
|
||||
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(rtcOk && timeSynced()) rtc.adjust(DateTime((uint32_t)time(nullptr)));
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user