softSerial.readStringUntil between Arduino and ESP Problem

i can't seem to get this figured out, I'm using an esp-01 serial to send data to an Arduino nano with software serial on the nano's side to receive the data, however when I do a comparison against the string sent I cant get the if statement to proceed, even thou the string are the same.

any help is appreciated.

both set to baud 9600

ESP code:

Serial.println("foo");
delay(500);

Arduino Code

#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11);

String SendMeData = "foo";
bool gotdata = false;
void setup() {
  Serial.begin(115200);
  softSerial.begin(9600);
void loop() {
  
    while (gotdata == false) {
     if(softSerial.available() > 0){
     String datafromESP = softSerial.readStringUntil('\n');
     }
      if (firstData == SendMeData) Serial.println("Rcd Foo data... do something"); gotdata = true;
      
     //delay(500);
     Serial.println("Waiting for ESP");
     Serial.println(datafromESP); //debug info
     Serial.println(SendMeData); //debug info 
    }
  }

if I change

String datafromESP = softSerial.readStringUntil('\n');

to

String datafromESP = Serial.readStringUntil('\n');

and input foo via the serial monitor it works as should.

where am i going wrong?

Using the String class on your nano. Do this instead: Serial Input Basics - updated

You send "foo\r\n". readStingUntil() will give you "foo\r".

Change the ESP code to below is one way to solve it. If that doesn't work, let us know.

Serial.print("foo");
Serial.print("\n");
delay(500);
1 Like

Worked perfectly :+1:

Serial.println("foo");
==> 
Serial.print("foo");  //3 ASCII frames are being sent for three characters
Serial.println();        //next 2 ASCII frames are being sent for two charcaters
==>
Serial.print("foo");  //3 frames
Serial.print('\r');      //next 1 frame
Serial.print('\n');     //next 1 frame

So, this code String datafromESP = softSerial.readStringUntil('\n'); at the receiver is saving ASCII codes for four-charcaters as @sterretje has mentioned in his Post-3.

The above flaw could be corrected by the following code or by the code of @sterretje of Post-3.
String datafromESP = softSerial.readStringUntil('\r');

1 Like

That will leave the '\n' in the buffer and the next time that data is received and processed the compare will fail.

Correct; however, I expected that OP would use only print('\r') method.