| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #include "time_sntp.h"
- /* LwIP SNTP example
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <string>
- #include <time.h>
- #include <sys/time.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/event_groups.h"
- #include "esp_system.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "esp_attr.h"
- #include "esp_sleep.h"
- #include "nvs_flash.h"
- #include "protocol_examples_common.h"
- #include "esp_sntp.h"
- static const char *TAG = "sntp";
- RTC_DATA_ATTR int boot_count = 0;
- bool setTimeAlwaysOnReboot = true;
- /* Variable holding number of times ESP32 restarted since first boot.
- * It is placed into RTC memory using RTC_DATA_ATTR and
- * maintains its value when ESP32 wakes from deep sleep.
- */
- static void obtain_time(void);
- static void initialize_sntp(void);
- void time_sync_notification_cb(struct timeval *tv)
- {
- ESP_LOGI(TAG, "Notification of a time synchronization event");
- }
- std::string gettimestring(const char * frm)
- {
- time_t now;
- struct tm timeinfo;
- time(&now);
- localtime_r(&now, &timeinfo);
- // Is time set? If not, tm_year will be (1970 - 1900).
- if (setTimeAlwaysOnReboot || (timeinfo.tm_year < (2016 - 1900))) {
- ESP_LOGI(TAG, "Reboot - Connecting to WiFi and getting time over NTP.");
- obtain_time();
- // update 'now' variable with current time
- time(&now);
- }
- char strftime_buf[64];
- setenv("TZ", "UTC-2", 1);
- tzset();
- localtime_r(&now, &timeinfo);
- strftime(strftime_buf, sizeof(strftime_buf), frm, &timeinfo);
- std::string result(strftime_buf);
- return result;
- }
- void setup_time(void)
- {
- ++boot_count;
- ESP_LOGI(TAG, "Boot count: %d", boot_count);
- time_t now;
- struct tm timeinfo;
- time(&now);
- localtime_r(&now, &timeinfo);
- // Is time set? If not, tm_year will be (1970 - 1900).
- if (timeinfo.tm_year < (2016 - 1900)) {
- ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");
- obtain_time();
- // update 'now' variable with current time
- time(&now);
- }
- char strftime_buf[64];
- /*
- // Set timezone to Eastern Standard Time and print local time
- setenv("TZ", "EST5EDT,M3.2.0/2,M11.1.0", 1);
- tzset();
- localtime_r(&now, &timeinfo);
- strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
- ESP_LOGI(TAG, "The current date/time in New York is: %s", strftime_buf);
- // Set timezone to China Standard Time
- setenv("TZ", "CST-8", 1);
- tzset();
- localtime_r(&now, &timeinfo);
- strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
- ESP_LOGI(TAG, "The current date/time in Shanghai is: %s", strftime_buf);
- */
- // Set timezone to Berlin Standard Time
- setenv("TZ", "UTC+9", 1);
- tzset();
- localtime_r(&now, &timeinfo);
- strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
- ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf);
- strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M", &timeinfo);
- ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf);
- std::string zw = gettimestring("%Y%m%d-%H%M%S");
- printf("time %s\n", zw.c_str());
- }
- static void obtain_time(void)
- {
- initialize_sntp();
- // wait for time to be set
- time_t now = 0;
- struct tm timeinfo = {};
- int retry = 0;
- const int retry_count = 10;
- while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
- ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
- vTaskDelay(2000 / portTICK_PERIOD_MS);
- }
- time(&now);
- localtime_r(&now, &timeinfo);
- }
- static void initialize_sntp(void)
- {
- ESP_LOGI(TAG, "Initializing SNTP");
- sntp_setoperatingmode(SNTP_OPMODE_POLL);
- sntp_setservername(0, "pool.ntp.org");
- sntp_set_time_sync_notification_cb(time_sync_notification_cb);
- sntp_init();
- }
|