time_sntp.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "ClassLogFile.h"
  23. static const char *TAG = "sntp";
  24. RTC_DATA_ATTR int boot_count = 0;
  25. bool setTimeAlwaysOnReboot = true;
  26. static void obtain_time(void);
  27. static void initialize_sntp(void);
  28. void time_sync_notification_cb(struct timeval *tv)
  29. {
  30. // LogFile.WriteToFile("Notification of a time synchronization event");
  31. ESP_LOGI(TAG, "Notification of a time synchronization event");
  32. }
  33. std::string gettimestring(const char * frm)
  34. {
  35. time_t now;
  36. struct tm timeinfo;
  37. time(&now);
  38. char strftime_buf[64];
  39. localtime_r(&now, &timeinfo);
  40. strftime(strftime_buf, sizeof(strftime_buf), frm, &timeinfo);
  41. std::string result(strftime_buf);
  42. return result;
  43. }
  44. void setup_time()
  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)) || setTimeAlwaysOnReboot) {
  54. ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");
  55. initialize_sntp();
  56. obtain_time();
  57. // update 'now' variable with current time
  58. time(&now);
  59. }
  60. char strftime_buf[64];
  61. setTimeZone("CET-1CEST,M3.5.0,M10.5.0/3");
  62. // setTimeZone("Europe/Berlin");
  63. // setTimeZone("Asia/Tokyo");
  64. localtime_r(&now, &timeinfo);
  65. strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
  66. ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf);
  67. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M", &timeinfo);
  68. ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf);
  69. std::string zw = gettimestring("%Y%m%d-%H%M%S");
  70. printf("timeist %s\n", zw.c_str());
  71. }
  72. void setTimeZone(std::string _tzstring)
  73. {
  74. setenv("TZ", _tzstring.c_str(), 1);
  75. tzset();
  76. printf("TimeZone set to %s\n", _tzstring.c_str());
  77. _tzstring = "Time zone set to " + _tzstring;
  78. LogFile.WriteToFile(_tzstring);
  79. }
  80. static void obtain_time(void)
  81. {
  82. // initialize_sntp();
  83. // wait for time to be set
  84. time_t now = 0;
  85. struct tm timeinfo = {};
  86. int retry = 0;
  87. const int retry_count = 10;
  88. time(&now);
  89. localtime_r(&now, &timeinfo);
  90. while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
  91. ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
  92. vTaskDelay(2000 / portTICK_PERIOD_MS);
  93. }
  94. if (retry == retry_count) {
  95. // LogFile.WriteToFile("Time Synchzronisation nicht erfolgreich ...");
  96. }
  97. else
  98. {
  99. // LogFile.WriteToFile("Time erfolgreich ...");
  100. }
  101. time(&now);
  102. localtime_r(&now, &timeinfo);
  103. }
  104. static void initialize_sntp(void)
  105. {
  106. ESP_LOGI(TAG, "Initializing SNTP");
  107. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  108. sntp_setservername(0, "pool.ntp.org");
  109. // sntp_set_time_sync_notification_cb(time_sync_notification_cb);
  110. sntp_init();
  111. }