Saving data received from MQTT to InfluxDB using Node-Red

My NodeMCU are sending data using MQTT to the broker. Then this flow get the topic and the data and save on InfluxDB using its HTTP API.

Screenshot_11

http://flows.nodered.org/flow/5c96cc92d3171b6434bf

In function we have this code:
 

var tokens = msg.topic.split("/");
var dest = tokens[tokens.length-1];
msg.url ="http://localhost:8086/write?db=greenhouse";
msg.payload = tokens[2] + ",device=" + tokens[1] + " value=" + msg.payload;
return msg;

Here is a graphics sample:

Screenshot_12

  • Joe Q.

    Thanks for making this code available. In my case I had to make a few changes (trial-and-error) to make it work.

    My version goes like this:

    var tokens = msg.topic.split(“/”);
    var dest = tokens[tokens.length-1];
    msg.url =”http://localhost:8086/write?db=insertDatabaseHere”;
    msg.payload = tokens[1] + “,device=” + tokens[0] + ” value=” + msg.payload;
    return msg;

    The change is that instead of referring to tokens[2] and tokens[1], I’m instead using tokens[1] and tokens[0].
    I’m also not sure what the purpose of the “dest” variable is.

    • Jean Brito

      Depends on how your data is sent to MQTT. “dest” variable is a copy and paste unused thing. :)
      Is very nice to see that this was interesting for some one.
      I’m reworking all the workflow for save the MQTT’s data. I will post here when I will have something good to show.

      • Joe Q.

        Right, in my case the topic is in the format “room/measurement” (e.g., “lvr/temp”, “bsmt/rh”) so I just need to extract those values from the topic.

        Like you, I have the data going from MQTT through Node-Red into InfluxDB, and then plotted on Grafana. All four of these packages running on the same Raspberry Pi 2. The temperature and humidity data comes from ESP8266 units.

        My next project is to find a way to manage the InfluxDB data so that I don’t fill up or kill the Raspberry Pi’s SD card by writing to it so often.

        I am a total novice coder (can do basic Python and Arduino scripts) so your contribution was very helpful to me!

  • Philipp Protschka

    what did you use to plot the data ? :)

    • Jean Brito

      That time, Grafana. Now I’m working in a full app that take data, save to PostgreSQL and show the data.