fiatlux/firmware/ntp.cpp

53 lines
1.2 KiB
C++
Raw Normal View History

2021-11-16 19:19:02 +00:00
//
// Created by jedi on 04.11.21.
//
#include "ntp.h"
#include "wifi.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Add extras/sntp component to makefile for this include to work */
#include <sntp.h>
#include <time.h>
#ifdef __cplusplus
}
#endif
#include <stdio.h>
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
"2.pool.ntp.org", "3.pool.ntp.org"
SemaphoreHandle_t time_available_semaphore = nullptr;
void sntp_task(void *pvParameters) {
const char *servers[] = {SNTP_SERVERS};
(void) pvParameters;
while (!uxSemaphoreGetCount(wan_available_semaphore)) {
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
/* Start SNTP */
//printf("Starting SNTP... ");
/* SNTP will request an update each 5 minutes */
sntp_set_update_delay(5 * 60000);
/* Set GMT+1 zone, daylight savings off */
const struct timezone tz = {1 * 60, 1};
/* SNTP initialization */
sntp_initialize(&tz);
/* Servers must be configured right after initialization */
sntp_set_servers(servers, sizeof(servers) / sizeof(char *));
printf("SNTP DONE!\n");
xSemaphoreGive(time_available_semaphore);
/* Print date and time each 5 seconds */
vTaskDelete(nullptr);
}