sntp_example_main.c 5.2 KB

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