如题,esp8266使用airkiss配网后,每次断电都需要重新配网,我是在原子的例程的基础上修改的,只是用了STA模式,具体程序如下
void atk_8266_test(void)
{
// u16 rlen=0;
u8 key;
u8 timex;
POINT_COLOR=RED;
Show_Str_Mid(0,30,"ATK-ESP8266 WIFI模块测试",16,240);
WIFIDA.Header=0XAA;
WIFIDA.DeviceClass1=0X10;
WIFIDA.DeviceClass2=0X02;
WIFIDA.DeviceClass3=0X33;
WIFIDA.DeviceClass4=0X44;
WIFIDA.DeviceClass5=0X05;
WIFIDA.DeviceClass6=0X06;
WIFIDA.DeviceClass7=0X07;
WIFIDA.DeviceClass8=0X08;
WIFIDA.JQ1=0X00;
WIFIDA.JQ2=0X04;
WIFIDA.TVOC1=0X10;
WIFIDA.TVOC2=0X78;
WIFIDA.CO21=0X01;
WIFIDA.CO22=0X23;
WIFIDA.CRCH16=0X01;
WIFIDA.CRCL16=0X63;
WIFIDA.Tail=0X55;
while(atk_8266_send_cmd("AT","OK",20))//检查WIFI模块是否在线
{
atk_8266_quit_trans();//退出透传
atk_8266_send_cmd("AT+CIPMODE=0","OK",200); //关闭透传模式
Show_Str(40,55,200,16,"未检测到模块!!!",16,0);
delay_ms(800);
LCD_Fill(40,55,200,55+16,WHITE);
Show_Str(40,55,200,16,"尝试连接模块...",16,0);
}
while(atk_8266_send_cmd("ATE0","OK",20));//关闭回显
atk_8266_mtest_ui(32,30);
while(1)
{
delay_ms(10);
atk_8266_at_response(1);//检查ATK-ESP8266模块发送过来的数据,及时上传给电脑
LCD_Clear(WHITE);
POINT_COLOR=RED;
Show_Str_Mid(0,30,"ATK-ESP WIFI-STA 测试",16,240);
Show_Str_Mid(0,50,"正在配置ATK-ESP8266模块,请稍等...",12,240);
atk_8266_wifista_test();//WIFI STA测试
}
}
u8 atk_8266_wifista_test(void)
{
u8 i=0;
u8 netpro=0; //网络模式
u8 key;
u8 timex=0;
u8 ipbuf[16]; //IP缓存
u8 *p;
u16 t=998; //加速第一次获取链接状态
u8 res=0;
u16 rlen=0;
u8 constate=0; //连接状态
p=mymalloc(32); //申请32字节内存
atk_8266_send_cmd("AT+RST","OK",20); //DHCP服务器关闭(仅AP模式有效)
// atk_8266_send_cmd("AT+CWMODE=1","OK",50); //设置WIFI STA模式
atk_8266_send_cmd("AT+CWMODE_DEF=1","OK",50); //配置为单sta模式
atk_8266_send_cmd("AT+CWAUTOCONN=1","OK",50); //使能上电自动连接AP
delay_ms(1000); //延时3S等待重启成功
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
//设置连接到的WIFI网络名称/加密方式/密码,这几个参数需要根据您自己的路由器设置进行修改!!
// sprintf((char*)p,"AT+CWJAP=\"%s\",\"%s\"",wifista_ssid,wifista_password);//设置无线参数:ssid,密码
while(atk_8266_send_cmd(p,"WIFI GOT IP",300))//连接目标路由器,并且获得IP
{
a++;
if(a==0x0f)
{
atk_8266_send_cmd("AT+RST","OK",20); //DHCP服务器关闭(仅AP模式有效)
//
atk_8266_send_cmd("AT+CWMODE_DEF=1","OK",50); //配置为单sta模式
atk_8266_send_cmd("AT+CWAUTOCONN=1","OK",50); //使能上电自动连接AP
atk_8266_send_cmd("AT+CWSTARTSMART=3","OK",50); // 开启智能配网
// atk_8266_send_cmd("AT+CIPSTATUS","OK",50); //释放快连所占内存
a=0;
}
};
PRESTA:
netpro=0x02;
LCD_Clear(WHITE);
POINT_COLOR=RED;
Show_Str_Mid(0,30,"ATK-ESP WIFI-STA 测试",16,240);
Show_Str(30,50,200,16,"正在配置ATK-ESP模块,请稍等...",12,0);
// if(atk_8266_ip_set("WIFI-STA 远端UDP IP设置",(u8*)ATK_ESP8266_WORKMODE_TBL[2],(u8*)portnum,ipbuf))goto PRESTA; //IP输入
sprintf((char*)p,"AT+CIPSTART=\"UDP\",\"%s\",%s",wifista_ip,(u8*)portnum); //配置目标UDP服务器
delay_ms(1000); //延时3S等待重启成功
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(200);
atk_8266_send_cmd("AT+CIPMUX=0","OK",20); //单链接模式
delay_ms(200);
LCD_Clear(WHITE);
while(atk_8266_send_cmd(p,"OK",500));
LCD_Clear(WHITE);
POINT_COLOR=RED;
Show_Str_Mid(0,30,"ATK-ESP WIFI-STA 测试",16,240);
Show_Str(30,50,200,16,"正在配置ATK-ESP模块,请稍等...",12,0);
LCD_Fill(30,50,239,50+12,WHITE); //清除之前的显示
Show_Str(30,50,200,16,"WK_UP:退出测试 KEY0:发送数据",12,0);
LCD_Fill(30,80,239,80+12,WHITE);
atk_8266_get_wanip(ipbuf);//服务器模式,获取WAN IP
sprintf((char*)p,"IP地址:%s 端口:%s",ipbuf,(u8*)portnum);
Show_Str(30,65,200,12,p,12,0); //显示IP地址和端口
Show_Str(30,80,200,12,"状态:",12,0); //连接状态
Show_Str(120,80,200,12,"模式:",12,0); //连接状态
Show_Str(30,100,200,12,"发送数据:",12,0); //发送数据
Show_Str(30,115,200,12,"接收数据:",12,0); //接收数据
atk_8266_wificonf_show(30,180,"请设置路由器无线参数为:",(u8*)wifista_ssid,(u8*)wifista_encryption,(u8*)wifista_password);
POINT_COLOR=BLUE;
Show_Str(120+30,80,200,12,(u8*)ATK_ESP8266_WORKMODE_TBL[2],12,0); //连接状态
USART2_RX_STA=0;
while(1)
{
key=KEY_Scan(0);
if(key==WKUP_PRES) //WK_UP 退出测试
{
res=0;
atk_8266_quit_trans(); //退出透传
atk_8266_send_cmd("AT+CIPMODE=0","OK",20); //关闭透传模式
break;
}
else if(key==KEY0_PRES) //KEY0 发送数据
{
sprintf((char*)p,"02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d",WIFIDA.Header,WIFIDA.DeviceClass1,WIFIDA.DeviceClass2,WIFIDA.DeviceClass3,WIFIDA.DeviceClass4,WIFIDA.DeviceClass5,WIFIDA.DeviceClass5,WIFIDA.DeviceClass7,WIFIDA.DeviceClass8,WIFIDA.WordLen1,WIFIDA.WordLen2,WIFIDA.WordLen3,WIFIDA.WordLen4,WIFIDA.CRCH16,WIFIDA.CRCL16,WIFIDA.Tail);//测试数据
// unsigned char WIFIsendbuf2[18] = {0x41,0x41,0x02,0x02,0x03,0x04,0x00,0x06,0x07,0x08, 0x0a,0x0b,0x0a,0x0b,0x23,0x32,0x35,0x35};
// Uart2_TX(WIFIsendbuf2,16);
// sprintf((char*)p,"AA55%02d\r\n",a);
Show_Str(30+54,100,200,12,p,12,0);
atk_8266_send_cmd("AT+CIPSEND=18","OK",200); //发送指定长度的数据
delay_ms(200);
// atk_8266_send_data(p,"OK",100); //发送指定长度的数据
atk_8266_send_data(WIFIsendbuf2,"OK",100,18); //发送指定长度的数据
timex=100;
}else;
if(timex)timex--;
if(timex==1)LCD_Fill(30+54,100,239,112,WHITE);
t++;
delay_ms(10);
if(USART2_RX_STA&0X8000) //接收到一次数据了
{
rlen=USART2_RX_STA&0X7FFF; //得到本次接收到的数据长度
USART2_RX_BUF[rlen]=0; //添加结束符
printf("%s",USART2_RX_BUF); //发送到串口
sprintf((char*)p,"收到%d字节,内容如下",rlen);//接收到的字节数
LCD_Fill(30+54,115,239,130,WHITE);
POINT_COLOR=BRED;
Show_Str(30+54,115,156,12,p,12,0); //显示接收到的数据长度
POINT_COLOR=BLUE;
LCD_Fill(30,130,239,319,WHITE);
Show_Str(30,130,180,190,USART2_RX_BUF,12,0);//显示接收到的数据
USART2_RX_STA=0;
if(constate!='+')t=1000; //状态为还未连接,立即更新连接状态
else t=0; //状态为已经连接了,10秒后再检查
}
if(t==1000)//连续10秒钟没有收到任何数据,检查连接是不是还存在.
{
constate=atk_8266_consta_check();//得到连接状态
if(constate=='+')Show_Str(30+30,80,200,12,"连接成功",12,0); //连接状态
else Show_Str(30+30,80,200,12,"连接失败",12,0);
t=0;
}
if((t%20)==0)LED0=!LED0;
atk_8266_at_response(1);
}
myfree(p); //释放内存
return res;
}
|