fiatlux/firmware/ntp.cpp
2021-11-21 00:37:10 +01:00

53 lines
No EOL
1.2 KiB
C++

//
// 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);
}