while I find your idea to display live data from MQTT directly in a panel interesting, I would like to point out a more conventional way where measurement data arriving over MQTT will get ingested into a timeseries database (here: InfluxDB) before being accessed by Grafana.
We answered a question coming from a similar context over here at
Thanks @amotl for your comment,
I agree normally we display data that is in a timeseries database, but I was toying with the idea of using Grafana for control as well.
In this case I would like to send commands via MQTT and see feedback / status. At the moment we use the Node-Red UI for this function but it would by nice to do it all from one interface.
I suppose we could embed Grafana in a Node-Red HTML element or vice versa.
Thanks, now I am getting your whole point. I probably should have scanned your code better.
This very much resonates with some thoughts we also already had when discussing the command & control channel to communicate back to upstream nodes within the Hiveeyes Project and how that would be operated exactly on top of the MQTT substrate.
While Node-RED is really powerful, we haven’t yet got friends as we wouldn’t know how to operate a multi-tenant non-interactive setup with it. As we just recently started to get better friends with Grafana and its internal interfaces in the course of [1-4], we would go for the “native way” of connecting the Browser to MQTT by MQTT over WebSockets, which in the best case would yield a Panel Plugin which can be operated in a standalone manner without depending on Node-RED. However, it still could interact with Node-RED using MQTT like with any other bus member.
In fact, this would be pretty much what you implemented and shared already. Thanks!
The reason for that is probably because the panel gets refreshed by the Grafana machinery on each refresh interval and maybe on additional conditions. So, by just folding all the connection management into a real Grafana plugin should already solve that issue.
Good to hear from people having similar ideas about how to bring MQTT closer to Grafana in a sensible way. Maybe we can find some time to work on a basic implementation which would go into that direction. Would you be able to share a screenshot of what your Node-RED interface for sending MQTT messages and displaying status information currently looks like to get an idea about where we should aim at interface-wise?
You can use a text panel in html mode and use the Grafana API to insert or update data to a data source, see Is it possible to execute sql queries from grafana APIs?. As they say this is a not documented feature and can change in the future.
I created a text panel with this test:
<script type="text/javascript" language="javascript">
function UserAction() {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/api/tsdb/query", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.setRequestHeader("Authorization", "Bearer eyJrIjoiWG9RV1lpbXh0a3RHUkJKUHlaVVBON1Z5djVON05pZUoiLCJuIjoidGVzdGtleSIsImlkIjoxfQ==");
xhttp.send(JSON.stringify({"from":"1598166093992","to":"1598187693992","queries":[{"refId":"A","intervalMs":60000,"maxDataPoints":1150,"datasourceId":1,"rawSql":"insert into table values (1)","format":"time_series"}]}));
var response = JSON.parse(xhttp.responseText);
}
</script>
<button type="submit" onclick="UserAction()">Clickme</button>
This may be simpler I just copied the json from a wireshark capture. The Authorization header can be generated in the configuration section, API Keys.
Remember you will need to disable html sanitize in the configuration file.
Once the data is in the database it can be forwarded to the MQTT device with a python script and/or database trigger.