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
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, privacy policy and cookie policy

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