Issues when sending AT commands from ESP32 to A7670SA module

Hello everyone!

I'm having some issues trying to send AT commands from my ESP32 to an A7670SA module. I have set up my connection with the module, but when sending the AT commands, I do not receive responses, and sometimes, the module.

Here is my setup:

  • The A7670SA module is powered by a 3.7V 1800mAh battery.
  • My ESP32 is connected to my PC.
  • The connections are as follows: A7670SA's TX, RX, and GND are connected to ESP32's TX, RX, and GND respectively.

I have verified that the A7670SA module works correctly using SSCOM; it responds to commands without any issues. However, when trying to communicate with it through my ESP32, these issues arise.

I am attaching the code I am using:

#include <SoftwareSerial.h>

#define a7670sa_RX 16
#define a7670sa_TX 17

SoftwareSerial a7670saSerial(a7670sa_RX, a7670sa_TX); // RX, TX
int LED_PIN = 2;

void setup() {
  pinMode(LED_PIN, OUTPUT); // Establece el pin del LED como salida
  digitalWrite(LED_PIN, HIGH); // Enciende el LED inicialmente
  Serial.begin(115200); // Inicializa la comunicación serial con el Monitor Serie de Arduino IDE
  a7670saSerial.begin(115200, SWSERIAL_8N1); // Inicializa la comunicación SoftwareSerial con el módulo a7670sa
  a7670saSerial.write(static_cast<char>(0x00)); // Envía un carácter nulo para limpiar cualquier dato pendiente

  Serial.println("Initializing...");
  delay(1000);
}

void loop() {
   if (a7670saSerial.available()) {
    String response = a7670saSerial.readStringUntil('\n');
    Serial.println("Respuesta de a7670sa: " + response);
  }

  if (Serial.available()) {
    String command = Serial.readStringUntil('\n');
    if (command == "AT") {
      sendATCommand("AT", "Comando AT");
    } else {
      Serial.println("Comando no reconocido.");
    }
  }

 
}

void sendATCommand(String command, String commandName) {
  Serial.print("Enviando comando: ");
  Serial.println(commandName);

  digitalWrite(LED_PIN, LOW); // Apaga el LED

  // Limpia el búfer serial
  while (a7670saSerial.available() > 0) {
    a7670saSerial.read();
  }

  // Envía el comando
  a7670saSerial.println(command);

  unsigned long startTime = millis();
  String response;
  do {
    while (a7670saSerial.available() > 0) {
      response = a7670saSerial.readStringUntil('\n');
      Serial.println(response);
      if (response.startsWith("OK")) {
        Serial.println(); // Imprime una nueva línea después de la respuesta
        digitalWrite(LED_PIN, HIGH); // Enciende el LED
        return; // Sale del bucle y la función
      }
      if (response.startsWith("ERROR")) {
        Serial.println("\nError en la respuesta de a7670sa.");
        digitalWrite(LED_PIN, HIGH); // Enciende el LED
        return; // Sale del bucle y la función
      }
    }
    // Si no hay respuesta aún, imprime puntos
    Serial.print(".");
    delay(500); // Espera medio segundo
  } while (millis() - startTime < 5000); // Tiempo de espera de 5 segundos

  // Si se alcanza el tiempo de espera
  digitalWrite(LED_PIN, HIGH); // Enciende el LED
  Serial.println("\nTiempo de espera alcanzado.");
}

Thats i what see on the serial port

• This is the ESP DevKit that im Using

• This the A7670SA
image

The Arduino Nano ESP32 section of the forum is specifically for the Arduino Nano ESP32. You're using a different board and hence your topic has been moved.

Thanks bro

I'm not familiar with ESP32s but as far as I know they have multiple UARTs. In which case I wonder why you use a band-aid like SoftwareSerial?

I decided to use a simpler code

#include <HardwareSerial.h>

HardwareSerial Sim800L(1);

void setup() {
  Serial.begin(115200);
  Sim800L.begin(115200, SERIAL_8N1, 16, 17); // Configura los pines RX y TX del SIM800L
  delay(200);
  Serial.println("\nInitializing...");
  delay(200);
  Serial.println("\nTest now");
}

void loop() {
  updateSerial();
}

void updateSerial() {
  while (Serial.available()) {
    String a = Serial.readString();
    Sim800L.println(a);
  }

  while (Sim800L.available()) {
    Serial.print(char(Sim800L.read()));
  }
}

responds to AT commands but gives an error

I suspect it may be the module's power supply.

UPDATE:
• By powering the SIMCOM through vin and gnd with 7.7V 3A, and the esp32 connected to the PC, it causes the SIMCOM to reboot constantly.

image

• When powering the simcom and the esp32 connected to the PC via USB, it does not receive commands that I send in the code.

-- It is strange because according to the specifications it must be able to be powered from 3.4V to 4.2V.

I can now bring information from my server, but when passing it to the ESP32 via serial it is lost or corrupted. What could I do to fix that part?


#include <HardwareSerial.h>

HardwareSerial Sim800L(1);
#define MAX_DATA_SIZE 240 // Tamaño máximo de datos a recibir

void setup() {
  Serial.begin(115200);
  Sim800L.begin(115200, SERIAL_8N1,16, 17); // Configura los pines RX y TX del SIM800L
  delay(200);
  Serial.println("\nInicializando...");
  delay(200);
}

void loop() {
  if (Serial.available()) {
    String command = Serial.readStringUntil('\n');
    sendCommand(command);
  }

  // Llama a la función para recibir datos del SIM800L
  char data[MAX_DATA_SIZE];
  int bytesRead = receiveFromSim800L(data, MAX_DATA_SIZE);

  // Si se recibieron datos del SIM800L, imprímelos en el monitor serial
  if (bytesRead > 0) {
    Serial.print("Datos recibidos del SIM800L: ");
    for (int i = 0; i < bytesRead; i++) {
      Serial.print(data[i]);
    }
    Serial.println(); // Agregar un salto de línea al final
  }
}

void sendCommand(const String& command) {
  Sim800L.println(command);
  delay(250); // Espera entre el envío de comandos y la recepción de respuestas
}

int receiveFromSim800L(char* dataArray, int maxArraySize) {
  int dataIndex = 0; // Índice actual en el array de datos
  
  // Mientras haya datos disponibles en la comunicación serial y haya espacio en el array
  while (Sim800L.available() && dataIndex < maxArraySize - 1) {
    // Leer el próximo byte de datos
    char receivedByte = Sim800L.read();

    // Verificar si es un salto de línea o retorno de carro
    if (receivedByte == '\n' || receivedByte == '\r') {
      continue; // Ignorar saltos de línea y retornos de carro
    }

    // Almacenar el byte en el array
    dataArray[dataIndex] = receivedByte;
    dataIndex++;
  }
  
  // Agregar un terminador nulo al final del array para convertirlo en una cadena de caracteres válida
  dataArray[dataIndex] = '\0';

  // Devolver la cantidad de datos recibidos
  return dataIndex;
}

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