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>
|
|
|
|
|
2016-03-13 17:29:30 +00:00
|
|
|
/* Add extras/sntp component to makefile for this include to work */
|
2016-03-09 16:18:51 +00:00
|
|
|
#include <sntp.h>
|
2016-03-31 14:05:15 +00:00
|
|
|
#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"
|
|
|
|
|
2016-11-05 10:04:03 +00:00
|
|
|
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS)
|
2016-03-09 16:18:51 +00:00
|
|
|
#define UNUSED_ARG(x) (void)x
|
|
|
|
|
2016-03-13 17:29:30 +00:00
|
|
|
void sntp_tsk(void *pvParameters)
|
2016-03-09 16:18:51 +00:00
|
|
|
{
|
|
|
|
char *servers[] = {SNTP_SERVERS};
|
|
|
|
UNUSED_ARG(pvParameters);
|
|
|
|
|
2016-03-13 17:29:30 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2016-03-13 17:29:30 +00:00
|
|
|
/* Start SNTP */
|
2016-03-09 16:18:51 +00:00
|
|
|
printf("Starting SNTP... ");
|
2016-03-13 17:29:30 +00:00
|
|
|
/* 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");
|
2016-03-11 12:11:15 +00:00
|
|
|
|
2016-03-13 17:29:30 +00:00
|
|
|
/* Print date and time each 5 seconds */
|
2016-03-09 16:18:51 +00:00
|
|
|
while(1) {
|
|
|
|
vTaskDelayMs(5000);
|
2016-03-13 17:29:30 +00:00
|
|
|
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);
|
|
|
|
|
2016-10-21 09:40:36 +00:00
|
|
|
xTaskCreate(sntp_tsk, "SNTP", 1024, NULL, 1, NULL);
|
2016-03-09 16:18:51 +00:00
|
|
|
}
|
|
|
|
|