NTP, send Voltage to Frontend

This commit is contained in:
Carsten Graf
2025-07-14 01:54:36 +02:00
parent d10a8fef6d
commit 58f0d234e0
9 changed files with 142 additions and 66 deletions

View File

@@ -49,6 +49,29 @@ String getCurrentTimeJSON() {
return response;
}
void syncTimeWithNTP(const char *ntpServer = "pool.ntp.org",
long gmtOffset_sec = 3600, int daylightOffset_sec = 0) {
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.println("Warte auf NTP-Zeit (max 5s)...");
struct tm timeinfo;
unsigned long start = millis();
bool synced = false;
while (millis() - start < 5000) {
if (getLocalTime(&timeinfo, 10)) { // 10ms Timeout pro Versuch
synced = true;
break;
}
delay(10); // Kurze Pause, damit der Loop nicht blockiert
}
if (synced) {
Serial.println("\nNTP-Zeit synchronisiert!");
Serial.printf("Aktuelle Zeit: %02d:%02d:%02d\n", timeinfo.tm_hour,
timeinfo.tm_min, timeinfo.tm_sec);
} else {
Serial.println("\nNTP-Sync fehlgeschlagen (Timeout nach 5s)");
}
}
// Hilfsfunktion: Setzt die Systemzeit auf den angegebenen Zeitstempel.
bool setSystemTime(long timestamp) {
struct timeval tv;