...
This application note explains how to run a sample TCP server with the WiFi ESP Click card, from the FreeRTOS demo application.
1. Understanding WiFi ESP Click Interfaces
1.1. FreeRTOS WiFi ESP Click Implementation
The FreeRTOS BSP makes use of the STM32CubeH7 software component to provide a device driver for the WiFi ESP Click. The driver is configured (enabled / disabled) at the BSP build time, using the WIFIESP_MODULE_ENABLED
and HAL_UART_MODULE_ENABLED
configuration option, defined in the stm32h7xx_hal_conf.h
file.
1.2. FreeRTOS WiFi ESP Click C-Binding API
The WiFi ESP Click driver implements the following C-binding API:
...
Code Block |
---|
typedef struct { uint8_t is_enabled; /* Enable flag */ uart_handler *uart; /* UART handle for communication with the WiFi ESP Click */ } wifiesp_handler; |
1.3. FreeRTOS WiFi ESP Click Macros
The WiFi ESP Click driver provides the following macros for AT instructions:
...
Refer to ESP8266 AT Instruction Set for details about AT instructions.
2. Understanding TCP/IP support in FreeRTOS
2.1. FreeRTOS TCP/IP Support Implementation
The FreeRTOS BSP makes use of the WiFi ESP Click driver to provide TCP/IP support by sending AT instructions to the ESP8266 controller.
2.2. FreeRTOS TCP/IP Instructions Examples
The following are the examples of using the WiFi ESP Click driver to support TCP/IP in FreeRTOS:
Example | Description | Comments |
wifiesp_send_cmd(&wifiesp, WIFIESP_SET_MULTIPLE_CONNECTION, "<mode>"); | ||
Enable/disable multiple connections | The module responds OK if the mode set successfully and ERROR otherwise. Recommended mode for a TCP server is 1 (multiple connections enabled). | |
wifiesp_send_cmd(&wifiesp, WIFIESP_SET_AS_SERVER, "1,<port>"); | ||
Start a TCP server with given port | The module responds OK if the server started successfully and ERROR otherwise. | |
wifiesp_send_cmd(&wifiesp, WIFIESP_START, "\"TCP\",\"<IP>\",<port>"); | ||
Connect to a TCP server | The module responds OK if the server started successfully and ERROR otherwise, If TCP is connected already, returns ALREADY CONNECT. | |
wifiesp_send_cmd(&wifiesp, WIFIESP_START, "<link ID>,\"UDP\",\"\<IP>",<port>,<local port>,0"); | ||
Register UDP ports | The module responds OK if the server started successfully and ERROR otherwise, If the "connection" already exists, returns ALREADY CONNECT. The module will send datagrams to the given <IP>; <local port> is required for receiving datagrams from remote host. | |
wifiesp_send_cmd(&wifiesp, WIFIESP_SEND, "<link ID>,<data len>"); | ||
Send data to the remote host via TCP or UDP | The module responds >, which means that it is ready to send data. The data should be sent to the module without carriage return. If the number of bytes sent are more than the size defined by <data len>, the system will reply busy, send the first <data len> bytes and after sending the first <data len> bytes, the system will reply SEND OK. <link ID> can be omitted for a TCP client or in single connection mode. | |
wifiesp_send_cmd(&wifiesp, WIFIESP_CLOSE, "<link ID>"); | ||
Close TCP connection or end UDP transmission | The module responds <link ID>, CLOSED and OK if the connection closed correctly and ERROR otherwise. <link ID> can be omitted for a TCP client or in single connection mode. |
3. Understanding Sample TCP Server
3.1. Sample TCP Server Implementation
The FreeRTOS test application implements a separate thread called WiFi Thread, which provides a sample TCP server utilizing the WiFi ESP Click board.
...
Read the SSID and passphrase from the U-Boot Environment variables wifi_ssid and wifi_passwd.
Initialize the WiFi ESP Click driver and start communications with the WiFi ESP Click board via UART.
Attempt to connect to the assigned AP (Access Point) and obtain an IP address via DCHCP.
Start the TCP server with the port 80, on the IP address obtained in the previous step, and run the endless loop that waits for connections. Having established a connection, it prints the received messages to the console and sends received data back to the connected client.
3.2. Sample TCP Server Sessions
Perform the following step-wise procedure, to validate the sample TCP server functionality:
...