Splitting payload from serial

#1

Hi :slight_smile: first time on forum, got a question:

Trying to split a payload into several outputs:

{"payload":"6434,-1.50,kg,26.50,25.50,\r\n","port":"/dev/ttyUSB0","_msgid":"870aa08d.f4a58"}

Im betting I need to use a split function, but how? I can't comma seperate. I want one output for "-1,5" , one output for 26,50, and one for 25,50.

Can anyone point me in the right direction?

PS this is a sparkfun openscale board with an external temp sensor, connected to a raspberry pi.

0 Likes

#2

I don't understand. In the string you have shown -1.50 not -1,50 so splitting on comma would seem to be what you want.

0 Likes

#3

its because if I comma split, I will get one message with "kg" in it :slight_smile:

0 Likes

#4

Why is that a problem?

0 Likes

#5

Use the CSV node. That way you can re-add the property names you want

2 Likes

#6

hm, maybe its my noobishness that is the cause of me not seing the solution here.
My goal with splitting the numbers in this payload into 3 seperate outputs, to be put into influxDB and from there into Grafana.

0 Likes

#7

Having split it you will get an array (assuming you do it in a function node). You can simply ignore the third element when you build the message to send to influx.

0 Likes

#8

As @dceejay suggested, the CSV node may well be the simplest way, using that you can allocate the influx topic names to the values automatically.

0 Likes

#9

I just tried it, the \r\n are causing javascript issues. Can't split it into an array, at least I couldn't.

0 Likes

#10

simple csv example

[{"id":"2297eb8e.63fa74","type":"inject","z":"b5ae4792.3b8598","name":"","topic":"","payload":"6434,-1.50,kg,26.50,25.50,\\r\\n","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":1220,"wires":[["fdfe718f.714d4"]]},{"id":"fdfe718f.714d4","type":"csv","z":"b5ae4792.3b8598","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":",kg,,temp1,temp2","skip":"0","x":270,"y":1220,"wires":[["b0b29c1b.1f12e"]]},{"id":"b0b29c1b.1f12e","type":"debug","z":"b5ae4792.3b8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":1220,"wires":[]}]
0 Likes

#11

Thank you for pointing me in the right directoin. Im gonna need to read up on the CSV nodeto understand it better, cause I am not quite sure how to implement the example in a flow :slight_smile:

Edit: I gotta say though, that the ca day and a half I've been spending with node-red, has got me impressed! Its a very powerful and versatile piece of software, and I cant help to wonder about all the other projects I could use it for.

0 Likes

#12

I'd feed the serialport direct into the csv above.
You could also use a function to split them into individual messages if you want.
I'm not sure what the influx node needs though so...

[{"id":"2297eb8e.63fa74","type":"inject","z":"b5ae4792.3b8598","name":"","topic":"","payload":"6434,-1.50,kg,26.50,25.50,\\r\\n","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":1220,"wires":[["fdfe718f.714d4"]]},{"id":"fdfe718f.714d4","type":"csv","z":"b5ae4792.3b8598","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":",kg,,temp1,temp2","skip":"0","x":270,"y":1220,"wires":[["b0b29c1b.1f12e","20896c5.bce1f94"]]},{"id":"b0b29c1b.1f12e","type":"debug","z":"b5ae4792.3b8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":1200,"wires":[]},{"id":"20896c5.bce1f94","type":"function","z":"b5ae4792.3b8598","name":"","func":"\nvar msg1 = {topic:\"kg\", payload:msg.payload.kg};\nvar msg2 = {topic:\"temp1\", payload:msg.payload.temp1};\nvar msg3 = {topic:\"temp2\", payload:msg.payload.temp2};\nreturn [msg1,msg2,msg3];","outputs":3,"noerr":0,"x":400,"y":1260,"wires":[["5d20a53f.0d7abc"],["5d20a53f.0d7abc"],["5d20a53f.0d7abc"]]},{"id":"5d20a53f.0d7abc","type":"debug","z":"b5ae4792.3b8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":1260,"wires":[]}]

or to do same with just core nodes (no function node)

[{"id":"2297eb8e.63fa74","type":"inject","z":"b5ae4792.3b8598","name":"","topic":"","payload":"6434,-1.50,kg,26.50,25.50,\\r\\n","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":1220,"wires":[["fdfe718f.714d4"]]},{"id":"fdfe718f.714d4","type":"csv","z":"b5ae4792.3b8598","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":",kg,,temp1,temp2","skip":"0","x":270,"y":1220,"wires":[["2b77b16e.df18be","b0b29c1b.1f12e"]]},{"id":"2b77b16e.df18be","type":"split","z":"b5ae4792.3b8598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":390,"y":1220,"wires":[["f2059d22.9ea2b"]]},{"id":"b0b29c1b.1f12e","type":"debug","z":"b5ae4792.3b8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":410,"y":1160,"wires":[]},{"id":"f2059d22.9ea2b","type":"switch","z":"b5ae4792.3b8598","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"kg","vt":"str"},{"t":"eq","v":"temp1","vt":"str"},{"t":"eq","v":"temp2","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":510,"y":1220,"wires":[["5d20a53f.0d7abc"],["5d20a53f.0d7abc"],["5d20a53f.0d7abc"]]},{"id":"5d20a53f.0d7abc","type":"debug","z":"b5ae4792.3b8598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":1220,"wires":[]}]
1 Like

#13

Ive been looking into the csv doc and some youtube vids, without really getting a grasp on how I should configure the CSV node.

When I use it at default config, I do however see that the output is perhaps easier to use than straight from a serial out, in the form that the payload now is indexed with col1 col2 col3 col4 col5.

node-red-openscale%20flow-csv1

Can I ask where I should input the code example you write?

0 Likes

#14

Go to an empty area in your flow (or create a new tab) then copy the example (Ctrl+C or similar) then in node-red click on the hamburger menu in the top right and select Import > Clipboard then paste it into the window and click Import. Then use the mouse to position the flow where you want, and click to drop it.

0 Likes

#15

That worked beautifully :slight_smile: Im going to play around with this a while, experimenting with InfluxDb (which is also new to me) and Grafana.

This is going to end up being a weight for one of my beehives, if anyone wondered:-)

0 Likes

#16

Nice ! :honeybee:

Please post the results in "share your projects" when done (including a picture from the :honeybee:beehive :honeybee:)

0 Likes

#17

Ofc I will :wink:

0 Likes

#18

Update
Like I said Colin, your code suggestion worked very well. This is what I am currently at. Working on a problem with what I believe is the adding of data to influx. Whereas I cant seem to get anything to show up in grafana. But when checking the database itself there seems to be data there.

node-red-openscale%20flow-csv3
Editadded a screenshot of the perfect printout of 3 temperatures, 1 humidity and i kg.
Edit
2* fixed the grafana problem too :slight_smile: Ty all for the good help.

[{"id":"e232e416.406958","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"d8f2a3b8.a5513","type":"serial in","z":"e232e416.406958","name":"OpenScale","serial":"ccdd97ab.3693a8","x":240,"y":720,"wires":[["9a70f63c.189ad8"]]},{"id":"b841e4cb.b41898","type":"rpi-dht22","z":"e232e416.406958","name":"","topic":"rpi-dht22","dht":22,"pintype":"0","pin":4,"x":520,"y":300,"wires":[["96013ecd.27ab1","9b2624cd.0c0d28"]]},{"id":"4f3518ef.2226c8","type":"inject","z":"e232e416.406958","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":300,"wires":[["b841e4cb.b41898"]]},{"id":"96013ecd.27ab1","type":"function","z":"e232e416.406958","name":"tempread","func":"msg.payload = msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":720,"y":260,"wires":[["d506efdb.82ddc","95d372b7.dcfc2","d1f2d47.88b5628"]]},{"id":"9b2624cd.0c0d28","type":"function","z":"e232e416.406958","name":"humread","func":"msg.payload = msg.humidity\nreturn msg;","outputs":1,"noerr":0,"x":720,"y":340,"wires":[["b6ad82bb.8abdb","79a460a4.ae748","d1fcc6e2.667538"]]},{"id":"d506efdb.82ddc","type":"debug","z":"e232e416.406958","name":"dht22-temp","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":930,"y":220,"wires":[]},{"id":"b6ad82bb.8abdb","type":"debug","z":"e232e416.406958","name":"dht22-hum","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":930,"y":360,"wires":[]},{"id":"95d372b7.dcfc2","type":"ui_gauge","z":"e232e416.406958","name":"dht22-temp","group":"b7a43d33.940bb","order":0,"width":"6","height":"4","gtype":"compass","title":"dht22-temp","label":"c","format":"{{value}}","min":"10","max":"40","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":930,"y":300,"wires":[]},{"id":"79a460a4.ae748","type":"ui_gauge","z":"e232e416.406958","name":"dht22-hum","group":"f146c9c2.9a4b08","order":0,"width":0,"height":0,"gtype":"gage","title":"dht22-hum","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":930,"y":440,"wires":[]},{"id":"d1fcc6e2.667538","type":"influxdb out","z":"e232e416.406958","influxdb":"37636a80.ee4ca6","name":"dht22-hum","measurement":"dht22-hum","precision":"","retentionPolicy":"","x":930,"y":400,"wires":[]},{"id":"9a70f63c.189ad8","type":"csv","z":"e232e416.406958","name":"","sep":",","hdrin":"","hdrout":"","multi":"one","ret":"\\n","temp":",kg,,temp1,temp2","skip":"0","x":410,"y":720,"wires":[["9b6a450b.c07308","527ff6a7.780a78"]]},{"id":"9b6a450b.c07308","type":"split","z":"e232e416.406958","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":530,"y":720,"wires":[["8fe60e8e.57cd6"]]},{"id":"527ff6a7.780a78","type":"debug","z":"e232e416.406958","name":"csv output payload","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":570,"y":660,"wires":[]},{"id":"8fe60e8e.57cd6","type":"switch","z":"e232e416.406958","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"kg","vt":"str"},{"t":"eq","v":"temp1","vt":"str"},{"t":"eq","v":"temp2","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":650,"y":720,"wires":[["9562fa92.6175a8","7dff7bcf.fb9614","6a2ece8e.cd605"],["febbde8e.1c211","33595486.a31c9c","f904bbab.25b9d8"],["223fd5b6.5b020a","c0da5e7d.d845b","5b3dbc7d.ff1b14"]]},{"id":"9562fa92.6175a8","type":"debug","z":"e232e416.406958","name":"OpenScale-kg","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":940,"y":500,"wires":[]},{"id":"febbde8e.1c211","type":"debug","z":"e232e416.406958","name":"temp-onboardOS","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":950,"y":640,"wires":[]},{"id":"223fd5b6.5b020a","type":"debug","z":"e232e416.406958","name":"temp-ds18B20","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":940,"y":780,"wires":[]},{"id":"d1f2d47.88b5628","type":"influxdb out","z":"e232e416.406958","influxdb":"37636a80.ee4ca6","name":"dht22-temp","measurement":"dht22-temp","precision":"","retentionPolicy":"","x":930,"y":260,"wires":[]},{"id":"7dff7bcf.fb9614","type":"influxdb out","z":"e232e416.406958","influxdb":"37636a80.ee4ca6","name":"OpenScale-kg","measurement":"kg","precision":"","retentionPolicy":"","x":940,"y":540,"wires":[]},{"id":"33595486.a31c9c","type":"influxdb out","z":"e232e416.406958","influxdb":"37636a80.ee4ca6","name":"temp-onboardOS","measurement":"temp-onboardOS","precision":"","retentionPolicy":"","x":950,"y":680,"wires":[]},{"id":"c0da5e7d.d845b","type":"influxdb out","z":"e232e416.406958","influxdb":"37636a80.ee4ca6","name":"temp-ds18B20","measurement":"temp-ds18B20","precision":"","retentionPolicy":"","x":940,"y":820,"wires":[]},{"id":"6a2ece8e.cd605","type":"ui_gauge","z":"e232e416.406958","name":"","group":"f146c9c2.9a4b08","order":0,"width":0,"height":0,"gtype":"gage","title":"OpenScale-kg","label":"OpenScale-kg","format":"{{value}}","min":"-1","max":"5","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":940,"y":580,"wires":[]},{"id":"f904bbab.25b9d8","type":"ui_gauge","z":"e232e416.406958","name":"temp-onboardOS","group":"f146c9c2.9a4b08","order":0,"width":0,"height":0,"gtype":"gage","title":"temp-onboardOS","label":"C","format":"{{value}}","min":"-1","max":"50","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":950,"y":720,"wires":[]},{"id":"5b3dbc7d.ff1b14","type":"ui_gauge","z":"e232e416.406958","name":"temp-ds18B20","group":"f146c9c2.9a4b08","order":0,"width":0,"height":0,"gtype":"gage","title":"temp-ds18B20","label":"C","format":"{{value}}","min":"-1","max":"50","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":940,"y":860,"wires":[]},{"id":"ccdd97ab.3693a8","type":"serial-port","z":"","serialport":"/dev/ttyUSB0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false,"responsetimeout":"10000"},{"id":"b7a43d33.940bb","type":"ui_group","z":"","name":"Default","tab":"32e61df0.efb002","disp":true,"width":"6","collapse":false},{"id":"f146c9c2.9a4b08","type":"ui_group","z":"","name":"Default","tab":"32e61df0.efb002","disp":true,"width":"6","collapse":false},{"id":"37636a80.ee4ca6","type":"influxdb","z":"","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"TEMPERATURER","name":"influxDBimport","usetls":false,"tls":""},{"id":"32e61df0.efb002","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
0 Likes

#19

The example flow was actually from @dceejay, not me, but I am happy to receive some of the reflected glory :slight_smile:

2 Likes