Communicate with esp8266 using stm32+app inventor

Communicate with esp8266 using stm32+app inventor


The purpose of the experiment in this chapter is to connect the mobile phone to the WIFI network provided by the ESP8266, open the custom app to connect to the ip address and port number of the ESP8266, realize the communication between the app and the ESP8266 module, and then achieve the purpose of the app controlling the 32 development board, and develop the The data on the board is returned to the app for display.
  About the communication between stm32 and esp8266, it has been introduced in the previous articles. This article mainly introduces the production of app inventor, then generates the app QR code, and finally downloads the app on the mobile phone, and then realizes the use of WIFI LAN. To control the stm32 development board, of course, some things between stm32 and esp8266 will be briefly mentioned.
The devices used in this chapter are as follows:
STM32F103ZET6 minimum system board
ESP8266-01S module
USB to microUSB data cable
Four DuPont cables

are connected as follows:
32 board 3.3v--------> 3.3v of ESP8266
32 board GND ----------> GND
of ESP8266 32 board PA2-------> ESP8266 RX
32 board PA3--------> ESP8266 TX

stm32 communicates with esp8266

  First attach the main code of the 32 program:

#include "stdio.h"
#include "delay.h"
#include "led.h"
#include "usart.h"
#include "serial.h"
#include "esp8266.h"

/*	AT指令返回值的结尾没有\r\n这样的回车换行符,
 *	而在串口调试助手中,需要MCU输出\r\n给串口调试助手才能刷新缓冲区把字符显示出来,
 *	同时这个\r\n还起到回车换行的目的*/
 
/*	AT指令的结尾处必须是\r\n*/

/*	serial.h中的usart1_send_usart2函数用于没有USB转TLL模块的人使用,
 *	此函数作用是利用串口1发送AT指令给串口2,串口2将AT指令发送给ESP8266,实现对ESP8266的AT指令配置,
 *	当然,如果你有USB转TTL模块的话,可以直接将ESP8266与USB转TTL模块直连,然后在串口调试助手里面直接配置AT指令*/

extern u8 	esp_sipsend1[];
extern u8 	esp_sipsend2[];

int main(void)
{
    
    	
	delay_init();	    	 								//延时函数初始化
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);			//设置NVIC中断分组2:2位抢占优先级,2位响应优先级
	uart_init(115200);	 									//串口1初始化为115200
	Uart2_Init(115200);										//串口2初始化为115200
	LED_Init();		  										//初始化与LED连接的硬件接口
	Esp8266_Init();
	
  while(1)
	{
    
    	
		usart1_send_usart2();								//利用串口1发送AT指令给串口2,从而达到配置ESP8266的目的,并且让模块的返回值在串口1打印出来
		if(Data_Compare((u8 *)"LEDK"))               		//点亮板上的led
		{
    
    		
            led(1);	
			Clear_Buf();
			Uart_SendStr(USART1, (u8 *)"led is open\r\n");	
			Uart_SendStr(USART2, esp_sipsend1);				//利用串口2给esp8266发送AT+CIPSEND指令,告诉它下一次将发送具体数据
			Usart2_Receive_Data(USART1);					//将串口2接收到的AT+CIPSEND指令返回的数据显示在串口1上
			Uart_SendStr(USART2, (u8 *)"led is open\r\n");	//发送具体数据给esp8266,让他转发到app上
		}
		else if(Data_Compare((u8 *)"LEDG")) 		   		//关闭板上的led
		{
    
    
            led(0);
			Clear_Buf();
			Uart_SendStr(USART1, (u8 *)"led is close\r\n");	
			Uart_SendStr(USART2, esp_sipsend2);				//利用串口2给esp8266发送AT+CIPSEND指令,告诉它下一次将发送具体数据
			Usart2_Receive_Data(USART1);					//将串口2接收到的AT+CIPSEND指令返回的数据显示在串口1上
			Uart_SendStr(USART2, (u8 *)"led is close\r\n");	//发送具体数据给esp8266,让他转发到app上		
		}
	}
}


  serial.h in this code contains several custom functions, and the functions of the functions are shown in the comments to the right. The code is as follows:

#ifndef	__SERIAL_H
#define __SERIAL_H
#include "stdio.h"	
#include "sys.h" 
#include "delay.h"
#include  "string.h"
#include "usart.h"

#define tbuf 100

void Uart2_Init(u32 bound);							//初始化串口2
void Clear_Buf(void);								//清除缓冲区数据
void Uart_SendStr(USART_TypeDef* USARTx, u8 *s);	//往某一个串口发送指定字符串
void Usart2_Receive_Data(USART_TypeDef* USARTx);	//将串口2返回的数据发送给串口1进行显示
u8 Data_Compare(u8 *p);								//指定字符串与缓存数组数据进行数据比较
void usart1_send_usart2(void);						//利用串口1发送AT指令给串口2,从而达到配置ESP8266的目的,并且让模块的返回值在串口1打印出来

#endif

  The Usart2_Receive_Data function is used to receive the data returned by esp8266 and display it on serial port 1. Because serial port 2 is connected to esp8266, as long as serial port 2 sends AT commands to esp8266 correctly, esp8266 will return the response value. Since there is no USB switch on hand The TTL module can only use this method to observe the return value of the esp8266 module.
  The function of usart1_send_usart2 is to use the serial port debugging assistant to send AT commands to serial port 1. After serial port 1 receives the data, it sends the data to the esp8266 module through serial port 2, so as to achieve the purpose of sending AT commands through the serial port debugging assistant to configure the esp8266 module. In this way, the reason is that there is no USB to TTL module on hand, so this is the last resort.
  If you have a USB to TTL module on hand, the above two functions are unnecessary. Directly connect the USB to TTL module to the esp8266 module, then plug the USB end into the computer, and use the serial debugging assistant to directly send AT commands to configure the esp8266 module and the return value of the display module.
  The Esp8266_Init function in the main function is to configure a series of esp8266 so that it can be connected to the app client. The detailed code is as follows:

#include "esp8266.h"

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=2\r\n";     			//设置ESP8266的工作模式2(AP模式,WIFI模块作为热点),返回"OK"或者"no change"
u8  esp_cwsap[] = "AT+CWSAP=\"ESP8266_TEST\",\"1234567890\",1,4\r\n";//设置WIFI的名称、密码、通道号、加密方式(4-WPA_WPA2_PSK)
u8  esp_cipmux[] = "AT+CIPMUX=1\r\n";   			//打开多连接	
u8  esp_cipserver[] = "AT+CIPSERVER=1,8080\r\n";  	//建立TCP服务器,开放端口8080,端口号可以改成其他的例如8086等等
u8 	esp_cipsto[] = "AT+CIPSTO=2880\r\n";			//设置服务器超时时间为2880s,也就是连接服务器过程中可以等待的时间

extern u8	RX_num;   								//接收计数变量
extern u8  RX_buffer[tbuf];

void Esp8266_Init(void)
{
    
    
	Uart_SendStr(USART2, esp_cwmode);	   	//设置ESP8266的工作模式2 AP,返回"OK"或者"no change"
	Usart2_Receive_Data(USART1);			//将串口2收到的数据发送给串口1进行显示
	Uart_SendStr(USART1, (u8 *)"\r\nOK,set mode as AP with ESP8266!\r\n----------\r\n");
	
	Uart_SendStr(USART2, esp_rst);	   		//设置ESP8266的工作模式后需要复位以生效该模式
	Usart2_Receive_Data(USART1);			//复位后可能会返回一堆乱码
	Uart_SendStr(USART1, (u8 *)"\r\nOK,set RST success!\r\n----------\r\n");
	
	Uart_SendStr(USART2, esp_cwsap);	   	//设置WIFI的名称及密码
	Usart2_Receive_Data(USART1);			//将串口2收到的数据发送给串口1进行显示
	Uart_SendStr(USART1, (u8 *)"\r\nOK,set cwsap success!\r\n----------\r\n");
	
	Uart_SendStr(USART2, esp_cipmux);	   	//设置多连接(多路连接模式)
	Usart2_Receive_Data(USART1);			//将串口2收到的数据发送给串口1进行显示
	Uart_SendStr(USART1, (u8 *)"\r\nOK,set cipmux success!\r\n----------\r\n");
	
	Uart_SendStr(USART2, esp_cipserver);	//设置wifi模块为TCP服务器模式,并配置端口为8080
	Usart2_Receive_Data(USART1);			//将串口2收到的数据发送给串口1进行显示
	Uart_SendStr(USART1, (u8 *)"\r\nOK,set server success!\r\n----------\r\n");
	
	Uart_SendStr(USART2, esp_cipsto);		//设置服务器超时时间为2880s
	Usart2_Receive_Data(USART1);			//将串口2收到的数据发送给串口1进行显示
	Uart_SendStr(USART1, (u8 *)"\r\nOK,set cipsto success!\r\n----------\r\n");
	
	Uart_SendStr(USART2, esp_cifsr);		//获取本机IP地址
	Usart2_Receive_Data(USART1);			//将串口2收到的数据发送给串口1进行显示
	Uart_SendStr(USART1, (u8 *)"\r\nOK,get ip address success!\r\n----------\r\n");
}

  As for the AT commands, there are a lot of detailed explanations about the AT commands if you find them on the Internet. I won't explain too much here, and there is nothing to say about other functions. So far, the stm32 code part is here. .

Mobile app production, generation and control

  Then proceed to the design of the app. The design of the app in this chapter uses app inventor.
  APP INVENTOR was developed by Google Labs and later handed over to MIT. It is a visual programming environment that uses building blocks to quickly build applications, and can be combined with AI companions to easily test and apply on mobile phones.
  APP INVENTOR is a completely web-based online programming tool, and there are some servers available at home and abroad.
MIT server login address: http://appinventor.mit.edu/explore/Guangzhou
server login address: http://app.gzjkw.net/login It is
recommended to use the domestic Guangzhou server.
insert image description here
  You can log in with QQ. After logging in, you can create a project and enter the programming interface.
insert image description here
  The middle display is the mobile phone screen. By dragging the tool to the mobile phone screen, you can start programming. Currently, only Android mobile phones are supported.
  In order to facilitate programming and debugging, AI Companion can be installed on the mobile phone, and the download address can be opened by clicking Help -> AI Companion Information in the menu bar to open the download help page.
insert image description here
  The AI ​​companion can be downloaded by scanning the QR code in the picture with the mobile phone. When debugging the program, select the menu bar connection on the server side -> AI companion to pop up the connection QR code and 6-digit code, and enter the 6-digit code in the corresponding mobile AI companion. Code, or scan the QR code, you can establish a connection with the server and start debugging the program. At the same time, as long as your web app interface changes, the corresponding interface on the mobile AI companion will be updated accordingly in real time.
insert image description here
  In the design process of this WIFI communication app, a tcp connection plug-in, which is an extension plug-in, needs to be used. It needs to be imported into app inventor to use tcp to connect to the wifi provided on the esp8266 module. The link of this plug-in is as follows: app inventor extension
  The app interface created in the above figure uses the basic controls and extension plug-ins on the left. The logic design diagram is as follows:
insert image description here
  This logic design is also very simple, just pieced together with some functional blocks on the left. There are a lot of detailed app inventor grammars, and there are also a lot of online searches, so I won't introduce them too much here. At this point, the design of the app is basically completed, and then the app will be generated. Click the package apk->package apk in the menu bar and display the QR code. At this time, a QR code of the app will be generated. AI companion app, and then use the scan QR code on the AI ​​companion to scan the generated app QR code. After scanning, it will automatically jump to the default browser to install the app, install and open the app, and enter ESP8266 on the new app. The ip address and port number (the ip address can be printed out on the serial debugging assistant when the esp8266 initialization function is executed, and the port number is the 8080 port number in the previous AT+CIPSERVER=1,8080), and then connect, the connection is successful Then, enter: LEDK in the sending area and click send!, the string "LEDK" will be sent to the ESP8266 module. After the module receives the information, it will be returned to the MCU through serial port 2. After the MCU receives the data, it will be set in the program. The strings are compared, and if they are consistent, the corresponding operation instructions are executed, and the data on the MCU is returned to the app for display.
Attach the link of the app project made by app inventor: app_inventor project
Attach the link of the communication code of the entire stm32 project: stm32+app inventor+esp8266 The communication code
should be the message below and I will send it to you as soon as possible, if you have any questions, you can also Comment below!

Guess you like

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