ESP-NOW communication MAC adress error
Posted: Fri Sep 08, 2023 9:33 am
Hello. I have problem regarding the ESP-NOW communication between ESP32 boards. I have a simple sender/receiver code. The problem I have is regarding with the MAC adress. If I use a boradcast adres 0xFF 0xFF .. I get the Delivery success message, but If I use the receiver's MAC adress I get Delivery Fail. I triple checked the MAC adress but it still deosen't work. Can you guys help me with this?
Sender Code:
Receiver Code:
Sender Code:
- #include <esp_now.h>
- #include <WiFi.h>
- // REPLACE WITH YOUR RECEIVER MAC Address
- uint8_t broadcastAddress[] = {0x10, 0xAA, 0xBB, 0xCC, 0x33, 0xF5};
- // Structure example to send data
- // Must match the receiver structure
- typedef struct struct_message {
- char a[32];
- int b;
- float c;
- bool d;
- } struct_message;
- // Create a struct_message called myData
- struct_message myData;
- esp_now_peer_info_t peerInfo;
- // callback when data is sent
- void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
- Serial.print("\r\nLast Packet Send Status:\t");
- Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
- }
- void setup() {
- // Init Serial Monitor
- Serial.begin(115200);
- // Set device as a Wi-Fi Station
- WiFi.mode(WIFI_STA);
- // Init ESP-NOW
- if (esp_now_init() != ESP_OK) {
- 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_register_send_cb(OnDataSent);
- // Register peer
- memcpy(peerInfo.peer_addr, broadcastAddress, 6);
- peerInfo.channel = 0;
- peerInfo.encrypt = false;
- // Add peer
- if (esp_now_add_peer(&peerInfo) != ESP_OK){
- Serial.println("Failed to add peer");
- return;
- }
- }
- void loop() {
- // Set values to send
- strcpy(myData.a, "THIS IS A CHAR");
- myData.b = random(1,20);
- myData.c = 1.2;
- myData.d = false;
- // Send message via ESP-NOW
- esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
- if (result == ESP_OK) {
- Serial.println("Sent with success");
- }
- else {
- Serial.println("Error sending the data");
- }
- delay(2000);
- }
- /*
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/esp-now-esp32-arduino-ide/
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files.
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- */
- #include <esp_now.h>
- #include <WiFi.h>
- // Structure example to receive data
- // Must match the sender structure
- typedef struct struct_message {
- char a[32];
- int b;
- float c;
- bool d;
- } struct_message;
- // Create a struct_message called myData
- struct_message myData;
- // callback function that will be executed when data is received
- void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
- memcpy(&myData, incomingData, sizeof(myData));
- Serial.print("Bytes received: ");
- Serial.println(len);
- Serial.print("Char: ");
- Serial.println(myData.a);
- Serial.print("Int: ");
- Serial.println(myData.b);
- Serial.print("Float: ");
- Serial.println(myData.c);
- Serial.print("Bool: ");
- Serial.println(myData.d);
- Serial.println();
- }
- void setup() {
- // Initialize Serial Monitor
- Serial.begin(115200);
- // Set device as a Wi-Fi Station
- WiFi.mode(WIFI_STA);
- // Init ESP-NOW
- if (esp_now_init() != ESP_OK) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- // Once ESPNow is successfully Init, we will register for recv CB to
- // get recv packer info
- esp_now_register_recv_cb(OnDataRecv);
- }
- void loop() {
- }