Select one of the symbols to view example projects that use it.
 
Outline
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "esp_netif.h"
#include "esp_netif_sntp.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "time_sync.h"
TAG
#define STORAGE_NAMESPACE
initialize_sntp()
obtain_time()
fetch_and_store_time_in_nvs(void *)
update_time_from_nvs()
Files
loading...
SourceVuESP-IDF Framework and Exampleshttps_request samplemain/time_sync.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <string.h> #include <stdlib.h> #include <time.h> #include <sys/time.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" #include "nvs.h" #include "esp_netif.h" #include "esp_netif_sntp.h" #include "lwip/err.h" #include "lwip/sockets.h" #include "lwip/sys.h" #include "lwip/netdb.h" #include "lwip/dns.h" #include "time_sync.h"21 includes static const char *TAG = "time_sync"; #define STORAGE_NAMESPACE "storage" void initialize_sntp(void) { ESP_LOGI(TAG, "Initializing SNTP"); esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(2, ESP_SNTP_SERVER_LIST("time.windows.com", "pool.ntp.org" ) ); esp_netif_sntp_init(&config); }{ ... } static esp_err_t obtain_time(void) { // wait for time to be set int retry = 0; const int retry_count = 10; while (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(2000)) != ESP_OK && ++retry < retry_count) { ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count); }{...} if (retry == retry_count) { return ESP_FAIL; }{...} return ESP_OK; }{ ... } esp_err_t fetch_and_store_time_in_nvs(void *args) { nvs_handle_t my_handle = 0; esp_err_t err; initialize_sntp(); if (obtain_time() != ESP_OK) { err = ESP_FAIL; goto exit; }{...} time_t now; time(&now); //Open err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle); if (err != ESP_OK) { goto exit; }{...} //Write err = nvs_set_i64(my_handle, "timestamp", now); if (err != ESP_OK) { goto exit; }{...} err = nvs_commit(my_handle); if (err != ESP_OK) { goto exit; }{...} exit: if (my_handle != 0) { nvs_close(my_handle); }{...} esp_netif_sntp_deinit(); if (err != ESP_OK) { ESP_LOGE(TAG, "Error updating time in nvs"); }{...} else { ESP_LOGI(TAG, "Updated time in NVS"); }{...} return err; }{ ... } esp_err_t update_time_from_nvs(void) { nvs_handle_t my_handle = 0; esp_err_t err; err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle); if (err != ESP_OK) { ESP_LOGE(TAG, "Error opening NVS"); goto exit; }{...} int64_t timestamp = 0; err = nvs_get_i64(my_handle, "timestamp", &timestamp); if (err == ESP_ERR_NVS_NOT_FOUND) { 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; settimeofday(&get_nvs_time, NULL); }{...} exit: if (my_handle != 0) { nvs_close(my_handle); }{...} return err; }{ ... }
Details