Publish and Subscribe to Messages on ThingSpeak Using MQTT Blocks on Raspberry Pi
This example shows how to use the Simulink® Support Package for Raspberry Pi® Hardware to publish a message to a topic from Raspberry Pi in the ThingSpeak™ MQTT broker. This example also shows how to subscribe to a topic and receive a message from the ThingSpeak MQTT broker to Raspberry Pi. For more information on MQTT protocol, see MQTT Basics (ThingSpeak) and Publish MQTT Messages and Subscribe to Message Topics. In this example, ThingSpeak is the MQTT broker and Raspberry Pi board is the MQTT client (publisher and subscriber).
Prerequisites
For more information on how to use Simulink Support Package for Raspberry Pi Hardware to run a Simulink model on Raspberry Pi, see Get Started with Simulink Support Package for Raspberry Pi Hardware.
For more information on MQTT protocol, see Publish MQTT Messages and Subscribe to Message Topics.
Required Hardware
Raspberry Pi hardware
Micro USB cable
Hardware Setup
Connect the micro USB cable into the micro USB port of the Raspberry Pi hardware.
Connect the other end of the USB cable to your host computer.
Connect the Ethernet port of Raspberry Pi to LAN.
Configure Publish MQTT Message Simulink Model
This support package provides a preconfigured Simulink model that uses a MQTT Publish block. This block accepts a message of data type uint8 that is to be published to the broker.
Open the raspberrypi_MQTT_publish
Simulink model.
Configure these parameters in the Block Parameters dialog box of the MQTT Publish block.
Set topic of the message to which Raspberry Pi publishes the message. The topic must use this format:
channels/_channelID_/publish/fields/field_fieldNumber_
.Set QoS to
0
. The ThingSpeak MQTT broker supports only QoS 0.
In the model, the output of the MQTT Publish block is marked for logging. For information on logging signals, see Mark Signals for Logging.
Configure Subscribe MQTT Message Simulink Model
This support package provides a preconfigured Simulink model that uses a MQTT Subscribe block. This block receives messages from the MQTT broker for the topic.
Open the raspberrypi_MQTT_subscribe
Simulink model.
Configure these parameters in the Block Parameters dialog box of the MQTT Subscribe block.
Set the topic of the message to which Raspberry Pi subscribes. The topic must use this format:
channels/_channelID_/subscribe/fields/field_fieldNumber_
.Set QoS to
0
. The ThingSpeak MQTT broker supports only QoS 0.Set Message length (N) to
1
.
In the model, the output of the MQTT Subscribe block is marked for logging. For information on logging signals, see Mark Signals for Logging.
Create ThingSpeak Device
MQTT access to your channels, including credentials, is handled by a ThingSpeak MQTT device. Your device 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 a MQTT device, see Create a ThingSpeak MQTT Device (ThingSpeak).
Configure MQTT Parameters for Simulink Models
Configure these parameters in both the Subscribe MQTT Message and Publish MQTT Message Simulink models.
In the Simulink model, on the Hardware tab, click Model Settings to open the Configuration Parameters dialog box.
Select Hardware Implementation and set the Hardware board parameter to
Raspberry Pi
.Under Target hardware resources, in the Group section, select MQTT. For more information on how to configure the MQTT properties, see MQTT. Use the same parameters as set while you create a ThingSpeak MQTT device.
In the External mode section, ensure that the Communication interface is set to
XCP on TCP/IP
.
Deploy Publish MQTT Message Simulink Model on Raspberry Pi
On the Hardware tab of the Publish MQTT Message Simulink Model, in the Mode section, select Run on board, and then click Build, Deploy & Start. The lower left corner of the model window displays the status while Simulink prepares, downloads, and runs the model on Raspberry Pi. The model is deployed as a standalone application on Raspberry Pi. After the model is successfully deployed, Raspberry Pi publishes the message to the Field 1 of the ThingSpeak MQTT broker.
To view the published message on the broker, select to Channels > My Channels, and then click the channel on which the message is published.
Run Subscribe MQTT Message Simulink Model in External Mode (Monitor and Tune)
In the Publish MQTT Message Simulink model, set the simulation stop time as
inf
.To monitor the effects of parameter tuning during simulation, mark the output of the
MQTT Publish
block for logging. Simulink displays a logged signal indicator for the logged signal. For information on logging signals, see Mark Signals for Logging.On the Hardware tab of the Simulink model, in the Mode section, select Run on board, and then click Monitor & Tune. The lower left corner of the model window displays status while Simulink prepares, downloads, and runs the model on Raspberry Pi. After the model successfully starts running, Raspberry Pi subscribes to the messages in the Field 1 of the ThingSpeak MQTT broker and receives the message. The
Display_Message
block displays the received message. When new simulation data becomes available in Simulation Data Inspector(SDI), the Simulation Data Inspector button appears highlighted.During simulation, change the value to be published and observe the new value displayed in the
Display_Message
block.Click the Simulation Data Inspector button to inspect and compare data from multiple simulations for validating model designs. For more information on SDI, see Analyze Simulation Results.
After you are satisfied with the results, disable External mode, and save the model.