Using Mikroe WiFi ESP Click in FreeRTOS Print

 

This application note explains how to run a sample TCP server with the WiFi ESP Click card, from the FreeRTOS demo application.


Understanding WiFi ESP Click Interfaces

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.

FreeRTOS WiFi ESP Click C-Binding API

The WiFi ESP Click driver implements the following C-binding API:

Function Description Comments
int32_t wifiesp_init(wifiesp_handler *ctx);
Initialize WiFi ESP Click ctx is the WiFi ESP Click handle
void wifiesp_deinit(wifiesp_handler *ctx);
De-initialise WiFi ESP Click ctx is the WiFi ESP Click handle
void wifiesp_enable(wifiesp_handler *ctx);
Enable WiFi ESP Click ctx is the WiFi ESP Click handle
void wifiesp_disable(wifiesp_handler *ctx);
Disable WiFi ESP Click ctx is the WiFi ESP Click handle
int32_t wifiesp_generic_write(wifiesp_handler *ctx, char *data_buf, uint16_t len, uint32_t timeout);
Send an amount of data ctx is the WiFi ESP Click handle; data_buf is the pointer to data buffer; len is the amount of bytes to be sent; timeout is the amount of ticks that the driver waits for a response from UART; returns a number of transmitted bytes or a negative error code
int32_t wifiesp_generic_read(wifiesp_handler *ctx, char *data_buf, int32_t max_len, uint32_t timeout);
Receive an amount of data ctx is the WiFi ESP Click handle; data_buf is the pointer to data buffer; max_len is the max amount of bytes to be received; timeout is the amount of ticks that the driver waits for data from UART
int32_t wifiesp_send_cmd(wifiesp_handler *ctx, char *cmd, char *args);
Send a command ctx is the WiFi ESP Click handle; cmd is the pointer to command string; args is the pointer to the command arguments; returns a number of transmitted bytes or a negative error code

The wifiesp_handler data structure used in the driver API has the following definition:

typedef struct { uint8_t is_enabled; /* Enable flag */ uart_handler *uart; /* UART handle for communication with the WiFi ESP Click */ } wifiesp_handler;


FreeRTOS WiFi ESP Click Macros

The WiFi ESP Click driver provides the following macros for AT instructions:

Macro Instruction Description
WIFIESP_CHECK AT Test AT startup
WIFIESP_RESTORE AT+RESTORE Factory reset.
WIFIESP_RST AT+RST Restart module
WIFIESP_CHECK_FIRMWARE AT+GMR View version info
WIFIESP_SET_MODE AT+CWMODE Set WiFi mode without writing to Flash
WIFIESP_CONNECT_AP AT+CWJAP_CUR Connect to AP without writing the SSID and passphrase to Flash
WIFIESP_LIST_AP AT+CWLAP List available APs
WIFIESP_QUIT_AP AT+CWQAP Disconnect from AP
WIFIESP_SET_AP_PARAMETERS AT+CWSAP_CUR Configuration of softAP mode without writing to Flash
WIFIESP_JOINED_DEV_IP AT+CWLIF Get station’s IP which is connected to the soft-AP
WIFIESP_CONNESTION_STATUS AT+CIPSTATUS Check network connection status
WIFIESP_START AT+CIPSTART Establish TCP connection, UDP transmission or SSL connection
WIFIESP_MODE AT+CIPMODE Set transfer mode
WIFIESP_SEND AT+CIPSEND Send data
WIFIESP_CLOSE AT+CIPCLOSE Close TCP, UDP or SSL connection
WIFIESP_GET_IP AT+CIFSR Get local IP address
WIFIESP_SET_DHCP AT+CWDHCP_CUR Enable/Disable DHCP
WIFIESP_SET_STATION_IP AT+CIPSTA_CUR Set IP address of station
WIFIESP_GET_STATION_IP AT+CIPSTA_CUR? Get IP address of station
WIFIESP_SET_MULTIPLE_CONNECTION AT+CIPMUX Enable multiple connections for server
WIFIESP_SET_AS_SERVER AT+CIPSERVER Configure as TCP server
WIFIESP_SET_SERVER_TIMEOUT AT+CIPSTO Set TCP server timeout in seconds
WIFIESP_GET_SERVER_TIMEOUT AT+CIPSTO? Get TCP server timeout
WIFIESP_PING AT+PING Function Ping

Refer to ESP8266 AT Instruction Set for details about AT instructions.


Understanding TCP/IP support in FreeRTOS

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.

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.


Understanding Sample TCP Server

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.

The sample TCP Server functions as follows:

  • 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.

Sample TCP Server Sessions

Perform the following step-wise procedure, to validate the sample TCP server functionality:

  1. Connect the WiFi ESP Click module to the CLICK interface of the STM32H7-BSB Rev 2A board.
  2. Power cycle the board and let it boot to the U-Boot command line.
  3. Set the wifi_ssid and wifi_passwd variables in the U-Boot environment:
  4. STM32H7-SOM U-Boot > setenv wifi_ssid testssid
    STM32H7-SOM U-Boot > setenv wifi_passwd testpass
    STM32H7-SOM U-Boot > saveenv

  5. Power cycle the board and let it boot to the FreeRTOS CLI.
  6. Validate that the FreeRTOS BSP prints out the following message with the correct SSID and passphrase:
  7. Starting kernel ...

    STM32H7 SOM FreeRTOS CLI, www.emcraft.com
    Type help to view a list of available commands.
    Connecting to WiFi AP testssid with passphrase testpass

  8. Validate that the FreeRTOS BSP prints out the following message after several seconds:
  9. CLI> AT+CWJAP_CUR="testssid","testpass"
    WIFI DISCONNECT
    WIFI CONNECTED
    WIFI GOT IP

    OK

    WiFi connected
    Starting TCP Server
    AT+CIFSR
    +CIFSR:STAIP,"192.168.0.252"
    +CIFSR:STAMAC,"8c:aa:b5:14:89:d2"

    OK

    TCP Server started
    Please connect to the IP address listed above

  10. From a Linux PC machine, connect to the TCP server via telnet or nc:
  11. $ telnet 192.168.0.252 80
    Trying 192.168.0.252...
    Connected to 192.168.0.252.
    Escape character is '^]'.

  12. Send a message to the target and validate that the server sends the received message back:
  13. $ telnet 192.168.0.252 80
    Trying 192.168.0.252...
    Connected to 192.168.0.252.
    Escape character is '^]'.
    HELLO
    HELLO
    TEST
    TEST
    12345
    12345

  14. The server will close the connection if it does not receive any data in several seconds:
  15. Connection closed by foreign host.