Main Content

Subscribe to an MQTT Wildcard Topic

This example shows how to use an MQTT client to subscribe to a wildcard topic.

ThingSpeak™ is used as the broker in this example.

Message Queuing Telemetry Transport (MQTT) is an OASIS standard messaging protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish/subscribe messaging transport that is ideal for connecting remote devices with a small code footprint and minimal network bandwidth.

ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud. You can send data to ThingSpeak from your devices, create instant visualization of live data, and send alerts.

Create an MQTT Client and Connect to the Broker

Set up a ThingSpeak broker and get Client ID, Username, and Password from it. Assign those values in MATLAB®.

clientID = "Your Client ID";
userName = "Your Username";
password = "Your Password";

Download the root certificate from thingspeak.com as described in How to Download Root Certificate for Use With Industrial Communication Toolbox MQTT Functions. Get the path of the downloaded root certificate. The location and file name extension depend on the browser you use. For example, using Edge you might set rootCert like this:

rootCert = "C:\Downloads\DigiCert Global Root CA.crt";

The certificate saved from Firefox® might have the file extension .pem.

Establish a secure connection to ThingSpeak with an appropriate port number using the mqttclient function.

brokerAddress = "ssl://mqtt3.thingspeak.com";
port = 8883;
mqClient = mqttclient(brokerAddress, Port = port, ClientID = clientID,...
           Username = userName, Password = password, CARootCertificate = rootCert);

Subscribe to a Wildcard Topic

To subscribe to all the topics under a certain hierarchy, use the subscribe function with a wildcard to make the subscription easier. The displayed table shows the wildcard topic has been subscribed successfully.

topicWildcard = "channels/1393455/subscribe/fields/+";
subscribe(mqClient, topicWildcard)
ans=1×3 table
                    Topic                    QualityOfService    Callback
    _____________________________________    ________________    ________

    "channels/1393455/subscribe/fields/+"           0               ""   

Write to Different Topics Under the Wildcard

To verify that the wildcard subscription is successful, make sure the messages written to different topics under the wildcard subscription are received by the MQTT client.

Use the write function to write messages to different topics under the wildcard. Pause for a few seconds after each write to avoid violating the rate limits in ThingSpeak.

topicToWrite1 = "channels/1393455/publish/fields/field1";
topicToWrite2 = "channels/1393455/publish/fields/field2";
msg1 = "60";
msg2 = "30";
write(mqClient, topicToWrite1, msg1)
pause(2)
write(mqClient, topicToWrite2, msg2)
pause(2)

Peek at the MQTT Client

Use the peek function to view the most recently received data for all subscribed topics. The displayed timetable indicates that the messages under the wildcard have been received successfully.

peek(mqClient)
ans=2×2 timetable
            Time                              Topic                       Data
    ____________________    __________________________________________    ____

    06-Jan-2022 13:36:40    "channels/1393455/subscribe/fields/field1"    "60"
    06-Jan-2022 13:36:43    "channels/1393455/subscribe/fields/field2"    "30"

Close the MQTT Client

Close access to ThingSpeak by clearing the MQTT client variable from the workspace.

clear mqClient