Securely Publish and Subscribe to Messages on Mosquitto Broker Using Arduino Hardware
This example shows how to establish secure MQTT communication between two Arduino-compatible boards using the Mosquitto™ MQTT broker. The Simulink® model in this example shows how one board can publish messages securely over SSL/TLS to multiple MQTT topics while the other board subscribes to those topics.
In this example, you configure the model to use TLS encryption via port 8884 for data confidentiality and integrity during communication. Both hardware boards leverage SSL certificates to connect securely to the public broker, Mosquitto.
The publisher and subscriber boards communicate through the Mosquitto MQTT broker over the secure SSL/TLS connection. When the Mosquitto MQTT broker publishes messages to the topics (either specific or a wildcard subscription), the subscriber board receives the incoming payloads through its MQTT Subscriber blocks.
Supported Arduino-Compatible Boards
ESP32-WROOM and WROVER
Raspberry Pi Pico W
Hardware Requirements
Any two of the supported boards. In this example, an ESP32-WROOM board acts as a publisher and a Raspberry Pi Pico W acts as a subscriber.
Two USB cables
Hardware Setup
1. Connect the boards to your host computer using USB cables.
2. Make sure that your boards are connected to a Wi-Fi® network. For more information, see WiFi properties.
Configure Arduino MQTT Publish Model and Calibrate Parameters
Open the arduinoSecureMQTTPublish model. This model is pre-configured to publish single and multiple topics securely using SSL/TLS encryption.

Publish Messages from ESP32-WROOM
The arduinoSecureMQTTPublish model periodically generates and publishes messages to the broker:
Publish to single topic — The string to ASCII conversion block converts the string constant
Hello!to ASCII format and sends the string to the MQTT Publish block for the topic.Publish to multiple topics — Use a separate MQTT Publish block for each topic.
The WiFi MQTT Publish block publishes these messages securely with an update interval of 3 or 5 seconds.
Each publish block outputs a status signal indicating success or specific error codes. Use these codes for debugging and reliability checks.
Subscribe to Messages on Raspberry Pi Pico W
On the receiving end, the Raspberry Pi Pico W board subscribes to topics using two different MQTT configurations:
Direct topic subscription — Hardware board listens to the topic
topicand is expecting the messageHello!.Wildcard subscription — Uses the topic filter
testTopic/#to receive any message published under thetestTopic/namespace, including subtopics like/topicAand/topicB.
When the broker sends messages:
The WiFi MQTT Subscribe blocks detects new payloads and outputs 1 at the
IsNewport.The client receives payload data as an ASCII array. For wildcard subscription, the block outputs the originating subtopic name at the
Topicport.
Data Processing
The data received on the client board passes through the ASCII to String Converter block that converts the raw values into readable messages and use the Display block to display these messages.
Set Model for Publishing
In the Configuration Parameters dialog box of the arduinoSecureMQTTPublish model, set Hardware board to ESP32-WROOM (Arduino Compatible). Then, in Target hardware resources > MQTT properties, set the following parameter values.
Encryption type —
SSL/TLSBroker service —
OtherBroker address —
test.mosquitto.orgPort —
8884Username, password, and client ID, browse certificate paths for server, client, and client key files
For more information, see MQTT properties.
In the WiFi MQTT Publish blocks, set the following parameter values.
Broker service to
OtherTopic to the topic that the publisher uses to filter messages
Update interval to
3
Configure Arduino MQTT Subscribe Model and Calibrate Parameters
Open the arduinoSecureMQTTSubscribe model. This example uses a pre-configured model to subscribe to a single or multiple topics securely using the SSL/TLS encryption.

Set Model for Subscribing
In the Configuration Parameters dialog box of the arduinoSecureMQTTSubscribe model, set the Hardware board to Raspberry Pi Pico W (Arduino Compatible). Then, in Target hardware resources > MQTT properties, set the following parameter values.
Encryption type —
SSL/TLSBroker service —
OtherBroker address —
test.mosquitto.orgPort —
8884Username, password, and client ID, browse certificate paths for server, client, and client key files
In the WiFi MQTT Subscribe blocks, set the following parameter values.
Topic to
testTopic/#to subscribe for wildcard subscriptions andtopicsfor single-mode subscription
Data length to
10for wildcard subscriptions and5for single-mode subscription
Deploy Publisher Model to Hardware
Publish to Single Topic
1. To publish message from the MQTT broker to a single field on the specified topic, in the arduinoSecureMQTTPublish model, comment out the Publish data to three different topics area.
2. On the Hardware tab, select Run on board. In the Deploy section, click Build, Deploy & Start. This action deploys the model as a standalone application on your board. To view the message that the board publishes onto the Mosquitto MQTT broker, follow the steps in the View Diagnostics Logs for MQTT Communication section. In this case, the board publishes the message Hello! to the broker.
Publish to Multiple Topics
1. To publish a message to multiple fields on the specified topic, in the Arduino Secure MQTT Publish model, uncomment the Publish data to three different topics area and comment out the Publish data to single topic area.
2. On the Hardware tab, select Run on board. In the Deploy section, click Build, Deploy & Start. This action deploys the model as a standalone application on your board. To view the message that the board publishes onto the Mosquitto MQTT broker, follow the steps in the View Diagnostics Logs for MQTT Communication section. In this case, the board publishes the characters A, B, and C to the broker.
Deploy Subscriber Model to Hardware
Subscribe to Single Topic
1. To receive a message from the Mosquitto MQTT broker for the specified topic, in the Arduino Secure MQTT Subscribe model, comment out the Subscribe to all fields using "+" wildcard area.
2. On the Hardware tab, select Run on board. In the Deploy section, click Build, Deploy & Start. This action deploys the model as a standalone application on your board. The board now subscribes to the messages from the Mosquitto MQTT broker and receives the message.
Subscribe to Multiple Topics
1. To receive a message from multiple topics on Mosquitto, in the Arduino Secure MQTT Subscribe model, uncomment the Subscribe to all fields using "+" wildcard area and comment out the Subscribe to individual field area.
2. On the Hardware tab, select Run on board. In the Deploy section, click Build, Deploy & Start. This action deploys the model as a standalone application on your board. The board now subscribes to all the sub-topics. Every time the board receives a new message from the topic, the block outputs the ASCII value that corresponds to the topic name of the received message and the actual data.
View Diagnostics Logs for MQTT Communication
To view diagnostic logs that reflects the status of WiFi MQTT Publish and WiFi MQTT Subscribe blocks:
1. Install and open Arduino IDE.
2. Select your board and select the port for communication.
3. Click Serial Monitor to open a terminal that displays the real-time data the board publishes and subscribes to. The terminal also helps to track the connection status between the boards to the Mosquitto broker to debug communication issues.