update project
This commit is contained in:
28
esp32/main/time_sync.cpp
Normal file
28
esp32/main/time_sync.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "time_sync.h"
|
||||
#include "esp_sntp.h"
|
||||
#include "esp_log.h"
|
||||
#include <time.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
void init_time() {
|
||||
ESP_LOGI("TIME", "Initializing SNTP");
|
||||
|
||||
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
esp_sntp_setservername(0, "pool.ntp.org");
|
||||
esp_sntp_init();
|
||||
|
||||
setenv("TZ", "EET-2EEST,M3.5.0/3,M10.5.0/4", 1);
|
||||
tzset();
|
||||
|
||||
time_t now = 0;
|
||||
struct tm timeinfo{}; // ← ВАЖНО
|
||||
|
||||
int retry = 0;
|
||||
while (timeinfo.tm_year < (2020 - 1900) && retry++ < 10) {
|
||||
ESP_LOGI("TIME", "Waiting for time...");
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
time(&now);
|
||||
localtime_r(&now, &timeinfo);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user