sntp_example_main.c 6.9 KB

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