III. InfluxDB & Node-RED & MQTT Tutorial: Getting data from MQTT
Learn Node-RED & InfluxDB. And MQTT: the best protocol for the IoT industry
24 September, 2021 by
III. InfluxDB & Node-RED & MQTT Tutorial: Getting data from MQTT
Boot & Work Corp. S.L., Fernandez Queralt Martinez

Introduction

In the previous blogs:

I. InfluxDB & Node-RED & MQTT Tutorial: How to install InfluxDB >>>

II. InfluxDB & Node-RED & MQTT Tutorial: Sending data to InfluxDB >>>

We learned how to install InfluxDB, and how to send any number from an inject node to the Influx database that we created. In this blog, we will learn how to get data from MQTT, and send it to the database!

Related links


How to install
Mosquitto

Read >>>


How to connect Raspberry PLC to WiFi

Read >>>


Raspberry PLC configurable

See >>>


MQTT & Raspberry PLC & Node-RED

Read >>>


How to find your perfect industrial PLC

Read >>>


Raspberry Pi based Panel PC products

See >>>


Connections

First of all, we have to make sure about the connections. Right now we have our laptop connected through Ethernet and SSH to the industrial Raspberry PLC, and we have an extra monitor to work better with the information from the Raspberry Pi based PLC. 

Connections - III. InfluxDB & Node-RED & MQTT Tutorial: Getting data from MQTT

Publish MQTT message from PC

What we are going to do now, is to publish a MQTT message from our PC. And we will subscribe to it from the industrial Raspberry Pi PLC.

1. So, the first thing we are going to do it is to open Node-RED from your PC:

localhost:1880
2. Add an inject node with a number msg.payload and we will send it to a MQTT out node with the topic: /industrial/shields:
Add an inject node - III. InfluxDB & Node-RED & MQTT Tutorial: Getting data from MQTT

Import our flow in order to compare it with yours by going to: Menu > Import > Paste this JSON > Click on Import

[{"id":"f04655a6.2e0548","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"18eca158.c30bef","type":"ui_spacer","name":"spacer","group":"","order":3,"width":3,"height":1},{"id":"66b1ac62.f204c4","type":"ui_spacer","name":"spacer","group":"","order":5,"width":2,"height":1},{"id":"57e7a054.1d3d","type":"ui_spacer","name":"spacer","group":"","order":6,"width":1,"height":1},{"id":"3d2f0a19.b50e16","type":"ui_spacer","name":"spacer","group":"","order":7,"width":3,"height":1},{"id":"2818fa86.78b246","type":"ui_spacer","name":"spacer","group":"","order":8,"width":2,"height":1},{"id":"b7071d44.aac77","type":"ui_spacer","name":"spacer","group":"","order":9,"width":14,"height":1},{"id":"76d851d4.191b1","type":"ui_spacer","name":"spacer","group":"","order":10,"width":1,"height":1},{"id":"2b8a21d5.7e913e","type":"ui_spacer","name":"spacer","group":"","order":12,"width":1,"height":1},{"id":"8ff507a5.bebcf8","type":"ui_spacer","name":"spacer","group":"","order":13,"width":1,"height":1},{"id":"4e0db42.3a7d74c","type":"ui_spacer","name":"spacer","group":"","order":14,"width":1,"height":1},{"id":"6ef46fcc.54f8d","type":"ui_spacer","name":"spacer","group":"","order":15,"width":14,"height":1},{"id":"ff69b32a.fafbb","type":"ui_spacer","name":"spacer","group":"","order":16,"width":1,"height":1},{"id":"a4c5a26b.0ba75","type":"ui_spacer","name":"spacer","group":"","order":18,"width":1,"height":1},{"id":"aba323e0.729b9","type":"ui_spacer","name":"spacer","group":"","order":19,"width":1,"height":1},{"id":"caa68f8b.b56ca","type":"ui_spacer","name":"spacer","group":"","order":20,"width":1,"height":1},{"id":"a9e2309bbb6821ba","type":"ui_spacer","name":"spacer","group":"","order":2,"width":1,"height":1},{"id":"21ea384a01fa887e","type":"ui_spacer","name":"spacer","group":"","order":3,"width":1,"height":1},{"id":"d5d81a9f0d428697","type":"ui_spacer","name":"spacer","group":"","order":4,"width":1,"height":1},{"id":"da4a771ac31dcf3d","type":"ui_spacer","name":"spacer","group":"","order":5,"width":1,"height":1},{"id":"fe4d904b8bb40878","type":"ui_spacer","name":"spacer","group":"","order":6,"width":1,"height":1},{"id":"6b3d8da4e9a96852","type":"ui_spacer","name":"spacer","group":"","order":7,"width":1,"height":1},{"id":"74821225a7ca51ca","type":"ui_spacer","name":"spacer","group":"","order":8,"width":1,"height":1},{"id":"e91dbd2767a763f6","type":"ui_spacer","name":"spacer","group":"","order":9,"width":1,"height":1},{"id":"93d3502802dcc1a4","type":"ui_spacer","name":"spacer","group":"","order":7,"width":2,"height":1},{"id":"9fc8c3278ee9b9c1","type":"ui_spacer","name":"spacer","group":"","order":10,"width":1,"height":1},{"id":"f774fbfaae54eb0e","type":"ui_spacer","name":"spacer","group":"","order":12,"width":10,"height":1},{"id":"08e6b009b586964d","type":"ui_spacer","name":"spacer","group":"","order":17,"width":1,"height":1},{"id":"b6232aa234a57cb1","type":"ui_spacer","name":"spacer","group":"","order":20,"width":1,"height":1},{"id":"a60378ac9f87fc5e","type":"ui_spacer","name":"spacer","group":"","order":8,"width":1,"height":1},{"id":"260c6340f6f14ad3","type":"ui_spacer","name":"spacer","group":"","order":12,"width":9,"height":1},{"id":"2b980305848cfeef","type":"ui_spacer","name":"spacer","group":"","order":19,"width":1,"height":1},{"id":"22a9f72987124d95","type":"ui_spacer","name":"spacer","group":"","order":5,"width":5,"height":1},{"id":"5e9d977e96083094","type":"ui_spacer","name":"spacer","group":"","order":9,"width":9,"height":1},{"id":"9110a4597689ae8b","type":"ui_spacer","name":"spacer","group":"","order":10,"width":9,"height":1},{"id":"459ab95d2d2a5ddb","type":"ui_spacer","name":"spacer","group":"","order":11,"width":9,"height":1},{"id":"90e1410acc8c38b9","type":"ui_spacer","name":"spacer","group":"","order":16,"width":5,"height":1},{"id":"7ad2d307c987a7df","type":"ui_spacer","name":"spacer","group":"","order":18,"width":2,"height":1},{"id":"e5e9feb0763ec81e","type":"ui_spacer","name":"spacer","group":"","order":20,"width":1,"height":1},{"id":"093ef67678864b13","type":"ui_spacer","name":"spacer","group":"","order":1,"width":6,"height":1},{"id":"b94afa82cf8ecc1f","type":"ui_spacer","name":"spacer","group":"","order":3,"width":6,"height":1},{"id":"faf9e9fde02df27d","type":"ui_spacer","name":"spacer","group":"","order":4,"width":2,"height":1},{"id":"a8cbdcf68248d1b9","type":"ui_spacer","name":"spacer","group":"","order":6,"width":2,"height":1},{"id":"1dfce3d8addd0131","type":"ui_spacer","name":"spacer","group":"","order":7,"width":2,"height":1},{"id":"add5f326020a30a1","type":"ui_spacer","name":"spacer","group":"","order":8,"width":2,"height":1},{"id":"0c6c63f5118690bf","type":"ui_spacer","name":"spacer","group":"","order":9,"width":2,"height":1},{"id":"73f373a14ff151ec","type":"ui_spacer","name":"spacer","group":"","order":11,"width":2,"height":1},{"id":"ce026db7ddc0b2d0","type":"ui_spacer","name":"spacer","group":"","order":13,"width":2,"height":1},{"id":"f8b17415c15a6072","type":"ui_spacer","name":"spacer","group":"","order":15,"width":2,"height":1},{"id":"8f4603f38cd344ad","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":2,"width":1,"height":1},{"id":"562468767aea6755","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":4,"width":2,"height":1},{"id":"6a6113cdfd6d877e","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":6,"width":2,"height":1},{"id":"4d89a801118d61ef","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":7,"width":2,"height":1},{"id":"72e7fd9dec97d5ce","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":8,"width":2,"height":1},{"id":"4a4b3e4669027be4","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":9,"width":2,"height":1},{"id":"79bd20907ddb50c4","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":10,"width":3,"height":1},{"id":"c1fed531493ab6fe","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":12,"width":3,"height":1},{"id":"3e3030c3709fe31c","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":13,"width":3,"height":1},{"id":"bf367c6bb97ffc0a","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":14,"width":3,"height":1},{"id":"79689c89c65ac453","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":15,"width":3,"height":1},{"id":"26078b1caf5a4049","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":17,"width":3,"height":1},{"id":"03d10337489964ad","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":18,"width":3,"height":1},{"id":"a3a820bc7fb53497","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":19,"width":3,"height":1},{"id":"9f9ae7141861e1c8","type":"ui_spacer","name":"spacer","group":"","order":2,"width":1,"height":1},{"id":"d9cb8337360336d6","type":"ui_spacer","name":"spacer","group":"","order":3,"width":1,"height":1},{"id":"a86c41aef3f1182e","type":"ui_spacer","name":"spacer","group":"","order":4,"width":1,"height":1},{"id":"9666ccbf2e136ba9","type":"ui_spacer","name":"spacer","group":"","order":5,"width":1,"height":1},{"id":"d6403fae75f2ce72","type":"ui_spacer","name":"spacer","group":"","order":6,"width":1,"height":1},{"id":"1577da1ad00267c8","type":"ui_spacer","name":"spacer","group":"","order":7,"width":1,"height":1},{"id":"85a6633f734a397f","type":"ui_spacer","name":"spacer","group":"","order":8,"width":1,"height":1},{"id":"6d0451543273b8e9","type":"ui_spacer","name":"spacer","group":"","order":9,"width":1,"height":1},{"id":"a096632e7e8c7fcc","type":"ui_spacer","name":"spacer","group":"","order":7,"width":2,"height":1},{"id":"01c034e071a22d06","type":"ui_spacer","name":"spacer","group":"","order":10,"width":1,"height":1},{"id":"a41773576044e9e2","type":"ui_spacer","name":"spacer","group":"","order":12,"width":10,"height":1},{"id":"38b3ea45b44de95a","type":"ui_spacer","name":"spacer","group":"","order":17,"width":1,"height":1},{"id":"4f7c5bfb900e811b","type":"ui_spacer","name":"spacer","group":"","order":20,"width":1,"height":1},{"id":"2bf13987b7d07c68","type":"ui_spacer","name":"spacer","group":"","order":8,"width":1,"height":1},{"id":"925897d6382cfc1e","type":"ui_spacer","name":"spacer","group":"","order":12,"width":9,"height":1},{"id":"f9dbd340829a20ab","type":"ui_spacer","name":"spacer","group":"","order":19,"width":1,"height":1},{"id":"84efe78acd668123","type":"ui_spacer","name":"spacer","group":"","order":5,"width":5,"height":1},{"id":"7403c4408d998eb9","type":"ui_spacer","name":"spacer","group":"","order":9,"width":9,"height":1},{"id":"a72cd60a04683869","type":"ui_spacer","name":"spacer","group":"","order":10,"width":9,"height":1},{"id":"27a841b63ebf78df","type":"ui_spacer","name":"spacer","group":"","order":11,"width":9,"height":1},{"id":"2507a7e17f059640","type":"ui_spacer","name":"spacer","group":"","order":16,"width":5,"height":1},{"id":"dd648ea2b4fc993e","type":"ui_spacer","name":"spacer","group":"","order":18,"width":2,"height":1},{"id":"f6f18b0119a0c7a8","type":"ui_spacer","name":"spacer","group":"","order":20,"width":1,"height":1},{"id":"6f20eee74019c663","type":"ui_spacer","name":"spacer","group":"","order":1,"width":6,"height":1},{"id":"7e797c689f839bd5","type":"ui_spacer","name":"spacer","group":"","order":3,"width":6,"height":1},{"id":"159875d396fd2e1d","type":"ui_spacer","name":"spacer","group":"","order":4,"width":2,"height":1},{"id":"09f15455e01dc3bd","type":"ui_spacer","name":"spacer","group":"","order":6,"width":2,"height":1},{"id":"00b530d543c6533d","type":"ui_spacer","name":"spacer","group":"","order":7,"width":2,"height":1},{"id":"9b68b46d65334e55","type":"ui_spacer","name":"spacer","group":"","order":8,"width":2,"height":1},{"id":"2b103ab7680be12c","type":"ui_spacer","name":"spacer","group":"","order":9,"width":2,"height":1},{"id":"a45e230894b860f4","type":"ui_spacer","name":"spacer","group":"","order":11,"width":2,"height":1},{"id":"d96da445e3f00f3b","type":"ui_spacer","name":"spacer","group":"","order":13,"width":2,"height":1},{"id":"8c8b1dc3d2ecd22e","type":"ui_spacer","name":"spacer","group":"","order":15,"width":2,"height":1},{"id":"ad65b0982b9515d2","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":2,"width":1,"height":1},{"id":"8572d6bead68d3fd","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":4,"width":2,"height":1},{"id":"7535d92b622832e2","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":6,"width":2,"height":1},{"id":"acc331cf74b68edf","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":7,"width":2,"height":1},{"id":"d8a017de905c275c","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":8,"width":2,"height":1},{"id":"30e75722341fe68d","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":9,"width":2,"height":1},{"id":"b1a10de362b4fc16","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":10,"width":3,"height":1},{"id":"6edfbbcaa60fa199","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":12,"width":3,"height":1},{"id":"15600e26f2233629","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":13,"width":3,"height":1},{"id":"90ddb4d963776480","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":14,"width":3,"height":1},{"id":"43c7e25f67aadcb1","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":15,"width":3,"height":1},{"id":"dd2ed8c042a0b788","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":17,"width":3,"height":1},{"id":"d3b14202340ff557","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":18,"width":3,"height":1},{"id":"da0c9031a45eeb31","type":"ui_spacer","name":"spacer","group":"78426f95.8f95a","order":19,"width":3,"height":1},{"id":"a4e70dca39a11812","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false},{"id":"6902ce18c912334f","type":"ui_base","theme":{"name":"theme-light","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":true,"reset":false},"darkTheme":{"default":"#097479","baseColor":"#097479","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":false},"customTheme":{"name":"Untitled Theme 1","default":"#4B7930","baseColor":"#4B7930","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"},"themeState":{"base-color":{"default":"#0094CE","value":"#0094CE","edited":false},"page-titlebar-backgroundColor":{"value":"#0094CE","edited":false},"page-backgroundColor":{"value":"#fafafa","edited":false},"page-sidebar-backgroundColor":{"value":"#ffffff","edited":false},"group-textColor":{"value":"#1bbfff","edited":false},"group-borderColor":{"value":"#ffffff","edited":false},"group-backgroundColor":{"value":"#ffffff","edited":false},"widget-textColor":{"value":"#111111","edited":false},"widget-backgroundColor":{"value":"#0094ce","edited":false},"widget-borderColor":{"value":"#ffffff","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}},"angularTheme":{"primary":"indigo","accents":"blue","warn":"red","background":"grey","palette":"light"}},"site":{"name":"Node-RED Dashboard","hideToolbar":"false","allowSwipe":"false","lockMenu":"false","allowTempTheme":"true","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"0a51eb25daf41663","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"5b73900a1aadda9c","type":"mqtt out","z":"f04655a6.2e0548","name":"","topic":"/industrial/shields","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"0a51eb25daf41663","x":470,"y":100,"wires":[]},{"id":"36cdcf455932b604","type":"inject","z":"f04655a6.2e0548","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":180,"y":100,"wires":[["5b73900a1aadda9c"]]}]

Subscribe to MQTT message from Raspberry Pi PLC controller

As we want to subscribe to the MQTT topic: /industrial/shields from the Raspberry Pi industrial PLC, we are going to open a new tab in our browser and type:

10.10.10.20:1880

Now, follow these steps to subscribe to the MQTT topic and send the information to the Influx database:

1. Add an IN MQTT node, set the server to your PC IP address, in our case: 10.10.10.60, and the MQTT port by default: 1883. Leave the rest of the parameters by default. Update. And Subscribe to the topic: /industrial/shields

2. If we add a debug node just right after the MQTT node, you will realize that the message you receive is a string, not a number. So now, we are going to parse it to a number by adding a change node and setting the msg.payload to a expression: $number(payload)

Change node - III. InfluxDB & Node-RED & MQTT Tutorial: Getting data from MQTT

3. Wire a debug node after the change node, click on the inject node on the flow of our laptop in order to publish the MQTT message. And check that now you receive the information as a number, not a string.

4. Connect this to the influxdb out node that we already configured and send the information straight to the database! 

5. Finally, go back to the terminal window and check that you got the information in the database.

Your flow should be something like the picture below.

Now, it is your time! Import the flow and start playing with Node-RED!

[{"id":"b716fdc48724e610","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"d50d0c9f.31e858","type":"tls-config","name":"","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"","servername":"","verifyservercert":false},{"id":"bc4ab5cb2a050021","type":"influxdb","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"test","name":"test","usetls":true,"tls":"d50d0c9f.31e858","influxdbVersion":"2.0","url":"http://localhost:8086","rejectUnauthorized":false},{"id":"562ec1085cc9dbf1","type":"mqtt-broker","name":"","broker":"10.10.10.60","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"0e17644c4b3628b4","type":"influxdb out","z":"b716fdc48724e610","influxdb":"bc4ab5cb2a050021","name":"","measurement":"test","precision":"","retentionPolicy":"","database":"","retentionPolicyV18Flux":"","org":"Industrial Shields","bucket":"test","x":680,"y":80,"wires":[]},{"id":"0c42600975da386c","type":"debug","z":"b716fdc48724e610","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":120,"wires":[]},{"id":"65e33f58d20fee33","type":"mqtt in","z":"b716fdc48724e610","name":"","topic":"/industrial/shields","qos":"2","datatype":"auto","broker":"562ec1085cc9dbf1","nl":false,"rap":true,"rh":0,"x":160,"y":80,"wires":[["69f73915a01982c3","1ba5715c13107a23"]]},{"id":"69f73915a01982c3","type":"change","z":"b716fdc48724e610","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":80,"wires":[["0e17644c4b3628b4","0c42600975da386c"]]},{"id":"1ba5715c13107a23","type":"debug","z":"b716fdc48724e610","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":390,"y":120,"wires":[]}]
Flow - III. InfluxDB & Node-RED & MQTT Tutorial: Getting data from MQTT
III. InfluxDB & Node-RED & MQTT Tutorial: Getting data from MQTT
Boot & Work Corp. S.L., Fernandez Queralt Martinez
24 September, 2021
Share this post
Archive

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>

Do you want more information?

Just fill the form!

Tell me more!