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?
-
1Does 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.StarCat– StarCat2021年07月26日 09:41:51 +00:00Commented Jul 26, 2021 at 9:41
1 Answer 1
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");
}
}
answered Jul 26, 2021 at 18:19
Explore related questions
See similar questions with these tags.