forked from Akcelerometry_drgania_WMT/PI_mikrokontroler
Dodano synchronizacje zegara RTC z uzyciem NTP (eliminuje ostrzezenie o dacie 1970 z API)
This commit is contained in:
17
src/main.cpp
17
src/main.cpp
@@ -17,6 +17,7 @@
|
||||
#include <Settings.h>
|
||||
#include "APIClient.h"
|
||||
#include "UploadManager.h"
|
||||
#include <time.h>
|
||||
|
||||
#define WDT_TIMEOUT 60 // Czas watchdoga do restartu
|
||||
#define MAX_ADXL345_SENSORS 4 // Maksymalna ilość podłączanych sensorów
|
||||
@@ -64,6 +65,7 @@ void checkWiFi();
|
||||
void showOfflineScreen();
|
||||
void measure();
|
||||
void setClockRTCBtn();
|
||||
void syncNTP();
|
||||
|
||||
/* ******************* SETUP() ************************* */
|
||||
void setup() {
|
||||
@@ -213,6 +215,7 @@ void setup() {
|
||||
|
||||
if(config.connect){
|
||||
wifi.begin();
|
||||
syncNTP(); // Zaktualizuj RTC z internetu od razu po połączniu z WiFi
|
||||
wifiTestThread.onRun(checkWiFi);
|
||||
wifiTestThread.setInterval(5000); // Test WiFi co 3 sekundy
|
||||
} else {
|
||||
@@ -383,6 +386,20 @@ void settingsDevice(){
|
||||
settings.finishConfigDevice();
|
||||
}
|
||||
|
||||
void syncNTP() {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
ESP_LOGI(TAG_MAIN, "Syncing RTC with NTP server...");
|
||||
configTime(3600, 3600, "pool.ntp.org", "time.nist.gov"); // Strefa czasowa CET (+1h, +1h DST)
|
||||
struct tm timeinfo;
|
||||
if (getLocalTime(&timeinfo, 10000)) { // Czekaj max 10 sekund na czas z neta
|
||||
rtc.adjust(DateTime(timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec));
|
||||
ESP_LOGI(TAG_MAIN, "NTP Time synced! New RTC date: %d-%d-%d", timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday);
|
||||
} else {
|
||||
ESP_LOGE(TAG_MAIN, "NTP Sync failed. Using old RTC date.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///////////// LOOP ////////////////////////////////////////////
|
||||
void loop() {
|
||||
if (settings.readBtnUp() || settings.readBtnDown() || settings.readBtnOk()) {
|
||||
|
||||
Reference in New Issue
Block a user