embedded 原文存档
esp8266 NONOS SDK STA TCP服务端
基于ESP8266_NONOS_SDK-2.2.0
`//ap dhcp struct espconn ST_NetCon; esp_tcp ST_TCP; #define ESP8266_STA_SSID "wifiname" #define ESP8266_STA_PASS "123456" void ICACHE_FLASH_ATTR delay_ms(u32 ms) { for(;0<ms;ms--) { os_delay_us(1000); } } void ICACHE_FLASH_ATTR ESP8266_WIFI_Send_cb(void * arg) { os_printf("esp8266 send OK!\r\n"); } void ICACHE_FLASH_ATTR ESP8266_WIFI_Rev_cb(void * arg,char * pdata,unsigned short len) { struct espconn *T_arg=arg; remot_info *P_port_info=NULL; if(pdata[0]=='k'||pdata[0]=='K')gpio16_output_set(0);//开 else if(pdata[0]=='g'||pdata[0]=='G')gpio16_output_set(1);//关 os_printf("esp8266 rev data =%s\r\n",pdata); os_printf("remot rev cb ip =%d.%d.%d.%d\r\n",T_arg->proto.udp->remote_ip[0],T_arg->proto.udp->remote_ip[1],T_arg->proto.udp->remote_ip[2],T_arg->proto.udp->remote_ip[3]); //tcp是面向连接的,直接T_arg信息 espconn_send(T_arg,"ESP8266 wifi rev TCP OK",os_strlen("ESP8266 wifi rev TCP OK"));//向对方发送应答 } void ICACHE_FLASH_ATTR ESP8266_TCP_Dissconnect_cb(void *arg) { os_printf("----ESP_TCP_Dissconnect CB!----\r\n"); } void ICACHE_FLASH_ATTR ESP_TCP_Connect_cb(void *arg) { espconn_regist_sentcb((struct espconn *)arg,ESP8266_WIFI_Send_cb); espconn_regist_recvcb((struct espconn *)arg,ESP8266_WIFI_Rev_cb); espconn_regist_disconcb((struct espconn *)arg,ESP8266_TCP_Dissconnect_cb); os_printf("----ESP_TCP_CONNECT OK!----\r\n"); } void ICACHE_FLASH_ATTR ESP_TCP_Break_cb(void *arg,sint8 err) { os_printf("----ESP_TCP_BREAK!----\r\n"); } void ICACHE_FLASH_ATTR ESP8266_NetCon_init(void) { ST_NetCon.type=ESPCONN_TCP;//通信协议TCP ST_NetCon.proto.tcp=&ST_TCP; //esp8266作为服务端,不需要先知道client的ip地址 ST_NetCon.proto.tcp->local_port=8266;//设置本地端口,客户端 espconn_regist_connectcb(&ST_NetCon,ESP_TCP_Connect_cb);//tcp连接成功建立后回调函数 espconn_regist_reconcb(&ST_NetCon,ESP_TCP_Break_cb);//tcp连接异常断开的回调函数 espconn_accept(&ST_NetCon);//创建TCP服务,建立侦听 espconn_regist_time(&ST_NetCon,300,0);//设置超时断开时间,最大7200秒 } 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 } 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通信 } } 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);//使能定时器 } //主函数 ESP8266_STA_init(); OS_Timer_1_Init(1000,1);`