time_sntp.cpp 5.2 KB

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