remove about edit spiffs try rtc
This commit is contained in:
@@ -5,6 +5,10 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <Wire.h>
|
||||
#include "RTClib.h"
|
||||
|
||||
RTC_PCF8523 rtc;
|
||||
|
||||
// Globale Zeitvariablen
|
||||
struct timeval tv;
|
||||
@@ -12,6 +16,9 @@ struct timezone tz;
|
||||
time_t now;
|
||||
struct tm timeinfo;
|
||||
|
||||
//Prototypen für Zeit-Management-Funktionen
|
||||
void setupRTC();
|
||||
void setRTC(DateTime dt);
|
||||
void setupTimeAPI(AsyncWebServer& server);
|
||||
String getCurrentTimeJSON();
|
||||
bool setSystemTime(long timestamp);
|
||||
@@ -49,6 +56,7 @@ bool setSystemTime(long timestamp) {
|
||||
|
||||
if (settimeofday(&tv, NULL) == 0) {
|
||||
Serial.println("Zeit erfolgreich gesetzt: " + String(timestamp));
|
||||
setRTC(DateTime(timestamp));
|
||||
return true;
|
||||
} else {
|
||||
Serial.println("Fehler beim Setzen der Zeit");
|
||||
@@ -58,6 +66,8 @@ bool setSystemTime(long timestamp) {
|
||||
|
||||
void setupTimeAPI(AsyncWebServer& server) {
|
||||
|
||||
setupRTC();
|
||||
|
||||
// API-Endpunkt: Aktuelle Zeit abrufen
|
||||
server.on("/api/time", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
String response = getCurrentTimeJSON();
|
||||
@@ -202,3 +212,37 @@ uint64_t getCurrentTimestampMs() {
|
||||
gettimeofday(&tv, NULL);
|
||||
return (uint64_t)tv.tv_sec * 1000LL + (uint64_t)tv.tv_usec / 1000LL;
|
||||
}
|
||||
|
||||
void setupRTC() {
|
||||
|
||||
Wire.begin();
|
||||
|
||||
Serial.println("Initialisiere RTC...");
|
||||
// Versuche RTC mit Wire zu initialisieren
|
||||
if (!rtc.begin()) { // Versuche RTC zu initialisieren, Timeout nach 10 Sekunden
|
||||
Serial.println("RTC nicht gefunden! Versuche erneut...");
|
||||
}
|
||||
|
||||
if (!rtc.initialized()) {
|
||||
Serial.println("RTC nicht initialisiert, versuche Initialisierung...");
|
||||
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
|
||||
} else {
|
||||
Serial.println("RTC bereits initialisiert.");
|
||||
}
|
||||
|
||||
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Setze die RTC auf die Kompilierungszeit
|
||||
Serial.println("RTC initialisiert.");
|
||||
// Aktuelle Zeit vom RTC abrufen
|
||||
DateTime now = rtc.now();
|
||||
Serial.printf("Aktuelle RTC-Zeit: %04d-%02d-%02d %02d:%02d:%02d\n",
|
||||
now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
|
||||
rtc.start(); // RTC starten, falls gestoppt
|
||||
}
|
||||
|
||||
// Funktion zum Setzen der RTC-Zeit
|
||||
void setRTC(DateTime dt) {
|
||||
rtc.adjust(dt);
|
||||
DateTime newtime = rtc.now();
|
||||
Serial.printf("RTC-Zeit gesetzt: %04d-%02d-%02d %02d:%02d:%02d\n",
|
||||
newtime.year(), newtime.month(), newtime.day(), newtime.hour(), newtime.minute(), newtime.second());
|
||||
}
|
||||
Reference in New Issue
Block a user