1 /* LwIP SNTP example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <string.h>
10 #include <time.h>
11 #include <sys/time.h>
12 #include "freertos/FreeRTOS.h"
13 #include "freertos/task.h"
14 #include "freertos/event_groups.h"
15 #include "esp_system.h"
16 #include "esp_event.h"
17 #include "esp_log.h"
18 #include "esp_attr.h"
19 #include "esp_sleep.h"
20 #include "nvs_flash.h"
21 #include "protocol_examples_common.h"
22 #include "esp_sntp.h"
23
24 static const char *TAG = "example";
25
26 /* Variable holding number of times ESP32 restarted since first boot.
27 * It is placed into RTC memory using RTC_DATA_ATTR and
28 * maintains its value when ESP32 wakes from deep sleep.
29 */
30 RTC_DATA_ATTR static int boot_count = 0;
31
32 static void obtain_time(void);
33 static void initialize_sntp(void);
34
35 #ifdef CONFIG_SNTP_TIME_SYNC_METHOD_CUSTOM
sntp_sync_time(struct timeval * tv)36 void sntp_sync_time(struct timeval *tv)
37 {
38 settimeofday(tv, NULL);
39 ESP_LOGI(TAG, "Time is synchronized from custom code");
40 sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED);
41 }
42 #endif
43
time_sync_notification_cb(struct timeval * tv)44 void time_sync_notification_cb(struct timeval *tv)
45 {
46 ESP_LOGI(TAG, "Notification of a time synchronization event");
47 }
48
app_main(void)49 void app_main(void)
50 {
51 ++boot_count;
52 ESP_LOGI(TAG, "Boot count: %d", boot_count);
53
54 time_t now;
55 struct tm timeinfo;
56 time(&now);
57 localtime_r(&now, &timeinfo);
58 // Is time set? If not, tm_year will be (1970 - 1900).
59 if (timeinfo.tm_year < (2016 - 1900)) {
60 ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");
61 obtain_time();
62 // update 'now' variable with current time
63 time(&now);
64 }
65 #ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
66 else {
67 // add 500 ms error to the current system time.
68 // Only to demonstrate a work of adjusting method!
69 {
70 ESP_LOGI(TAG, "Add a error for test adjtime");
71 struct timeval tv_now;
72 gettimeofday(&tv_now, NULL);
73 int64_t cpu_time = (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec;
74 int64_t error_time = cpu_time + 500 * 1000L;
75 struct timeval tv_error = { .tv_sec = error_time / 1000000L, .tv_usec = error_time % 1000000L };
76 settimeofday(&tv_error, NULL);
77 }
78
79 ESP_LOGI(TAG, "Time was set, now just adjusting it. Use SMOOTH SYNC method.");
80 obtain_time();
81 // update 'now' variable with current time
82 time(&now);
83 }
84 #endif
85
86 char strftime_buf[64];
87
88 // Set timezone to Eastern Standard Time and print local time
89 setenv("TZ", "EST5EDT,M3.2.0/2,M11.1.0", 1);
90 tzset();
91 localtime_r(&now, &timeinfo);
92 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
93 ESP_LOGI(TAG, "The current date/time in New York is: %s", strftime_buf);
94
95 // Set timezone to China Standard Time
96 setenv("TZ", "CST-8", 1);
97 tzset();
98 localtime_r(&now, &timeinfo);
99 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
100 ESP_LOGI(TAG, "The current date/time in Shanghai is: %s", strftime_buf);
101
102 if (sntp_get_sync_mode() == SNTP_SYNC_MODE_SMOOTH) {
103 struct timeval outdelta;
104 while (sntp_get_sync_status() == SNTP_SYNC_STATUS_IN_PROGRESS) {
105 adjtime(NULL, &outdelta);
106 ESP_LOGI(TAG, "Waiting for adjusting time ... outdelta = %li sec: %li ms: %li us",
107 (long)outdelta.tv_sec,
108 outdelta.tv_usec/1000,
109 outdelta.tv_usec%1000);
110 vTaskDelay(2000 / portTICK_PERIOD_MS);
111 }
112 }
113
114 const int deep_sleep_sec = 10;
115 ESP_LOGI(TAG, "Entering deep sleep for %d seconds", deep_sleep_sec);
116 esp_deep_sleep(1000000LL * deep_sleep_sec);
117 }
118
obtain_time(void)119 static void obtain_time(void)
120 {
121 ESP_ERROR_CHECK( nvs_flash_init() );
122 ESP_ERROR_CHECK(esp_netif_init());
123 ESP_ERROR_CHECK( esp_event_loop_create_default() );
124
125 /**
126 * NTP server address could be aquired via DHCP,
127 * see LWIP_DHCP_GET_NTP_SRV menuconfig option
128 */
129 #ifdef LWIP_DHCP_GET_NTP_SRV
130 sntp_servermode_dhcp(1);
131 #endif
132
133 /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
134 * Read "Establishing Wi-Fi or Ethernet Connection" section in
135 * examples/protocols/README.md for more information about this function.
136 */
137 ESP_ERROR_CHECK(example_connect());
138
139 initialize_sntp();
140
141 // wait for time to be set
142 time_t now = 0;
143 struct tm timeinfo = { 0 };
144 int retry = 0;
145 const int retry_count = 10;
146 while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
147 ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
148 vTaskDelay(2000 / portTICK_PERIOD_MS);
149 }
150 time(&now);
151 localtime_r(&now, &timeinfo);
152
153 ESP_ERROR_CHECK( example_disconnect() );
154 }
155
initialize_sntp(void)156 static void initialize_sntp(void)
157 {
158 ESP_LOGI(TAG, "Initializing SNTP");
159 sntp_setoperatingmode(SNTP_OPMODE_POLL);
160 sntp_setservername(0, "pool.ntp.org");
161 sntp_set_time_sync_notification_cb(time_sync_notification_cb);
162 #ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
163 sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
164 #endif
165 sntp_init();
166 }
167