TCP communication(WiFi) receive of STM32 with ESP32

Nucleo(STM32) is converted to wireless(WiFi) using the ESP32 wireless module and configured as a TCP server. I will introduce an application that remotely controls MCU by sending a string of command codes from a terminal emulator on a PC or smartphone. For more details about the ESP32 wireless module, see "ESP32 makes STM32Nucleo easily WiFi-enabled".

Configure a TCP server and connect to it with a PC, smartphone, or other client terminal application. The same as "TCP communication(WiFi) transmission of STM32 with ESP32".

To control MCU by command codes from the client side, all you need to do is to process incoming character codes and add what you want to be processed according to the codes.

If you connect the ESP32 to the terminal app, type the 9 characters of "depfields" and press Enter, you have sent 11 characters, including the newline code. If the terminal is set to local echo, the characters you entered will appear.

On the TCP server side, when a character is received from the client side, the message "+IPD,0,11:depfields" is displayed. +The +IPD indicates incoming data, and the connection ID is 0, meaning that a total of 11 characters were received, including the string and newline code. The response string contains the line feed codes CR+LF.

Character string reception

The following is an application for communication between ESP32 and MCU, which replaces the above-mentioned operation from PC terminal software with Nucleo(STM32). This application divides the process according to the match or mismatch between the code "ABC" registered in advance and the receiving code. The input code includes "@" at the end to indicate the end of the character string including the receiving code.

Data receiving flow

Enter "ABC@" while connected to the TCP server side of the Nucleo (STM32) WiFi enabled with ESP32 using a TCP communication terminal such as a PC or smartphone, etc. When ESP32 receives the string, it will send "+IPD,0,6:ABC@" as a character via UART communication. The STM32 side can extract the "ABC" code. 

Receiving Process

Once the receiving code is obtained, the application can then create what it wants to process, just as in serial communication.
For example, if the code is registered as "ON" or "OFF", and the processing can be divided into two types when the code matches these codes, it will be possible to remotely control the device as a software switch.

Data reception process

Receiving strings with ESP32 seems easy once you know how to do it, but there are some quirks. Understanding them will allow you to deal with them flexibly, so please try to develop them in various ways. Some of them are described below.

■ A certain amount of waiting time is required each time an AT command is sent.
■ The newline code CR+LF must be included before the response string (CR cannot be specified as the terminating character for this reason).
■ Whenever an AT command is sent, there must be some kind of response.

Note that if the AT command is used when an interrupt is used for reception, the response from the ESP32 will react as an interrupt.

Follow me!