diff options
Diffstat (limited to 'software/main/sntp.c')
-rw-r--r-- | software/main/sntp.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/software/main/sntp.c b/software/main/sntp.c new file mode 100644 index 0000000..c639a79 --- /dev/null +++ b/software/main/sntp.c @@ -0,0 +1,45 @@ +#include <string.h> +#include <time.h> +#include <sys/time.h> +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "esp_system.h" +#include "esp_event.h" +#include "esp_log.h" +#include "esp_attr.h" +#include "esp_sleep.h" +#include "nvs_flash.h" +#include "esp_sntp.h" + +#include "../include/sntp.h" + +void obtain_time(void); +void initialize_sntp(void); + +void time_sync_notification_cb(struct timeval *tv) { + ESP_LOGI(SNTP_TAG, "Notification of a time synchronization event"); +} + +void initialize_sntp(void) { + ESP_LOGI(SNTP_TAG, "Initializing SNTP"); + sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_setservername(0, "pool.ntp.org"); + sntp_set_time_sync_notification_cb(time_sync_notification_cb); + sntp_init(); +} + +void obtain_time(void) { + initialize_sntp(); + + time_t now = 0; + struct tm timeinfo = {0}; + int retry = 0; + const int retry_count = 10; + while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) { + ESP_LOGI(SNTP_TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count); + vTaskDelay(2000 / portTICK_PERIOD_MS); + } + time(&now); + localtime_r(&now, &timeinfo); +}
\ No newline at end of file |