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.
#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.");
}
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.
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;
}