To receive data in the MATLAB using MQTT protocol
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Vinayak Gupta
2023-4-3
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MQTT Protocol Communication 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!