Formatting serial data from arduino in node-red

2,252 views
Skip to first unread message

Dan Hoover

unread,
Apr 13, 2017, 10:46:33 AM4/13/17
to Node-RED
I have an arduino that is connected to 15 ds18b20 temperature sensors and is sending the data to my pi (running node red) via serial.

I've been able to deal with this in the past by just grabbing the {{{payload}}} when it was only one sensor, but now that I have 15 of them, I'm not sure how I can grab the data and know what data I'm grabbing.  For simplicity's sake, let's say that my arduino is sending data that looks like this...

indoorTemp: 70
outdoorTemp:36

How would I parse that data?

Or should I format the data differently to begin with?  (like give each sensor a 3 digit code and skip those digits when I'm looking for the temp itself).  I'm just having a brain cramp here and appreciate how helpful you all have been.  

Dan Hoover

unread,
Apr 13, 2017, 10:56:31 AM4/13/17
to Node-RED
For a little more context, I'll be doing some math (figuring out the BTUs that our boiler is putting out, etc) and sending that data off to a webserver.  

So I need to be able to use global.set and global.get on this data with the indoorTemp being the variable and the 70 being the value.

Zenofmud

unread,
Apr 13, 2017, 11:07:16 AM4/13/17
to node...@googlegroups.com
How is the arduino sending the data? Your code in the arduino will  determine what is sent to node red. Is each of the 15 ds18b20’s going to send a message or will they all be sent as one chunk? 

Have you thought of using MQTT? Send each device as a separate MQTT message.

On Apr 13, 2017, at 10:56 AM, Dan Hoover <mud...@gmail.com> wrote:

For a little more context, I'll be doing some math (figuring out the BTUs that our boiler is putting out, etc) and sending that data off to a webserver.  

So I need to be able to use global.set and global.get on this data with the indoorTemp being the variable and the 70 being the value.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/006af617-52d0-4cf7-b743-95259467e562%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

steve rickus

unread,
Apr 13, 2017, 11:19:27 AM4/13/17
to Node-RED
Or set up the serial node to split on newlines, read each line and send the plain text to a function or change node that pulls out the name of the sensor and its value, something like this (untested):

var (sensor, value) = msg.payload.split(":");
msg
.topic = sensor.trim();
msg
.payload = value.trim();
return msg;

So each line of output from the arduino (e.g. "indoorTemp:23.6") ends up as a simple msg (e.g. { "topic": "indoorTemp", "payload": "23.6" }) in your flow.

Dan Hoover

unread,
Apr 13, 2017, 11:32:46 AM4/13/17
to Node-RED
Steve. I think that's what I'm looking for on the splitting part, but that first line is saying,

expected identifier saw (

Dan Hoover

unread,
Apr 13, 2017, 11:36:22 AM4/13/17
to Node-RED
Paul. I absolutely believe that mqtt would solve a bunch of problems for me, but I just don't "get" it.  I don't know how to use it in my context.  I'm in rural alaska so I'm kind of on my own for figuring this stuff out. I'd be happy to pay for someone's time to do a half hour hangout or something to show me what I'm missing.  It there's just a gap in my brain with that.  It's like I know it's the answer to a the problems, but after watching a few dozen youtube videos...I still don't have the whole picture of how to use it in a practical sense.

Rant over haha. 

As far as the arduino data, it's all being formatted on the arduino and concatenated there with a Serial.println for each sensor.  So I'm getting a burst of serial data with each sensor on its own line and as its own message if that makes sense.

steve rickus

unread,
Apr 13, 2017, 11:39:45 AM4/13/17
to Node-RED
Oh, sorry, that new ES6 syntax is not going to work everywhere -- try this instead:

var parts = msg.payload.split(":");
msg
.topic = parts[0].trim();
msg
.payload = parts[1].trim();
return msg;

steve rickus

unread,
Apr 13, 2017, 11:49:55 AM4/13/17
to Node-RED
As long as you are in control of the data coming from the arduino, you may want to output an actual JSON string instead, like:

{ "timestamp": 1492098238503, "device": "Arduino-1", "indoorTemp": 23.6, "outdoorTemp": 16.7, ...etc... }

Then you can just pass the output from the serial node into a JSON node, and the msg will contain all the information in one object. That one msg can then be sent to many outputs, each of which can use the values by referring to them by name (e.g. msg.payload.indoorTemp )

Plus, knowing the originating device and timestamp can be very useful for charting, or persisting to a database...

Dan Hoover

unread,
Apr 13, 2017, 11:57:27 AM4/13/17
to Node-RED
STEVE! That is PERFECT! Thank you so much. I was playing with the trim function, but I would not have come up with that solution on my own.  Simple. Elegant. Dynamic. Perfect.  Thank you so much!

Dan Hoover

unread,
Apr 13, 2017, 11:58:24 AM4/13/17
to Node-RED
Just to be clear...you're saying to create the json string on the arduino itself and then parse it on the pi, right?

steve rickus

unread,
Apr 13, 2017, 12:12:46 PM4/13/17
to Node-RED
If node-red is running on the pi, then yes, that's what I was thinking. You have to "serialize" your data in some way, even in your original example of multiple lines separated by "\n" with each line being "key: value". So if the programming on the pi has a convenient way to build a json string, I would use that to transfer the information. That way, the JSON parsing to msg object happens in the flow...

Dan Hoover

unread,
Apr 13, 2017, 12:17:48 PM4/13/17
to Node-RED
Yep. That sounds great. Thank you again!

Dave C-J

unread,
Apr 13, 2017, 1:01:51 PM4/13/17
to node...@googlegroups.com
There is a library available for Arduino to help build json which I find useful to save working out all the float to string conversions etc.

sent from phone

Dan Hoover

unread,
Apr 13, 2017, 3:01:02 PM4/13/17
to Node-RED
Awesome. Thank you. 
Reply all
Reply to author
Forward
0 new messages