To receive data in the MATLAB using MQTT protocol
5 views (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1325835/image.png)
However, 'sub_obj' is not accumulating proper data (e.g., data to date, data that is updated in real time).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1325840/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1325845/image.png)
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?
0 Comments
Answers (1)
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.
0 Comments
See Also
Categories
Find more on MQTT Protocol Communication in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!