domotic/main/obtain_time.c
2024-10-25 21:38:47 +02:00

32 lines
963 B
C

#include "esp_log.h"
#include "esp_netif_sntp.h"
#include "esp_sntp.h"
#include "obtain_time.h"
static const char *TAG = "sntp";
void time_sync_notification_cb(struct timeval *tv)
{
ESP_LOGI(TAG, "Notification of a time synchronization event");
}
void obtain_time(void)
{
ESP_LOGI(TAG, "Initializing and starting SNTP");
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
config.sync_cb = time_sync_notification_cb; // Note: This is only needed if we want
esp_netif_sntp_init(&config);
// wait for time to be set
time_t now = 0;
struct tm timeinfo = { 0 };
int retry = 0;
const int retry_count = 15;
while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) == ESP_ERR_TIMEOUT && ++retry < retry_count) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
}
time(&now);
localtime_r(&now, &timeinfo);
esp_netif_sntp_deinit();
}