2016-11-03 00:46:46 -04:00
|
|
|
/* 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.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_system.h"
|
2018-11-20 11:41:31 -05:00
|
|
|
#include "esp_event.h"
|
2016-11-03 00:46:46 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_attr.h"
|
2017-04-21 00:32:50 -04:00
|
|
|
#include "esp_sleep.h"
|
2016-11-03 00:46:46 -04:00
|
|
|
#include "nvs_flash.h"
|
2018-11-20 11:41:31 -05:00
|
|
|
#include "protocol_examples_common.h"
|
2016-11-03 00:46:46 -04:00
|
|
|
|
|
|
|
#include "lwip/err.h"
|
2018-09-06 07:43:08 -04:00
|
|
|
#include "lwip/apps/sntp.h"
|
2016-11-03 00:46:46 -04:00
|
|
|
|
|
|
|
static const char *TAG = "example";
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
RTC_DATA_ATTR static int boot_count = 0;
|
|
|
|
|
|
|
|
static void obtain_time(void);
|
|
|
|
static void initialize_sntp(void);
|
|
|
|
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
|
|
|
++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
|
2017-04-28 07:37:14 -04:00
|
|
|
setenv("TZ", "CST-8", 1);
|
2016-11-03 00:46:46 -04:00
|
|
|
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);
|
|
|
|
|
|
|
|
const int deep_sleep_sec = 10;
|
|
|
|
ESP_LOGI(TAG, "Entering deep sleep for %d seconds", deep_sleep_sec);
|
2016-11-21 10:05:23 -05:00
|
|
|
esp_deep_sleep(1000000LL * deep_sleep_sec);
|
2016-11-03 00:46:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void obtain_time(void)
|
|
|
|
{
|
2017-03-14 09:39:44 -04:00
|
|
|
ESP_ERROR_CHECK( nvs_flash_init() );
|
2018-11-20 11:41:31 -05:00
|
|
|
tcpip_adapter_init();
|
|
|
|
ESP_ERROR_CHECK( esp_event_loop_create_default() );
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
|
2016-11-03 00:46:46 -04:00
|
|
|
initialize_sntp();
|
|
|
|
|
|
|
|
// wait for time to be set
|
|
|
|
time_t now = 0;
|
|
|
|
struct tm timeinfo = { 0 };
|
|
|
|
int retry = 0;
|
|
|
|
const int retry_count = 10;
|
|
|
|
while(timeinfo.tm_year < (2016 - 1900) && ++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);
|
|
|
|
}
|
2017-02-20 23:29:25 -05:00
|
|
|
|
2018-11-20 11:41:31 -05:00
|
|
|
ESP_ERROR_CHECK( example_disconnect() );
|
2016-11-03 00:46:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void initialize_sntp(void)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Initializing SNTP");
|
|
|
|
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
|
|
|
sntp_setservername(0, "pool.ntp.org");
|
|
|
|
sntp_init();
|
|
|
|
}
|