Air quality sensor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
esp-airsensor/main/sntp_cli.c

63 lines
1.6 KiB

#include "sntp_cli.h"
#include <time.h>
#include <sys/time.h>
#include "esp_log.h"
#include "tasks.h"
#include "utils.h"
#include "lwip/err.h"
#include "lwip/apps/sntp.h"
#include "settings.h"
static const char * TAG = "sntp_cli";
static volatile bool sntp_running = false;
static void vTaskSNTPclient(void * pvParameters)
{
(void)pvParameters; //UN-USED
ESP_LOGI(TAG, "Starting SNTP client, server %s", g_Settings.ntp_srv);
vTaskDelay(configTICK_RATE_HZ);
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, g_Settings.ntp_srv);
sntp_init();
vTaskDelay(2*configTICK_RATE_HZ);
time_t now;
struct tm timeinfo;
int retry = 0;
const int retry_count = 10;
time(&now);
localtime_r(&now, &timeinfo);
while(timeinfo.tm_year < (2016 - 1900) && ++retry < retry_count) {
ESP_LOGD(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
vTaskDelay(3*configTICK_RATE_HZ);
time(&now);
localtime_r(&now, &timeinfo);
}
if(timeinfo.tm_year < (2016 - 1900)) {
ESP_LOGW(TAG, "SNTP client failed to get time!");
} else {
char strftime_buf[64];
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
ESP_LOGI(TAG, "The current UTC date/time is: %s, SNTP client done", strftime_buf);
}
sntp_stop();
sntp_running = false;
vTaskDelete(NULL);
}
bool sntp_cli_start()
{
if (sntp_running) {
return false;
}
sntp_running = true;
RTOS_ERROR_CHECK(xTaskCreate(vTaskSNTPclient, "SNTPc", 4096, NULL, PRIO_LOW, NULL));
return true;
}