To receive data in the MATLAB using MQTT protocol

13 views (last 30 days)
Hi, I want to utilize MQTT communication in the cloud as MATLAB input data.
The MATLAB version I am currently using is R2022b version, and I wrote the code as below.
However, 'sub_obj' is not accumulating proper data (e.g., data to date, data that is updated in real time).
Real data is updated every 3 seconds.
Is there a code line that I forgot to get real-time data?
If there is, please give me feedback.
Additionally, should the data I receive as a subscription be in the form of column 2?

Answers (1)

Vinayak Gupta
Vinayak Gupta on 3 Apr 2023
Hi
Your code seems to be accurate until the while loop starts. I am not sure if read function works in the way you used it.
In the documentation(Read available messages from MQTT topic), we can see there are two ways to use read, one is reading all available messages on the broker, while the other adds a filter by using a named argument for topic.
msg = read(mqttVlient,Topic=topic1);
The returned object is a table with Time, Topic and Data as the available columns.
The Payload object was available in the older mqtt client, the newer way (post R2022a) does not follow that approach.
Also, you will need to have ‘Industrial communication Toolbox’ installed for the code to work.
To read the data every 3 seconds, you can add a pause within the while loop.
The subscriptions table within the client object does not store the messages.
You can read more about MQTT protocol at MQTT Protocol Communication.

Categories

Find more on Read Data from Channel in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!