赞
踩
接线:
注:所有的AT指令都要勾选发送新行!
1.上电后发送AT检测功能是否正常
AT
OK
2.因为模块初始化为115200波特率所以 通过一下命令配置成9600波特率 然后重启关闭串口后选择9600波特率连接
AT+UART=9600,8,1,0,0
3.设置工作模式
AT+CWMODE=3 //1. 是station(设备)模式 2.是AP(路由)模式 3.是双模
OK
4.以设备模式接入家中路由器配置
AT+CWJAP="kkk","hqk123456" //1.将模块接入到达路由器中
WIFI CONNECTED //结果
WIFI GOT IP //结果
5.查询IP地址
AT+CIFSR//指令
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"ca:2b:96:09:cf:ce"
+CIFSR:STAIP,"192.168.0.192"
+CIFSR:STAMAC,"c8:2b:96:09:cf:ce"OK
6.连接服务器
AT+CIPSTART="TCP","192.168.0.190",8880 //指令,注意双引号逗号都要半角(英文)输入 CONNECT //结果:成功
OK //结果:成功
7.发送数据
AT+CIPSEND=4 // 设置即将发送数据的长度 (这里是4个字节)
>CLCA // 看到大于号后,输入消息,CLCA,不要带回车
Response :SEND OK //结果:成功 //注意,这种情况下,每次发送前都要先发送AT+CIPSEND=长度 的指令,再发数据!
8.设置透传模式
AT+CIPMODE=1 //开启透传模式
Response :OK
AT+CIPSEND //带回车 Response:
> //这个时候随意发送接收数据咯
9.退出透传模式
//在透传发送数据过程中,若识别到单独的⼀包数据 “+++”,则退出透传发送
利用白盒测试 :
1.main函数中让ESP8266模块联网设置一个标志位让联网只执行一次
2.串口中断函数中编写代码判断接收到的数据是0 还是 1
- #include "reg52.h"
- #include "intrins.h"
- #include <string.h>
-
- sfr AUXR = 0x8e;
- sbit D5 = P3^6;
-
- char cmd[12];
-
- code char JRWL[] = "AT+CWJAP=\"kkk\",\"hqk123456\"\r\n";
- code char LFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.151\",8880\r\n";
- char SZTC[] = "AT+CIPMODE=1\r\n";
- char KACS[] = "AT+CIPSEND\r\n";
-
- int mark = 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 UartInit(void) //9600bps@11.0592MHz
- {
- SCON = 0x50; //
- TMOD &= 0x0f;
- TMOD |= 0x20;
-
- TH1 = 0xFD;
- TL1 = 0xFD;
-
- TR1 = 1;
-
- ES = 1;//´ò¿ª´®¿ÚÖжÏ
- EA = 1;//´ò¿ª×ÜÖжÏ
- }
- //1.·â×°·¢Ë͵¥¸ö×Ö·û´®µÄº¯ÊýÌå
- void sendstr(char data1)
- {
- SBUF = data1;//½«½ÓÊÕµ½µÄµ¥¸ö×Ö·û±£´æµ½SBUF»º³åÇøÖÐ
- while(!TI); //Ñ»·Ö´ÐÐÅжÏTIÊÇÒ»¸ö·¢ËÍÖжÏÇëÇó±ê־λ
- TI = 0; //ÊÖ¶¯ÖÃ0
- }
-
- //2.·â×°·¢ËÍ×Ö·û´®µÄº¯ÊýÌå ÀûÓÃÖ¸Õë´«²Î¸øµ¥¸ö×Ö·û´®º¯ÊýÌå·¢ËÍ
- void sendstring(char *str)
- {
- while(*str != '\0')//Ñ»·ÅжÏ×Ö·û´®µ½´ïĩβûÓÐ
- {
- sendstr(*str);
- str++;
- }
- }
-
- //Ö÷º¯Êý
- void main()
- {
- UartInit();//´®¿Ú³õʼ»¯º¯Êý
- while(1)
- {
-
- if(mark == 0)
- {
- sendstring(JRWL);
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- sendstring(LFWQ);
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- sendstring(SZTC);
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- sendstring(KACS);
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- Delay1000ms();
- mark = 1;
- }
- else
- {
- Delay1000ms();//ÑÓʱ ÿ1Ãë·¢ËÍÒ»´Î
- sendstring("Walker F.M\r\n");//ÐèÒª·¢Ë͵ÄÄÚÈÝ
- }
- }
- }
-
- void UART_Handler() interrupt 4 //´®¿ÚÖжϱêÖ¾ÊÇinterrupt 4
- {
- if(RI)
- {
- RI = 0;
- cmd[0] = SBUF;
- if(cmd[0] == '1')
- {
- D5 = 0;
- }
- if(cmd[0] == '0')
- {
- D5 = 1;
- }
- }
- }
- #include "reg52.h"
- #include "intrins.h"
- #include <string.h>
-
-
- #define SIZE 12
- sfr AUXR = 0x8E;
- sbit D5 = P3^7;
- sbit D6 = P3^6;
-
- char buffer[SIZE];
- code char LJWL[] = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n"; //入网指令
- code char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n"; //连接服务器指令
- char TCMS[] = "AT+CIPMODE=1\r\n"; //透传指令
- char SJCS[] = "AT+CIPSEND\r\n"; //数据传输开始指令
- char RESET[] = "AT+RST\r\n"; //重启模块指令
- char AT_OK_Flag = 0; //OK返回值的标志位
- char AT_Connect_Net_Flag = 0; //WIFI GOT IP返回值的标志位
-
- 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 = 1;//开启串口中断
- }
-
- 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 main()
- {
- int mark = 0;
- D5 = D6 = 1;//灭状态灯
- //配置C51串口的通信方式
- UartInit();
- Delay1000ms();//给espwifi模块上电时间
-
- //发送联网AT指令并等待成功
- sendString(LJWL);
- //while(!AT_Connect_Net_Flag);
- while(!AT_OK_Flag);
- AT_OK_Flag = 0;
- //发送连服务器指令并等待成功
- sendString(LJFWQ);
- while(!AT_OK_Flag);
- AT_OK_Flag = 0;
- //发送透传模式指令并等待成功
- sendString(TCMS);
- while(!AT_OK_Flag);
- AT_OK_Flag = 0;
- //发送数据传输指令并等待成功
- sendString(SJCS);
- while(!AT_OK_Flag);
-
- if(AT_Connect_Net_Flag){
- D5 = 0;//点亮D5,代表入网成功
- }
- if(AT_OK_Flag){
- D6 = 0;//点亮D6,代表连接服务器并打开透传模式成功
- }
-
- while(1){
- Delay1000ms();
- //“心跳包”
- sendString("chenlichen shuai\r\n");
- }
- }
-
- void Uart_Handler() interrupt 4
- {
- static int i = 0;//静态变量,被初始化一次
- char tmp;
-
- if(RI)//中断处理函数中,对于接收中断的响应
- {
- RI = 0;//清除接收中断标志位
- tmp = SBUF;
- if(tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == 'F'){
- i = 0;
- }
- buffer[i++] = tmp;
- //入网成功的判断依据WIFI GOT IP
- if(buffer[0] == 'W' && buffer[5] == 'G'){
- AT_Connect_Net_Flag = 1;
- memset(buffer, '\0', SIZE);
- }
- //连接服务器等OK返回值指令的判断
- if(buffer[0] == 'O' && buffer[1] == 'K'){
- AT_OK_Flag = 1;
- memset(buffer, '\0', SIZE);
- }
- //联网失败出现FAIL字样捕获
- if(buffer[0] == 'F' && buffer[1] == 'A'){
- for(i=0;i<5;i++){
- D5 = 0;
- Delay1000ms();
- D5 = 1;
- Delay1000ms();
- }
- sendString(RESET);
- memset(buffer, '\0', SIZE);
- }
- //灯控指令
- if(buffer[0] == 'L' && buffer[2] == '1'){
- D5 = 0;//点亮D5
- memset(buffer, '\0', SIZE);
- }
- if(buffer[0] == 'L' && buffer[2] == '0'){
- D5 = 1;//熄灭D5
- memset(buffer, '\0', SIZE);
- }
- if(i == 12) i = 0;
- }
-
- }
USB转TTL插入电脑,TX--RX RX-TX VCC-3.3V GDN-GND
查询IP地址:AT+CIFSR
//1 配置成双模
AT+CWMODE=2
Response :OK
//2 使能多链接
AT+CIPMUX=1Response :OK
//3 建立TCPServer
AT+CIPSERVER=1 // default port = 333
Response :OK
//4 发送数据 AT+CIPSEND=0,4 // 发送4个字节在连接0通道上
>abcd //输入数据,不带回车
Response :SEND OK
//• 接收数据
+IPD, 0, n: xxxxxxxxxx //+IPD是固定字符串 0是通道,n是数据长度,xxx是数据
//断开连接
AT+CIPCLOSE=0
Response :0, CLOSED OK
代码:
- #include "reg52.h"
- #include "intrins.h"
- #include <string.h>
-
-
- #define SIZE 12
- sfr AUXR = 0x8E;
- sbit D5 = P3^7;
- sbit D6 = P3^6;
-
- char buffer[SIZE];
-
- //1 工作在路由模式
- char LYMO[] = "AT+CWMODE=2\r\n";
- //2 使能多链接
- char DLJ[] = "AT+CIPMUX=1\r\n";
- //3 建立TCPServer
- char JLFW[] = "AT+CIPSERVER=1\r\n"; // default port = 333
-
- //发送数据
- char FSSJ[] = "AT+CIPSEND=0,5\r\n";
-
-
- char AT_OK_Flag = 0; //OK返回值的标志位
- char AT_Connect_Net_Flag = 0; //WIFI GOT IP返回值的标志位
- char Client_Connect_Flag = 0;
-
- 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 = 1;//开启串口中断
- }
-
- 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 main()
- {
- int mark = 0;
- D5 = D6 = 1;//灭状态灯
- //配置C51串口的通信方式
- UartInit();
- Delay1000ms();//给espwifi模块上电时间
-
-
- sendString(LYMO);
- while(!AT_OK_Flag);
- AT_OK_Flag = 0;
- sendString(DLJ);
- while(!AT_OK_Flag);
- AT_OK_Flag = 0;
- sendString(JLFW);
-
- while(!Client_Connect_Flag);
- AT_OK_Flag = 0;
- if(Client_Connect_Flag){
- D5 = 0;//点亮D5,代表有客户端接入
- D6 = 0;
- }
-
-
- while(1){
-
- //4 发送数据
- sendString(FSSJ);
- Delay1000ms();
- Delay1000ms();
- sendString("Hello");
- Delay1000ms();
- Delay1000ms();
-
- }
- }
-
- void Uart_Handler() interrupt 4
- {
- static int i = 0;//静态变量,被初始化一次
- char tmp;
-
- if(RI)//中断处理函数中,对于接收中断的响应
- {
- RI = 0;//清除接收中断标志位
- tmp = SBUF;
- if(tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == '0' || tmp == ':'){
- i = 0;
- }
- buffer[i++] = tmp;
- //入网成功的判断依据WIFI GOT IP
- if(buffer[0] == 'W' && buffer[5] == 'G'){
- AT_Connect_Net_Flag = 1;
- memset(buffer, '\0', SIZE);
- }
- //连接服务器等OK返回值指令的判断
- if(buffer[0] == 'O' && buffer[1] == 'K'){
- AT_OK_Flag = 1;
- memset(buffer, '\0', SIZE);
- }
-
- if(buffer[0] == '0' && buffer[2] == 'C'){
- Client_Connect_Flag = 1;
- memset(buffer, '\0', SIZE);
- }
-
- //灯控指令
- if(buffer[0] == ':' && buffer[1] == 'o' && buffer[2] == 'p'){
- D5 = 0;//点亮D5
- memset(buffer, '\0', SIZE);
- }
- if(buffer[0] == ':' && buffer[1] == 'c' && buffer[2] == 'l'){
- D5 = 1;//熄灭D5
- memset(buffer, '\0', SIZE);
- }
- if(i == 12) i = 0;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。