基于ESP8266_NONOS_SDK-2.2.0
/********* 全局变量 ********/
//ap dhcp
struct espconn ST_NetCon;
esp_tcp ST_TCP;
ip_addr_t IP_SEVER;
/********* 宏定义 ********/
#define ESP8266_STA_SSID "wifiname"
#define ESP8266_STA_PASS "123456"
void ICACHE_FLASH_ATTR OS_Timer_SNTP_cb(void *arg)
{
u8 C_STR=0;//字符串字节计数
char A_Str_Data[20]={0};//【日期】字符串
char *T_A_Str_Data=A_Str_Data;//缓存数组指针
char A_Str_Clock[10]={0};//【时间】字符串
char *Str_Head_week;
char *Str_Head_month;
char *Str_Head_day;
char *Str_Head_clock;
char *Str_Head_year;
u32 timestamp;//时间截
char *str_realtime;//实际时间字符串
timestamp=sntp_get_current_timestamp();//查询当前距离基准时间的时间差,单位秒1970.01.01 00:00:00 GMT+8
if(timestamp)
{
str_realtime=sntp_get_real_time(timestamp);//查询实际时间,GMT+8北京时间
os_printf("\r\n--------------------------\r\n");
os_printf("SNTP timestamp = %d\r\n",timestamp);
os_printf("Real Time = %s\r\n",str_realtime);
os_printf("\r\n--------------------------\r\n");
Str_Head_year=str_realtime;
while(*Str_Head_year)
Str_Head_year++;
Str_Head_year-=5;
T_A_Str_Data[4]=' ';
os_memcpy(T_A_Str_Data,Str_Head_year,4);
T_A_Str_Data+=5;
Str_Head_week=str_realtime;
Str_Head_month=os_strstr(Str_Head_week," ")+1;
Str_Head_day=os_strstr(Str_Head_month," ")+1;
Str_Head_clock=os_strstr(Str_Head_day," ")+1;
C_STR=Str_Head_day-Str_Head_month;
os_memcpy(T_A_Str_Data,Str_Head_month,C_STR);
T_A_Str_Data+=C_STR;
C_STR=Str_Head_clock-Str_Head_day;
os_memcpy(T_A_Str_Data,Str_Head_day,C_STR);
T_A_Str_Data+=C_STR;
C_STR=Str_Head_month-Str_Head_week-1;
os_memcpy(T_A_Str_Data,Str_Head_week,C_STR);
T_A_Str_Data+=C_STR;
*T_A_Str_Data='\0';
os_printf("OLED DATA =%s\r\n",A_Str_Data);//打印日期
os_memcpy(A_Str_Clock,Str_Head_clock,8);
A_Str_Clock[8]='\0';
os_printf("OLED TIME =%s\r\n",A_Str_Clock);//打印时间
}
}
void ICACHE_FLASH_ATTR OS_Timer_SNTP_Init(u32 timemsm,bool time_repetitive)//软件定时器初始化
{
os_timer_disarm(&OS_Timer_1);//关闭软件定时器
os_timer_setfn(&OS_Timer_1,(os_timer_func_t *)OS_Timer_SNTP_cb,NULL);//设置定时器,设置回调函数
os_timer_arm(&OS_Timer_1,timemsm, time_repetitive);//使能定时器
}
void ICACHE_FLASH_ATTR ESP8266_SNTP_init(void)
{
ip_addr_t *addr=(ip_addr_t *)os_zalloc(sizeof(ip_addr_t));
sntp_setservername(0,"us.pool.ntp.org");
sntp_setservername(1,"ntp.sjtu.edu.cn");
ipaddr_aton("210.72.145.44",addr);
sntp_setserver(2,addr);
os_free(addr);
sntp_init();
OS_Timer_SNTP_Init(1000,1);//1秒重复定时sntp
}
void ICACHE_FLASH_ATTR OS_Timer_1_cb(void)//软件定时器的回调函数
{
u8 C_LED_flash=0;//led闪烁计数
u8 S_WIFI_STA_connect; //wifi接入状态标志
struct ip_info ST_ESP8266_ip;//esp8266 ip 信息
u8 esp8266_ip[4];//esp8266 ip地址
S_WIFI_STA_connect=wifi_station_get_connect_status();//查询STA接入wifi状态
switch(S_WIFI_STA_connect)
{
case 0: os_printf("\nSTA IDLE\n");break;
case 1: os_printf("\nSTA connecting...\n");break;
case 2: os_printf("\nSTA password error...\n");break;
case 3: os_printf("\nSTA no AP found...\n");break;
case 4: os_printf("\nSTA connect fail...\n");break;
case 5: os_printf("\nSTA geted IP,suss!\n");break;
}
if(S_WIFI_STA_connect==STATION_GOT_IP)//判断是否获取IP
{
wifi_get_ip_info(STATION_IF,&ST_ESP8266_ip);//获取STATION_IF下的ip地址
if(ST_ESP8266_ip.ip.addr!=0)
{
esp8266_ip[0]=ST_ESP8266_ip.ip.addr;
esp8266_ip[1]=ST_ESP8266_ip.ip.addr>>8;
esp8266_ip[2]=ST_ESP8266_ip.ip.addr>>16;
esp8266_ip[3]=ST_ESP8266_ip.ip.addr>>24;
os_printf("ESP8266 ip=%d.%d.%d.%d\r\n",esp8266_ip[0],esp8266_ip[1],esp8266_ip[2],esp8266_ip[3]);
}
for(;C_LED_flash<=5;C_LED_flash++)//接入WIFI,LED快闪3次
{
gpio16_output_set(C_LED_flash%2);
delay_ms(100);
}
os_timer_disarm(&OS_Timer_1);//关闭软件定时器
//ESP8266_NetCon_init();//初始化网路UDP通信
ESP8266_SNTP_init();
}
}
void ICACHE_FLASH_ATTR OS_Timer_1_Init(u32 timemsm,bool time_repetitive)//软件定时器初始化
{
os_timer_disarm(&OS_Timer_1);//关闭软件定时器
os_timer_setfn(&OS_Timer_1,(os_timer_func_t *)OS_Timer_1_cb,NULL);//设置定时器,设置回调函数
os_timer_arm(&OS_Timer_1,timemsm, time_repetitive);//使能定时器
}
void ICACHE_FLASH_ATTR ESP8266_STA_init(void)
{
struct station_config STA_config;//STA参数结构体
struct ip_info ST_ESP8266_IP;//STA信息结构体
wifi_set_opmode(0x01);//设置STA模式,并保存到FLASH
// //设置STA模式下的IP地址,ESP8266默认试DHCP模式,接入WIFI会自动分配IP地址
// wifi_station_dhcpc_stop();//关闭DHCP
// IP4_ADDR(&ST_ESP8266_IP.ip,192,168,1,8);//配置IP地址
// IP4_ADDR(&ST_ESP8266_IP.netmask,255,255,255,0);//配置子网掩码
// IP4_ADDR(&ST_ESP8266_IP.gw,192,168,1,1);//配置网关地址
// wifi_set_ip_info(STATION_IF,&ST_ESP8266_IP);//设置STA模式下的IP地址
os_memset(&STA_config,0,sizeof(struct station_config));//STA参数结构体清零
os_strcpy(STA_config.ssid,ESP8266_STA_SSID);//设置wifi名
os_strcpy(STA_config.password,ESP8266_STA_PASS);//设置wifi密码
wifi_station_set_config(&STA_config);//设置STA参数,并保存到flash
}
//主函数
ESP8266_STA_init();
OS_Timer_1_Init(1000,1);
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
走着的小站
本文地址: esp8266 NONOS SDK SNTP服务器
本文地址: esp8266 NONOS SDK SNTP服务器