Main Content

Get Started with MQTT

This example shows how to establish a secure connection in MATLAB with an MQTT broker and communicate with the MQTT broker.

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.

Set Up the Broker and Get a Root Certificate

To establish a connection with ThingSpeak, see Create a ThingSpeak MQTT Device. After creating the ThingSpeak MQTT device, you can get its 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 to the Downloads folder. Get the path of the downloaded root certificate.

rootCert = "C:\Downloads\thingspeakcert.cer";

Create an MQTT Client and Connect to the Broker with SSL

Prepare the broker address and port number you want to connect. In this case, set up a secure connection to ThingSpeak via SSL with an appropriate port number.

brokerAddress = "ssl://mqtt3.thingspeak.com";
port = 8883;

Create an MQTT client using the mqttclient function.

mqClient = mqttclient(brokerAddress, Port = port, ClientID = clientID,...
           Username = userName, Password = password, CARootCertificate = rootCert);

Note that the Connected property indicates the connection to the broker has been established.

mqClient.Connected
ans = int32
    1

Subscribe to a Topic

With the connected MQTT client, use the subscribe function to subscribe to the topic of interest. The displayed table shows the subscribed topic. For details about the topics to subscribe to in ThingSpeak, see Subscribe to a Channel Field Feed.

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

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

Write to a Topic

To verify that the subscription is successful, make sure a message written to the subscribed topic is received by the MQTT client.

Use the write function to write messages to the topic of interest. For details about the topics to write to in ThingSpeak, see Publish to a Channel Field Feed.

topicToWrite = "channels/1393455/publish/fields/field2";
msg = "70";
write(mqClient, topicToWrite, msg)

Peek the MQTT Client

Use the peek function to peek the most recently received message for all subscribed topics in the MQTT client. The displayed timetable indicates the MQTT client has successfully received the message from the broker.

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

    06-Jan-2022 10:42:29    "channels/1393455/subscribe/fields/field2"    "70"

Close the MQTT Client

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

clear mqClient