subscribe to an mqtt topic with a callback function -凯发k8网页登录
this example shows how to use an mqtt client to subscribe to a topic with a callback function.
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.
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 . 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
.
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 with a callback function
to subscribe with a callback function, create a callback function named showmessage
. the showmessage
function prints the received data and corresponding topic when triggered.
use the subscribe
function to subscribe to the topic of interest. use a name-value pair argument to assign the callback function at the same time. the displayed table shows the subscribed topic and the corresponding callback function.
topictosub = "channels/1393455/subscribe/fields/field2"; subscribe(mqclient, topictosub, callback = "showmessage")
ans=1×3 table
topic qualityofservice callback
__________________________________________ ________________ _____________
"channels/1393455/subscribe/fields/field2" 0 "showmessage"
write to the subscribed topic
to trigger the callback function, the mqtt client needs to receive messages for the subscribed topic. use the write
function to write messages to the subscribed topic.
topictowrite = "channels/1393455/publish/fields/field2"; msg = "70"; write(mqclient, topictowrite, msg)
trigger callback function
pause to allow the message to transfer from the mqtt client, to the mqtt broker, and back to the client.
pause(2)
when the mqtt client receives the message from the subscribed topic, the callback function showmessage
is automatically triggered. the following context is printed in the matlab command window.
topic: channels/1393455/subscribe/fields/field2, message: 70
close the mqtt client
close access to thingspeak by clearing the mqtt client variable from the workspace.
clear mqclient