Using JSON object to send data from arduino by serial to node red

1,870 views
Skip to first unread message

timothym...@gmail.com

unread,
May 7, 2018, 9:23:07 PM5/7/18
to Node-RED
Am I on the right track to think that the correct way to output constantly changing sensor data (say 10 sensors max) from my Arduino to node-red running on the Rasp Pi is by way of generating a JSON object on the Arduino?  Will this be a fast transfer of data from the Arduino to the Pi?  Can I then use the data received by node red to move gauges on the dashboard, trigger warnings, etc.?

Does node red have to request the data from the Arduino?  Or will I be setting the Arduino to just serial stream the JSON formatted objects as fast as they can be generated?

timothym...@gmail.com

unread,
May 7, 2018, 11:02:41 PM5/7/18
to Node-RED
Is it possible to parse the Arduino output with node red if I simply format the serial output to be something like:

{"leftDistance":4, "centreDistance":0} etc?


On Monday, May 7, 2018 at 8:23:07 PM UTC-5, timothym...@gmail.com wrote:

timothym...@gmail.com

unread,
May 8, 2018, 1:05:52 PM5/8/18
to Node-RED
I have tried the following flow, but get no output.  The debug shows error messages.

I wish there was a way to freeze the debug output so I could read it or copy it.

The debug of the JSON node says: unexpected token { in JSON at position 38

The debug output from com4 moves by so quickly I can't read it.  But it seems to just show the data, {"leftDistance": 175, "centerDistance": 0}

I am trying the following in the function node, based on an example, but clearly I am screwing things up

var leftDistance = msg.payload[0].leftDistance;
msg.payload = leftDistance;
return msg;

Below is my flow.

[{"id":"5eca83b9.0365dc","type":"serial in","z":"4284f9c0.bf64b8","name":"","serial":"c8d31ac4.444c58","x":200.625,"y":423.93749237060547,"wires":[["95d95f96.7fce1","d54e539f.1df66","c9d00bf4.cfc0f8"]]},{"id":"95d95f96.7fce1","type":"ui_text","z":"4284f9c0.bf64b8","group":"e35c4135.063af","order":0,"width":0,"height":0,"name":"","label":"text output serial","format":"{{msg.payload}}","layout":"row-spread","x":508.125,"y":429.18750762939453,"wires":[]},{"id":"d54e539f.1df66","type":"json","z":"4284f9c0.bf64b8","name":"","property":"payload","action":"","pretty":false,"x":387.5,"y":476.45001220703125,"wires":[["ca37edab.a4253"]]},{"id":"c9d00bf4.cfc0f8","type":"debug","z":"4284f9c0.bf64b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":328.5,"y":598.2999877929687,"wires":[]},{"id":"ca37edab.a4253","type":"function","z":"4284f9c0.bf64b8","name":"","func":"var leftDistance = msg.payload[0].leftDistance;\nmsg.payload = leftDistance;\nreturn msg;\n","outputs":1,"noerr":0,"x":543.5,"y":488.45001220703125,"wires":[["508ad3d9.982fec"]]},{"id":"508ad3d9.982fec","type":"ui_text","z":"4284f9c0.bf64b8","group":"e35c4135.063af","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":682.5,"y":487.3499755859375,"wires":[]},{"id":"c8d31ac4.444c58","type":"serial-port","z":"","serialport":"COM4","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":true},{"id":"e35c4135.063af","type":"ui_group","z":"","name":"serial to arduino","tab":"e5eacb1c.ee1f78","disp":true,"width":"23","collapse":false},{"id":"e5eacb1c.ee1f78","type":"ui_tab","z":"","name":"Test serial arduino to pi","icon":"dashboard","order":3}]

Mark Setrem

unread,
May 8, 2018, 1:20:59 PM5/8/18
to Node-RED
You could send the debug data to the console log.

You can also use the delay node before the debug to limit the data going to the debug node.

Zenofmud

unread,
May 8, 2018, 3:46:25 PM5/8/18
to node...@googlegroups.com
You could also add an inject node to inject the data manually for testing....

Paul

On May 8, 2018, at 1:20 PM, Mark Setrem <mse...@gmail.com> wrote:

You could send the debug data to the console log.

You can also use the delay node before the debug to limit the data going to the debug node.

--
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 an 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/49d685ee-c860-49f3-b895-d8aeeb3471f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

AIOT MAKER

unread,
May 8, 2018, 5:17:08 PM5/8/18
to Node-RED
Hi,

I simulated (successfully) your scenario, using below coding:

Node-RED:

[{"id":"e3ba257a.120ef8","type":"json","z":"4c0723b.de3d7dc","name":"","property":"payload","action":"obj","pretty":false,"x":352.875,"y":285.5125198364258,"wires":[["f9543ad4.f93d18"]]},{"id":"f9543ad4.f93d18","type":"function","z":"4c0723b.de3d7dc","name":"","func":"msg.payload = msg.payload.leftDistance\nreturn msg;\n","outputs":1,"noerr":0,"x":508.875,"y":297.5125198364258,"wires":[["27790e3f.f74692","6ca735e3.1dfcac"]]},{"id":"27790e3f.f74692","type":"ui_text","z":"4c0723b.de3d7dc","group":"6f87ba10.1c0e04","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":705.8750076293945,"y":293.41247940063477,"wires":[]},{"id":"6ca735e3.1dfcac","type":"debug","z":"4c0723b.de3d7dc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":722,"y":379,"wires":[]},{"id":"9114e33e.d251f","type":"serial in","z":"4c0723b.de3d7dc","name":"","serial":"250124fe.5ce29c","x":163.10000610351562,"y":275,"wires":[["e3ba257a.120ef8"]]},{"id":"6f87ba10.1c0e04","type":"ui_group","z":"","name":"serial to arduino","tab":"acc2f535.4268e8","disp":true,"width":"23","collapse":false},{"id":"250124fe.5ce29c","type":"serial-port","z":"","serialport":"COM7","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false},{"id":"acc2f535.4268e8","type":"ui_tab","z":"","name":"Test serial arduino to pi","icon":"dashboard","order":3}]


Arduino:

void setup() {

  Serial.begin(9600);

  }

void loop() {
 delay(1000);
 Serial.println("{\"leftDistance\": 175, \"centerDistance\": 0}");

}



What I suspect:

1- The code in your function node is not working as you expect. Try using the one below:

msg.payload = msg.payload.leftDistance
return msg;



2- Revise how you configured the serial node (in Node-Red of course)  to split the flow coming from Arduino. I tested using /n . I am not sure if Arduino adds /r/n or just /n when using the statement Serial.println(). If you are seeing endless msgs in the debug side pane it is likely that you are splitting the flow in each and every character from the stream.






timothym...@gmail.com

unread,
May 8, 2018, 6:34:38 PM5/8/18
to Node-RED
Thank you so much.  It works!

I used your flow, and had to tweak the Arduino code to:


   Serial.print ("{\"leftDistance\":");
   Serial.print (cm[0]);
   Serial.print (",");
   Serial.print ("\"centerDistance\":");
   Serial.print (cm[1]);
   Serial.println ("}");

timothym...@gmail.com

unread,
May 8, 2018, 9:26:37 PM5/8/18
to Node-RED
Beautiful.  I have it running 4 sr04 ping sensors using arduino's newping library, they feed the data into variables cm[0] to cm[3] and node red is displaying nice accurate, quickly updated and consistent distance values.  I am ready to abandon firmata on the Arduino to interface with node-red.  I have code on the Arduino that parses the incoming serial for the pin number and value to be written as well for controlling servos.   Some functions, like running sr04 ultrasonic distance sensors seems to run much better on the Arduino than on the Pi.  I'm getting 3x the range on the same sensors on the Arduino, and very little junk data.  
Reply all
Reply to author
Forward
0 new messages