time_sntp.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. bool setTimeAlwaysOnReboot = true;
  17. time_t bootTime;
  18. static void obtain_time(void);
  19. static void initialize_sntp(void);
  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. // Is time set? If not, tm_year will be (1970 - 1900).
  51. if ((timeinfo.tm_year < (2016 - 1900)) || setTimeAlwaysOnReboot) {
  52. ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");
  53. initialize_sntp();
  54. obtain_time();
  55. // update 'now' variable with current time
  56. time(&now);
  57. }
  58. char strftime_buf[64];
  59. setTimeZone("CET-1CEST,M3.5.0,M10.5.0/3");
  60. localtime_r(&now, &timeinfo);
  61. strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
  62. ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf);
  63. strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M", &timeinfo);
  64. ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf);
  65. std::string zw = gettimestring("%Y%m%d-%H%M%S");
  66. printf("timeist %s\n", zw.c_str());
  67. }
  68. void setTimeZone(std::string _tzstring)
  69. {
  70. setenv("TZ", _tzstring.c_str(), 1);
  71. tzset();
  72. printf("TimeZone set to %s\n", _tzstring.c_str());
  73. _tzstring = "Time zone set to " + _tzstring;
  74. LogFile.WriteToFile(_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. while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
  85. ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
  86. vTaskDelay(2000 / portTICK_PERIOD_MS);
  87. }
  88. time(&now);
  89. localtime_r(&now, &timeinfo);
  90. }
  91. void reset_servername(std::string _servername)
  92. {
  93. printf("Set SNTP-Server: %s\n", _servername.c_str());
  94. sntp_stop();
  95. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  96. sntp_setservername(0, _servername.c_str());
  97. sntp_init();
  98. obtain_time();
  99. std::string zw = gettimestring("%Y%m%d-%H%M%S");
  100. printf("Time ist %s\n", zw.c_str());
  101. }
  102. static void initialize_sntp(void)
  103. {
  104. ESP_LOGI(TAG, "Initializing SNTP");
  105. sntp_setoperatingmode(SNTP_OPMODE_POLL);
  106. sntp_setservername(0, "pool.ntp.org");
  107. // sntp_set_time_sync_notification_cb(time_sync_notification_cb);
  108. sntp_init();
  109. }
  110. void setBootTime()
  111. {
  112. time(&bootTime);
  113. }