From 9f938ab8ba5af561bd44dbc7142f338ce317a01a Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sat, 1 Nov 2025 09:46:52 +0800 Subject: Etlas project. --- esp32/main/ntp.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 esp32/main/ntp.c (limited to 'esp32/main/ntp.c') diff --git a/esp32/main/ntp.c b/esp32/main/ntp.c new file mode 100644 index 0000000..a1a7ded --- /dev/null +++ b/esp32/main/ntp.c @@ -0,0 +1,36 @@ +#include + +#include +#include +#include + +#include "ntp.h" + +const static char *TAG = "ntp"; + +void ntp_init(void) +{ + setenv("TZ", "CST-8", 1); + tzset(); + + ESP_LOGI(TAG, "initializing SNTP"); + esp_sntp_config_t sntp_conf = ESP_NETIF_SNTP_DEFAULT_CONFIG(CONFIG_SNTP_TIME_SERVER); + ESP_ERROR_CHECK(esp_netif_sntp_init(&sntp_conf)); +} + +int ntp_sync(void) +{ + int retry = 0; + const int retry_count = 15; + + while (esp_netif_sntp_sync_wait(2000 / portTICK_PERIOD_MS) == ESP_ERR_TIMEOUT) { + ESP_LOGI(TAG, "waiting for system time to be set... (%d/%d)", retry, retry_count); + if (++retry >= retry_count) { + ESP_LOGE(TAG, "Failed to sync system time"); + return 0; + } + } + + ESP_LOGI(TAG, "system time set"); + return 1; +} -- cgit v1.2.3