From Simulink to Grafana using MQTT Protocol

8 views (last 30 days)
EMILIO
EMILIO on 26 Feb 2025
Commented: EMILIO on 10 Mar 2025
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?

Answers (1)

Aravind
Aravind on 5 Mar 2025
Hello @EMILIO,
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.
  1 Comment
EMILIO
EMILIO on 10 Mar 2025
Hello @Aravind,
I'm using R2024b because I need a newer version to use MQTT Publish blocks in the Simulink Support Package for Android Devices. However, I'm struggling to get it to work—I can't communicate with the topic.
Do you know how to correctly set the topic?
Thanks!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!