time_sntp.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "time_sntp.h"
  2. #include <string>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "freertos/event_groups.h"
  8. #include "esp_system.h"
  9. #include "esp_event.h"
  10. #include "esp_log.h"
  11. #include "esp_attr.h"
  12. #include "esp_sleep.h"
  13. #include "esp_sntp.h"
  14. #include "ClassLogFile.h"
  15. static const char *TAG = "SNTP";
  16. time_t bootTime;
  17. static void obtain_time(void);
  18. static void initialize_sntp(void);
  19. static void logNtpStatus(sntp_sync_status_t status);
  20. void time_sync_notification_cb(struct timeval *tv)
  21. {
  22. ESP_LOGI(TAG, "Notification of a time synchronization event");
  23. }
  24. std::string ConvertTimeToString(time_t _time, const char * frm)
  25. {
  26. struct tm timeinfo;
  27. char strftime_buf[64];
  28. localtime_r(&_time, &timeinfo);
  29. strftime(strftime_buf, sizeof(strftime_buf), frm, &timeinfo);
  30. std::string result(strftime_buf);
  31. return result;
  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. time_t now;
  47. struct tm timeinfo;
  48. time(&now);
  49. localtime_r(&now, &timeinfo);
  50. char strftime_buf[64];
  51. // Is time set? If not, tm_year will be (1970 - 1900).
  52. if (!getTimeIsSet()) {
  53. ESP_LOGI(TAG, "Time is not set yet. Getting time over NTP.");
  54. initialize_sntp();
  55. obtain_time();
  56. // update 'now' variable with current time
  57. time(&now);
  58. setTimeZone("CET-1CEST,M3.5.0,M10.5.0/3");
  59. localtime_r(&now, &timeinfo);
  60. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M:%S", &timeinfo);
  61. ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf);
  62. }
  63. else {
  64. localtime_r(&now, &timeinfo);
  65. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M:%S", &timeinfo);
  66. ESP_LOGI(TAG, "Time is already set (%s)", strftime_buf);
  67. }
  68. }
  69. void setTimeZone(std::string _tzstring)
  70. {
  71. setenv("TZ", _tzstring.c_str(), 1);
  72. tzset();
  73. _tzstring = "Time zone set to " + _tzstring;
  74. LogFile.WriteToFile(ESP_LOG_INFO, TAG, _tzstring);
  75. }
  76. static void obtain_time(void)
  77. {
  78. time_t now = 0;
  79. struct tm timeinfo = {};
  80. int retry = 0;
  81. const int retry_count = 10;
  82. time(&now);
  83. localtime_r(&now, &timeinfo);
  84. ESP_LOGI(TAG, "Waiting until we get a time from the NTP server...");
  85. while (true) {
  86. retry++;
  87. if (retry == retry_count) {
  88. ESP_LOGW(TAG, "NTP time fetching seems to take longer, will check again on next round!"); // The NTP client will automatically retry periodically!
  89. break;
  90. }
  91. sntp_sync_status_t status = sntp_get_sync_status();
  92. logNtpStatus(status);
  93. if (status == SNTP_SYNC_STATUS_COMPLETED) {
  94. ESP_LOGI(TAG, "Time is synced with NTP Server");
  95. break;
  96. }
  97. vTaskDelay(2000 / portTICK_PERIOD_MS);
  98. }
  99. time(&now);
  100. localtime_r(&now, &timeinfo);
  101. }
  102. void logNtpStatus(sntp_sync_status_t status) {
  103. if (status == SNTP_SYNC_STATUS_COMPLETED) {
  104. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "NTP Status OK");
  105. }
  106. else if (status == SNTP_SYNC_STATUS_IN_PROGRESS) {
  107. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "NTP Status: In Progress");
  108. }
  109. else { // SNTP_SYNC_STATUS_RESET
  110. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "NTP Status: Reset");
  111. }
  112. }
  113. void reset_servername(std::string _servername)
  114. {
  115. ESP_LOGD(TAG, "Set SNTP-Server: %s", _servername.c_str());
  116. sntp_stop();
  117. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  118. sntp_setservername(0, _servername.c_str());
  119. sntp_init();
  120. obtain_time();
  121. std::string zw = gettimestring("%Y%m%d-%H%M%S");
  122. ESP_LOGD(TAG, "Time ist %s", zw.c_str());
  123. }
  124. static void initialize_sntp(void)
  125. {
  126. ESP_LOGI(TAG, "Initializing SNTP");
  127. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  128. sntp_setservername(0, "pool.ntp.org");
  129. // sntp_set_time_sync_notification_cb(time_sync_notification_cb);
  130. sntp_init();
  131. }
  132. void setBootTime()
  133. {
  134. time(&bootTime);
  135. }
  136. time_t getUpTime()
  137. {
  138. time_t now;
  139. time(&now);
  140. return now - bootTime;
  141. }
  142. bool getTimeIsSet(void) {
  143. time_t now;
  144. struct tm timeinfo;
  145. time(&now);
  146. localtime_r(&now, &timeinfo);
  147. char strftime_buf[64];
  148. localtime_r(&now, &timeinfo);
  149. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M:%S", &timeinfo);
  150. ESP_LOGD(TAG, "The current date/time in Berlin is: %s", strftime_buf);
  151. // Is time set? If not, tm_year will be (1970 - 1900).
  152. if ((timeinfo.tm_year < (2022 - 1900))) {
  153. return false;
  154. }
  155. else {
  156. return true;
  157. }
  158. }
  159. void restartNtpClient(void) {
  160. sntp_restart();
  161. obtain_time();
  162. }