Main Content

Publish and Subscribe to Messages on ThingSpeak Using MQTT Blocks

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 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

Required Hardware

  • Raspberry Pi hardware

  • Micro USB cable

Hardware Setup

  1. Connect the micro USB cable into the micro USB port of the Raspberry Pi hardware.

  2. Connect the other end of the USB cable to your host computer.

  3. 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_system('raspberrypi_MQTT_publish')

Configure these parameters in the Block Parameters dialog box of the MQTT Publish block.

  1. Set topic of the message to which Raspberry Pi publishes the message. The topic must use this format: channels/<channelID>/publish/fields/field<fieldNumber>.

  2. 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 a Signal 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_system('raspberrypi_MQTT_subscribe')

Configure these parameters in the Block Parameters dialog box of the MQTT Subscribe block.

  1. Set topic of the message to which Raspberry Pi subscribes. The topic must use this format: channels/<channelID>/subscribe/fields/field<fieldNumber>.

  2. Set QoS to 0. The ThingSpeak MQTT broker supports only QoS 0.

  3. 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 a Signal 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.

Configure MQTT Parameters for Simulink Models

Configure these parameters in both the Subscribe MQTT Message and Publish MQTT Message Simulink models.

  1. In the Simulink model, on the Hardware tab, click Model Settings to open the Configuration Parameters dialog box.

  2. Select Hardware Implementation and set the Hardware board parameter to Raspberry Pi.

  3. 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.

  4. 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

  1. 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.

  2. 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)

  1. In the Publish MQTT Message Simulink model, set the simulation stop time as inf.

  2. 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 more information, see Mark a Signal for Logging.

  3. 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.

  4. During simulation, change the value to be published and observe the new value displayed in the Display_Message block.

  5. 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.

  6. After you are satisfied with the results, disable External mode, and save the model.

See Also