1

How can I filter a garbage completely blank line over my serial data? It comes from the wifi module ESP8266

How I read data:

 while (Serial1.available()){
 String inData = Serial1.readStringUntil('\n');
 Serial.println("Got: " + inData); 
 }

Here is my common output:

Got: AT+CIPSEND=6
Got: 
Got: OK
Got: > 
Got: Recv 6 bytes
Got: 
Got: SEND OK
Got: 
Got: +IPD,21:Volume From: 99 To 89AT+CIPCLOSE
Got: CLOSED
Got: 
Got: OK
asked May 25, 2016 at 14:29
4
  • 2
    That question may be too broad, and lacking in relevant context, for a meaningful answer to be provided. Eg, we don't know if just suppressing all white space would work for you. What kind of data, in what format, needs to be passed through the filter? Commented May 25, 2016 at 14:41
  • It's just a blank line / newline that prints between responses to my commands. I only want to get rid of that line. The only way to do it is to filter cos I don't have control over the firmware. I'll add some output. Thanks for pointing that out. Commented May 25, 2016 at 15:11
  • It might be a \r \n, carriage return and new-line. So effectively, the cr+lf is printed out as two blank lines, one cr and one lf. You may try String inData = Serial1.readStringUntil('\r'); but other than that, why even bother? Commented May 31, 2016 at 12:46
  • You are right in your assumption! It's a combination of both. I'll give it a try! For me the right solution is to filter any newline and concentrate on regex. It's quite difficult but it's working Commented Jun 3, 2016 at 5:06

1 Answer 1

1
inData.trim();
if( inData.length() > 0 ){
 Serial.println("Got: " + inData + "\n");

::trim() removes all leading and trailing whitespace characters including newlines. A line of [zero or more whitespace] + [newline] will get trimmed to the null string. If the result isn't null, then append the stripped off newline and print it. Note that this will flatten any indentation or vertical spacing that was supplied in the incoming text.

answered May 25, 2016 at 16:23
2
  • Worked like charm! Thanks! Since I don't want to process any blank line/newline I just removed the "+ \n" since "println()" does it automatically Commented May 25, 2016 at 17:11
  • You're right - missed that. (oops!). Glad you got it working. Commented May 25, 2016 at 17:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.