Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP_NOW_Broadcast_Slave unable to handle more than 1 master #11165

Open
1 task done
faultylee opened this issue Mar 23, 2025 · 0 comments
Open
1 task done

ESP_NOW_Broadcast_Slave unable to handle more than 1 master #11165

faultylee opened this issue Mar 23, 2025 · 0 comments
Assignees
Labels
Area: ESP-NOW Issues and Feature Request about ESP-NOW Protocol Status: Needs investigation We need to do some research before taking next steps on this issue

Comments

@faultylee
Copy link

faultylee commented Mar 23, 2025

Board

XIAO_ESP32C3

Device Description

Plain XIAO_ESP32C3

Hardware Configuration

WIFI
Serial
Built-in LED (GPIO 8)

Version

v3.0.7

IDE Name

Arduino IDE

Operating System

ArchLinux

Flash frequency

80

PSRAM enabled

yes

Upload speed

921600

Description

I adopted the example of libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Slave/ESP_NOW_Broadcast_Slave.ino and proceed to setup 2 masters. It works fine with 1 master. Once the 2nd master started transmit and the slave registered it, it stop reacting to the first 1 master, as if nothing was received. I use the example as a push button remote control, and it's only transmitting one at a time when I was testing back and forth.

The master code is the same as the example except it transmit every 10ms continiously when the button is pressed.

I then found example online that uses the esp_now library directly instead of through ESP_NOW as per the example, and directly registering the callback using esp_now_register_recv_cb, then I was able to see messages from both the masters, while still using the same example code for master.

I couldn't figure out what went wrong in the slave example, but I suspect something within the vector or the peer registration that prevents the callback from working. Or that only 1 copy of ccallback is supported by esp_now?

Sketch

/*
    ESP-NOW Broadcast Slave
    Lucas Saavedra Vaz - 2024

    This sketch demonstrates how to receive broadcast messages from a master device using the ESP-NOW protocol.

    The master device will broadcast a message every 5 seconds to all devices within the network.

    The slave devices will receive the broadcasted messages. If they are not from a known master, they will be registered as a new master
    using a callback function.
*/

#include "ESP32_NOW.h"
#include "WiFi.h"

#include <esp_mac.h>  // For the MAC2STR and MACSTR macros

#include <vector>

/* Definitions */

#define ESPNOW_WIFI_CHANNEL 6
#define LED_BUILTIN 8

/* Global Variables */

bool is_triggered = false;

/* Classes */

// Creating a new class that inherits from the ESP_NOW_Peer class is required.

class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
  public:
    // Constructor of the class
    ESP_NOW_Peer_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}

    // Destructor of the class
    ~ESP_NOW_Peer_Class() {}

    // Function to register the master peer
    bool add_peer() {
      if (!add()) {
        log_e("Failed to register the broadcast peer");
        return false;
      }
      return true;
    }

    // Function to print the received messages from the master
    void onReceive(const uint8_t *data, size_t len, bool broadcast) {
      Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
      Serial.printf("  Message: %s\n", (char *)data);
      if (String((char *)data) == "Testing") {
        is_triggered = true;
      }
    }
};

// List of all the masters. It will be populated when a new master is registered
std::vector<ESP_NOW_Peer_Class> masters;


/* Callbacks */

// Callback called when an unknown peer sends a message
void register_new_master(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) {
  if (memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, 6) == 0) {

    Serial.printf("Unknown peer " MACSTR " sent a broadcast message\n", MAC2STR(info->src_addr));
    Serial.println("Registering the peer as a master");

    ESP_NOW_Peer_Class new_master(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);

    masters.push_back(new_master);
    if (!masters.back().add_peer()) {
      Serial.println("Failed to register the new master");
      return;
    }

  } else {
    // The slave will only receive broadcast messages
    log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
    log_v("Igorning the message");
  }
}

/* Main */

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  // Initialize the Wi-Fi module
  WiFi.mode(WIFI_STA);
  WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
  while (!WiFi.STA.started()) {
    delay(100);
  }

  Serial.println("ESP-NOW Example - Broadcast Slave");
  Serial.println("Wi-Fi parameters:");
  Serial.println("  Mode: STA");
  Serial.println("  MAC Address: " + WiFi.macAddress());
  Serial.printf("  Channel: %d\n", ESPNOW_WIFI_CHANNEL);

  // Initialize the ESP-NOW protocol
  if (!ESP_NOW.begin()) {
    Serial.println("Failed to initialize ESP-NOW");
    Serial.println("Reeboting in 5 seconds...");
    delay(5000);
    ESP.restart();
  }

  // Register the new peer callback
  ESP_NOW.onNewPeer(register_new_master, NULL);

  Serial.println("Setup complete. Waiting for a master to broadcast a message...");
}

void loop() {
  if (is_triggered) {
    digitalWrite(LED_BUILTIN, LOW);
    is_triggered = false;
  } else {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  delay(50);
}

Debug Message

09:09:43.042 -> ESP-NOW Example - Broadcast Slave

09:09:43.042 -> Wi-Fi parameters:

09:09:43.042 ->   Mode: STA

09:09:43.042 ->   MAC Address: 34:B7:DA:F8:14:50

09:09:43.042 ->   Channel: 6
09:09:43.042 -> Setup complete. Waiting for a master to broadcast a message...

09:09:48.415 -> Unknown peer f0:f5:bd:fd:96:a0 sent a broadcast message
09:09:48.415 -> Registering the peer as a master

09:09:48.415 -> Received a message from master f0:f5:bd:fd:96:a0 (broadcast)
09:09:48.415 ->   Message: Testing
09:09:48.415 -> Received a message from master f0:f5:bd:fd:96:a0 (broadcast)
09:09:48.415 ->   Message: Testing
09:09:48.448 -> Received a message from master f0:f5:bd:fd:96:a0 (broadcast)
09:09:48.448 ->   Message: Testing
09:09:48.448 -> Received a message from master f0:f5:bd:fd:96:a0 (broadcast)
09:09:48.448 ->   Message: Testing
09:09:48.448 -> Received a message from master f0:f5:bd:fd:96:a0 (broadcast)
09:09:48.448 ->   Message: Testing
09:09:48.482 -> Received a message from master f0:f5:bd:fd:96:a0 (broadcast)
09:09:48.482 ->   Message: Testing
09:09:50.605 -> Unknown peer 34:b7:da:f8:2c:f8 sent a broadcast message
09:09:50.605 -> Registering the peer as a master

09:09:50.605 -> Received a message from master 34:b7:da:f8:2c:f8 (broadcast)
09:09:50.605 ->   Message: Testing
09:09:50.638 -> Received a message from master 34:b7:da:f8:2c:f8 (broadcast)
09:09:50.638 ->   Message: Testing
09:09:50.638 -> Received a message from master 34:b7:da:f8:2c:f8 (broadcast)
09:09:50.638 ->   Message: Testing

NOTE: sending message from first master here and nothing show up

09:09:55.815 -> Received a message from master 34:b7:da:f8:2c:f8 (broadcast)
09:09:55.815 ->   Message: Testing
09:09:55.848 -> Received a message from master 34:b7:da:f8:2c:f8 (broadcast)
09:09:55.848 ->   Message: Testing
09:09:55.848 -> Received a message from master 34:b7:da:f8:2c:f8 (broadcast)
09:09:55.848 ->   Message: Testing
09:09:55.848 -> Received a message from master 34:b7:da:f8:2c:f8 (broadcast)
09:09:55.848 ->   Message: Testing

Other Steps to Reproduce

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@faultylee faultylee added the Status: Awaiting triage Issue is waiting for triage label Mar 23, 2025
@lucasssvaz lucasssvaz self-assigned this Mar 24, 2025
@lucasssvaz lucasssvaz added Status: Needs investigation We need to do some research before taking next steps on this issue Area: ESP-NOW Issues and Feature Request about ESP-NOW Protocol and removed Status: Awaiting triage Issue is waiting for triage labels Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: ESP-NOW Issues and Feature Request about ESP-NOW Protocol Status: Needs investigation We need to do some research before taking next steps on this issue
Projects
None yet
Development

No branches or pull requests

2 participants