单片机通过TX接WIFI模块的RX将设置的AT代码写入WIFI模块(连接WIFI+调为设备模式(有设备,路由,双模等模式))
WIFI模块将响应信号通过TX通过CH340发给PC的RX
通过STC-ISP或安信可串口调试助手查看响应信息
PC可以通过TX给单片机的RX发送信息
#include "reg52.h"
#include "intrins.h"
#include <string.h>#define SIZE 12
sbit D5 = P3^0;
sbit jdq = P3^4;
sfr AUXR = 0x8e;
int mark=0;
char cmd = '1';
code char ruwang[]="AT+CWJAP=\"wifi名称\",\"密码\"\r\n";
code char ljfwq[]="AT+CIPSTART=\"TCP\",\"IP地址\",端口\r\n";
code char TCMS[] = "AT+CIPMODE=1\r\n";
code char SJCS[] = "AT+CIPSEND\r\n";void UartInit(void) //9600bps@11.0592MHz
{AUXR = 0x01;SCON = 0x50; //????????1,REN????TMOD &= 0x0F;TMOD |= 0x20;//???1?????8?????TH1 = 0xFD;TL1 = 0xFD;//9600??????TR1 = 1;//?????EA = 1;//?????ES = 0;//??????
}void Delay1000ms() //@11.0592MHz
{unsigned char i, j, k;_nop_();i = 8;j = 1;k = 243;do{do{while (--k);} while (--j);} while (--i);
}void sendByte(char data_msg)
{SBUF = data_msg;while(!TI);TI = 0;
}void sendString(char* str)
{while( *str != '\0'){sendByte(*str);str++;}
}
void Uart_Handler() interrupt 4
{if (RI){RI = 0;jdq = 0;Delay1000ms();jdq = 1;}
}
void main()
{//D5 = 1;//??C51???????UartInit();jdq = 0;while(1){if(mark==0){Delay1000ms();//??????????,????????//sendString("chenlichen shuai\r\n");sendString(ruwang);Delay1000ms();Delay1000ms();Delay1000ms();Delay1000ms();Delay1000ms();sendString(ljfwq);Delay1000ms();Delay1000ms();Delay1000ms();Delay1000ms();//Delay1000ms();sendString(TCMS);Delay1000ms();Delay1000ms();//Delay1000ms();//Delay1000ms();//Delay1000ms();sendString(SJCS);Delay1000ms();Delay1000ms();//Delay1000ms();//Delay1000ms();//Delay1000ms();mark=1;}else if (mark==1){if (mark){sendString("ok");Delay1000ms();mark=3;ES = 1;jdq = 1;}}}
}