1

I am trying to figure out how to capture the OK or the ERROR from a SIM800l.

I have tried

mySerial.println("AT");
while (mySerial.available() > 0 ) {
String str = mySerial.readString();
Serial.println(str);
     if (str.equals("OK")) {
        Serial.println("ok");
     } else {
        Serial.println("unknown");
     }
   }

But I always getting nothing back?

1
  • 1
    Does this answer your question? Comparing a String after reading it from Serial fails. The issue is that the string read from the serial port also contains a closing "newline" character so the comparison fails. You need to strip off the newline. Commented Jul 26, 2021 at 9:41

1 Answer 1

0

You can use .indexOf to find characters in a String. It returns a -1 if a match is not found. See https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/indexof/

mySerial.println("AT");
String str;

while (mySerial.available() > 0 ) 
{
    str = mySerial.readString();
    Serial.println(str);

    if (str.indexOf("OK") != -1) 
    {
        Serial.println("ok");
    } else if (str.indexOf("ERROR") != -1) 
    {
        Serial.println("error!");
    } else {
        Serial.println("unknown");
    }
}

Your Answer

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.