So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By lc1045
#72552 Hi there, I have 2 ESP8266 NodeMCU and I'm testing ESPNow for my first time with Arduino IDE 1.8.5 and libraries 2.4.0-rc2. When I use normal peer-to-peer communication my sketch works flawless but when I try to use broadcasting feature I can't get it works. On controller adding a broadcast peer returns -3 and obviuosly sending to NULL doesn't work. But sending directly to a broadcast MAC address apparently works as send callback function receive status=0. On the slave size nothing happens, no packets received, nor receive callback function is called. What am I doing wrong ?
This is the code:
Code: Select all#include <ESP8266WiFi.h>

extern "C" {
#include <espnow.h>
#include <user_interface.h>
}

#define WIFI_DEFAULT_CHANNEL 3
#define MYMODE_CONTROLLER 0
#define MYMODE_SLAVE 1

#define ESP_BROADCAST


// First ESP8266 as controller : 0x5C,0xCF,0x7F,0xD5,0xEB,0xA8
// Second ESP8266 as slave     : 0x5C,0xCF,0x7F,0xB5,0x68,0x98

uint8_t espController[] = {0x5C, 0xCF, 0x7F, 0xD5, 0xEB, 0xA8};
uint8_t espSlave[] = {0x5C, 0xCF, 0x7F, 0xB5, 0x68, 0x98};
uint8_t espBroadcast[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};


uint8_t myMode = MYMODE_CONTROLLER;
uint8_t data[1] = {0};


void printMacAddress(uint8_t* macaddr) {
  Serial.print("{");
  for (int i = 0; i < 6; i++) {
    Serial.print("0x");
    Serial.print(macaddr[i], HEX);
    if (i < 5) Serial.print(',');
  }
  Serial.println("}");
}



bool macAddressMatch(uint8_t* mac1, uint8_t* mac2) {
  bool isMatch = true;
  for (int i = 0; i < 6; i++) {
    if (mac1[i] != mac2[i]) {
      isMatch = false;
      break;
    }
  }
  return isMatch;
}
void setup() {
  Serial.begin(74880);
  //WiFi.disconnect();


  WiFi.mode(WIFI_STA);
  //  WiFi.mode(WIFI_AP);
  //  WiFi.softAP("aaa", "", WIFI_DEFAULT_CHANNEL); //strAPPwd);

  if (esp_now_init() == 0) {
    Serial.println("EspNow init ok");
  } else {
    Serial.println("EspNow init failed");
    //    ESP.restart();
    return;
  }

  uint8_t macaddr[6];
  wifi_get_macaddr(STATION_IF, macaddr);
  //wifi_get_macaddr(SOFTAP_IF, macaddr);
  Serial.print("Station Mac Address: ");
  printMacAddress(macaddr);

  if (macAddressMatch(macaddr, espController)) {
    Serial.println("\nSet this device as controller");
    myMode = MYMODE_CONTROLLER;
    esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
#ifdef ESP_BROADCAST
    int res1 = esp_now_add_peer(espBroadcast, (uint8_t)ESP_NOW_ROLE_SLAVE, WIFI_DEFAULT_CHANNEL, NULL, 0);
#else
    int res1 = esp_now_add_peer(espSlave, (uint8_t)ESP_NOW_ROLE_SLAVE, WIFI_DEFAULT_CHANNEL, NULL, 0);
#endif
    Serial.println("\nPeer add result " + String(res1));
  } else {
    Serial.println("\nSet this device as slave");
    myMode = MYMODE_SLAVE;
    esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
#ifdef ESP_BROADCAST
    int res1 = esp_now_add_peer(espBroadcast, (uint8_t)ESP_NOW_ROLE_CONTROLLER, WIFI_DEFAULT_CHANNEL, NULL, 0);
#else
    int res1 = esp_now_add_peer(espController, (uint8_t)ESP_NOW_ROLE_CONTROLLER, WIFI_DEFAULT_CHANNEL, NULL, 0);
#endif
    Serial.println("\nPeer add result " + String(res1));
  }

  esp_now_register_recv_cb([](uint8_t *macaddr, uint8_t *data, uint8_t len) {
    Serial.println("recv_cb");

    Serial.print("mac address: ");
    printMacAddress(macaddr);

    Serial.print("data: ");
    for (int i = 0; i < len; i++) {
      Serial.print(data[i], HEX);
      Serial.print(" ");
    }
    Serial.println("");
  });

  esp_now_register_send_cb([](uint8_t* macaddr, uint8_t status) {
    Serial.print("status = "); Serial.println(status);
    if (status != 0) {
      Serial.println("Packet not received by ");
      printMacAddress(macaddr);
    }
    if (status == 0) {
      Serial.println("Packet succesfull received by ");
      printMacAddress(macaddr);
    }
    Serial.println("");
  });
}

void loop() {
  if (myMode == MYMODE_CONTROLLER) {
    data[0] = (data[0] != 0) ? 0 : 1;
#ifdef ESP_BROADCAST
    esp_now_send(espBroadcast, data, 1);
#else
    esp_now_send(NULL, data, 1);
#endif
    delay(1000);
  }
}


and logs:
Code: Select all*********************************
** NORMAL RUN:
*********************************
------ CONTROLLER LOG (SLAVE OFF) ---------------------
 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v0c897c37
~ld
EspNow init ok
Station Mac Address: {0x5C,0xCF,0x7F,0xD5,0xEB,0xA8}

Set this device as controller

Peer add result 0
status = 1
Packet not received by
{0x5C,0xCF,0x7F,0xB5,0x68,0x98}

status = 1
Packet not received by
{0x5C,0xCF,0x7F,0xB5,0x68,0x98}

status = 1
Packet not received by
{0x5C,0xCF,0x7F,0xB5,0x68,0x98}

status = 1
Packet not received by
{0x5C,0xCF,0x7F,0xB5,0x68,0x98}

------ CONTROLLER LOG (SLAVE ON) ---------------------
ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v0c897c37
~ld
EspNow init ok
Station Mac Address: {0x5C,0xCF,0x7F,0xD5,0xEB,0xA8}

Set this device as controller

Peer add result 0
status = 0
Packet succesfull received by
{0x5C,0xCF,0x7F,0xB5,0x68,0x98}

status = 0
Packet succesfull received by
{0x5C,0xCF,0x7F,0xB5,0x68,0x98}

status = 0
Packet succesfull received by
{0x5C,0xCF,0x7F,0xB5,0x68,0x98}

status = 0
Packet succesfull received by
{0x5C,0xCF,0x7F,0xB5,0x68,0x98}


------ SLAVE LOG (CONTROLLER ON) ---------------------
 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v0c897c37
~ld
EspNow init ok
Station Mac Address: {0x5C,0xCF,0x7F,0xB5,0x68,0x98}

Set this device as slave

Peer add result 0
recv_cb
mac address: {0x5C,0xCF,0x7F,0xD5,0xEB,0xA8}
data: 1
recv_cb
mac address: {0x5C,0xCF,0x7F,0xD5,0xEB,0xA8}
data: 0
recv_cb
mac address: {0x5C,0xCF,0x7F,0xD5,0xEB,0xA8}
data: 1
recv_cb
mac address: {0x5C,0xCF,0x7F,0xD5,0xEB,0xA8}
data: 0



*********************************
** BROADCAST TEST:
*********************************
------- CONTROLLER LOG ---------------------------------
 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v0c897c37
~ld
EspNow init ok
Station Mac Address: {0x5C,0xCF,0x7F,0xD5,0xEB,0xA8}

Set this device as controller

Peer add result -3
status = 0
Packet succesfull received by
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}

status = 0
Packet succesfull received by
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}

status = 0
Packet succesfull received by
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}

status = 0
Packet succesfull received by
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}

status = 0
Packet succesfull received by
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}

status = 0
Packet succesfull received by
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}

------ SLAVE LOG ---------------------------------------
 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v0c897c37
~ld
EspNow init ok
Station Mac Address: {0x5C,0xCF,0x7F,0xB5,0x68,0x98}

Set this device as slave

Peer add result -3



Thanks in advance.