Extract values from serial port data & display on dashboard

Estoy tratando de armar un dashboard en donde extraigo diferentes valores por puerto serial (COM), esto es lo que me está llegando pero ocupo extraer solamente los valores de BAT, TC, HUM Y PRESS.
Soy muy nuevo usando red node por lo cual necesito ayuda para extraer estos datos de la cadena.

Gracias

I am trying to put together a dashboard where I extract different values by serial port (COM), this is what is coming to me but I only need to extract the values of BAT, TC, HUM AND PRESS.
I am very new to using red node so I need help to extract this data from the string.

Thank you

Welcome to the forum @Allanch7.

You are lucky that the interesting data is framed by a pair of # characters. This makes possible to use a split node to separate the data. After the split node you can use a switch node to route the data to different outputs. Something like below:

Testing flow:

[{"id":"613d1be1.756e94","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"688fcd43.63b134","type":"inject","z":"613d1be1.756e94","name":"Your serial node","topic":"","payload":"Data: <=>fijierfji10440#EVENTSBOARD#252#STR:new_sensor_frame#BAT:93#TC:26.45#HUM:35.0#PRES:91231.56#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":180,"wires":[["c3727b79.b17958"]]},{"id":"6c7ea4d9.1df1dc","type":"debug","z":"613d1be1.756e94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":140,"wires":[]},{"id":"c3727b79.b17958","type":"split","z":"613d1be1.756e94","name":"","splt":"#","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":410,"y":180,"wires":[["5b228f02.4fdd9"]]},{"id":"5b228f02.4fdd9","type":"switch","z":"613d1be1.756e94","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"BAT","vt":"str"},{"t":"cont","v":"TC","vt":"str"},{"t":"cont","v":"HUM","vt":"str"},{"t":"cont","v":"PRES","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":550,"y":180,"wires":[["6c7ea4d9.1df1dc"],["79927bd7.6b8264"],["ba1175cd.e6e118"],["74ca4f6f.1d76e"]]},{"id":"79927bd7.6b8264","type":"debug","z":"613d1be1.756e94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":180,"wires":[]},{"id":"ba1175cd.e6e118","type":"debug","z":"613d1be1.756e94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":220,"wires":[]},{"id":"74ca4f6f.1d76e","type":"debug","z":"613d1be1.756e94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":260,"wires":[]}]

You could set the serial settings to split on # then, using a function, split each string on :. if it matches what you want, return a new msg

EDIT...

Serial port setting...
image

Example...

Function code...

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

var s = msg.payload;
if(typeof s == "string" && s.includes(":")) {
    let pair = s.split(":");
    let name = pair[0];
    let val = pair[1];
    if(isNumeric(val)){
        val = Number(val);
    }
    msg.topic =  name;
    msg.payload =  val;
    return msg;
}
return null;

flow...

[{"id":"be0c31c5.4f9ed","type":"inject","z":"c70ba4a4.e7fb58","name":"","topic":"","payload":"<=>SDASDEFASF#BAT:93#TC:26.45#HUM:35.0#PRES:91231.56#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":690,"y":700,"wires":[["337183dd.3c553c","d65d45ae.d11a08"]]},{"id":"337183dd.3c553c","type":"split","z":"c70ba4a4.e7fb58","name":"","splt":"#","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":true,"addname":"","x":820,"y":700,"wires":[["9d077fb7.14cf4"]]},{"id":"a64f74d4.8f5778","type":"comment","z":"c70ba4a4.e7fb58","name":"Use inject+split to fake the serial data","info":"","x":750,"y":660,"wires":[]},{"id":"9d077fb7.14cf4","type":"function","z":"c70ba4a4.e7fb58","name":"split on :","func":"function isNumeric(n) {\n  return !isNaN(parseFloat(n)) && isFinite(n);\n}\n\nvar s = msg.payload;\nif(typeof s == \"string\" && s.includes(\":\")) {\n    let pair = s.split(\":\");\n    let name = pair[0];\n    let val = pair[1];\n    if(isNumeric(val)){\n        val = Number(val);\n    }\n    msg.topic =  name;\n    msg.payload =  val;\n    return msg;\n}\nreturn null;","outputs":1,"noerr":0,"x":1020,"y":700,"wires":[["cfab5df0.2fa3c"]]},{"id":"cfab5df0.2fa3c","type":"debug","z":"c70ba4a4.e7fb58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1170,"y":700,"wires":[]},{"id":"28998841.424988","type":"comment","z":"c70ba4a4.e7fb58","name":"Connect the function to output of serial node","info":"","x":1130,"y":660,"wires":[]},{"id":"d65d45ae.d11a08","type":"debug","z":"c70ba4a4.e7fb58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":810,"y":780,"wires":[]}]
2 Likes

As you can see one of the strengths of Node-RED is that you have several ways to achieve what you want. An alternative to split the values is by using REGEX inside a change node.

Flow:

[{"id":"613d1be1.756e94","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"688fcd43.63b134","type":"inject","z":"613d1be1.756e94","name":"Your serial node","topic":"","payload":"Data: <=>fijierfji10440#EVENTSBOARD#252#STR:new_sensor_frame#BAT:93#TC:26.45#HUM:35.0#PRES:91231.56#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":180,"wires":[["c3727b79.b17958"]]},{"id":"c3727b79.b17958","type":"split","z":"613d1be1.756e94","name":"","splt":"#","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":410,"y":180,"wires":[["5b228f02.4fdd9"]]},{"id":"5b228f02.4fdd9","type":"switch","z":"613d1be1.756e94","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"BAT","vt":"str"},{"t":"cont","v":"TC","vt":"str"},{"t":"cont","v":"HUM","vt":"str"},{"t":"cont","v":"PRES","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":550,"y":240,"wires":[["c1302d98.3895f"],["7c03fd8c.357bb4"],["7775b6c6.146cd8"],["7b773e27.d85c9"]]},{"id":"542e0391.8eb54c","type":"debug","z":"613d1be1.756e94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"bat","targetType":"msg","x":920,"y":160,"wires":[]},{"id":"c1302d98.3895f","type":"change","z":"613d1be1.756e94","name":"","rules":[{"t":"set","p":"temp","pt":"msg","to":"$match(payload,/\\d+\\.*\\d*/)","tot":"jsonata"},{"t":"set","p":"bat","pt":"msg","to":"temp.match","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":160,"wires":[["542e0391.8eb54c"]]},{"id":"7c03fd8c.357bb4","type":"change","z":"613d1be1.756e94","name":"","rules":[{"t":"set","p":"temp","pt":"msg","to":"$match(payload,/\\d+\\.*\\d*/)","tot":"jsonata"},{"t":"set","p":"tc","pt":"msg","to":"temp.match","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":220,"wires":[["73bd8466.77928c"]]},{"id":"7775b6c6.146cd8","type":"change","z":"613d1be1.756e94","name":"","rules":[{"t":"set","p":"temp","pt":"msg","to":"$match(payload,/\\d+\\.*\\d*/)","tot":"jsonata"},{"t":"set","p":"hum","pt":"msg","to":"temp.match","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":280,"wires":[["81f1ea1b.a00768"]]},{"id":"7b773e27.d85c9","type":"change","z":"613d1be1.756e94","name":"","rules":[{"t":"set","p":"temp","pt":"msg","to":"$match(payload,/\\d+\\.*\\d*/)","tot":"jsonata"},{"t":"set","p":"pres","pt":"msg","to":"temp.match","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":340,"wires":[["a994e0b8.085ab"]]},{"id":"73bd8466.77928c","type":"debug","z":"613d1be1.756e94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"tc","targetType":"msg","x":910,"y":220,"wires":[]},{"id":"81f1ea1b.a00768","type":"debug","z":"613d1be1.756e94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"hum","targetType":"msg","x":920,"y":280,"wires":[]},{"id":"a994e0b8.085ab","type":"debug","z":"613d1be1.756e94","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"pres","targetType":"msg","x":920,"y":340,"wires":[]}]
1 Like

He podido avanzar, esto es lo que me esta mostrando, como puedo sacar esos valores de TC,HUM,PRES,BAT por ejemplo a un gauge ya que estos son valores de sensores.

Hi @Allanch7 - sorry, my Spanish is not great and this is predominantly an English forum.

As i understand it, you want to know how to send these values to a gauge (for example)?

What have you tried?

Have you installed node-red-dashboard?

Its quite simple (PS, read the INFO on the Side-bar for help)

Add these...
image

Get this...
image

Demo flow...

[{"id":"19c61b33.f26065","type":"inject","z":"c70ba4a4.e7fb58","name":"","topic":"","payload":"<=>SDASDEFASF#BAT:93#TC:26.45#HUM:35.0#PRES:91231.56#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1310,"y":720,"wires":[["b3bf3347.93cb8","cdb376f2.428208"]]},{"id":"b3bf3347.93cb8","type":"split","z":"c70ba4a4.e7fb58","name":"","splt":"#","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":true,"addname":"","x":1440,"y":720,"wires":[["2301b865.c6ad88"]]},{"id":"a2ca5411.c38888","type":"comment","z":"c70ba4a4.e7fb58","name":"Use inject+split to fake the serial data","info":"","x":1370,"y":680,"wires":[]},{"id":"2301b865.c6ad88","type":"function","z":"c70ba4a4.e7fb58","name":"split on :","func":"function isNumeric(n) {\n  return !isNaN(parseFloat(n)) && isFinite(n);\n}\n\nvar s = msg.payload;\nif(typeof s == \"string\" && s.includes(\":\")) {\n    let pair = s.split(\":\");\n    let name = pair[0];\n    let val = pair[1];\n    if(isNumeric(val)){\n        val = Number(val);\n    }\n    msg.topic =  name;\n    msg.payload =  val;\n    return msg;\n}\nreturn null;","outputs":1,"noerr":0,"x":1640,"y":720,"wires":[["8d741514.2fe0e8"]]},{"id":"8691f52f.ee7b68","type":"comment","z":"c70ba4a4.e7fb58","name":"Connect the function to output of serial node","info":"","x":1750,"y":680,"wires":[]},{"id":"cdb376f2.428208","type":"debug","z":"c70ba4a4.e7fb58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1430,"y":800,"wires":[]},{"id":"8d741514.2fe0e8","type":"switch","z":"c70ba4a4.e7fb58","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"BAT","vt":"str"},{"t":"eq","v":"PRES","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1840,"y":720,"wires":[["a7a27243.02e6d"],["264f8075.9e932"]]},{"id":"a7a27243.02e6d","type":"ui_gauge","z":"c70ba4a4.e7fb58","name":"","group":"f659c41f.adaeb8","order":1,"width":0,"height":0,"gtype":"gage","title":"gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":2020,"y":680,"wires":[]},{"id":"264f8075.9e932","type":"ui_chart","z":"c70ba4a4.e7fb58","name":"","group":"f659c41f.adaeb8","order":2,"width":0,"height":0,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":2020,"y":740,"wires":[[]]},{"id":"f659c41f.adaeb8","type":"ui_group","z":"","name":"HOME2","tab":"9ac44b8b.164fe8","order":1,"disp":true,"width":"6","collapse":false},{"id":"9ac44b8b.164fe8","type":"ui_tab","z":"","name":"TEST2","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

yes but it doesn't show the values, what I want is to put 4 indicators on a board.

Put debug nodes after each node. See what's happening.

If i put the debug modules after the switch they don't show data

That's why your graphs are empty.

Did you put debug nodes further back to see what was going into the switch node like I said...

Put them everywhere. See what they contain, figure out why the messages are not getting through.

(Just to add my contribution to the discussion) I strongly encourage you to try to develop this flow by using the standard NR nodes (split, switch, change, and so on) without using the function node.

We have been working with Node-RED since 5 years and what we see is that developing Flows using "Function" nodes can be very hard if you are not a Javascript developer, in the other hand, if you understand how to use the standard nodes you can go from newbie to pro much much faster, trust me.

3 Likes

Hi Steve
gauge = 100 looks better
Regards