Hello, i want to communicate using a ESP32 and ESP8266 to a esp8266 with ESP NOW. The information is something like this 360,100,180,1,0,0,1. the first 9 numbers are from a esp32, and the 1 and 0 are from a esp8266, this will be send to a 8266.
The codes i used are:
Tx esp8266
#include <ESP8266WiFi.h>
#include <espnow.h>
// Dirección MAC del receptor ESP8266
uint8_t receiverAddress[] = {0xE0, 0x97, 0x06, 0x99, 0xA2, 0x1C};
// Estructura para los datos del ESP8266
typedef struct struct_esp8266_data {
int Up;
int Rh;
int Dn;
int Lh;
} struct_esp8266_data;
struct_esp8266_data esp8266Data;
// Callback cuando se envían datos
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Estado del último envío: ");
Serial.println(sendStatus == 0 ? "Éxito" : "Fallo");
}
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
Serial.println("Error al iniciar ESP-NOW");
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_send_cb(OnDataSent);
esp_now_add_peer(receiverAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
void loop() {
// Valores a enviar
esp8266Data.Up = 1;
esp8266Data.Rh = 0;
esp8266Data.Dn = 0;
esp8266Data.Lh = 1;
esp_now_send(receiverAddress, (uint8_t *) &esp8266Data, sizeof(esp8266Data));
delay(100); // Enviar cada 100 ms
}
Tx esp32
#include <esp_now.h>
#include <WiFi.h>
// Dirección MAC del receptor ESP8266
uint8_t broadcastAddress[] = {0xE0, 0x97, 0x06, 0x99, 0xA2, 0x1C};
// Estructura para enviar datos desde el ESP32
typedef struct struct_message {
int grado;
int distancia;
int angulo;
} struct_message;
struct_message myData;
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("Estado del envío: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Éxito" : "Fallido");
}
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error al inicializar ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Error al añadir peer");
return;
}
}
void loop() {
myData.grado = 360;
myData.distancia = 100;
myData.angulo = 180;
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Datos enviados con éxito");
} else {
Serial.println("Error al enviar los datos");
}
delay(100); // Envía cada 100 ms
}
Rx esp8266
#include <ESP8266WiFi.h>
#include <espnow.h>
typedef struct struct_message_esp32 {
int grado;
int distancia;
int angulo;
} struct_message_esp32;
typedef struct struct_message_esp8266 {
int Up;
int Rh;
int Dn;
int Lh;
} struct_message_esp8266;
struct_message_esp32 dataESP32;
struct_message_esp8266 dataESP8266;
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
if (len == sizeof(dataESP32)) {
memcpy(&dataESP32, incomingData, sizeof(dataESP32));
} else if (len == sizeof(dataESP8266)) {
memcpy(&dataESP8266, incomingData, sizeof(dataESP8266));
}
Serial.print("Datos recibidos: ");
Serial.print(dataESP32.grado); Serial.print(",");
Serial.print(dataESP32.distancia); Serial.print(",");
Serial.print(dataESP32.angulo); Serial.print(",");
Serial.print(dataESP8266.Up); Serial.print(",");
Serial.print(dataESP8266.Rh); Serial.print(",");
Serial.print(dataESP8266.Dn); Serial.print(",");
Serial.println(dataESP8266.Lh);
}
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
Serial.println("Error al inicializar ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
// Mantener el ESP8266 escuchando
}
I ask here if someone already do it and know what to do. I dont have any console error. The only i have problems is from the esp32, the two esp8266 transfer data with no problem.
PD: sorry if this the wrong channel, also its in spanish but i think there are quicker in english.