mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-05 13:44:32 +02:00
example: Updated https_request example to fail when time is not synced.
Also added additional server for syncing time
This commit is contained in:
@@ -280,7 +280,7 @@ void app_main(void)
|
||||
}
|
||||
|
||||
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;
|
||||
|
@@ -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
|
||||
*/
|
||||
@@ -20,7 +20,7 @@ esp_err_t update_time_from_nvs(void);
|
||||
* @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
|
||||
}
|
||||
|
@@ -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
|
||||
*/
|
||||
@@ -34,14 +34,15 @@ void initialize_sntp(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing SNTP");
|
||||
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
|
||||
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
|
||||
#endif
|
||||
sntp_init();
|
||||
}
|
||||
|
||||
static void obtain_time(void)
|
||||
static esp_err_t obtain_time(void)
|
||||
{
|
||||
/**
|
||||
* 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);
|
||||
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();
|
||||
obtain_time();
|
||||
if (obtain_time() != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
nvs_handle_t my_handle;
|
||||
esp_err_t err;
|
||||
@@ -97,6 +104,7 @@ exit:
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Updated time in NVS");
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
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", ×tamp);
|
||||
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.");
|
||||
if (fetch_and_store_time_in_nvs(NULL) != ESP_OK) {
|
||||
err = ESP_FAIL;
|
||||
} else {
|
||||
err = ESP_OK;
|
||||
}
|
||||
} else if (err == ESP_OK) {
|
||||
struct timeval get_nvs_time;
|
||||
get_nvs_time.tv_sec = timestamp;
|
||||
|
@@ -1 +1,2 @@
|
||||
CONFIG_MBEDTLS_HAVE_TIME_DATE=y
|
||||
CONFIG_LWIP_SNTP_MAX_SERVERS=2
|
||||
|
Reference in New Issue
Block a user