send ultrasonic value from arduino to raspberry pi running Node-red

235 views
Skip to first unread message

Stijn

unread,
May 1, 2017, 9:40:52 AM5/1/17
to Node-RED
Hi guys,

new on this forum, and no luck yet finding a solution for my (simple) problem.

Setup: ultrasonic sensor connected to arduino nano sending a serial value via USB to a raspberry running Node-red.

arduino code:


#include <NewPing.h>

#define TRIGGER_PIN  8  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     7  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 350 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

int test1 = 123;
double test2 = 456;
float test3 = 789;

void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(5000);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.println("Sonar");
  Serial.println(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("test123");
  Serial.println(test1);
  Serial.println(test2);
  Serial.println(test3);
}

Node red



probably I need to implement some kind of conversion but nu luck so far.
can somebody help me out?

thanks,

Stijn


Auto Generated Inline Image 1

Colin Law

unread,
May 1, 2017, 10:29:43 AM5/1/17
to node...@googlegroups.com
Which bit isn't working? You seem to be getting a value of 4 back if I interpret your code correctly.

Colin

--
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/ca46055b-d1bc-42fd-873c-a996999c5913%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stijn

unread,
May 1, 2017, 10:53:17 AM5/1/17
to Node-RED
the 4 value that is displayed in node-red is not the correct value. The correct value is something like 110 or around that.
while the integer, double and float values are received correctly, the sonar.ping_cm() is not.

when I open the serial monitor this is what I get:

Sonar
108   --> displayed in node red as 4??
test123
123
456.00
789.00

Hope this was more usefull :)

thanks in advance!



Op maandag 1 mei 2017 16:29:43 UTC+2 schreef Colin Law:
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Colin Law

unread,
May 1, 2017, 4:40:33 PM5/1/17
to node...@googlegroups.com
I am having difficulty seeing how 108 gets converted to "4".
Perhaps you could try converting the result of sonar.ping_cm() to a string before printing it, unless someone can see what the fundamental problem is.

Colin

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.

Stijn

unread,
May 1, 2017, 5:46:00 PM5/1/17
to Node-RED
thanks for your reply,

updated code:


#include <NewPing.h>

#define TRIGGER_PIN  8  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     7  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

int test1 = 0;


void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(5000);                   
 
  test1 = sonar.ping_cm();
  Serial.println(test1);
  String stringone = String(test1);
  String stringtwo = String(test1,DEC);
  Serial.println("test1");
  Serial.println(test1); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("stringone");
  Serial.println(stringone);
  Serial.println("stringtwo");
  Serial.println(stringtwo);
 
}


serial monitor arduino connected to pc:


test1
118
stringone
118
stringtwo
118


Node red debug screen on Raspberry pi



no doubt I'm doing something wrong, but I can't figure it out.
Anyone any idea?

Thanks!







Op maandag 1 mei 2017 22:40:33 UTC+2 schreef Colin Law:
Auto Generated Inline Image 1

Stijn

unread,
May 5, 2017, 11:56:57 AM5/5/17
to Node-RED
No one able to help me out?



Op maandag 1 mei 2017 23:46:00 UTC+2 schreef Stijn:

Colin Law

unread,
May 5, 2017, 5:23:16 PM5/5/17
to node...@googlegroups.com
On 1 May 2017 at 22:46, Stijn <stijn.b...@gmail.com> wrote:
 ...            
 
  test1 = sonar.ping_cm();
  Serial.println(test1);
  String stringone = String(test1);

What happens if you replace this with
String stringone = "118";

Colin


Message has been deleted

Stijn

unread,
May 7, 2017, 5:15:59 PM5/7/17
to Node-RED
118 is displayed correctly, both on serial monitor and in node red.

apparantly the sonar.ping value is a "long unsigned int", tried to look up how to convert it but didn't find any working example.

maybe that helps you pointing me in the right direction?

thanks in advance!

Stijn




Op vrijdag 5 mei 2017 23:23:16 UTC+2 schreef Colin Law:
Auto Generated Inline Image 1

steve rickus

unread,
May 8, 2017, 10:45:44 AM5/8/17
to Node-RED
From what I've seen in the forums, the Serial.println() function does not handle some of the larger 32/64 bit data types as you would expect.
Since your data range in small (0 - 200 it seems?) the output from the ping function should fit in an int. Have you tried casting it as such?

  test1 = (int) sonar.ping_cm();
 
Serial.println(test1);

Of course, this would truncate any decimal precision, but that may not be an issue for this use case. But if the received value is still "4" then there are other issues with the connection, I guess...

Colin Law

unread,
May 8, 2017, 11:07:27 AM5/8/17
to node...@googlegroups.com
Their must be a way of converting a long unsigned int to a string.
But this is not a node-red issue of course.

Colin
> --
> 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/ea6160c3-0e49-444f-9418-c5f1304c4cb1%40googlegroups.com.

mark hubrich

unread,
May 8, 2017, 2:04:14 PM5/8/17
to Node-RED
I don't understand why it needs to be a string. To get the most use of Node-Red wouldn't number work better? For example the smooth node won't work on string. 

I can post a flow I'm messing with when I get home. It's running on RasPi3 with Arduino Mega hooked to it via usb. Aside from the library routine to get data from sonar, it is pretty straight forward sending lots of sensor data just by serial.print. The easiest way I have found is to...

Serial.print(var1);
Serial.print(", ");
Serial.print(var2);
Serial.print(", ");
Serial.print(var3);
Serial.print(", ");
Serial.println(var4);


In node-red I use the CSV node to name the values. It is way easier than doing it on arduino. 

Mark 
Reply all
Reply to author
Forward
0 new messages