Code working on UNO but not on ESP32 (SIM800L)

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.

No, it's not that specific. It only implies that there is something that is not being able to receive the "OK".

In both I'm able to get a connection with the module

How did you test connectivity to the SIM800L, specifically?

Hi!

With the UNO, the program works flawlessly, with the NMCU I get stucked at the first function at boot (setup).

I believe SIM800L is connected to the MCU, as it's answering the AT command with an OK, do you think there could be a problem with the serial?

That period after "AT" looks like it comes from:

That would mean that getResponse() is returning 'false' on the ESP32 and not on the UNO. I think that means getResponse() is timing out. Have you tried increasing the timeout?

I've increased it from 1000ms to 2000ms, with the same result.
The strange thing is that I get the correct answer (OK) but ir seems that the MCU doesn't get it, si it prints the dot and keeps trying.

Hi John!
FYI, I've edited the output, since I was wrong in what I was getting.

It looks like this:

.AT
AT

OK

Until it resets the module and is stuck in that loop. I've tried with 2000ms timeout, but I'm still getting the same output.

Thanks again!
Chelo

How are you powering the SIM800L when running with the ESP32?

Hi! I'm powering both with 5V stepped down from a PSU, from an old 3d printer

Can you add the following prints to getResponse() and advise results with both Uno & ESP32.

      response = Serial2.readString();
      //----------------------------------------
      Serial.print("Response received >");
      Serial.print(response);
      Serial.println("<");
      Serial.print("Length: ");
      Serial.println(response.length());

How are you powering your sim800?
Is it 3.3V compliant?

Hi! It's a SIM800L V2, I'm powering ir with 5V directly

How many volts does an ESP32 expect on its GPIO pins? How many volts does the simie-thing put out on its I/O pins?

Hi! i wasn't really able to find this information, either for the microcontrollers nor the modem. I don't think the problem resides in there, since there's communication between the UNO (the program works absolutely fine there) and the modem. In the ESP case, there's also communication, but there's some kind of loop with the program, since I am able to get the resposne I want from the modem, and the MCU is printing it fine, but there's no "break" of the loop, and I keep getting the modem reset because I reach the 20 tries that is programmed to make.

Did you add the print statements as requested in post #9.

It's possible that you may be getting extra (non-printable) characters and therefore the check for matching the expected response is failing.

Provide a link and show us the circuit

ESP32 Dev Module
1. Input Voltage from PC via USB Port: 5 V
2. Microprocessor Operating Voltage: 3.3 V
3. Logic level of the IO Pins: 0 V for LOW and 3.3 V for HIGH

SIM800L GSM Module (2G Compatible)
1. Operating Voltage: 3.3V to 4.4 V (ideal: 4.1V)
2. Logic level of the IO Pins: 0 V for LOW and 3.3 v for HIGH

SIM800L V2 GSM Module
1. Operating Voltage: 5V
2. Logic level of the IO Pins: 0 V for LOW and 5V V for HIGH

Hi red_car, sorry for the delay! I was setting some things up.

Here's the response with the added code:

ESP32

.Response received >AT
OK
<c
Length: 9
AT
OK

.Response received >AT
OK
<c
Length: 9
AT
OK


And with the Nano it's kinda strange, because the response bonds itself with the answers, but the program still works fine:

AT
OK

Response received >AT
OK
<
Length: 9
AT
OK

**keeps booting the program fine**

UPDATE: Not working either with ESP8266. I'll upload the scheme ASAP

Hi Golam! Thank you very much for this info! Does this mean that either SIM800L (V1 or V2) should work fine with the UNO/NANO and (theoretically) with ESP? I've been able to make both work without trouble, excepto for the funcion described on top.

Hi JML! I'm working right now with the board; the circuit that I made is kind of the same as the one in this link for both, UNO/NANO and ESP. Both are working completely fine with the program provided by that web page.