Node-RED – Voorbeeld – Modbus uitlezen en visualiseren
Op deze pagina vind je een voorbeeld hoe je een modbus (multi) sensor of ander apparaat uitleest en visualiseert met Node-RED.
Wat heb je nodig?
Stap 1
Voeg een Modbus Node toe en configureer:
- De Unit-id
- Functie om te gebruiken
- Start adres van het register
- Hoeveel registers je uit wilt lezen
- Configureer de “server”, in dit geval een USB <> RS485 Stick
Server configuratie:
Hier configureer je de USB stick:
- Type: Serial
- Poort, LET OP: Standaard staat er /dev/ttyUSB, vergeet de 0 (of 1) niet toe te voegen (/dev/ttyUSB0)
- Serial type: RTU
- Baudrate
- Unit-Id
Stap 2
Plaats een payload debug om te kijken of er data binnenkomt, indien alles goed gevuld, moeten er array’s aan data binnenkomen:
Stap 3
Om nu de array’s te distilleren in FLOAT’s heb ik een functie gebruikt die van de array payloads FLOATs maakt en deze op een uitgang zet:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var temp = parseFloat(msg.payload[0] + "." + msg.payload[1]); var hum = parseFloat(msg.payload[2] + "." + msg.payload[3]); var bar = parseFloat(msg.payload[4] + "." + msg.payload[5]); var co2 = parseFloat(msg.payload[6]); var lux = parseFloat(msg.payload[7]); var msg1 = {payload:temp}; var msg2 = {payload:hum}; var msg3 = {payload:bar}; var msg4 = {payload:co2}; var msg5 = {payload:lux}; return [msg1,msg2,msg3,msg4,msg5]; |
Stap 4
Nu kan je achter elke uitgang de desbetreffende meter koppelen en configureren:
Op het dashboard vind je alle data van multisensor:
Project in Node-RED clipboard:
1 |
[{"id":"49eb8d8a.070834","type":"function","z":"72ca7ddb.f1fef4","name":"Array Parser","func":"var temp = parseFloat(msg.payload[0] + \".\" + msg.payload[1]);\nvar hum = parseFloat(msg.payload[2] + \".\" + msg.payload[3]);\nvar bar = parseFloat(msg.payload[4] + \".\" + msg.payload[5]);\nvar co2 = parseFloat(msg.payload[6]);\nvar lux = parseFloat(msg.payload[7]);\n\nvar msg1 = {payload:temp};\nvar msg2 = {payload:hum};\nvar msg3 = {payload:bar};\nvar msg4 = {payload:co2};\nvar msg5 = {payload:lux};\n\nreturn [msg1,msg2,msg3,msg4,msg5];","outputs":5,"noerr":0,"x":510,"y":720,"wires":[["bb0891db.874d2","86d007c8.3a6008"],["c0c58094.4b6a5","2c5b4d23.6b4b72"],["74367ead.e996d","50773cf2.b00354"],["f4c6bc47.4fd2f","9cc6fb13.3d4738"],[]],"inputLabels":["msg"],"outputLabels":["temp","","","",""]},{"id":"bb0891db.874d2","type":"ui_gauge","z":"72ca7ddb.f1fef4","name":"TempMeter","group":"fab0db25.fb55b8","order":0,"width":0,"height":0,"gtype":"gage","title":"Temperatuur","label":"Graden C","format":"{{value}}","min":"5","max":"40","colors":["#00b500","#e6e600","#ca3838"],"seg1":"15","seg2":"30","x":790,"y":500,"wires":[]},{"id":"e0b743ba.a403c","type":"modbus-read","z":"72ca7ddb.f1fef4","name":"MODBUS LEZEN","topic":"","showStatusActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"0","quantity":"8","rate":"5","rateUnit":"s","delayOnStart":false,"startDelayTime":"","server":"fc47c2e6.2888a","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":260,"y":720,"wires":[["49eb8d8a.070834","d9ea452d.6c7468"],[]]},{"id":"d9ea452d.6c7468","type":"debug","z":"72ca7ddb.f1fef4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":640,"wires":[]},{"id":"86d007c8.3a6008","type":"ui_chart","z":"72ca7ddb.f1fef4","name":"TempChart","group":"fab0db25.fb55b8","order":0,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"5","ymax":"40","removeOlder":"2","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":790,"y":540,"wires":[[],[]]},{"id":"2c5b4d23.6b4b72","type":"ui_chart","z":"72ca7ddb.f1fef4","name":"HumChart","group":"ed5e1c9c.66396","order":2,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"20","ymax":"100","removeOlder":"2","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":790,"y":640,"wires":[[],[]]},{"id":"c0c58094.4b6a5","type":"ui_gauge","z":"72ca7ddb.f1fef4","name":"HumMeter","group":"ed5e1c9c.66396","order":1,"width":0,"height":0,"gtype":"gage","title":"Luchtvochtigheid","label":"%","format":"{{value}}","min":"20","max":"100","colors":["#0080ff","#0000ff","#0000a0"],"seg1":"40","seg2":"80","x":790,"y":600,"wires":[]},{"id":"50773cf2.b00354","type":"ui_chart","z":"72ca7ddb.f1fef4","name":"BarChart","group":"81c9e14d.0b0e","order":2,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"950","ymax":"1080","removeOlder":"2","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":780,"y":740,"wires":[[],[]]},{"id":"74367ead.e996d","type":"ui_gauge","z":"72ca7ddb.f1fef4","name":"BarMeter","group":"81c9e14d.0b0e","order":1,"width":"5","height":"4","gtype":"gage","title":"Luchtdruk","label":"hPa","format":"{{value}}","min":"950","max":"1080","colors":["#0080ff","#0080ff","#0080ff"],"seg1":"","seg2":"","x":780,"y":700,"wires":[]},{"id":"9cc6fb13.3d4738","type":"ui_chart","z":"72ca7ddb.f1fef4","name":"CO2Chart","group":"763642a9.9cd5fc","order":2,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"410","ymax":"5000","removeOlder":"2","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":780,"y":840,"wires":[[],[]]},{"id":"f4c6bc47.4fd2f","type":"ui_gauge","z":"72ca7ddb.f1fef4","name":"CO2Meter","group":"763642a9.9cd5fc","order":1,"width":"0","height":"0","gtype":"gage","title":"CO2","label":"PPM","format":"{{value}}","min":"410","max":"5000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"1000","seg2":"2500","x":790,"y":800,"wires":[]},{"id":"fab0db25.fb55b8","type":"ui_group","z":"","name":"Temperatuur","tab":"12acef78.d57cc1","disp":true,"width":"6","collapse":false},{"id":"fc47c2e6.2888a","type":"modbus-client","z":"","name":"UNIT 1","clienttype":"simpleser","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB0","serialType":"RTU","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"},{"id":"ed5e1c9c.66396","type":"ui_group","z":"","name":"Luchtvochtigheid","tab":"12acef78.d57cc1","order":2,"disp":true,"width":"6","collapse":false},{"id":"81c9e14d.0b0e","type":"ui_group","z":"","name":"Luchtdruk","tab":"12acef78.d57cc1","order":3,"disp":true,"width":"6","collapse":false},{"id":"763642a9.9cd5fc","type":"ui_group","z":"","name":"CO2","tab":"12acef78.d57cc1","order":4,"disp":true,"width":"6","collapse":false},{"id":"12acef78.d57cc1","type":"ui_tab","z":"","name":"MODBUS SENSOR TEST","icon":"dashboard"}] |
Voorbeeld van een pull-treintje
Onderstaand voorbeeld is een pull-treintje, dat eventueel handig kan zijn als je registers wilt uitlezen die ver uit elkaar staan (bijvoorbeeld 1-4 en 180-185), of verschillende functies gebruikt voor het uitlezen van het apparaat (1-4):
Het is zo opgebouwd dat de binnengekomen data van de eerste pull hoger moet zijn dan 0 alvorens de volgende pull getriggerd wordt:
- Zet een inject node neer en stel deze met een interval in op ca 3 seconden:
2. Plaats een “modbus getter” node en stel deze in voor het apparaat en welke register er uitgelezen moeten worden:
3. Achter de uitgang van de “modbus getter” node kan je een “modbus response” node en een node voor je dashboard hangen, maar plaats daarachter een switch node, die je instelt om te kijken of de waarde > 0 is vanuit de eerste pull, om zo door te gaan naar de volgende:
Let op: omdat je payload een array is, kan je in de switch node de (1e) waarde selecteren met payload[0] (2e waarde is payload[1] etc…)
Project in Node-RED clipboard:
1 |
[{"id":"8e78edc0.e088f","type":"ui_text","z":"275b1aed.17edf6","group":"59fea312.1c986c","order":3,"width":0,"height":0,"name":"","label":"DALLAS","format":"{{msg.payload}} C","layout":"row-spread","x":1240,"y":360,"wires":[]},{"id":"9932807f.e3b7d","type":"ui_text","z":"275b1aed.17edf6","group":"59fea312.1c986c","order":4,"width":0,"height":0,"name":"","label":"BME280","format":"{{msg.payload}} C","layout":"row-spread","x":1240,"y":480,"wires":[]},{"id":"a9a8a930.1d5c08","type":"ui_text","z":"275b1aed.17edf6","group":"59fea312.1c986c","order":4,"width":0,"height":0,"name":"","label":"GEM","format":"{{msg.payload}} C","layout":"row-spread","x":1230,"y":600,"wires":[]},{"id":"149d1e36.a10be2","type":"modbus-response","z":"275b1aed.17edf6","name":"Data","registerShowMax":20,"x":1230,"y":300,"wires":[]},{"id":"b8701d0f.92621","type":"modbus-getter","z":"275b1aed.17edf6","name":"MB get data","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"unitid":"1","dataType":"HoldingRegister","adr":"2","quantity":"2","server":"f61e9aef.8c9838","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":810,"y":460,"wires":[["9932807f.e3b7d","fb83867e.8b75f8","c389f70e.17b158"],[]]},{"id":"fb83867e.8b75f8","type":"modbus-response","z":"275b1aed.17edf6","name":"Data","registerShowMax":20,"x":1230,"y":420,"wires":[]},{"id":"c1989522.602688","type":"modbus-getter","z":"275b1aed.17edf6","name":"MB get data","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"unitid":"1","dataType":"HoldingRegister","adr":"4","quantity":"2","server":"f61e9aef.8c9838","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":810,"y":580,"wires":[["a9a8a930.1d5c08","519ae75f.d918e8"],[]]},{"id":"519ae75f.d918e8","type":"modbus-response","z":"275b1aed.17edf6","name":"Data","registerShowMax":20,"x":1230,"y":540,"wires":[]},{"id":"bebdfe50.3a0d4","type":"switch","z":"275b1aed.17edf6","name":"pull next data?","property":"payload[0]","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1020,"y":380,"wires":[["b8701d0f.92621"]]},{"id":"c389f70e.17b158","type":"switch","z":"275b1aed.17edf6","name":"pull next data?","property":"payload[0]","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1020,"y":500,"wires":[["c1989522.602688"]]},{"id":"d120d174.68b9a","type":"modbus-getter","z":"275b1aed.17edf6","name":"MB get data","showStatusActivities":true,"showErrors":true,"logIOActivities":false,"unitid":"1","dataType":"HoldingRegister","adr":"0","quantity":"2","server":"f61e9aef.8c9838","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":810,"y":340,"wires":[["149d1e36.a10be2","8e78edc0.e088f","bebdfe50.3a0d4"],[]]},{"id":"67abaf93.5ed63","type":"inject","z":"275b1aed.17edf6","name":"Trigger","topic":"","payload":"","payloadType":"date","repeat":"3","crontab":"","once":false,"onceDelay":0.1,"x":620,"y":340,"wires":[["d120d174.68b9a"]]},{"id":"59fea312.1c986c","type":"ui_group","z":"","name":"SDM120 Warmtepomp","tab":"e86598.95b6ca68","order":1,"disp":true,"width":"8","collapse":false},{"id":"f61e9aef.8c9838","type":"modbus-client","z":"","name":"Serial_9600_8_N_1","clienttype":"serial","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB0","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"","commandDelay":"30","clientTimeout":"2000","reconnectOnTimeout":false,"reconnectTimeout":"5000","parallelUnitIdsAllowed":false},{"id":"e86598.95b6ca68","type":"ui_tab","z":"","name":"Node red Mulder","icon":"dashboard","order":1,"disabled":false,"hidden":false}] |