Arduino/ESP32 Serial to Influxdb, Mqtt Bridge

The flow is a bridge from serial port to mqtt, Influxdb, Error reports to file.

when connecting arduino / esp to serial port (USB2Serial),

How to use:

  1. Selecting the serial port in the first Serial node.
  2. Select InfluxDB Settings
  3. Select Mqtt Settings
  4. Change Default Mqtt Topic

Mqtt Topics: Data Topic : "feeds/nodes/NODEID" (or test of NODEID is empty) Errors: feeds/nodes/errors

Errors file : "node-errors.txt" on home dir

Tips: Selecting baud rate (9600 best for ESP32 on 80MHZ Clock), the program listens to commands sent by arduino.

Arduino sends json encoded data and the nodes parse them into mqtt

Arduino sending data like : {"node-id":10,"data0":2,"data1":768,"data2":0}

Note: The sketch exports the json data to Influxdb multi data output type format. Connect JSON2OBJ Function output to influxdb.

[{"id":"e6ad0d04.1d775","type":"comment","z":"344ad79e.ebd1a","name":"Serial Data Parase from json to mqtt & ERR file (Example data from arduino: {\"nodeid\":10,\"data0\":2,\"data1\":768,\"data2\":0} )","info":"","x":440,"y":60,"wires":[]},{"id":"bae68f94.30b09","type":"function","z":"344ad79e.ebd1a","name":"JSON2OBJ (For InfluxDB)","func":"var t = msg.payload;\nt = t.replace(\"\\r\",\"\");\nt = t.replace(\"\\n\",\"\");\nvar obj = JSON.parse(t);\nvar nodeid = obj.nodeid;\nmsg.payload=obj;\nmsg.measurement=\"node\"+nodeid;\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":160,"wires":[["6d5acd94.af66bc"]]},{"id":"4ec5d3c3.4da0fc","type":"mqtt out","z":"344ad79e.ebd1a","name":"","topic":"","qos":"0","retain":"","broker":"b778e59e.e51278","x":810,"y":260,"wires":[]},{"id":"7e029392.865394","type":"function","z":"344ad79e.ebd1a","name":"JSON2MQTT","func":"var t = msg.payload;\nt = t.replace(\"\\r\",\"\");\nt = t.replace(\"\\n\",\"\");\nvar obj = JSON.parse(t);\nvar nodeid = obj.nodeid;\n\n//Empty NodeID\nif(nodeid.length<=0)nodeid=\"test\";\n\nmsg.payload=t;\nmsg.topic=\"feeds/nodes/\"+nodeid;\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":260,"wires":[["4ec5d3c3.4da0fc","a3c868e9.b72758"]]},{"id":"e2c47750.fda978","type":"serial in","z":"344ad79e.ebd1a","name":"Arduino Serial Port (Select)","serial":"ce3207cb.28cb3","x":140,"y":180,"wires":[["7a45ccfe.8703ac"]]},{"id":"d3e18215.ba549","type":"function","z":"344ad79e.ebd1a","name":"Err to File","func":"\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":340,"wires":[["891382e7.87d678"]]},{"id":"891382e7.87d678","type":"file","z":"344ad79e.ebd1a","name":"File Append","filename":"node-errors.txt","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":810,"y":340,"wires":[[]]},{"id":"982b3f2e.5cbf5","type":"function","z":"344ad79e.ebd1a","name":"Err to MQTT","func":"//Format Error String as Json\nmsg.payload = msg.payload.replace(\"!ERR\",\"Error\");\nmsg.payload = \"\\\"\" + msg.payload + \"\\\"\"\nmsg.topic=\"feeds/nodes/errors\";\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":380,"wires":[["4ec5d3c3.4da0fc"]]},{"id":"7a45ccfe.8703ac","type":"function","z":"344ad79e.ebd1a","name":"ERR Filter","func":"var str = msg.payload;\n\nvar n = str.indexOf(\"!ERR\"); \n\nif(n>=0){\n//Error\nreturn [null,msg];    \n}else{\n//OK\nreturn [msg,null];\n}","outputs":2,"noerr":0,"x":330,"y":260,"wires":[["bae68f94.30b09","7e029392.865394","8b690a6c.546708"],["d3e18215.ba549","982b3f2e.5cbf5"]],"inputLabels":["MSG"],"outputLabels":["OK","ERR"],"icon":"node-red/comment.png"},{"id":"eca6fbe2.5e44c8","type":"inject","z":"344ad79e.ebd1a","name":"","topic":"","payload":"!ERR Test Error String","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":120,"wires":[["7a45ccfe.8703ac"]]},{"id":"8b690a6c.546708","type":"debug","z":"344ad79e.ebd1a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":530,"y":120,"wires":[]},{"id":"a3c868e9.b72758","type":"debug","z":"344ad79e.ebd1a","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":830,"y":220,"wires":[]},{"id":"6d5acd94.af66bc","type":"influxdb out","z":"344ad79e.ebd1a","influxdb":"b494e610.9f55f8","name":"","measurement":"","precision":"","retentionPolicy":"","x":870,"y":160,"wires":[]},{"id":"b778e59e.e51278","type":"mqtt-broker","z":"","name":"localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"ce3207cb.28cb3","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"},{"id":"b494e610.9f55f8","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"sensors","name":"","usetls":false,"tls":""}]

Flow Info

Created 5 years, 6 months ago
Updated 5 years, 5 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • comment (x1)
  • debug (x2)
  • file (x1)
  • function (x5)
  • inject (x1)
  • mqtt out (x1)
  • mqtt-broker (x1)
Other
  • influxdb (x1)
  • influxdb out (x1)
  • serial in (x1)
  • serial-port (x1)

Tags

  • arduino
  • esp32
  • esp8266
  • serial
  • parse
  • data
  • input
  • raspberry
  • com
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option