File Exchange

image thumbnail

MQTT in MATLAB

Connect to MQTT brokers and pub/sub to topics on the broker.

37 Downloads

Updated 07 Feb 2018

View License

Connect to MQTT compliant brokers over TCP or websocket protocols. After connection, subscribe to topics or wildcards on the broker. Define custom functions to evaluate on receiving messages on subscribed topics. Publish messages to topics on the broker. All QoS levels supported for both publish and subscribe functions.
You can also establish a secure connection to a broker using TLS v1.2 over both TCP and websocket.

After installation, run the following command to learn more about the functions:

>> help mqtt

Comments and Ratings (20)

Kyle Huggins

I solved the "Undefined variable "com" or class "com.mathworks.mqttclient.client.Client" by adding the following paths to my javaclasspath (I have R2018a and it didn't work):
javaaddpath('<install_path>\mqtt\mqttasync.jar');
javaaddpath('<install_path>\mqtt\jar\org.eclipse.paho.client.mqttv3-1.1.0.jar');
The second one may not be needed.

Steven Yeh

Ismail Faruqi

I installed MATLAB R2018a and still getting this error : Undefined variable "com" or class "com.mathworks.mqttclient.client.Client"

Sven

Sven (view profile)

Just a small example how to use the callback ...

% callback function
function myMQTT_Callback(topic, msg)
fprintf('MQTT callback topic "%s": "%s"\n', topic, msg)
end

% code to connect to broker and register callback function:
myMQTT = mqtt('tcp://broker.hivemq.com');
mySub = subscribe(myMQTT,'myTopic', 'QoS', 2)
% methods(mySub)
% readall(mySub)
mySub.Callback = @myMQTT_Callback;
% send a test message to check callback
publish(myMQTT, 'myTopic', 'testMessage03');

Abdullah Shah

FYI: My issue is solved. I was using MATLAB R207b and I was getting the error shown in my previous comment. I installed MATLAB R2018a and now the tool is working properly.

Abdullah Shah

same problem, I keep getting the following error:
Undefined variable "com" or class "com.mathworks.mqttclient.client.Client".

i investigated several documentations, checked the function's source, 3rd part software documentation, the community topics, forum, others questions. unfortunately, nothing helped with this specific error.

Chiu

Chiu (view profile)

Chiu

Chiu (view profile)

Hi, may I know how do I define the callback function for subscribe? I have tried with function handle and function file but both gives the error 'Invalid function defined with input parameter type 'string'

Hi Adarsh, can we expect support for R2015a ? Since this is a closed-source project, I sadly can't do much more than ask, but maybe you are willing to share thecode as it seems to be just an interface to a java client anyway. Please contact me, as soon as possible.

Note that this functionality is only supported in MATLAB R2017b and later. Thanks!

Hi Tobias, Thanks for the feedback. Regarding instantiating with options inputs, the mqtt function does accept the parameters you mentioned. Refer to the examples in the function help - you can access this by evaluating "help mqtt" in MATLAB or by visiting the 'Functions' tab. Thanks!

Hello eveyone.

Also recenly installed the "MQTT in MATLAB" add-on.
Unfortunately, i keep getting the following error :
>> myMQTT = mqtt('tcp://broker.hivemq.com')
Error: File: Mqtt.p Line: 127 Column: 0
Unbalanced or unexpected parenthesis or bracket.

It seems that the package works for everybody else, so it should be something I am doing wrong.
Could you please Support? i am using Matlab2014b
Thx and have a good weekend

Hi, until now, tho only useful MQTT-Client i could find to use MQTT out of the box. Works fine on R2017b.

Request: It would be nice to in instantiate the class directly without using the function mqtt(). Also it would be helpful to instantiate with separate or optional inputs like protocoll, port, qos, username, pw, client id, etc.

Tim

Tim (view profile)

Hi, i recently installed MQTT in MATLAB but sadly always get this error message:
>> myMQTT = mqtt('tcp://broker.hivemq.com');
Error: File: Mqtt.p Line: 127 Column: 0
Unexpected MATLAB expression.

Can you help me?

Max Person

Hi, how to get subscribe msg in real-time ?

Hello, use the read or readall functions on the subscription to retrieve the messages from the subscription. For eg: mySub = subscribe(myMQTT, 'TestTopic'); read(mySub)

Tommy Langers

Hello. Everything works fine but how can I retrieve the message from a subscription? The counter increases after a message is published, but the message itself I don't find

Lampros

Does this MQTT toolbox support older version of Matlab (e.g R2016a)?

Hans Scharler

Updates

1.3.0.0

Added support for MATLAB Online workflows.

1.2.0.0

Updated image in description.

1.1.0.0

Added documentation.

MATLAB Release Compatibility
Created with R2017b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

MATLAB Online Live Editor Challenge

View the winning live scripts from faculty and students who participated in the recent challenge.

Learn more

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video