ESP-NOW communication MAC adress error

csrobert
Posts: 1
Joined: Fri Sep 08, 2023 9:26 am

ESP-NOW communication MAC adress error

Postby csrobert » 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:
  1. #include <esp_now.h>
  2. #include <WiFi.h>
  3.  
  4. // REPLACE WITH YOUR RECEIVER MAC Address
  5. uint8_t broadcastAddress[] = {0x10, 0xAA, 0xBB, 0xCC, 0x33, 0xF5};
  6.  
  7. // Structure example to send data
  8. // Must match the receiver structure
  9. typedef struct struct_message {
  10.   char a[32];
  11.   int b;
  12.   float c;
  13.   bool d;
  14. } struct_message;
  15.  
  16. // Create a struct_message called myData
  17. struct_message myData;
  18.  
  19. esp_now_peer_info_t peerInfo;
  20.  
  21. // callback when data is sent
  22. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  23.   Serial.print("\r\nLast Packet Send Status:\t");
  24.   Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  25. }
  26.  
  27. void setup() {
  28.   // Init Serial Monitor
  29.   Serial.begin(115200);
  30.  
  31.   // Set device as a Wi-Fi Station
  32.   WiFi.mode(WIFI_STA);
  33.  
  34.   // Init ESP-NOW
  35.   if (esp_now_init() != ESP_OK) {
  36.     Serial.println("Error initializing ESP-NOW");
  37.     return;
  38.   }
  39.  
  40.   // Once ESPNow is successfully Init, we will register for Send CB to
  41.   // get the status of Trasnmitted packet
  42.   esp_now_register_send_cb(OnDataSent);
  43.  
  44.   // Register peer
  45.   memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  46.   peerInfo.channel = 0;  
  47.   peerInfo.encrypt = false;
  48.  
  49.   // Add peer        
  50.   if (esp_now_add_peer(&peerInfo) != ESP_OK){
  51.     Serial.println("Failed to add peer");
  52.     return;
  53.   }
  54. }
  55.  
  56. void loop() {
  57.   // Set values to send
  58.   strcpy(myData.a, "THIS IS A CHAR");
  59.   myData.b = random(1,20);
  60.   myData.c = 1.2;
  61.   myData.d = false;
  62.  
  63.   // Send message via ESP-NOW
  64.   esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
  65.    
  66.   if (result == ESP_OK) {
  67.     Serial.println("Sent with success");
  68.   }
  69.   else {
  70.     Serial.println("Error sending the data");
  71.   }
  72.   delay(2000);
  73. }
Receiver Code:
  1. /*
  2.   Rui Santos
  3.   Complete project details at https://RandomNerdTutorials.com/esp-now-esp32-arduino-ide/
  4.  
  5.   Permission is hereby granted, free of charge, to any person obtaining a copy
  6.   of this software and associated documentation files.
  7.  
  8.   The above copyright notice and this permission notice shall be included in all
  9.   copies or substantial portions of the Software.
  10. */
  11.  
  12. #include <esp_now.h>
  13. #include <WiFi.h>
  14.  
  15. // Structure example to receive data
  16. // Must match the sender structure
  17. typedef struct struct_message {
  18.     char a[32];
  19.     int b;
  20.     float c;
  21.     bool d;
  22. } struct_message;
  23.  
  24. // Create a struct_message called myData
  25. struct_message myData;
  26.  
  27. // callback function that will be executed when data is received
  28. void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  29.   memcpy(&myData, incomingData, sizeof(myData));
  30.   Serial.print("Bytes received: ");
  31.   Serial.println(len);
  32.   Serial.print("Char: ");
  33.   Serial.println(myData.a);
  34.   Serial.print("Int: ");
  35.   Serial.println(myData.b);
  36.   Serial.print("Float: ");
  37.   Serial.println(myData.c);
  38.   Serial.print("Bool: ");
  39.   Serial.println(myData.d);
  40.   Serial.println();
  41. }
  42.  
  43. void setup() {
  44.   // Initialize Serial Monitor
  45.   Serial.begin(115200);
  46.  
  47.   // Set device as a Wi-Fi Station
  48.   WiFi.mode(WIFI_STA);
  49.  
  50.   // Init ESP-NOW
  51.   if (esp_now_init() != ESP_OK) {
  52.     Serial.println("Error initializing ESP-NOW");
  53.     return;
  54.   }
  55.  
  56.   // Once ESPNow is successfully Init, we will register for recv CB to
  57.   // get recv packer info
  58.   esp_now_register_recv_cb(OnDataRecv);
  59. }
  60.  
  61. void loop() {
  62.  
  63. }

Who is online

Users browsing this forum: No registered users and 103 guests