W55RP20-S2E MQTT client Example (MicroPython)
This example demonstrates MQTT client on W55RP20 S2E via UART/SPI using MicroPython on a Raspberry Pi Pico (Master)
Software Apps and online services
How to MQTT Client Example (MicroPython)
This program demonstrates how to configure the W55RP20-S2E board as an MQTT Client (OP=5) using a Raspberry Pi Pico (Host) via UART or SPI. It connects to an MQTT broker, subscribes to a specific topic, and publishes messages to the network.
Step 1: Prepare software
The following software is required to run and test the MicroPython example.
- Thonny : An integrated development environment for MicroPython. used to write, upload, and run code.
- MQTT Broker: A server to mediate messages (e.g., Mosquitto installed on your PC).
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.
- Important Note on Mode Switching: In this example setup, GPIO 13 of the W55RP20 EVB Pico is physically connected to GPIO 13 of the Raspberry Pi Pico. The Host Pico sets the mode (High/Low) automatically based on the software configuration.
- If you change the
MODEin the code (e.g., UART ↔ SPI): You MUST reset both boards (Raspberry Pi Pico and W55RP20 EVB Pico) to ensure the mode is switched and recognized correctly.
If UART mode :
| Raspberry Pi Pico | W55RP20 EVB Pico |
| GPIO4 (UART_TX) | GPIO5 (UART_RX) |
| GPIO5 (UART_RX) | GPIO4 (UART_TX) |
| GPIO13 (MODE_SEL) | GPIO13 (MODE_SEL) |
| GND | GND |
If SPI mode :
| Raspberry Pi Pico(Master) | W55RP20 EVB Pico(Slave) |
| 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) |
| GPIO13 (MODE_SEL) | GPIO13 (MODE_SEL) |
| 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 Example
- Raspberry Pi Pico (Master) : Getting started with Raspberry Pi Pico (MicroPython)
Please refer to the link below to install the MicroPython firmware on the Raspberry Pi Pico.
- W55RP20 EVB Pico : Getting started with W55RP20 EVB Pico
Please refer to the link below for instructions on how to use the W55RP20 S2E.
Step 4: Upload Code
- Raspberry Pi Pico
Open Thonny IDE and connect to the Raspberry Pi Pico. Upload the following driver and example files to the Pico's storage.
Required Files:
w55rp20_s2e_uart.py(Required for UART mode)w55rp20_s2e_spi.py(Required for SPI mode)
Example Application Files (Choose one to test):
08_mqtt_client.py(The main application)
Configuration: Before running an example, open the Python file you intend to use and modify the User Configuration section at the top.
# 08_mqtt_client.py
#
# MQTT Client example (Supports both SPI and UART modes):
# - Configure the module as MQTT client (OP=5) + DHCP
# - Connect to broker, subscribe to topic, and handle incoming messages
#
# Select mode by changing the MODE variable below.
import time
# -------------------------------------------------------------------------
# Configuration
# -------------------------------------------------------------------------
MODE = "uart" # Set to "spi" or "uart"
USE_DHCP = True # True: DHCP (IM=1), False: Static IP (IM=0)
# MQTT Broker Settings
BROKER_HOST = "192.168.11.2"
BROKER_PORT = "1883"
CLIENT_ID = "w55rp20_mpy_client"
SUB_TOPIC = "w55rp20/sub"
PUB_TOPIC = "w55rp20/pub"
PUB_MESSAGE = "Hello from W55RP20-S2E"
KEEP_ALIVE = 30
# Network Configuration
LOCAL_IP = "192.168.11.100" # Local IP (Used when USE_DHCP=False)
SUBNET_MASK = "255.255.255.0" # Subnet Mask (Used when USE_DHCP=False)
GATEWAY = "192.168.11.1" # Gateway (Used when USE_DHCP=False)
DNS_SERVER = "8.8.8.8" # DNS Server (Used when USE_DHCP=False)
.
.Step 5: Testing with Mosquitto (PC Side)
Follow these steps to set up your PC as the MQTT environment.
1. Start the Mosquitto Broker
Open a terminal on your PC and start the broker.
Note: On Windows, you may need to navigate to
C:\Program Files\mosquitto.
# Start broker with verbose logging to see connections
mosquitto -v# Start broker with verbose logging to see connections
mosquitto -v
2. Monitor Incoming Messages (Subscribe)
Open a second terminal. This will show you the messages sent from the Pico.
# Listen for messages from the Pico
mosquitto_sub -h localhost -t w55rp20/pub# Listen for messages from the Pico
mosquitto_sub -h localhost -t w55rp20/pub
3. Send Messages to the Pico (Publish)
Open a third terminal. Use this to send a command to the Pico.
# Send a message to the Pico
mosquitto_pub -h localhost -t w55rp20/sub -m "Hello from PC"# Send a message to the Pico
mosquitto_pub -h localhost -t w55rp20/sub -m "LED_ON"Step 6: Run
Pico (Thonny):
- Open
08_mqtt_client.py. - Run the script (F5).
PC (Verify Publish):
- Check the second terminal on your PC.
- You should see
"Hello from W55RP20-S2E"
Pico (Thonny) : Verify Subscribe
- Use the third terminal command to send a message.
- In the Thonny shell, you should see
[RX] w55rp20/sub :Hello from PC.


