ESP32 + Module SIM800C: Make voice call second number if first number not answer

I'm trying to use ESP32 to connect to the SIM800C module via Serial2 port to implement the function of calling the second number if the first number doesn't answer. However, when the first number doesn't answer, ESP32 executes ATD+second number, but the serial monitor returns an error

#define RXD2 16
#define TXD2 17

const char* firstNumber = "+xxxxxxxxxxx";
const char* secondNumber = "+xxxxxxxxxxx";

void setup() {
  Serial.begin(9600);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial.println("Type 's' to send an SMS, 'r' to receive an SMS, 'c' to make a call");
  Serial.println("Initializing...");
  Serial2.println("AT+CLCC=1");
  delay(1000);
}

void loop() {
  if (Serial.available() > 0) {
    char command = Serial.read();
    switch (command) {
      case 's':
        SendMessage();
        break;
      case 'r':
        ReceiveMessage();
        break;
      case 'c':
        makeCall();
        break;
    }
  }

  while (Serial2.available()) {
    String response = Serial2.readStringUntil('\n');
    Serial.println(response);
    delay(10);
  }
}

void makeCall(){
  Serial2.println("ATD" + String(firstNumber) + ";");
  delay(5000); 
  
  String input;
  unsigned long startTime = millis();
  
  
  while (millis() - startTime < 20000) { 
    if (Serial2.available()) {
      input = Serial2.readStringUntil('\n');
      input.trim();
      if (input.startsWith("+CLCC: 1,0,0,0,0")) {
        Serial2.println("ATH"); 
        return;
      }
    }
    delay(100);
  }
  
  
  delay(5000);
  Serial2.println("ATD" + String(secondNumber) + ";");
}


void SendMessage() {
  Serial2.println("AT+CMGF=1");
  delay(1000); 

  Serial2.println("AT+CMGS=\"" + String(firstNumber) + "\"");
  delay(1000); 

  Serial2.println("Hi this is veron");
  Serial2.write((char)26); 
}

void ReceiveMessage() {
  Serial2.println("AT+CMGF=1");
  delay(1000); 

  Serial2.println("AT+CNMI=1,2,0,0,0");
  delay(1000); 
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.