time_sntp.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "time_sntp.h"
  2. /* LwIP SNTP example
  3. This example code is in the Public Domain (or CC0 licensed, at your option.)
  4. Unless required by applicable law or agreed to in writing, this
  5. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  6. CONDITIONS OF ANY KIND, either express or implied.
  7. */
  8. #include <string>
  9. #include <time.h>
  10. #include <sys/time.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/event_groups.h"
  14. #include "esp_system.h"
  15. #include "esp_event.h"
  16. #include "esp_log.h"
  17. #include "esp_attr.h"
  18. #include "esp_sleep.h"
  19. #include "nvs_flash.h"
  20. #include "protocol_examples_common.h"
  21. #include "esp_sntp.h"
  22. static const char *TAG = "sntp";
  23. RTC_DATA_ATTR int boot_count = 0;
  24. bool setTimeAlwaysOnReboot = true;
  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. static void obtain_time(void);
  30. static void initialize_sntp(void);
  31. void time_sync_notification_cb(struct timeval *tv)
  32. {
  33. ESP_LOGI(TAG, "Notification of a time synchronization event");
  34. }
  35. std::string gettimestring(const char * frm)
  36. {
  37. time_t now;
  38. struct tm timeinfo;
  39. time(&now);
  40. localtime_r(&now, &timeinfo);
  41. // Is time set? If not, tm_year will be (1970 - 1900).
  42. if (timeinfo.tm_year < (2016 - 1900)) {
  43. ESP_LOGI(TAG, "Reboot - Connecting to WiFi and getting time over NTP.");
  44. obtain_time();
  45. // update 'now' variable with current time
  46. time(&now);
  47. }
  48. char strftime_buf[64];
  49. setenv("TZ", "UTC-2", 1);
  50. tzset();
  51. localtime_r(&now, &timeinfo);
  52. strftime(strftime_buf, sizeof(strftime_buf), frm, &timeinfo);
  53. std::string result(strftime_buf);
  54. return result;
  55. }
  56. void setup_time(void)
  57. {
  58. ++boot_count;
  59. ESP_LOGI(TAG, "Boot count: %d", boot_count);
  60. time_t now;
  61. struct tm timeinfo;
  62. time(&now);
  63. localtime_r(&now, &timeinfo);
  64. // Is time set? If not, tm_year will be (1970 - 1900).
  65. if ((timeinfo.tm_year < (2016 - 1900)) || setTimeAlwaysOnReboot) {
  66. ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");
  67. initialize_sntp();
  68. obtain_time();
  69. // update 'now' variable with current time
  70. time(&now);
  71. }
  72. char strftime_buf[64];
  73. // Set timezone to Berlin Standard Time
  74. setenv("TZ", "UTC+9", 1);
  75. // setenv("TZ", "Europe/Berlin", 1);
  76. tzset();
  77. localtime_r(&now, &timeinfo);
  78. strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
  79. ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf);
  80. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M", &timeinfo);
  81. ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf);
  82. std::string zw = gettimestring("%Y%m%d-%H%M%S");
  83. printf("time %s\n", zw.c_str());
  84. }
  85. static void obtain_time(void)
  86. {
  87. // initialize_sntp();
  88. // wait for time to be set
  89. time_t now = 0;
  90. struct tm timeinfo = {};
  91. int retry = 0;
  92. const int retry_count = 10;
  93. while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
  94. ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
  95. vTaskDelay(2000 / portTICK_PERIOD_MS);
  96. }
  97. time(&now);
  98. localtime_r(&now, &timeinfo);
  99. }
  100. static void initialize_sntp(void)
  101. {
  102. ESP_LOGI(TAG, "Initializing SNTP");
  103. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  104. sntp_setservername(0, "pool.ntp.org");
  105. sntp_set_time_sync_notification_cb(time_sync_notification_cb);
  106. sntp_init();
  107. }