I configured 2 ESP-12E cards with ESPNOW protocol.
My first attempts were successful. But for no apparent reason I now have a messaging problem.
When I plug in my sender board: Delivery success
When I press the reset button on sender board: Delivery fail
Why do I now have a failure with the reset button?
Sender code
#include <espnow.h>
// Set your Board ID (ESP8266 Sender #1 = BOARD_ID 1, ESP8266 Sender #2 = BOARD_ID 2, etc)
#define BOARD_ID 1
//MAC Address of the receiver -- ESP-12E
uint8_t broadcastAddress[] = {0x99, 0x99, 0x99, 0x99, 0x99, 0x99};
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
int topic;
char payload[2];
} struct_message;
//Create a struct_message called myData
struct_message myData;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0){
Serial.println("Delivery success\n");
}
else{
Serial.println("Delivery fail\n" + String(sendStatus));
}
}
void setup() {
//Init Serial Monitor
Serial.begin(9600);
delay(10);
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_send_cb(OnDataSent);
//Préparer les données à transmettre
myData.topic = BOARD_ID;
strcpy(myData.payload, "&1");
Serial.println("\nMyData : " );
Serial.println(*((uint8_t *) &myData.topic));
Serial.println(*((uint8_t *) &myData.payload));
Serial.println("\n");
//Send data
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
}
void loop() {
}
Receiver code
#include <espnow.h>
#include <ESP8266WiFi.h>
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int topic;
char payload[2];
} struct_message;
struct_message incomingReadings;
// callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len) {
// send the received data to the Raspberry Pi
// copier les résultats en mémoire
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
//cout<<incomingReadings.topic<<incomingReadings.payload;
Serial.printf("%d%s\n",incomingReadings.topic,incomingReadings.payload);
}
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Set the device as a Station and Soft Access Point simultaneously
WiFi.mode(WIFI_AP_STA);
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;}
else{
Serial.println("Initializing ESP-NOW completed");
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
}
I have already done some basic checks: MAC address, reboot of the 2 boards, distance between the boards, prints in the code, ... But I can find absolutely nothing, everything seems to be correct.