STM32 single-chip microcomputer realizes data transmission through ESP8266WiFi module and Android APP (1) --- lower computer hardware configuration

The difficulty of affairs is far lower than the fear of things.
The STM32F407 MCU is connected to the Android mobile phone APP through the ESP8266 WiFi module to realize the mutual transmission of data. The status of the connection and the data transmitted between each other are displayed in real time on the LCD display on the MCU. Let’s look at the rendering first. :

  • STM32 MCU

  • Android phone APP

1. The hardware configuration of the lower computer

The MCU model used in this article is STM32F407. If you are using other types of MCU, you only need to change the program slightly. The WiFi module used is the ESP8266 module of punctual atom. The communication between this module and the MCU is realized through the serial port. , since the serial port 1 of the microcontroller is used as a download program, this article uses serial port 3. All the codes used by STM32 can be downloaded through the link at the end of this article. In the program, you can see the configuration of serial port 3, WiFi module The connection with the MCU can be directly inserted into the reserved interface of the MCU development board, as shown in the figure above.
If there is no reserved interface, then you need to use Dupont wires to connect, the power supply of the WiFi module can be connected to 3.3V and 5V, and then the RX pin of the WiFi module is connected to the TX pin of the MCU serial port 3, and the TX pin of the WiFi module Connect to the RX pin of serial port 3 of the microcontroller, and connect the ground of the WiFi module to the ground of the microcontroller.

2. WiFi module configuration

The WiFi module used in this article has three working modes, namely: STA mode, AP mode and STA+AP mode. In each mode, there are three modes: UDP, TCP Client and TCP Server modes. If not If you know too much about the working mode of the WiFi module, you can go to understand it first.
In this article, the WiFi module is set to the TCP Server mode under the AP mode, which is equivalent to a router, and the IP address and port number of the WiFi module are configured. After that, you can connect to the WiFi module through the mobile phone APP and transmit data to each other. It can control the hardware devices connected to the single-chip microcomputer to realize the function of simple Internet of Things.
The WiFi module configuration is done through AT commands, and some commonly used AT commands are listed below:

  • AT
    queries whether the connected WiFi module works normally, and returns OK if it works normally.

  • AT+RST
    restarts the WiFi module.

  • AT+CWMODE=N
    When N=1: set the working mode of the WiFi module to STA mode;
    when N=2: set the working mode of the WiFi module to AP mode;
    when N=3: set the working mode of the WiFi module The mode is set to STA+AP mode;

  • AT+CWMODE?
    Query the current working mode of the WiFi module.

  • AT+CWSAP=<ssid>,<pwd>,<chl>,<ecn>
    Configure AP parameters, only valid in AP mode.
    <ssid>: WiFi access point name;
    <pwd>: WiFi password;
    <chl>: channel;
    <ecn>: encryption method, 0—OPEN, 1—WEP, 2—WPA_PSK, 3—WPA2_PSK,4— WPA_WPA2_PSK;
    after this command is set. The connection to the network may fail. At this time, it is necessary to send the AT+RST restart command and wait for a while.

  • AT+CIPMUX=N
    When N=0: WiFi module is set to single connection mode;
    when N=1: WiFi module is set to multi-connection mode;

  • AT+CIPMUX?
    Query whether the WiFi module has established multiple connections currently.

  • AT+CIPMODE=N
    When N=0: WiFi module is set to non-transparent mode;
    when N=1: WiFi module is set to transparent mode;

  • AT+CIPMODE?
    Query the current transmission mode of the WiFi module.

  • AT+CIPSTO=XXXX
    set the timeout of WiFi module.

  • AT+CIPSTO?
    Query the server timeout of the WiFi module.

  • AT+CIPSERVER=<mode>,<port>
    create/close server.
    <mode>: 0—close the server, 1—create the server.
    <port>: Set the port number of the server.
    Note:
    1) The server can only be turned on when the WiFi module is in multi-connection mode (AT+CIPMUX=1), and a restart is required to turn off the server.
    2) After the server is turned on, the server monitor is automatically established, and when a client accesses, it will automatically occupy a connection in sequence.

  • AT+CIPSTATUS
    to view the current connection of the WiFi module.

  • AT+CIPSEND
    WiFi module sends data to a certain connection.
    1) For single-channel connection (+CIPMUX=0), the command is AT+CIPSEND=<lenth>
    2) For multi-channel connection (+CIPMUX=1), the command is AT+CIPSEND=<id>,<lenth>
    <id >: is the id of the client.
    <lenth>: the length of the sent data.
    Response: After receiving this command, return ">" first, then start receiving serial data, and send data when the data length reaches lenth. If the connection is not
    established or the connection is disconnected, return ERROR;
    if the data is sent successfully, return SEND OK;

  • AT+CIPSTART
    to establish a connection.
    1) For single-channel connection (+CIPMUX=0), the command is AT+CIPSTART=<type>,<addr>,<port> 2
    ) For multi-channel connection (+CIPMUX=1), the command is AT+CIPSEND=< id>,<type>,<addr>,<port>
    For example, if you want to establish a TCP connection, the command is as follows: AT+CIPSTART="2", "TCP", "192.168.4.101", 8080
    If it is correct and successful, return OK , otherwise return ERROR;
    if the connection already exists, return ALREAY CONNECT;

  • AT+CWLAR
    View the current wireless router list.

  • AT+CWJAP=<ssid>,<pwd>
    Join the specified network.

  • AT+CWJAP?
    Query the AP currently connected to the WiFi module.

  • AT+CWLIF
    queries the currently connected IP of the WiFi module.

  • AT+CIFSR
    queries the IP address of the WiFi module.

After understanding the commonly used AT commands, then we will use AT commands to configure the WiFi module. This article configures the WiFi module in the program, or it can be configured on the PC side by using the serial port assistant to send AT commands. TCP in AP mode The configuration process of SERVER mode is as follows:

1.	AT		//查看芯片是否正常工作
响应:
	OK
2.	ATE0	//关闭回显
响应:
	OK
3.	AT+CWMODE=2		//配置成AP模式
响应:
	OK
4.	AT+RST		//重启WiFi模块
响应:
	ready
5.	AT+CWSAP="ssid","pwd",1,4	//配置WiFi名称、密码等
响应:
	OK
6.	AT+CIPMUX=1		//开启多连接
响应:
	OK
7.	AT+CIPSERVER=1,8088		//开启TCP服务器,端口号设置成8088
响应:
	OK
8.	AT+RST		//重启WiFi模块
响应:
	OK
9.	AT+CIFSR	//查看WiFi模块的IP和MAC地址
响应:
	+CIFSR:APIP,"192.168.4.1"
	+CIFSR:APMAC,"ea,68,e6,86,c5,39"
	OK

After configuring the WiFi module according to the above steps, you can enter the IP address ("192.168.4.1") and port number (8088) on the mobile APP to establish a TCP connection, and then transmit data.

STM32 MCU and Android APP source code free access method:
insert image description here

For the steps of Android APP configuration, please refer to:
STM32 single-chip microcomputer realizes data transmission with Android APP through ESP8266WiFi module (2) - host computer construction

Guess you like

Origin blog.csdn.net/weixin_44355077/article/details/116121764