Print and Println work differently with Node-Red

Hi everybody,

I hope I make this post to the right place !
I just started to use Arduino for fun today, and I don't fully understand something.

I try to send a message with Node-red to my Arduino Uno, and when it receives the message, the Arduino has to send me it back.

So I tried this :

String incomingText = ""; 

void setup() {
  Serial.begin(9600); 
}

void loop() {
  if (Serial.available() > 0) {
    incomingText = Serial.readString();   
    Serial.println("I received: " + incomingText);
  }
}

It works like a charm ! I can see my message in the Node-red debug console.

But, when I try to change "Serial.println" with "Serial.print", I can't see anything and I really don't get it why... Any ideas ?

Thanks !

println() adds a trailing newline '\n' after the message. You have to make sure the sender and receiver both want/expect that to be the line terminator.

Oh sorry, I think I just found why what was going "wrong" !
Node-red was waiting for a splint input that was by default ... "\n" !

Sorry for the useless post guys !

EDIT : Thanks for the answer blh64 ! You were faster than me ! :smiley: Have a nice day !

just for completeness println() actually adds both '\r' (carriage return) and '\n' (new line) to whatever you printed

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.