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
  • 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? – James Waldby - jwpat7 May 25 '16 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. – DarkXDroid May 25 '16 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? – Paul May 31 '16 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 – DarkXDroid Jun 3 '16 at 5:06
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.

| improve this answer | |
  • 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 – DarkXDroid May 25 '16 at 17:11
  • You're right - missed that. (oops!). Glad you got it working. – JRobert May 25 '16 at 17:19
  • Don't worry It's alright. Thanks for your effort – DarkXDroid May 25 '16 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.