Publish and Subscribe to Messages on ThingSpeak Using MQTT Blocks on Arduino
This example shows how to use the WiFi MQTT Publish and WiFi MQTT Subscribe blocks from the Simulink® Support Package for Arduino® Hardware to publish to and receive messages from MQTT broker to Arduino board. In this example, ThingSpeak™ is the MQTT broker and Arduino board is the MQTT client.
This example uses two Arduino Simulink models, namely Arduino MQTT Publish and Arduino MQTT Subscribe. The Arduino MQTT Publish model is deployed on Arduino Nano 33 IoT board, while the Arduino MQTT Subscribe model is deployed on another Arduino Nano 33 IoT board. You can use any of the supported Arduino boards to deploy these Simulink models.
Supported Arduino Boards
Arduino MKR WiFi 1010
Arduino MKR 1000
Arduino Nano 33 IoT
ESP32-WROOM (Arduino Compatible)
ESP32-WROVER (Arduino Compatible)
Prerequisites
For more information on how to use the Simulink Support Package for Arduino Hardware to run a Simulink model on the Arduino board, see Get Started with Arduino Hardware.
For more information on MQTT protocol, see MQTT Basics (ThingSpeak).
Required Hardware
Any two of the supported Arduino boards
Two USB cables
Hardware Setup
Connect the Arduino boards to your host computer using the USB cable.
Ensure that your Arduino boards are connected to Wi-Fi®. For more information, see WiFi properties.
Create ThingSpeak Device
MQTT access to your channel, including credentials, is handled by a ThingSpeak MQTT broker. Your Arduino board is configured with the credentials necessary for your MQTT client to communicate with ThingSpeak, and for authorizing specific channels. For more information on how to create an MQTT device, see Create a ThingSpeak MQTT Device (ThingSpeak). Make a note of the list of credentials that ThingSpeak generates.
For more information on how to create multiple fields on a channel, see Collect Data in a New Channel (ThingSpeak).
Configure MQTT Properties for Simulink Models
In the Arduino MQTT Publish and Arduino MQTT Subscribe Simulink models, go to the Configuration Parameters dialog box > Target hardware resources > MQTT properties and configure these properties.
Set Broker service to
ThingSpeak
.After the ThingSpeak generates a list of credentials (username, client ID, and password) for the Arduino board, populate these credentials in the Username, Password, and Client ID parameters of the MQTT properties.
For more information on MQTT properties, see MQTT properties.
Configure Arduino MQTT Publish Simulink Model and Calibrate Parameters
This example uses a preconfigured Simulink model that uses two WiFi MQTT Publish blocks, one to publish a message to a single field of the topic, while the other to publish a message to multiple fields of the topic.
Open the arduino_MQTT_publish
Simulink model.
In the Block Parameter dialog box of both the WiFi MQTT Publish blocks in the model, set the topic of the message to which the Arduino board publishes the message. The topic must use the format: channels/<channelID>/publish
. You can obtain the channel ID from the ThingSpeak channel.
Configure Arduino MQTT Subscribe Simulink Model and Calibrate Parameters
This example uses a preconfigured Simulink model that uses two WiFi MQTT Subscribe blocks, one to subscribe to single topic from ThingSpeak, while the other is used to subscribe to multiple topics from ThingSpeak.
Open the arduino_MQTT_subscribe
Simulink model.
In the Subscribe to individual field area of the model, configure this parameter in the Blocks Parameters dialog box of the WiFi MQTT Subscribe block.
Set the topic of the message to which the Arduino board subscribes. The topic must use the format:
channels/<channelID>/subscribe/fields/<field_number>
.
In the Subscribe to all fields using "+" wildcard area of the model, configure this parameter in the Blocks Parameters dialog box of the WiFi MQTT Subscribe block.
Set the topic parameter to the format: channels/<channelID1>/subscribe/fields/+
.
Deploy Arduino MQTT Publish Simulink Model on Arduino Board
Publish Data to Single Field on Specified Topic
1. To publish message from the MQTT broker to a single field on the specified topic, in the Arduino MQTT Publish model, comment out the Publish data to multiple fields area. Select the area, right-click, and select Comment Out.
2. On the Hardware tab of the arduino_mqtt_publish
model, select Run on board
. In the Deploy section, click Build, Deploy & Start. The Simulink model is deployed as a standalone application on your Arduino board. After the model is successfully deployed, the Arduino board published the message 96
to field 1 of the ThingSpeak MQTT broker.
3. To view the published message on ThingSpeak, on ThingSpeak, select Channels > My Channels, and then click the channel on which the message is published. The message is published to ThingSpeak after every update interval that you have specified in the Block Parameter dialog box of the WiFi MQTT Publish block.
4. Every time ThingSpeak MQTT broker accepts the message for publishing the message on the channel, the output port of the block displays 1, indicating that ThingSpeak has accepted the message from the Arduino board.
5. To publish a new message to the ThingSpeak channel, change the value in the String Constant block and deploy the Simulink model on your Arduino board once again. Observe the output on the ThingSpeak channel.
Publish Data to Multiple Fields on Specified Topic
1. To publish message to multiple fields on the specified topic, in the Arduino MQTT Publish model, uncomment the Publish data to multiple fields area and comment out the Publish data to a single field area.
2. On the Hardware tab of the arduino_mqtt_publish
model, select Run on board
. In the Deploy section, click Build, Deploy & Start. The Simulink model is deployed as a standalone application on your Arduino board. After the model is successfully deployed, the Arduino board published the messages 100
, 1
, 2
to fields 1, 2, and 3 respectively of the ThingSpeak MQTT broker.
Deploy Arduino MQTT Subscribe Simulink Model on Arduino Board
Subscribe Data to Single Field on Specified Topic
1. To receive a message from ThingSpeak for the specified topic, in the Arduino MQTT Subscribe model, comment out the Subscribe to all fields using "+" wildcard area.
2. On the Hardware tab of the arduino_mqtt_subscribe
model, select Run on board
. In the Deploy section, click Build, Deploy & Start. The Simulink model is deployed as a standalone application on your Arduino board. After the model is successfully deployed, Arduino board subscribes to the messages from field 1 of the ThingSpeak channel and receives the message.
3. Every time the Arduino board receives a new message, the output port of the block displays 1. The Data output port of the block, displays the message received on the Arduino board.
Subscribe Data to Multiple Topics
1. To receive a message from multiple topics on ThingSpeak, in the Arduino 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 of the arduino_mqtt_subscribe
model, select Run on board
. In the Deploy section, click Build, Deploy & Start. The Simulink model is deployed as a standalone application on your Arduino board. After the model is successfully deployed, Arduino board subscribes to the multiple fields of the ThingSpeak channel.
3. Every time the Arduino board receives a new message from the topic, the block outputs the ASCII value that corresponds to the topic name of the received message. The block also outputs the data received by the Arduino board.
Other Things to Try
Try running the
arduino_mqtt_publish
andarduino_mqtt_subscribe
Simulink models in external model (Monitor & Tune). For more information, see Signal Monitoring and Parameter Tuning.Use a different MQTT broker such as HiveMQ® or Mosquitto® and deploy the Simulink models on your Arduino boards.