stm32f103c8t6 controls ESP8266 to communicate with mobile APP

stm32f103c8t6 controls ESP8266 to communicate with mobile APP


The article on using stm32 to control the communication between the Bluetooth module and the mobile APP has been published before, so now I will talk about using the WIFI module to control the communication with the mobile APP. The
first required devices are as follows:
stm32f103c8t6
ESP8266-01S (default baud rate 115200)
In addition, you need to download a communication software on the mobile phone. I use the TCP connection on the Android mobile application treasure to connect the APP
WIFI module as follows:
insert image description here

The code is very simple, I mainly attach the main function code:

/*
ESP8266 AP+Station服务器模式测试

UART2 PA2 PA3  与网络模块ESP进行通信

UART1 PA9 PA10 进行数据跟踪
*/

#include  "delay.h"
#include  "led.h"
#include  "usart.h"
#include  "string.h"
#include  "stdio.h"
extern  u8 RX_buffer[tbuf];
extern u8 RX_num;				 //接收计数变量

u8  esp_at[]="AT\r\n";                  // 握手连接指令,返回"OK"
u8  esp_cifsr[]="AT+CIFSR\r\n";         // 本机IP地址查询指令
u8  esp_cipsend[]="AT+CIPSEND=6\r\n";   // 设置发送数据长度
u8  esp_test[]="sunny\r\n";   			//  数据内容
u8  esp_rst[]="AT+RST\r\n"; 					// 软件复位
 
u8  esp_cwmode[]="AT+CWMODE=3\r\n";     // 设置ESP8266的工作模式3 AP+Station,返回"OK"或者"no change"
u8  esp_cwsap[]="AT+CWSAP=\"ESP8266_TEST\",\"1234567890\",1,3\r\n";//设置WIFI的名称及密码
u8  esp_cipmux[]="AT+CIPMUX=1\r\n";   			//打开多连接	
u8  esp_cipserver[]="AT+CIPSERVER=1,8080\r\n";  //建立TCP服务器,开放端口8080

//指定字符串与缓存数组数据进行数据比较
//*p 要比较的指定字符串指针数据
//返回:1 数据一致  0 数据不一致 
u8 Data_compare(u8 *p)
{
    
     
	if(strstr(RX_buffer,p)!=NULL)
	    return 1;
	else
		return 0;
}

int main(void)
   {
    
    	
   	delay_init();	    	 //延时函数初始化
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);		//设置NVIC中断分组2:2位抢占优先级,2位响应优先级
	uart_init(115200);	 //串口初始化为9600  
	LED_Init();		  	//初始化与LED连接的硬件接口
	memset(RX_buffer, 0, tbuf);//清缓存数据
	RX_num=0;				   //接收计数变量清0

	//配置wifi工作模式为ap+sta模式
    while(1)
	{
    
    
	 Uart2SendStr(esp_cwmode);	   // 设置ESP8266的工作模式3 AP+Station,返回"OK"或者"no change"
	 if(Data_compare("OK")||Data_compare("no change"))break;
	 else  Uart1SendStr("ERROR1,some problems with ESP8266 \r\n");
	 delay_ms(600);
	}
	 Uart1SendStr("OK,set mode as AP+Station with ESP8266! \r\n");	 
	 memset(RX_buffer, 0, tbuf);//清缓存数据	
	 RX_num=0;				   //接收计数变量清0
	
	while(1)
	{
    
    
	 Uart2SendStr(esp_cwsap);	   //设置WIFI的名称及密码
	 if(Data_compare("OK"))break;
	 else  Uart1SendStr("ERROR2,some problems with ESP8266 \r\n");
	 delay_ms(600);
	}
	 Uart1SendStr("OK,set cwsap success! \r\n"); 
	 memset(RX_buffer, 0, tbuf);//清缓存数据	
	 RX_num=0;					//接收计数变量清0
	
    while(1)
	{
    
    
	 Uart2SendStr(esp_cipmux);	   //设置多连接 (多路连接模式)
	 if(Data_compare("OK"))break;
	 else  Uart1SendStr("ERROR3,some problems with ESP8266 \r\n");
	 delay_ms(600);
	}
	Uart1SendStr("OK,set cipmux success! \r\n");
	memset(RX_buffer, 0, tbuf);//清缓存数据
	RX_num=0;				   //接收计数变量清0

    while(1)
	{
    
    
	 Uart2SendStr(esp_cipserver);	   //设置wifi模块为TCP服务器模式,并配置端口为8080
	 if(Data_compare("OK"))break;
	 else  Uart1SendStr("ERROR4,some problems with ESP8266 \r\n");
	 delay_ms(600);
	}
	Uart1SendStr("OK,set server success! \r\n");
	memset(RX_buffer, 0, tbuf);//清缓存数据
	RX_num=0;				   //接收计数变量清0

	while(1)
	{
    
    	
		if(Data_compare("LEDK"))               //点亮板上了的led
		{
    
    		
            led(1);	
			memset(RX_buffer, 0, tbuf);//清缓存数据
			RX_num=0;				   //接收计数变量
			Uart1SendStr("led is open!\r\n");							
		}
	    else if(Data_compare("LEDG")) 		   //关闭板上了的led
		{
    
    
            led(0);		
			memset(RX_buffer, 0, tbuf);//清缓存数据
			RX_num=0;				   //接收计数变量清0
			Uart1SendStr("led is close!\r\n");									
		}	
	}
   }

The following AT commands are mainly used here:
AT+CWMODE=3 (set the working mode of ESP8266 to 3 AP+Station, return "OK" or "no change")
AT+CWSAP="ESP8266_TEST","1234567890",1 ,3 (set the name and password of the WIFI, you can choose the name and password you like, the latter two are the channel number and password mode, the default is 1, 3)
AT+CIPMUX=1 (open multi-connection )
AT+CIPSERVER=1,8080 (create a TCP server, open port 8080)
I execute these AT commands in the program, but you can also use the USB to TTL module to directly connect to the WIFI module for configuration . AP+Station mode and configuration of WIFI name and password only need to be configured once. Even if the power is turned off and then powered on, there is no need to repeat the configuration. Just enter AT+CIPMUX=1 and AT+CIPSERVER=1,8080 after each power-on. In order to allow people who are new to the WIFI module to use this module directly, I wrote the AP+Station mode and the WIFI name and password in the program, so that no matter what your module was configured as before, after these four After configuration, you can find this WIFI in the hotspot on your computer, as shown in the figure below: insert image description here
At this point, our WIFI has been successfully set up.
In addition, every time you send an AT command to the WIFI module, the WIFI module will basically send a string of data back after receiving the specific command you sent (as a data response, in order to let you know whether your AT command is configured successfully, specifically I will attach a link to the AT command set at the end of the article. In my opinion, the AT command set below is very comprehensive.)
Next, I will briefly talk about how to use the APP "TCP connection" on the mobile phone:
After opening the APP, click on the connection in the upper right corner. At this time, he will ask you to select a remote host to connect to (if you just downloaded it, there is no connection below), and after you click on the upper right corner to connect, you will see There is an ellipsis in the upper right corner. After clicking it, he will ask you to enter the address and port number. This address is fixed at: 192.168.4.1. Connect, and then the serial debugging assistant on the PC side can see that after you send AT+CIFSR, the data returned by the WIFI module will be displayed on it, and the returned data includes this address), port: 8080, which corresponds to " AT+CIPSERVER=1,8080”. Then click the connection on the APP, so that the communication between the mobile APP and the WIFI module is completed (that is, the mobile phone is connected to the WIFI created by the WIFI module, but this WIFI cannot access the Internet, it can only be used for communication), just It is possible to control the WIFI module through the mobile phone APP, and then control the STM32 control board. In the above program, I wrote that the red light PA1 above the stm32f103c8t6 will light up when LEDK (that is, the abbreviation of LED on) is received, and LEDG (also It is the abbreviation of LED off) to turn off the red light on the stm32f103c8t6. The interface of the mobile phone is as follows:
insert image description here
Does it look similar to the interface of the Bluetooth serial port? This is also another method of communication. There is also an article in my previous article that is dedicated to Bluetooth communication. If you are interested, you can take a look. Well, that's all there is to say, the following is a link to the relevant program and AT command set.
stm32f103c8t6 controls the ESP8266 and the mobile phone APP communication program
ESP8266AT instruction set
code. I will send it to you as soon as possible. If you have any questions, you can comment below!

Guess you like

Origin blog.csdn.net/weixin_44069765/article/details/112187587