time_sntp.cpp 3.4 KB

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