Publish and Subscribe to Messages on ThingSpeak Using MQTT Blocks
This example shows how to communicate using the Message Queuing Telemetry Transport (MQTT) on NVIDIA® Jetson® and DRIVE® platforms.
MQTT is a wireless publish/subscribe architecture that enables messages to be pushed to the client devices without the need to continuously poll the broker. In this example, ThingSpeak™ acts as the broker; a central point of communication, in charge of dispatching messages to and from clients and an NVIDIA Jetson board acts as the MQTT client (publisher and subscriber). For more information on MQTT protocol, see MQTT Basics (ThingSpeak) and Introduction to MQTT.
The Simulink® model in this example uses the MQTT Publish
and MQTT Subscribe
blocks from MATLAB® Coder™ Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms to send and receive MQTT messages to a ThingSpeak MQTT broker.
Prerequisites
Target Board Requirements
NVIDIA Jetson embedded platform.
Ethernet crossover cable to connect the target board and host PC (if you cannot connect the target board to a local network).
Tools, libraries, and environment variables on the target board for the compilers and libraries. For more information, see Install and Setup Prerequisites for NVIDIA Boards.
ThingSpeak Broker Requirements
An MQTT ThingSpeak device that is configured for the NVIDIA Jetson board to communicate with ThingSpeak. MQTT access to user-created channels, including credentials, is handled by a ThingSpeak MQTT device. This device must be configured with the credentials necessary for the NVIDIA Jetson 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 the Simulink Model
The Simulink model in this example uses the output of a Counter Limited
block as the MQTT message. MQTT Publish
block is used to publish this message on a publish topic and the MQTT Subscribe
block is used to subscribe to corresponding subscribe topic to receive the above published message.
open_system('nvidia_MQTT_publish_subscribe')
The MQTT Publish
block is preconfigured with the topic of this format:
channels/<channelID>/publish/fields/<fieldNumber>
Set the channelID
and fieldNumber
corresponding to the channel-ID and field-number in the ThingSpeak channel.
The MQTT Subscribe
block is preconfigured with the topic of this format:
channels/<channelID>/subscribe/<fieldNumber>.
Set the channelID
and fieldNumber
corresponding to the channel-ID and field-number in the ThingSpeak channel.
Prepare the Simulink Model for External Mode
Configure these parameters in the model.
On the
Hardware
tab, clickModel Settings
to open the Configuration Parameters dialog box.Select
Hardware Implementation
and set theHardware board
parameter toNVIDIA Jetson
.Under
Target hardware resources
, in theGroup
section, selectMQTT
. For more information on how to configure the MQTT properties, see Model Configuration Parameters for NVIDIA Hardware Board. Use the same parameters as set while you create a ThingSpeak MQTT device.In the
External mode
section, ensure that theCommunication interface
is set toXCP on TCP/IP
.
Run External Mode
On the Hardware
tab, click Monitor & Tune
. This deploys the model on the target hardware and runs the model in external mode which communicates the following outputs to the Simulink Model.
MQTT Publish
block -Status
output.MQTT Subscribe
block -IsNew
andMessage
outputs.
View the Results on Simulink Data Inspector
Once the model stops, open the Simulink Data Inspector to view the Publish message and the Subscribe message at each Sample-time.
View the Published Data on the ThingSpeak 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.