example: Updated https_request example to fail when time is not synced.

Also added additional server for syncing time
This commit is contained in:
Harshit Malpani
2022-08-22 16:00:24 +05:30
parent cb48a7b9f9
commit eb47729301
4 changed files with 23 additions and 10 deletions

View File

@@ -280,7 +280,7 @@ void app_main(void)
} }
const esp_timer_create_args_t nvs_update_timer_args = { const esp_timer_create_args_t nvs_update_timer_args = {
.callback = &fetch_and_store_time_in_nvs, .callback = (void *)&fetch_and_store_time_in_nvs,
}; };
esp_timer_handle_t nvs_update_timer; esp_timer_handle_t nvs_update_timer;

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -20,7 +20,7 @@ esp_err_t update_time_from_nvs(void);
* @brief Fetch the current time from SNTP and stores it in NVS. * @brief Fetch the current time from SNTP and stores it in NVS.
* *
*/ */
void fetch_and_store_time_in_nvs(void*); esp_err_t fetch_and_store_time_in_nvs(void*);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -34,14 +34,15 @@ void initialize_sntp(void)
{ {
ESP_LOGI(TAG, "Initializing SNTP"); ESP_LOGI(TAG, "Initializing SNTP");
sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org"); sntp_setservername(0, "time.windows.com");
sntp_setservername(1, "pool.ntp.org");
#ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH #ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH); sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
#endif #endif
sntp_init(); sntp_init();
} }
static void obtain_time(void) static esp_err_t obtain_time(void)
{ {
/** /**
* NTP server address could be aquired via DHCP, * NTP server address could be aquired via DHCP,
@@ -58,12 +59,18 @@ static void obtain_time(void)
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count); ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
vTaskDelay(2000 / portTICK_PERIOD_MS); vTaskDelay(2000 / portTICK_PERIOD_MS);
} }
if (retry == retry_count) {
return ESP_FAIL;
}
return ESP_OK;
} }
void fetch_and_store_time_in_nvs(void *args) esp_err_t fetch_and_store_time_in_nvs(void *args)
{ {
initialize_sntp(); initialize_sntp();
obtain_time(); if (obtain_time() != ESP_OK) {
return ESP_FAIL;
}
nvs_handle_t my_handle; nvs_handle_t my_handle;
esp_err_t err; esp_err_t err;
@@ -97,6 +104,7 @@ exit:
} else { } else {
ESP_LOGI(TAG, "Updated time in NVS"); ESP_LOGI(TAG, "Updated time in NVS");
} }
return err;
} }
esp_err_t update_time_from_nvs(void) esp_err_t update_time_from_nvs(void)
@@ -114,8 +122,12 @@ esp_err_t update_time_from_nvs(void)
err = nvs_get_i64(my_handle, "timestamp", &timestamp); err = nvs_get_i64(my_handle, "timestamp", &timestamp);
if (err == ESP_ERR_NVS_NOT_FOUND) { if (err == ESP_ERR_NVS_NOT_FOUND) {
fetch_and_store_time_in_nvs(NULL); ESP_LOGI(TAG, "Time not found in NVS. Syncing time from SNTP server.");
err = ESP_OK; if (fetch_and_store_time_in_nvs(NULL) != ESP_OK) {
err = ESP_FAIL;
} else {
err = ESP_OK;
}
} else if (err == ESP_OK) { } else if (err == ESP_OK) {
struct timeval get_nvs_time; struct timeval get_nvs_time;
get_nvs_time.tv_sec = timestamp; get_nvs_time.tv_sec = timestamp;

View File

@@ -1 +1,2 @@
CONFIG_MBEDTLS_HAVE_TIME_DATE=y CONFIG_MBEDTLS_HAVE_TIME_DATE=y
CONFIG_LWIP_SNTP_MAX_SERVERS=2