time_sntp.cpp 3.1 KB

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