Main Content

Get Started with MQTT Protocol on Android Devices

This example shows how to use the Simulink® Support Package for Android™ Devices to subscribe and publish data to topics of ThingSpeak™ and Mosquitto™ brokers via MQTT protocol.

This example shows the capabilities of different broker types for publishing and subscribing data either from the channel feed or from the field of the channel using an Android device. It also shows how to publish and subscribe data using different data types and in the JSON format. This example uses three Simulink models for demonstrating these capabilities.

  • androidMQTTThingSpeak1 — This Simulink model shows how to use ThingSpeak as a MQTT broker to publish and subscribe to a field of a channel. The accelerometer sensor data X-, Y-, and Z- axes values are published to three fields of a ThingSpeak channel and also subscribed to a ThingSpeak field.

  • androidMQTTThingSpeak2 — This Simulink model uses ThingSpeak as the broker to publish and subscribe accelerometer data in the JSON format from the channel field.

  • androidMQTTTMosquitto — This Simulink model shows how to use a third-party MQTT broker such as Mosquitto with or without SSL.

Prerequisites

Required Hardware

Android device, such as a phone or tablet

Create ThingSpeak Channel

For more information on how to create a new ThingSpeak channel to collect data, see Collect Data in a New Channel.

Configure androidMQTTThingSpeak1 Simulink Model and Calibrate Parameters

Open the androidMQTTThingSpeak1 Simulink model.

In this Simulink model, the MQTT Publish block in the ThingSpeak publish to a channel feed area, publishes the X-, Y-, and Z-axes accelerometer data on fields 1, 2, and 3 of the ThingSpeak channel, respectively. The block utilizes the topic channels/channelID/publish that updates the channel feed by publishing the accelerometer data to this topic. Replace channel_ID with your ThingSpeak channel ID. Ensure to set the broker service to ThingSpeak.

You can subscribe to only one field data or the entire channel data. The MQTT Subscribe block in the ThingSpeak Subscribe to a field feed area subscribes to only the Z-axis data of the accelerometer. The MQTT Subscribe block in the ThingSpeak Subscribe to a channel feed area subscribes all the three fields of the ThingSpeak channel.

In this model, the MQTT Publish block publishes floating point data type. Instead of floating point data type, you can also publish heterogenous data types on the ThingSpeak channel such as integer, character, and so on.

In the Configuration Parameters dialog box, go to Hardware board settings > Target hardware resources > MQTT properties. Set Broker service to ThingSpeak. Configure the Username, Password, and Client ID parameters. For more information, see MQTT properties.

Deploy androidMQTTThingSpeak1 Simulink Model on Android Device

1. Open the androidMQTTThingSpeak1 Simulink model.

2. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start.

3. Login to your ThingSpeak account and observe the channel getting updated with the X-, Y-, and Z- axes data of the accelerometer.

On your Android device, the androidMQTTThingSpeak1 application launches automatically. Observe the Z-axis accelerometer data on field3 of the application.

Configure androidMQTTThingSpeak2 Simulink Model and Calibrate Parameters

Open the androidMQTTThingSpeak2 Simulink model.

In this Simulink model, the MQTT Publish block in the ThingSpeak publish to a channel feed area, publishes the accelerometer data on field 8 of the ThingSpeak channel, in the JSON format. The block utilizes the topic channels/channel_ID/publish/fields/field8 that updates the channel feed by publishing the accelerometer data to this topic. Replace channel_ID with your ThingSpeak channel ID. Ensure to set the broker service to ThingSpeak. The MQTT Subscribe block subscribes to all the axes data of the accelerometer. The input data to the block is in the JSON format.

In the Configuration Parameters dialog box, go to Hardware board settings > Target hardware resources > MQTT properties. Set Broker service to ThingSpeak. Configure the Username and Password, and Client ID parameters. For more information, see MQTT properties.

Deploy androidMQTTThingSpeak2 Simulink Model on Android Device

1. Open the androidMQTTThingSpeak2 Simulink model.

2. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start.

On your Android device, the androidMQTTThingSpeak2 application launches automatically. Observe the X-, Y-, and Z-axes accelerometer data on the application.

Configure androidMQTTMosquitto Simulink Model and Calibrate Parameters

Open the androidMQTTMosquitto Simulink model.

You can use any third-party MQTT broker to publish and subscribe data from a Simulink model for Android devices. This example uses Mosquitto as the third-party MQTT broker.

In the Configuration Parameters dialog box, go to Hardware board settings > Target hardware resources > MQTT properties. Set Broker service to Other. Configure the Broker address, Port, Username, Password, and Client ID parameters. For more information, see MQTT properties.

Deploy androidMQTTMosquitto Simulink Model on Android Device

1. Open the androidMQTTMosquitto Simulink model.

2. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start.

On your Android device, the androidMQTTMosquitto application launches automatically. Observe the X-, Y-, and Z-axes accelerometer data on the application.