Main Content

Get Data from Subscribed Topics in an MQTT Client

This example shows how to get data from subscribed topics in an MQTT client.

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. Download the root certificate from ThingSpeak and use its local path. Assign those values in MATLAB®.

clientID = "Your Client ID";
userName = "Your Username";
password = "Your Password";
rootCert = "C:\Downloads\thingspeakcert.cer";

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 Topic

Use the subscribe function to subscribe to the topic of interest. After subscription, the MQTT client in MATLAB receives and stores all the data written to the topic of interest.

topicToSub = "channels/1393455/subscribe/fields/field2";
subscribe(mqClient, topicToSub)
ans=1×3 table
                      Topic                       QualityOfService    Callback
    __________________________________________    ________________    ________

    "channels/1393455/subscribe/fields/field2"           0               ""   

Write to the Subscribed Topic

Use the write function to write messages to the topic of interest. In this case, 3 messages are written to the subscribed topic. Pause for a few seconds after each write to avoid violating the rate limits in ThingSpeak.

topicToWrite = "channels/1393455/publish/fields/field2";
msg1 = "70";
msg2 = "73";
msg3 = "69";
write(mqClient, topicToWrite, msg1)
pause(2)
write(mqClient, topicToWrite, msg2)
pause(2)
write(mqClient, topicToWrite, msg3)
pause(2)

Read Received Data from the Subscribed Topic

Use the read function to read all data received from the subscribed topic into a timetable. Note that read removes all the data stored in the subscribed topic you just read from.

dataTT = read(mqClient)
dataTT=3×2 timetable
            Time                              Topic                       Data
    ____________________    __________________________________________    ____

    06-Jan-2022 14:21:50    "channels/1393455/subscribe/fields/field2"    "70"
    06-Jan-2022 14:21:52    "channels/1393455/subscribe/fields/field2"    "73"
    06-Jan-2022 14:21:54    "channels/1393455/subscribe/fields/field2"    "69"

Visualize the Received Data

To visualize the information, plot the received data from the subscribed topic.

t = dataTT.Time;
data = str2double(dataTT.Data);
plot(t, data, 'o-')
title(topicToSub)
xlabel("Time")
ylabel("Data Value")

Close the MQTT Client

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

clear mqClient