diff --git a/firmware/fiatlux.c b/firmware/fiatlux.c index 9e3efeb..a645a85 100644 --- a/firmware/fiatlux.c +++ b/firmware/fiatlux.c @@ -1,18 +1,43 @@ +#include +#include +#include +#include +#include +#include + #include "system.h" #include "wifi.h" #include "web.h" #include "mqtt.h" #include "lux.h" -#include -#include -#include +#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \ + "2.pool.ntp.org", "3.pool.ntp.org" -#include -#include +#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS) -void user_init(void) -{ +_Noreturn void sntp_task(void *pvParameters) { + (void) pvParameters; + const char *servers[] = {SNTP_SERVERS}; + + while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) { + vTaskDelayMs(1000); + } + + sntp_set_update_delay(5 * 60000); + const struct timezone tz = {1 * 60, 1}; + sntp_initialize(&tz); + sntp_set_servers(servers, sizeof(servers) / sizeof(char *)); + + while (1) { + vTaskDelayMs(5000); + time_t ts = time(NULL); + long long t = ts; + printf("TIME: %lld %s", t, ctime(&ts)); + } +} + +void user_init(void) { uart_set_baud(0, 115200); printf("SDK version: %s\n", sdk_system_get_sdk_version()); @@ -27,4 +52,5 @@ void user_init(void) xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL); xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL); + xTaskCreate(&sntp_task, "SNTP", 512, NULL, 2, NULL); }