main content

get started with mqtt -凯发k8网页登录

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.

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.

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 (thingspeak). 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 as described in . get the path of the downloaded root certificate. the location and file name extension depends 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.

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 (thingspeak).

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 (thingspeak).

topictowrite = "channels/1393455/publish/fields/field2";
msg = "70";
write(mqclient, topictowrite, msg)

peek at the mqtt client

use the peek function to view 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
网站地图