From Simulink to Grafana using MQTT Protocol
8 views (last 30 days)
Show older comments
Hi,
I’m currently using Grafana to visualize data from my Simulink model in MATLAB 2021b. So far, I’ve been able to collect the data at the end of the simulation by saving it as a .mat file with the ToFile block in Simulink. Then, I use a script to convert the data into a .csv file for Grafana to read.
However, I want to transition to a real-time model where I can send the data (possibly using the MQTT protocol) while the simulation is running, and visualize it live in Grafana. Ideally, I would like to stay with MATLAB 2021b for this task.
How can I achieve this?
0 Comments
Answers (1)
Aravind
on 5 Mar 2025
In MATLAB R2021b, there is no built-in support for creating an MQTT client to facilitate data communication from Simulink to Grafana. However, you can utilize REST APIs to transfer data from MATLAB to Grafana. You can achieve this by using MATLAB's "webwrite" function to send HTTP POST requests to Grafana's REST API, which allows you to dynamically publish data points as your Simulink simulation runs. For more details on the "webwrite" function, visit: https://www.mathworks.com/help/releases/R2021b/matlab/ref/webwrite.html. To incorporate the MATLAB function into your Simulink model, use a "MATLAB Function" block. This block enables the execution of MATLAB functions and code within a Simulink model. More information about this block is available at: https://www.mathworks.com/help/releases/R2021b/simulink/slref/matlabfunction.html.
Alternatively, if you can upgrade to R2022a, you can take advantage of the MQTT Client introduced in that version. This feature allows you to publish data directly from Simulink to an MQTT broker. More information is available in the documentation: https://www.mathworks.com/help/releases/R2022a/icomm/mqtt.html. You can use "MATLAB Function" blocks to incorporate these codes into your Simulink model for real-time data transmission to Grafana.
I hope this addresses your question.
See Also
Categories
Find more on Development Computer Setup 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!