Hola a todos, soy nuevo en el foro y también en el uso de arduino. Estoy tratando de realizar un proyecto que involucra al siguiente hardware: elecrow 7´´ con esp32s3 integrado; arduino mega wifi r3 con esp8266 integrado, dos controladores de reles optoacopladores de 16 canales cada uno de ellos. Se trata de enviar órdenes desde una interface gráfica en elecrow 7´´ a arduino mega wifi (mediante una red wifi creada ad hoc) y que una vez recibidos los datos arduino mega wifi encienda o apague los reles. He diseñado la interface grafica para elecrow 7´´ y escrito el código para esp32s3. esta parte funciona correctamente y, al tocar los swichts envía la orden de encender o apagar el rele "x" a esp8266 que recibe la orden. sin embargo, cuando esp8266 le envía la orden a arduino mega, se muestra en el monitor serie de arduino ide el mensaje "comando desconocido". Alguien puede ayudarme con este problema. He leido otros post sobre el tema pero no soy capaz de ajustar el código. Adjunto código para arduino mega, código para esp8266 y captura de pantalla de monitor serie. Gracias.
Hello everyone, I’m new to the forum and also to using Arduino. I’m working on a project involving the following hardware: Elecrow 7’’ with an integrated ESP32-S3, Arduino Mega WiFi R3 with an integrated ESP8266, and two 16-channel optocoupler relay controllers. The goal is to send commands from a graphical interface on the Elecrow 7’’ to the Arduino Mega WiFi (via an ad hoc WiFi network), and once the data is received, the Arduino Mega WiFi should turn the relays on or off.
I have designed the graphical interface for the Elecrow 7’’ and written the code for the ESP32-S3. This part works correctly, and when I touch the switches, it sends the command to turn relay "x" on or off to the ESP8266, which successfully receives the command. However, when the ESP8266 sends the command to the Arduino Mega, the Arduino IDE serial monitor displays the message "unknown command."
Can anyone help me with this issue? I’ve read other posts on the topic but haven’t been able to adjust the code properly. I’m attaching the code for the Arduino Mega, the code for the ESP8266, and a screenshot of the serial monitor. Thank you!
Arduino Mega Sketch:
// Definición de los pines de los 33 relés
const int relayPins[33] = {
22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 2 // Pin 2 para el relé 33
};
void setup() {
Serial.begin(115200); // Comunicación con el monitor serie
Serial3.begin(115200); // Comunicación con el ESP8266 (RX3/TX3)
pinMode(13, OUTPUT); // Indicador LED para depuración
digitalWrite(13, LOW); // Apagar el LED inicialmente
// Inicializar los pines de los relés como salida y apagarlos (lógica inversa)
for (int i = 0; i < 33; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], HIGH); // Relés apagados inicialmente (lógica inversa)
}
// Configuración inicial del ESP8266 con comandos AT
delay(500);
Serial.println("Configurando el ESP8266...");
Serial3.println("AT+CIPMUX=1"); // Permitir múltiples conexiones
delay(2000);
Serial3.println("AT+CIPSERVER=1,5000"); // Configurar servidor TCP en el puerto 5000
delay(2000);
Serial3.println("AT+CIPSTO=3600"); // Establecer tiempo de espera de la conexión
delay(2000);
Serial.println("ESP8266 configurado y listo.");
}
void loop() {
// Escuchar comunicación del ESP8266 y procesar comandos
if (Serial3.available()) {
String command = Serial3.readStringUntil('\n'); // Leer el comando completo
Serial.println("Comando recibido desde ESP8266: " + command); // Depuración
processCommand(command);
}
// Escuchar entrada del usuario desde el monitor serie y enviarla al ESP8266
if (Serial.available()) {
String userInput = Serial.readStringUntil('\n');
Serial3.println(userInput); // Enviar al ESP8266
}
}
// Función para procesar los comandos recibidos
void processCommand(String command) {
// Ejemplo de formato de comando esperado: "REL:1:ON"
if (command.startsWith("REL:")) {
int firstColon = command.indexOf(':');
int secondColon = command.indexOf(':', firstColon + 1);
if (firstColon > 0 && secondColon > firstColon) {
// Extraer el número de relé y el estado
int relayNumber = command.substring(firstColon + 1, secondColon).toInt();
String state = command.substring(secondColon + 1);
// Validar el número de relé
if (relayNumber >= 1 && relayNumber <= 33) {
bool relayState = (state == "ON"); // Determinar el estado (ON/OFF)
// Ajustar para lógica inversa
digitalWrite(relayPins[relayNumber - 1], relayState ? LOW : HIGH); // LOW = Encender, HIGH = Apagar
Serial.print("Relé ");
Serial.print(relayNumber);
Serial.print(" ");
Serial.println(relayState ? "Encendido" : "Apagado");
} else {
Serial.println("Número de relé fuera de rango.");
}
} else {
Serial.println("Comando malformado.");
}
} else {
Serial.println("Comando desconocido.");
}
}
ESP8266 Sketch:
#include <ESP8266WiFi.h>
#include <espnow.h>
// Estructura de datos para las órdenes
typedef struct {
int relayNumber; // Número del relé (1-33)
bool state; // Estado del relé (true = ON, false = OFF)
} RelayCommand;
// Callback para recibir datos desde el ESP32-S3
void onDataRecv(uint8_t *mac, uint8_t *incomingData, uint8_t len) {
// Validar que los datos tengan el tamaño esperado
if (len != sizeof(RelayCommand)) {
Serial.println("Error: Tamaño de datos incorrecto.");
return;
}
RelayCommand command;
memcpy(&command, incomingData, sizeof(command));
// Verificar rango válido del número de relé
if (command.relayNumber >= 1 && command.relayNumber <= 33) {
// Preparar el comando para enviar al Arduino Mega
String commandToMega = "REL:" + String(command.relayNumber) + ":" + (command.state ? "ON" : "OFF") + "\n";
// Depuración en el monitor serie
Serial.println("Comando recibido desde ESP32-S3:");
Serial.println(" - Relé: " + String(command.relayNumber));
Serial.println(" - Estado: " + String(command.state ? "ON" : "OFF"));
Serial.println("Enviando comando al Mega: " + commandToMega);
// Enviar el comando al Arduino Mega a través de Serial1
Serial1.print(commandToMega);
} else {
Serial.println("Error: Número de relé fuera de rango.");
}
}
void setup() {
// Configuración de la comunicación serial
Serial.begin(115200); // Depuración con el monitor serie
Serial1.begin(115200); // Comunicación con Arduino Mega vía Serial1 (RX/TX)
// Configuración de ESP-NOW
WiFi.mode(WIFI_STA); // Configurar WiFi en modo estación
if (esp_now_init() == 0) { // `esp_now_init` devuelve 0 si tiene éxito
Serial.println("ESP-NOW inicializado correctamente.");
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(onDataRecv); // Configurar callback para recepción
} else {
Serial.println("Error al inicializar ESP-NOW.");
}
}
void loop() {
// El ESP8266 no realiza ninguna acción activa en el bucle principal
}