Embedded MQTT#

1. Purpose / Scope#

This application demonstrates how to configure the RS9116W EVK as MQTT client and establish connection with MQTT broker and how to subscribe, publish and receive the MQTT messages from MQTT broker.

In this application, RS9116W EVK configured as WiFi station and connects to the Access Point. After successful WiFi connection, RS9116W EVK connects to MQTT broker and subscribes to the topic "SILABS_TEST" and publishes a message "THIS IS MQTT CLIENT DEMO FROM SILABS" on that subscribed topic. After publishing the message on the subscribed topic, the MQTT client un-subscribes and disconnects with the MQTT broker.

2. Prerequisites / Setup Requirements#

Before running the application, the user will need the following things to setup.

2.1 Hardware Requirements#

Demonstration of MQTT protocol

2.2 Software Requirements#

3. Application Build Environment#

3.1 Platform#

The Application can be built and executed on below Host platforms

3.2 Host Interface#

  • By default, the application is configured to use the SPI bus for interfacing between Host platforms(STM32F411 Nucleo / EFR32MG21) and the RS9116W EVK.

  • This application is also configured to use the SDIO bus for interfacing between Host platforms(EFM32GG11) and the RS9116W EVK.

3.3 Project Configuration#

The application is provided with the project folder containing Keil and Simplicity Studio project files.

  • Keil Project

    • The Keil project is used to evaluate the application on STM32.

    • Project path: <SDK>/examples/snippets/embedded_mqtt/projects/embedded_mqtt-nucleo-f411re.uvprojx

  • Simplicity Studio

    • The Simplicity Studio project is used to evaluate the application on EFR32MG21.

    • Project path:

      • If the Radio Board is BRD4180A or BRD4181A, then access the path <SDK>/examples/snippets/embedded_mqtt/projects/embedded_mqtt-brd4180a-mg21.slsproj

      • If the Radio Board is BRD4180B or BRD4181B, then access the path <SDK>/examples/snippets/embedded_mqtt/projects/embedded_mqtt-brd4180b-mg21.slsproj

      • User can find the Radio Board version as given below

EFR Radio Boards

  • EFM32GG11 platform

    • The Simplicity Studio project is used to evaluate the application on EFM32GG11.

      • Project path:<SDK>/examples/snippets/wlan/embedded_mqtt/projects/embedded_mqtt-brd2204a-gg11.slsproj

3.4 Bare Metal/RTOS Support#

This application supports bare metal and RTOS configuration. By default, the application project files (Keil and Simplicity studio) are provided with bare metal environment in the SDK.

4. Application Configuration Parameters#

Note : If the user wants to use embedded (in firmware) MQTT library, then user can opt for this emb_mqtt application.

The application can be configured to suit user requirements and development environment. Read through the following sections and make any changes needed.

4.1 Open rsi_emb_mqtt.c file#

4.1.1 User must update the below parameters#

SSID refers to the name of the Access point.

#define SSID                                       "SILABS_AP"

SECURITY_TYPE refers to the type of security. In this application, STA supports Open, WPA-PSK, WPA2-PSK securities.

The valid configuration is:

  • RSI_OPEN - For OPEN security mode

  • RSI_WPA - For WPA security mode

  • RSI_WPA2 - For WPA2 security mode

#define SECURITY_TYPE                              RSI_OPEN

PSK refers to the secret key if the Access point configured in WPA-PSK/WPA2-PSK security modes.

#define PSK                                        "<psk>"

CLIENT_PORT port refers to device MQTT client port number

#define CLIENT_PORT                                5001

SERVER_PORT port refers remote MQTT broker/server port number

#define SERVER_PORT                                1883

SERVER_IP_ADDRESS refers remote peer IP address (Windows PC2) to connect with MQTT broker/server socket.

#define SERVER_IP_ADDRESS                          192.168.10.1

MQTT client keep alive period

#define RSI_KEEP_ALIVE_PERIOD                      0

QOS indicates the level of assurance for delivery of an Application Message.

QoS levels are:

0 - At most once delivery

1 - At least once delivery

2 - Exactly once delivery

#define QOS                                        0

RSI_MQTT_TOPIC refers to which topic WiSeConnect MQTT client is supposed to subscribe.

#define RSI_MQTT_TOPIC                             "SILABS"

MQTT Message to publish on the topic subscribed

uint8_t publish_message[] ="THIS IS MQTT CLIENT DEMO FROM SILABS"

MQTT Client ID with which MQTT client connects to MQTT broker/server

uint8_t clientID[] = "MQTTCLIENT"

User name for login credentials

int8_t username[] = "username"

Password for login credentials

int8_t password[] = "password"

4.1.2 The desired parameters are provided below. User can also modify the parameters as per their needs and requirements.#

Application memory length which is required by the driver

#define GLOBAL_BUFF_LEN                            15000

To configure IP address DHCP_MODE refers whether IP address configured through DHCP or STATIC

#define DHCP_MODE                                  1

Note: If user wants to configure STA IP address through DHCP then set DHCP_MODE to "1" and skip configuring the following DEVICE_IP, GATEWAY and NETMASK macros. (Or) If user wants to configure STA IP address through STATIC then set DHCP_MODE macro to "0" and configure following DEVICE_IP, GATEWAY and NETMASK macros.

IP address to be configured to the device in STA mode should be in long format and in little endian byte order.

Example: To configure "192.168.10.10" as IP address, update the macro DEVICE_IP as 0x0A0AA8C0.

#define DEVICE_IP                                  0X0A0AA8C0

IP address of the gateway should also be in long format and in little endian byte order

Example: To configure "192.168.10.1" as Gateway, update the macro GATEWAY as 0x010AA8C0.

#define GATEWAY                                    0x010AA8C0

IP address of the network mask should also be in long format and in little endian byte order.

Example: To configure "255.255.255.0" as network mask, update the macro NETMASK as 0x00FFFFFF.

#define NETMASK                                    0x00FFFFFF

4.2 Open rsi_wlan_config.h file. User can also modify the below parameters as per their needs and requirements.#

#define CONCURRENT_MODE                            RSI_DISABLE
#define RSI_FEATURE_BIT_MAP                        FEAT_SECURITY_OPEN
#define RSI_TCP_IP_BYPASS                          RSI_DISABLE
#define RSI_TCP_IP_FEATURE_BIT_MAP                 (TCP_IP_FEAT_DHCPV4_CLIENT | TCP_IP_FEAT_EXTENSION_VALID)
#define RSI_CUSTOM_FEATURE_BIT_MAP                 0
#define RSI_EXT_TCPIP_FEATURE_BITMAP               EXT_EMB_MQTT_ENABLE
#define RSI_BAND                                   RSI_BAND_2P4GHZ

For running EMB_MQTT with SSL, please enable TCP_IP_FEAT_SSL in rsi_wlan_config.h file, as shown below. Also load the related SSL Certificates in the module using rsi_wlan_set_certificate() API and and Need to enable SSL flag RSI_EMB_MQTT_SSL_ENABLE in rsi_emb_mqtt_client_init() API.

#define CONCURRENT_MODE                            RSI_DISABLE
#define RSI_FEATURE_BIT_MAP                        FEAT_SECURITY_OPEN
#define RSI_TCP_IP_BYPASS                          RSI_DISABLE
#define RSI_TCP_IP_FEATURE_BIT_MAP                 (TCP_IP_FEAT_DHCPV4_CLIENT | TCP_IP_FEAT_SSLTCP_IP_FEAT_DNS_CLIENT | TCP_IP_FEAT_EXTENSION_VALID)
#define RSI_CUSTOM_FEATURE_BIT_MAP                 EXT_FEAT_CUSTOM_FEAT_EXTENTION_VALID
#define RSI_EXT_CUSTOM_FEATURE_BIT_MAP             EXT_FEAT_256k_MODE
#define RSI_EXT_TCPIP_FEATURE_BITMAP               EXT_EMB_MQTT_ENABLE
#define RSI_BAND                                   RSI_BAND_2P4GHZ

Power save configuration

  • By default, the application is configured without power save.

#define ENABLE_POWER_SAVE 0
  • If user wants to run the application in power save, modify the below macro.

#define ENABLE_POWER_SAVE 1

Note: In rsi_mqtt_client.h change 'MQTT_VERSION' macro to either 3 or 4 based on the MQTT broker support version (Supported versions are 3 and 4).

5. Testing the Application#

Follow the steps below for the successful execution of the application.

5.1 Loading the RS9116W Firmware#

Refer Getting started with a PC to load the firmware into RS9116W EVK.

The firmware file is located in <SDK>/firmware/

5.2 Building the Application on Host Platform#

5.2.1 Using STM32#

Refer Getting started with STM32

  • Open the project <SDK>/examples/snippets/wlan/embedded_mqtt/projects/embedded_mqtt-nucleo-f411re.uvprojx

  • Build and Debug the project

  • Check for the RESET pin:

    • If RESET pin is connected from STM32 to RS9116W EVK, then user need not press the RESET button on RS9116W EVK before free run.

    • If RESET pin is not connected from STM32 to RS9116W EVK, then user need to press the RESET button on RS9116W EVK before free run.

  • Free run the project

  • Then continue the common steps from 5.3

5.2.2 Using EFX32#

Refer Getting started with EFX32, for settin-up EFR & EFM host platforms

  • Open Simplicity Studio and import the EFR32/EFM32 project from <SDK>/examples/snippets/wlan/embedded_mqtt/projects

    • Select the appropriate .slsproj as per Radio Board type mentioned in Section 3.3 for EFR32 board. (or)

    • Select the *.brd2204a-gg11.slsproj for EFM32GG11 board.

  • Compile and flash the project in to Host MCU

  • Debug the project

  • Check for the RESET pin:

    • If RESET pin is connected from STM32 to RS9116W EVK, then user need not press the RESET button on RS9116W EVK before free run

    • If RESET pin is not connected from STM32 to RS9116W EVK, then user need to press the RESET button on RS9116W EVK before free run

  • Free run the project

  • Then continue the common steps from 5.3

5.3 Common Steps#

  1. Configure the Access point in OPEN/WPA-PSK/WPA2-PSK mode to connect RS9116W device in STA mode.

  2. Install MQTT broker in Windows PC1 which is connected to Access Point.

  3. Run MQTT broker in Windows PC1 using following command. Open Command prompt and go to MQTT installed folder (Ex: C:\Program Files\mosquitto) and run the following command:

mosquito.exe -p 1883 -v

Run MQTT broker in Windows PC1

  1. Open MQTT client utility in Windows PC2 and connect to MQTT broker by giving Windows PC1 IP address and MQTT broker port number in Broker TCP/IP address field.

MQTT client utility in Windows PC2

  1. After successful connection, subscribe to the topic from MQTT client utility.

Subscribe to the topic from MQTT client utility

  1. After the program gets executed, RS9116W EVK will get connected to the same access point having the configuration same as that of in the application and get IP.

  2. Once the RS9116W EVK gets connected to the MQTT broker, it will subscribe to the topic RSI_MQTT_TOPIC (Ex: "SILABS_TEST"). The user can see the client connected and subscribe information in the MQTT broker.

Client Connected and Subscribe Information in the MQTT broker

  1. After successful subscription to the topic RSI_MQTT_TOPIC (Ex: "SILABS"), the device publishes a message which is given in publish_message array (Ex: "THIS IS MQTT CLIENT DEMO FROM SILABS") on the subscribed topic.

  2. MQTT client utility which is running on Windows PC3 will receive the message published by the device as it subscribes to the same topic.

    • Refer to the below image for MQTT client utility and message history.

MQTT client utility and message history

  1. Now publish a message using MQTT Utility on the same topic. Now this message is the message received by the device.

Publish a message using MQTT Utility

Note: Multiple MQTT client instances can be created

5.4 Procedure For exexcuting the Application when enabled with SSL#

  1. Configure the Access point in OPEN/WPA-PSK/WPA2-PSK mode to connect Silicon Labs device in STA mode.

  2. Install MQTT broker in Windows PC2 which is connected to Access Point through LAN.

  3. User needs to update the mosquitto.conf file with the proper file paths, in which the certificates are available in the mosquitto.conf file.

  4. Also, add "certs" folder to the mosquitto broker folder.

  5. Execute the following command in MQTT server installed folder. (Ex: C:\Program Files\mosquitto>mosquitto.exe -c mosquitto.conf -v) (Port can be 1883/8883)

    mosquitto.exe -c mosquitto.conf -v

For opening MQTT server

  1. If you see any error - Unsupported tls_version "tlsv1", just comment the "tls_version tlsv1" in mosquitto.conf file.

  2. From here, repeat the steps from step 4 to step 9 of 5.3 to complete the execution.