ioNIC MQTT Client C Example
The W55RP20-S2E operates as an MQTT client and publishes messages to, and subscribes to topics from, an MQTT broker.
How to MQTT Client Example
This example explains how the Raspberry Pi Pico transmits AT commands to the W55RP20-S2E board via the UART or SPI interface to configure it in MQTT client mode.
After the MQTT client configuration is completed, the W55RP20-S2E connects to an MQTT broker running on a PC.
When a message is published to a subscribed topic from the MQTT broker, the W55RP20-S2E receives the message and forwards it to the Raspberry Pi Pico via UART or SPI.
The Raspberry Pi Pico processes the received message, generates a publish message, and sends it to the W55RP20-S2E through UART or SPI. The W55RP20-S2E then publishes the message to the configured topic via the MQTT broker.
Step 1: Prepare software
The following serial terminal programs are required for test, download and install from below links.
Step 2: Prepare hardware
1. Connect GPIO13 according to the selected UART/SPI mode of the W55RP20 EVB Pico, as illustrated below.
| GPIO13 Pin | Desc |
| LOW(GND) | UART mode (default) |
| HIGH(3.3V) | SPI mode |
2. Connect the Raspberry Pi Pico and the W55RP20 EVB Pico using jumper wires as shown below, depending on the selected UART/SPI mode.
If UART mode :
| Raspberry Pi Pico | W55RP20 EVB Pico |
| GPIO4 (UART_TX) | GPIO5 (UART_RX) |
| GPIO5 (UART_RX) | GPIO4 (UART_TX) |
| GND | GND |
If SPI mode :
| Raspberry Pi Pico | W55RP20 EVB Pico |
| GPIO2 (SPI_CLK) | GPIO2 (SPI_CLK) |
| GPIO3 (SPI_TX) | GPIO4 (SPI_RX) |
| GPIO4 (SPI_RX) | GPIO3 (SPI_TX) |
| GPIO5 (SPI_CS) | GPIO5 (SPI_CS) |
| GPIO26 (SPI_INT) | GPIO26 (SPI_INT) |
| GND | GND |
3. Connect the Raspberry Pi Pico to your PC (desktop or laptop) using a 5-pin Micro USB cable.
4. Connect the W55RP20 EVB Pico to your PC (desktop or laptop) using a USB Type-C cable.
Step 3: Setup MQTT Client Example
Raspberry Pi Pico
Please refer to the link below for setting up the Raspberry Pi Pico development environment.
Getting started with Raspberry Pi Pico
We recommend the following versions for successful build and development:
- pico-sdk:
2.2.0 - ARM GCC Toolchain:
14.2.Rel1
To test the MQTT Client example, minor settings shall be done in code.
Setup network configuration such as IP and Port etc... in 'mqtt_pub_sub_uart/spi.c' in 'mqtt/' directory.
// mqtt_pub_sub_uart.c
// ======= Server Information =======
const char* SERVER_IP = "192.168.11.100"; // PC(Server) IP
const char* SERVER_PORT = "1883"; // PC(Server) MQTT Port
const char* MQTT_CLIENT_ID = "user"; // MQTT client id
const char* PUB_TOPIC = "/w55rp20/pub"; // MQTT Publish Topic
const char* SUB_TOPIC = "/w55rp20/sub"; // MQTT Subsribe Topic
int main() {
...
printf("\n--- Config W55RP20 with AT command(UART) ---\n");
{
enter_command_mode(); // Send +++ command
factory_reset(); // Send Factory Reset command
// If you send Reset or Factory Reset command, W55RP20 will reboot and enter to default gateway mode
enter_command_mode(); // Send +++ command
at_set("OP", "5"); // Set W55RP20 MQTT client mode
at_set("LI", "192.168.11.2"); // Set W55RP20's Local IP : 192.168.11.2
at_set("SM", "255.255.255.0"); // Set W55RP20's Subnet mask : 255.255.255.0
at_set("GW", "192.168.11.1"); // Set W55RP20's Gateway : 192.168.11.1
at_set("DS", "8.8.8.8"); // Set W55RP20's DNS Address : 8.8.8.8
at_set("LP", "5000"); // Set W55RP20's Local Port : 5000
at_set("RH", SERVER_IP); // Set Remote IP(ex. PC)
at_set("RP", SERVER_PORT); // Set Remote Port
at_set("PT", "10"); // Set Serial Data Packing Time : 10ms
at_set("QU", "wiznet"); // Set MQTT user name
at_set("QP", ""); // Set MQTT password
at_set("QC", MQTT_CLIENT_ID); // Set MQTT client ID
at_set("QK", "60"); // Set MQTT Keep-alive
at_set("PU", PUB_TOPIC); // Set MQTT public topic
at_set("U0", SUB_TOPIC); // Set MQTT subscribe topic1
at_set("Q0", "0"); // Set MQTT QoS level
at_set("SV", NULL); // Send Save command
sleep_ms(100);
device_reset(); // Send Reset command
uart_rx_flush();
}
...
}// mqtt_pub_sub_spi.c
// ======= Server Information =======
const char* SERVER_IP = "192.168.11.100"; // PC(Server) IP
const char* SERVER_PORT = "1883"; // PC(Server) MQTT Port
const char* MQTT_CLIENT_ID = "user"; // MQTT client id
const char* PUB_TOPIC = "/w55rp20/pub"; // MQTT Publish Topic
const char* SUB_TOPIC = "/w55rp20/sub"; // MQTT Subsribe Topic
int main() {
...
printf("\n--- Config W55RP20 with AT command(SPI) ---\n");
{
at_set("FR", NULL); // Send Factory Reset command
printf("W55RP20 is Rebooting...\n");
sleep_ms(3000);
at_set("OP", "5"); // Set W55RP20 MQTT client mode
at_set("LI", "192.168.11.2"); // Set W55RP20's Local IP : 192.168.11.2
at_set("SM", "255.255.255.0"); // Set W55RP20's Subnet mask : 255.255.255.0
at_set("GW", "192.168.11.1"); // Set W55RP20's Gateway : 192.168.11.1
at_set("DS", "8.8.8.8"); // Set W55RP20's DNS Address : 8.8.8.8
at_set("LP", "5000"); // Set W55RP20's Local Port : 5000
at_set("RH", SERVER_IP); // Set Remote IP(ex. PC)
at_set("RP", SERVER_PORT); // Set Remote Port
at_set("PT", "10"); // Set Serial Data Packing Time : 10ms
at_set("QU", "wiznet"); // Set MQTT user name
at_set("QP", ""); // Set MQTT password
at_set("QC", MQTT_CLIENT_ID); // Set MQTT client ID
at_set("QK", "60"); // Set MQTT Keep-alive
at_set("PU", PUB_TOPIC); // Set MQTT public topic
at_set("U0", SUB_TOPIC); // Set MQTT subscribe topic1
at_set("Q0", "0"); // Set MQTT QoS level
at_set("SV", NULL); // Send Save command
at_set("RT", NULL); // Send Reset command
printf("W55RP20 is Rebooting...\n");
sleep_ms(3000);
spi_rx_pending = false;
}
...
}W55RP20 EVB Pico
Please refer to the link below for instructions on how to use the W55RP20 S2E.
Getting started with W55RP20 EVB Pico
PC(Mosquitto)
Execute the following command to start the MQTT broker using Mosquitto.
If the MQTT broker starts successfully, the broker’s IP address will be the current IP address of your desktop or laptop, and the default port is 1883.
## Windows cmd.exe
#Mosquitto Broker
> mosquitto -c mosquitto.conf -vStep 4: Build and Download
Raspberry Pi Pico
To build the project in Visual Studio Code, select one of the three methods below:
1. Click Build in the bottom status bar
2. Press F7 on your keyboard
3. Run the following command in the Visual Studio Code terminal
$ mkdir build
$ cd ./build
$ cmake -G "NMake Makefiles" ..
$ nmakeWhen the build is completed, the '{example_name}_uart.uf2' file is generated for UART mode, and the '{example_name}_spi.uf2' file is generated for SPI mode in the {Repository}/build/mqtt/ directory. Download the UF2 file to the Raspberry Pi Pico.
W55RP20 EVB Pico
Following Step 3 in the Getting Started Guide, program the W55RP20 EVB Pico with the UF2 file and write the MAC address to complete the setup.
Step 5: Run
Connect to the serial COM port of the Raspberry Pi Pico using Tera Term.
Step 5.1: UART mode
1. Configuring the W55RP20 S2E as a MQTT Client and Setting Up the Network with AT command.
2. After the W55RP20-S2E is configured as an MQTT client and successfully connects to the MQTT broker running on the PC, the Windows terminal displays a message as shown below.
3. After starting the Mosquitto MQTT broker on the PC, open two separate terminal windows.
In one terminal window, use the mosquitto_sub command to subscribe to a specific topic.
In the other terminal window, use the mosquitto_pub command to publish a message (e.g., “Hello World!”) to the same topic.
The published message is delivered to the subscribed W55RP20-S2E module through the MQTT broker.
4. The W55RP20-S2E receives messages from a subscribed topic through the MQTT broker and forwards them to the Raspberry Pi Pico via the UART interface.
The Raspberry Pi Pico prints the received message to a terminal, generates a publish message, and transmits it back to the W55RP20-S2E over UART.
5. The W55RP20-S2E publishes the response message received via UART to the configured topic through the MQTT broker.
Afterward, the published message can be verified in the terminal window that is subscribing to the same topic using the mosquitto_sub command.
Step 5.2: SPI mode
1. Configuring the W55RP20 S2E as a MQTT Client and Setting Up the Network with AT command.
2. After the W55RP20-S2E is configured as an MQTT client and successfully connects to the MQTT broker running on the PC, the Windows terminal displays a message as shown below.
3. After starting the Mosquitto MQTT broker on the PC, open two separate terminal windows.
In one terminal window, use the mosquitto_sub command to subscribe to a specific topic.
In the other terminal window, use the mosquitto_pub command to publish a message (e.g., “Hello World!”) to the same topic.
The published message is delivered to the subscribed W55RP20-S2E module through the MQTT broker.
4. The W55RP20-S2E receives messages from a subscribed topic through the MQTT broker and forwards them to the Raspberry Pi Pico via the SPI interface.
The Raspberry Pi Pico prints the received message to a terminal, generates a publish message, and transmits it back to the W55RP20-S2E over SPI.
5. The W55RP20-S2E publishes the response message received via SPI to the configured topic through the MQTT broker.
Afterward, the published message can be verified in the terminal window that is subscribing to the same topic using the mosquitto_sub command.

