拷贝运行sdk文档中的 SNTP示例代码,失败,不能获取时钟?

Goland
Posts: 28
Joined: Fri Oct 23, 2015 10:45 am

拷贝运行sdk文档中的 SNTP示例代码,失败,不能获取时钟?

Postby Goland » Sun Nov 01, 2015 11:25 am

SDK-v1.3, 文档中代码如下,运行时串口打印提示 "please start sntp first !"
我是在station模式下,获取到ip后执行的,在 user_esp_platform_check_ip()中调用,公网是可用的。
网上看了下公开的sntp.c代码,调用sntp_init()后,代码好像时异步执行的,不知在ESP的sdk中如何处理?
请帮忙,谢谢!
void my_sntp_test2() // change sntp server, tested pass on win7
{
// 202.120.2.101 (上海交通大学网络中心NTP服务器地址)
// 3.cn.pool.ntp.org 202.112.29.82
// 3.tw.pool.ntp.org 120.119.28.1
//uint8 ip0 = {210.72.145.44};//210.72.145.44 (国家授时中心服务器IP地址)
ip_addr_t ip0, ip1, ip2;
IP4_ADDR(&ip0, 202, 120, 2, 101);
IP4_ADDR(&ip1, 202,112,29,82);
IP4_ADDR(&ip2, 120, 119, 28, 1);
sntp_setserver(0, &ip0);
sntp_setserver(1, &ip1);
sntp_setserver(2, &ip2);
sntp_init();
uint32 current_stamp;
current_stamp = sntp_get_current_timestamp();
os_printf(">>>>>>>>>1111>>>>>>sntp: %d, %s \n",current_stamp, sntp_get_real_time(current_stamp));
}
void my_sntp_test() // copy from sdk-doc
{
ip_addr_t *addr = (ip_addr_t *)os_zalloc(sizeof(ip_addr_t));
sntp_setservername(0, "us.pool.ntp.org"); // set server 0 by domain name
sntp_setservername(1, "ntp.sjtu.edu.cn"); // set server 1 by domain name
ipaddr_aton("210.72.145.44", addr);
sntp_setserver(2, addr); // set server 2 by IP address
sntp_init();
os_free(addr);
uint32 current_stamp;
current_stamp = sntp_get_current_timestamp();
os_printf(">>>>>>>>>>>>>>>>>>>>>>>sntp: %d, %s \n",current_stamp, sntp_get_real_time(current_stamp));
}
-----------------
log:
connected with my_ssid, channel 1
dhcp client start...
ip:192.168.1.106,mask:255.255.255.0,gw:192.168.1.1
please start sntp first !
>>>>>>>>>>>>>>>>>>>>>>>sntp: 0, Thu Jan 01 00:00:00 1970

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: 拷贝运行sdk文档中的 SNTP示例代码,失败,不能获取时钟?

Postby ESP_Faye » Mon Nov 02, 2015 2:14 pm

您好,

SNTP 是 UDP 传输,并且网络通信需要一些时间。

请加上判断 sntp_get_current_timestamp 不等于 0 了,再打印。也就是等获取到了时间再打印。

我们在编程手册中更新 SNTP 的示例,请使用最新版本的文档。

感谢您对 ESP8266 的关注!

Goland
Posts: 28
Joined: Fri Oct 23, 2015 10:45 am

Re: 拷贝运行sdk文档中的 SNTP示例代码,失败,不能获取时钟?

Postby Goland » Tue Nov 03, 2015 11:40 am

看了v1.4版的 doc,sntp示例代码使用了 timer去查询sntp过程是否完成,
但是程序中使用了很多的 timer,个人觉得代码逻辑有点凌乱,
请给点建议,或 用RTOS方式编程是否 清晰些?

Who is online

Users browsing this forum: No registered users and 83 guests