time_sntp.cpp 5.0 KB

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