0
回答
编程控制esp8266 wifi模块
百度AI开发者大赛带你边学边开发,赢100万奖金,加群:418589053   

硬件:将开发板的串口3的RX,TX分别与wifi模块的TX,RX连接。

程序想要实现的功能:通过串口控制模块连上服务器,并不断向服务器发送数据,这里的服务器是用测试工具创建的。

问题:当程序循环了5、6次之后服务器并没有接收到数据,但此时程序却还在不断的运行。

我的整个的流程如下:

1. 复位,将AT+RST命令写入com口,wifi复位

2.选择模式,写入命令:AT+CWMODE=3

3.连接wifi

4.设置为单连接 (AT+CIPMUX=0)

5.连接服务器(TCP)

6.发送数据

7.关闭TCP连接,并跳转至第5步

//初始化并打开串口
comInit();
com3 = comOpen(3, 115200, DATABITS_8, STOPBITS_1, PARITY_NONE, FLOWCONTROL_RTSCTS, COM_READWRITE, 1024);
//复位wifi
sprintf(CMD_Buf,"AT+RST\r\n");	
comWrite(com3, (unsigned char *)CMD_Buf, strlen(CMD_Buf));
nanosleep(&wait_2s,NULL); 
//选择模式
sprintf(CMD_Buf,"AT+CWMODE=3\r\n");
comWrite(com3, (unsigned char *)CMD_Buf, strlen(CMD_Buf));	
nanosleep(&wait_500ms,NULL);
//连接AP进入公网
sprintf(CMD_Buf,"AT+CWJAP=\"xd\",\"87654321\"\r\n");
comWrite(com3, (unsigned char *)CMD_Buf, strlen(CMD_Buf));
nanosleep(&wait_15s,NULL);

//设置为单连接	
sprintf(CMD_Buf,"AT+CIPMUX=0\r\n");
comWrite(com3, (unsigned char *)CMD_Buf, strlen(CMD_Buf));
nanosleep(&wait_500ms,NULL);
 while(1)
{
//与服务器建立TCP连接 
sprintf(CMD_Buf,"AT+CIPSTART=\"TCP\",\"172.18.89.1\",6000\r\n");
comWrite(com3, (unsigned char *)CMD_Buf, strlen(CMD_Buf));
nanosleep(&wait_500ms,NULL);

//关闭透传模式
sprintf(CMD_Buf,"AT+CIPMODE=0\r\n");	 
comWrite(com3, (unsigned char *)CMD_Buf, strlen(CMD_Buf));
nanosleep(&wait_500ms,NULL);

//发送数据
char send_buf[10]="hello";
int len=strlen(send_buf);
sprintf(CMD_Buf,"AT+CIPSEND=%d\r\n",len);	
comWrite(com3, (unsigned char *)CMD_Buf, strlen(CMD_Buf));
nanosleep(&wait_1s,NULL); 
comWrite(com3,(unsigned char *)send_buf,len);
nanosleep(&wait_1s,NULL); 
 
//关闭TCP连接
sprintf(CMD_Buf,"AT+CIPCLOSE\r\n");
nanosleep(&wait_2s,NULL); 	
comWrite(com3, (unsigned char *)CMD_Buf, strlen(CMD_Buf));
nanosleep(&wait_2s,NULL); 
}



举报
_Mamahoohoo
发帖于2年前 0回/430阅
顶部