TCP communication (WiFi) send of STM32 with ESP32

This section introduces an application that uses the ESP32 wireless module to make Nucleo (STM32) wireless (WiFi) and send strings to a terminal on a PC or smartphone as a TCP server. The wireless module ESP32 is explained in detail in "ESP32 makes STM32Nucleo easily WiFi-enabled".

The application introduced here is a basic one that sends strings from Nucleo (STM32), which is WiFi enabled with ESP32 and configured as a TCP server, to a terminal on a client PC or smartphone, but once you have a solid understanding and can use it, you can apply it in various ways.

Nucleo (STM32) and ESP32 use serial UART to send and receive data by AT commands, which are commonly used in serial USART.

ESP32 and Nucleo (STM32) wiring

The mode of the wireless module ESP32 should be set to the general STA (Station) mode to be assigned an IP address by the WiFi router at home or at work. Other modes include AP (access point) mode, in which the ESP32 itself is used as the parent device, so please try to implement this mode.

This example assumes that the WiFi router address of the DHCP server in the parent unit is "192.168.3.1" and the IP address assigned to ESP32 is "192.168.3.19." Please note that each environment is different.

Network environment in STA mode

Let's take a look at the program, which is configured using FreeRTOS. The main program flow is to set up each peripheral immediately after startup, and then configure WiFi settings. After that, the program is divided into two tasks, prvTask_data for creating data for the demonstration program and prvTask_monitor for monitoring the data.

Main Program Structure

The WiFi settings are summarized in the wifi_setting function, which generates a TCP server with port number 50000.

Data creation for the demonstration is done in the task prvTask_data, which adds data (data) every 20 ms. data is unsigned 8 bits and repeatedly returns to 0 when it exceeds 255.

The task prvTask_monitor for data monitor sends a 5-character transmission request command "AT+CIPSEND=0,5\r\n", which includes 2 newline codes, to send a 3-digit number. Insert a wait time. Numeric data is converted to 3-digit numbers (digit_conv function) and USART is sent as a character string.

The wait time to be inserted after the "AT+CIPSEND=0,5\r\n" request command depends on the baud rate of the UART communication. We have tried it and found that it worked properly if the baud rate was set to 25 ms or more at 9600 bps and 3 ms or more at 115200 bps. The reference program is for 9600 bps.

Colum

The ESP32 UART communication settings can be changed with the command AT+UART_DEF=9600,8,1,0,0. For details, please refer to "ESP32 makes STM32Nucleo easily WiFi-enabled".

This is the basic configuration for WiFi-enabled string transmission. Once the TCP server is configured, the rest is as simple as serial communication. The communication protocol to realize the TCP server is built into the ESP32 firmware and only needs to be specified in the configuration code at the beginning.

WiFi setup and each task

To monitor the numbers sent from the TCP server side of Nucleo (STM32) on the client side, use a terminal application. Here we use TeraTerm as the terminal emulator on the PC; you should be able to connect by specifying the IP address and port.

Start a client-side terminal

The numbers sent from the Nucleo (STM32) side are updated every 0.5 seconds. Success is achieved if one cycle of numbers from 0 to 255 takes approximately 5 seconds.

Data monitor

Follow me!