esp-open-rtos/examples/sntp/sntp_example.c

80 lines
1.9 KiB
C
Raw Permalink Normal View History

2016-03-09 16:18:51 +00:00
/*
* Test code for SNTP on esp-open-rtos.
*
* Jesus Alonso (doragasu)
*/
#include <espressif/esp_common.h>
#include <esp/uart.h>
#include <string.h>
#include <stdio.h>
#include <FreeRTOS.h>
#include <task.h>
#include <lwip/err.h>
#include <lwip/sockets.h>
#include <lwip/sys.h>
#include <lwip/netdb.h>
#include <lwip/dns.h>
#include <ssid_config.h>
/* Add extras/sntp component to makefile for this include to work */
2016-03-09 16:18:51 +00:00
#include <sntp.h>
#include <time.h>
2016-03-09 16:18:51 +00:00
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
"2.pool.ntp.org", "3.pool.ntp.org"
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_RATE_MS)
#define UNUSED_ARG(x) (void)x
void sntp_tsk(void *pvParameters)
2016-03-09 16:18:51 +00:00
{
char *servers[] = {SNTP_SERVERS};
UNUSED_ARG(pvParameters);
/* Wait until we have joined AP and are assigned an IP */
2016-03-09 16:18:51 +00:00
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
vTaskDelayMs(100);
}
/* Start SNTP */
2016-03-09 16:18:51 +00:00
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, 0};
/* SNTP initialization */
sntp_initialize(&tz);
/* Servers must be configured right after initialization */
2016-03-09 16:18:51 +00:00
sntp_set_servers(servers, sizeof(servers) / sizeof(char*));
printf("DONE!\n");
/* Print date and time each 5 seconds */
2016-03-09 16:18:51 +00:00
while(1) {
vTaskDelayMs(5000);
time_t ts = time(NULL);
2016-03-09 16:18:51 +00:00
printf("TIME: %s", ctime(&ts));
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
struct sdk_station_config config = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
};
/* required to call wifi_set_opmode before station_set_config */
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
xTaskCreate(sntp_tsk, (signed char *)"SNTP", 1024, NULL, 1, NULL);
2016-03-09 16:18:51 +00:00
}