Hi all!
I'm having trouble with the code I'm working on with.
It simply connects to a SIM800L and waits for a specific string.
The thing is, my main funcion works OK with an UNO, but not with an ESP32, and the only difference in between is that (for the UNO) I'm using the SoftwareLibrary, while with the ESP32 im using Serial2.
In both I'm able to get a connection with the module, but when I try to boot the ESP32 I get a loop in which the ESP32 is unable to exit when it gets the correct answer ("OK").
Any ideas?
Here's the code (as used in the ESP32, for the UNO I only add the #include softwareSerial and rename the serial sim800l, while changing the name instead of Serial2, and corresponding pins):
#define RESET_PIN 4
String number = "+MY_PHONE_NUMBER";
unsigned long previousMillis = 0;
boolean getResponse(String expected_answer, unsigned int timeout=2000,boolean reset=false){
boolean flag = false;
String response = "";
unsigned long previous;
//*************************************************************
for(previous=millis(); (millis() - previous) < timeout;){
while(Serial2.available()){
response = Serial2.readString();
//----------------------------------------
if(response != ""){
Serial.println(response);
if(reset == true)
return true;
}
//----------------------------------------
if(response.indexOf(expected_answer) > -1){
return true;
}
}
}
//*************************************************************
return false;
}
//---------------------------------------------------------------------------------------------
boolean tryATcommand(String cmd, String expected_answer, int timeout, int total_tries){
TryAgain:
//*************************************************************
for(int i=1; i<=total_tries; i++){
Serial2.println(cmd);
if(getResponse(expected_answer, timeout) == true){
break;
}
else {
Serial.print(".");
}
if(i == total_tries){
Serial.println("Failed! Resetting the Module");
digitalWrite(RESET_PIN, LOW);
delay(100);
digitalWrite(RESET_PIN, HIGH);
goto TryAgain;
}
}
//*************************************************************
}
//---------------------------------------------------------------------------------------------
void setup() {
pinMode(RESET_PIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(RESET_PIN, HIGH);
Serial2.begin(115200);
Serial.begin(115200);
tryATcommand("AT","OK",2000,20);
tryATcommand("AT+CMGF=1","OK",2000,20);
tryATcommand("AT+CNMI=1,2,0,0,0","OK",1000,20);
delay(1000);
Serial2.println("AT+CMGS=\"" + number + "\"\r");
delay(250);
Serial2.print("Boot OK");
delay(250);
Serial2.write(26);
delay(250);
tryATcommand("AT+CMGD=1,4","OK",1000,20);
}
void loop() {
while(Serial2.available()){
String response = Serial2.readString();
Serial.println(response);
if(response.indexOf("+CMT:") > 0){
if(response.indexOf("ledon") > 0){
digitalWrite(LED_BUILTIN, HIGH);
Serial2.println("AT+CMGS=\"" + number + "\"\r");
delay(250);
Serial2.print("LED on");
delay(250);
Serial2.write(26);
delay(250);
tryATcommand("AT+CMGD=1,4","OK",1000,20);
}
if(response.indexOf("ledoff") > 0){
digitalWrite(LED_BUILTIN, LOW);
Serial2.println("AT+CMGS=\"" + number + "\"\r");
delay(250);
Serial2.print("LED off");
delay(250);
Serial2.write(26);
delay(250);
tryATcommand("AT+CMGD=1,4","OK",1000,20);
}
}
//Serial.println(buff);
}
while(Serial.available()){
Serial2.println(Serial.readString());
}
if(millis() - previousMillis > 300000) { //15 Minutes interval
tryATcommand("AT+CMGF=1","OK",1000,20);
tryATcommand("AT+CNMI=1,2,0,0,0","OK",1000,15);
previousMillis = millis();
}
}
The typical answer I get with the ESP32 in the serial monitor is this, which implies that there's something in some function that is not being able to receive the "OK" status and break the timeout:
.AT
AT
OK
.AT
AT
OK
.AT
AT
OK
.AT
AT
OK
.AT
AT
OK
.AT
AT
OK
......... (20 times)
Failed! Resetting the Module
EDIT:
Whit the UNO, it boots up and functions perfectly fine, this is the serial example:
AT
OK
AT+CMGF=1
OK
AT+CNMI=1,2,0,0,0
OK
And everything after that, and sending the messages works just fine.

