Seperate out Sensor Data from Arduino over serial

1,774 views
Skip to first unread message

Stuart Llewellyn

unread,
Feb 3, 2017, 7:22:00 PM2/3/17
to Node-RED
Hi all, started playing with Node RED few days ago for an IoT project. 

I've got 2 sensors sending data from an Arduino over serial. Successful set up and getting a string of data from debug. After spending some time searching around, closes I've got is this guide. Where it looks overly complicated to recognise different characters thats been sent. 


I just want to be able to isolate out the values seperated by a tab or comma coming from the Arduino, so I can;

a) make a local data log
b) make a nice looking local dashboard

Any advice much appreciated, as I'm finding intro level guides for Node RED few and far between. 

Regards
Stuart

Luis Montes

unread,
Feb 3, 2017, 7:29:35 PM2/3/17
to node...@googlegroups.com
Hi Stuart,

Have you looked at either the Arduino nodes or node-red-contrib-gpio ?
If you put StandardFirmta.ino on the arduino, you don't have to parse anything, you'll just a feed of analog input values.  Firmata basically encodes the stream with meta data similar to the old MIDI format.  You just configure which pins you want in node-red and you'll get data events.

-Luis


--
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+unsubscribe@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/fab53542-46b7-4dfc-ac3c-9b9c5953093a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stuart Llewellyn

unread,
Feb 3, 2017, 7:32:56 PM2/3/17
to node...@googlegroups.com
Hi sorry context, I'm doing this on a rasberry pi which will be deployed in the field as a remote monitor. 

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/XCm7IUn0Txc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Dave C-J

unread,
Feb 3, 2017, 7:37:46 PM2/3/17
to node...@googlegroups.com
Stuart,

if they are already coming over as comma separated - then the CSV node ought to help.
You can give it a template of labels and it will assign each value to the column label and then send out a javascript object of them all.
You can then use several change nodes in parallel - each one picking out an individual property from the incoming msg. eg... move msg.payload.temperature to msg.payload.
At the same time add an appropriate msg.topic -  set msg.topic = "temperature";
Then feed each of those into a ui_gauge node or whatever from node-red-dashboard.

You could of course just log the csv values - using a file node.

Hope this gets you going a bit further. 

Stuart Llewellyn

unread,
Feb 6, 2017, 7:58:56 PM2/6/17
to node...@googlegroups.com
Hi Luis,

Thanks for this suggestion, I tried implementing this, but having issues Dave CJ is assisting me in another thread. This method looked simple to implement but I have Arduino codedeveloped which takes the ADC figure at gives meaningful data, it/s for off grid wind turbines so, voltage current, power and wind speed.

Would mean redoing all that but, I would consider it. Certainly happy to play with it, currently got just a LRD and Pot and sending the ADC figure of serial.

Dave C-J: Thanks for your comments (add help on other thread) This was the kind of thing I had in mind, just didn't know how to go about it. I plan on finishing the tutorials on noderedguide.com hopefully that'll help me learn about columns and change nodes. If Im struggling I may ask for guidance. Hope thats ok.

Regards

Stu

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/XCm7IUn0Txc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Dave C-J

unread,
Feb 7, 2017, 2:22:28 AM2/7/17
to node...@googlegroups.com
Stuart, we need to decide which way to go. if you have programmed up the Arduino and it is sending data via serial just fine then there is no need to struggle with the Arduino node (in other thread). If not and you want to use firmata then we will continue the investigation.

Stuart Llewellyn

unread,
Feb 7, 2017, 10:34:26 AM2/7/17
to node...@googlegroups.com
Point taken, for this specific project. But I'd love to be able to at least experiment with the Arduino node as it looks very powerful and can see it being very useful for other projects within our institute. 

Also documenting the steps to resolve on the forum would help others if they run into the same problem.

But I will continue trying to separate out the values being send over serial.

Thanks for your help this far. 

On Tue, Feb 7, 2017 at 2:22 AM, Dave C-J <dce...@gmail.com> wrote:
Stuart, we need to decide which way to go. if you have programmed up the Arduino and it is sending data via serial just fine then there is no need to struggle with the Arduino node (in other thread). If not and you want to use firmata then we will continue the investigation.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/XCm7IUn0Txc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Dave C-J

unread,
Feb 7, 2017, 11:57:00 AM2/7/17
to node...@googlegroups.com
Ok - no probs - will keep on keeping on.

Stuart Llewellyn

unread,
Feb 7, 2017, 5:41:11 PM2/7/17
to node...@googlegroups.com
Hi all, 

I've spent the afternoon going through the examples on noderedguides.com, unfortunately no comments are allowed on the page. Where would people suggest I can give some feedback, and also get some help with these tutorials? Thats a side thing.

I've found various intrsuctables / guides using serial input, but they are only ever sending one bit of data over serial. I'd really appreciate it if anyone can guide me on how to seperate out the analogue inputs of the arduino, or if they've come across a page where it explains in a little bit more detail.

Thanks very much
Stu

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/XCm7IUn0Txc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Dave C-J

unread,
Feb 7, 2017, 5:43:49 PM2/7/17
to node...@googlegroups.com
So what arrives over serial at the moment ? Can you show us what turns up in debug ?
And what do you want it to be like ?

Stuart Llewellyn

unread,
Feb 8, 2017, 4:25:28 PM2/8/17
to node...@googlegroups.com
At the moment I'm playing with a simple prototype set up to get used to Node RED, that takes an LDR and Pot Resistor sensor into the Analogue inputs, and I'm sending the the ADC value from 0 -1023 as a float.

Here's the Arduino code;
void setup() {
  // start serial comms at 9600 bits per second
  Serial.begin(9600);
}


//loop routine keeps running over and over
void loop () {
  // Wait a few seconds between measurements
  delay(2000);
 
  // read input of analogue pin 0
  float LDRValue = analogRead(A0);
  float PotValue = analogRead(A1);
  // print out the value to Serial Monitor
    Serial.print(LDRValue);
    Serial.print(","); // introduces comma between data
    Serial.print(PotValue);
   Serial.println(" "); //creates new line in serial
   
 
}

Here's what comes over in the debug;

08/02/2017, 16:20:22node: Arduino Serial Debugmsg.payload : string[16]
"389.00 ↵233.00,"

I'd like to be able to seperate them out so I can use the individual values on a dashboard. Also add a time stamp to create a local log file, but I'm envisioning those a seperate process, potentially joining the string of variables back together for csv logging.

Many thanks
Stu


On Tue, Feb 7, 2017 at 5:43 PM, Dave C-J <dce...@gmail.com> wrote:
So what arrives over serial at the moment ? Can you show us what turns up in debug ?
And what do you want it to be like ?

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/XCm7IUn0Txc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Dave C-J

unread,
Feb 8, 2017, 5:08:21 PM2/8/17
to node...@googlegroups.com
Hi
that looks slightly odd - "389.00 ↵233.00,"
(you don't need the space in the println)
The Serial node should have split on \n so the data should look like
233.00,389.00
(you could try \r instead)
Then you can use function with something like below (and set to have 2 outputs)
var m1 = {topic: "ldr", payload: msg.payload.split(",")[0]};
var m2 = {topic: "pot", payload: msg.payload.split(",")[1]};
return [m1, m2];

should provide two outputs - each with a topic and the correct payload.


Stuart Llewellyn

unread,
Feb 9, 2017, 3:04:03 PM2/9/17
to node...@googlegroups.com
Hi,

After a little while of not much success.... the fact that the serial message looks a little odd "389.00 ↵233.00," Is the reason I can't split the variable out.

I've tested your function code by just inject a string of two numbers, and code works fine, seperating them out.

I've checked the serial monito on the Arduino IDE, and it outputs as expected;

1023.00,567.00

My Serial In node has the following setup;

Serial Port : /dev/tty/ACM0

Baud Rate :  9600 (same as Arduino)
Data Bits :    8 (not been set on Arduino)
Parity           None (not been set on Arduino)
Stop Bits     1 (not been set on Arduino)

Input
Split Input: on the character ,
and deliver ascii strings

Output add split charactet to output messages not enabled

Any clues on what might be introducing the ↵??

Thanks
Stu

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/XCm7IUn0Txc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Colin Law

unread,
Feb 9, 2017, 4:08:43 PM2/9/17
to node...@googlegroups.com
You are saying split on the ',' which is exactly what it is doing, so
in each message you the second number then newline then first number
and then the comma which splits it. I think you want to split on the
newline character instead, then you will get number,number

Colin

On 9 February 2017 at 20:04, Stuart Llewellyn
>> 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/CACXWFw%2BBWQKK6WAuryMMTxJR-Q7r0ua42RGZ6yhPjxkDs6RP8w%40mail.gmail.com.
>>
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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/CAAHKbAkm7K-GeDYCmjW4M0dSR0KB26cG_STG%3Dtg8Ar%3DnO_CoQw%40mail.gmail.com.

steve rickus

unread,
Feb 9, 2017, 4:14:30 PM2/9/17
to Node-RED
That usually represents the 'carriage return' ascii char 0x13 -- it appears from your code that the println(" ") function call is appending the Windows-style C/R + N/L char sequence to your serial output. Very odd that a linux-based system would do that, so I'm guessing there is some serial option that enables that behavior?

As recommended in this post, the simplest thing to do is replace that line with print("\n") as Dave suggested earlier.
--
Steve

Stuart Llewellyn

unread,
Feb 9, 2017, 4:48:05 PM2/9/17
to node...@googlegroups.com
Thanks you for pointing out my mistake. In the serial setup I am now seperating with \n new line, and I can seperate the payloads out.

 Thanks everyone who has assisted. Now for some dashboards!


>> 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/CACXWFw%2BBWQKK6WAuryMMTxJR-Q7r0ua42RGZ6yhPjxkDs6RP8w%40mail.gmail.com.
>>
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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

> 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
--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/XCm7IUn0Txc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.

rutuja mhatre

unread,
Mar 5, 2018, 8:25:57 AM3/5/18
to Node-RED
Hi
I have been trying the same project with 4 analogue inputs and 1 led output. 
I have connected ADXL335 and potentiometer to Arduino Uno and sending data serially to RPi. The data is then sent to ubidots cloud through MQTT. Also the led is been controlled from ubidots cloud.
In my case, one of the accelerometer analogue pin and pot is working fine i.e. sending values on cloud. But all other values can be seen on node-red debug terminal.
On node-red flow :
serial in- function node(4 outputs)-JSON node(each)- MQTT out(1 for each analogue out)
 
Will the same work in node-red function with 4 outputs?
My arduino code:
int a;
const int ledPin = 8;
const int xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)
const int pot= A0;
String x,y,z="";
String outvalue="";
int xout,yout,zout,potvalue=0;
int flag=0;


void setup() {
  pinMode(ledPin,OUTPUT);
  pinMode(xpin,INPUT);
  pinMode(ypin,INPUT);
  pinMode(zpin,INPUT);
  pinMode(pot,INPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available()>0)
  { 
    a=Serial.read();
    a=a-48;
    if (a==1)
    {
      digitalWrite(ledPin,HIGH);
      Serial.println("Led ON");
    }
    if(a==0)
    {
      digitalWrite(ledPin,LOW);
      Serial.println("Led OFF");
    }
  }
else {

  potvalue= analogRead(pot);
  outvalue = map(potvalue, 0, 1023, 0, 255);  
 
  x = analogRead(xpin);

  y=analogRead(ypin);

  z=analogRead(zpin);
  
 String one=(outvalue+","+x+","+y+","+z);
 Serial.println(one);
  delay(10000);
}
}

Jan Golcak

unread,
Mar 29, 2018, 12:57:25 PM3/29/18
to Node-RED

Hi,
Arduino code:
void setup() {
 Serial.begin(9600);

}

void loop() {
 
  float ldr = analogRead(A0);
  float pot = analogRead(A1);
 Serial.print(ldr);
 Serial.print(",");
 Serial.print(pot);
 Serial.println(" ");
 
 
delay(2000);
}

Node-red:
function ldr:
var m1 = {topic: "ldr", payload: msg.payload.split(",")[0]};
return [ [ m1] ];

function pot:


var m2 = {topic: "pot", payload: msg.payload.split(",")[1]};
return [ [ m2] ];


code:

[{"id":"2c9a74dd.5d3dac","type":"serial in","z":"82712492.c3f68","name":"","serial":"6cc47d0d.1234b4","x":541.36669921875,"y":225.25,"wires":[["de63d92.78af228","c6ec3fa0.745ef","34d8d9d7.6ac96e"]]},{"id":"de63d92.78af228","type":"debug","z":"82712492.c3f68","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":715.36669921875,"y":103.5,"wires":[]},{"id":"c6ec3fa0.745ef","type":"function","z":"82712492.c3f68","name":"","func":"var m1 = {topic: \"ldr\", payload: msg.payload.split(\",\")[0]};\n\nreturn [ [ m1] ];\n","outputs":1,"noerr":0,"x":826.36669921875,"y":276.75,"wires":[["48b07d02.e235bc","c9052fa2.04756"]]},{"id":"48b07d02.e235bc","type":"debug","z":"82712492.c3f68","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":987.36669921875,"y":345.5,"wires":[]},{"id":"d294db9e.221578","type":"debug","z":"82712492.c3f68","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1206.36669921875,"y":100.75,"wires":[]},{"id":"34d8d9d7.6ac96e","type":"function","z":"82712492.c3f68","name":"","func":"\nvar m2 = {topic: \"pot\", payload: msg.payload.split(\",\")[1]};\nreturn [ [ m2] ];","outputs":1,"noerr":0,"x":831.36669921875,"y":191.75,"wires":[["d294db9e.221578","19e13d72.be8b5b"]]},{"id":"19e13d72.be8b5b","type":"ui_gauge","z":"82712492.c3f68","name":"","group":"5df90361.eca4bc","order":0,"width":0,"height":0,"gtype":"gage","title":"Gauge","label":"units","format":"{{value}}","min":0,"max":"500","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":1316.36669921875,"y":191.25,"wires":[]},{"id":"c9052fa2.04756","type":"ui_gauge","z":"82712492.c3f68","name":"","group":"5df90361.eca4bc","order":0,"width":0,"height":0,"gtype":"gage","title":"Gauge","label":"units","format":"{{value}}","min":0,"max":"500","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":1326.36669921875,"y":309.25,"wires":[]},{"id":"6cc47d0d.1234b4","type":"serial-port","z":"","serialport":"COM5","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false},{"id":"5df90361.eca4bc","type":"ui_group","name":"Group 1","tab":"da032460.86c1b","order":1,"disp":true,"width":6},{"id":"da032460.86c1b","type":"ui_tab","name":"Tab 1","icon":"dashboard","order":1}]






Dňa sobota, 4. februára 2017 1:22:00 UTC+1 Stuart Llewellyn napísal(-a):
Reply all
Reply to author
Forward
0 new messages