ESP NOW multiple slaves

Can someone help me with ESP NOW?

Rui from ESP-NOW with ESP8266: Send Data to Multiple Boards (one-to-many) | Random Nerd Tutorials made a nice example with one master, and multiple slaves and it works. But I need MAC addresses from all slaves.

Is there a way to send messages to multiple slaves without knowing MAC address? I read somewhere that all I have to do is to set NULL here:

esp_now_send(NULL, (uint8_t *) &myData, sizeof(myData));
instead of:
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));

This obviously doesn't work.

it is not null you can use the broadcast-adress

// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

see

best regards Stefan

1 Like

But can I go without any MAC?
Just send data, and whoever is there can receive it.

that IS what the broadcast-mac-adress is doing. ALL and EVERY ESP that is setup to listen on the SAME WiFi-Channel will receive the messages send with broadcast-adress

You can set the WiFi-channel with the command

wifi_set_channel("channelOfYourChoice");

If a specified MAC-adress different from 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF does not match the receivers MAC-adress the send will fail and nothing will be received.

If you setup any ESP8266 with the same configuration of
using the same WiFi-channel and using the boradcast-adress {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
as "adress" of the receiver

uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

wifi_set_channel(ChannelOfYourChoice);
esp_now_send(broadcastAddress,......

all ESP8266 will receive this message

The broadcast-adress must have a number to make the ESP8266 recognise

If the adress is {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} DON'T compare with your own adress receive the message and process the message

do you get it?
If not flash three ESP8266 with the exact same code all using

uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

wifi_set_channel(ChannelOfYourChoice);
esp_now_send(broadcastAddress,......

and test it
best regards Stefan

Here you are pointing to a one-way example, master to slave.

Although I understand MAC is a MUST, I don't understand how and where to add this

Let's copy examples here. They work, btw. I can send data to an ESP which MAC I know.

Sender:

#include <ESP8266WiFi.h>
#include <espnow.h>

// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x44, 0x17, 0x93, 0x3B, 0xC9, 0xEA};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  String d;
  bool e;
} struct_message;

// Create a struct_message called myData
struct_message myData;

unsigned long lastTime = 0;  
unsigned long timerDelay = 2000;  // send readings timer

// 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");
  }
  else{
    Serial.println("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() != 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);
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
 
void loop() {
  if ((millis() - lastTime) > timerDelay) {
    // Set values to send
    strcpy(myData.a, "THIS IS A CHAR");
    myData.b = random(1,20);
    myData.c = 1.2;
    myData.d = "Hello";
    myData.e = false;

    // Send message via ESP-NOW
    esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));

    lastTime = millis();
  }
}

Receiver:

#include <ESP8266WiFi.h>
#include <espnow.h>

// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
    char a[32];
    int b;
    float c;
    String d;
    bool e;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t 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("String: ");
  Serial.println(myData.d);
  Serial.print("Bool: ");
  Serial.println(myData.e);
  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() != 0) {
    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_set_self_role(ESP_NOW_ROLE_SLAVE);
  esp_now_register_recv_cb(OnDataRecv);
}

void loop() {
  
}

These are examples from the page you pointed out. In this case, I should upload the sender on one ESP, and the receiver on multiple others.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.