ESP-NOW broadcast transmisison latency

Hi everyone,

I have setup a esp-now network using Adafruit ESP8266. From one sender I Broadcast 1 byte at a time every second. All my 5 slaves receive this byte with a jitter of ~80 Micro second.

But when I look at how long it takes the sender to pass that 1 byte to the receivers, i have a fixed delay of 800 Micro second and a jitter of up to 5 milli second. Where does this 5 milli second jitter come from?

Thanks in advance!

the exact source of the 5 millisecond jitter could be influenced by various factors like the internal processing of the ESP8266 (are you doing anything else there?), radio channel issues and ESP-NOW's handling of broadcasting and retries...

hard to say.

Thanks! I am measuring time from start of broadcast message transmission to reception.

Here are sender and receiver codes:

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(REF_PIN, OUTPUT);
  // 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
  // actually send callback is not userful for broadcast

  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(OnDataSent);

  // Register peer
  esp_now_add_peer(broadcastAddress0, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
  //esp_now_add_peer(broadcastAddress1, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
  //esp_now_add_peer(broadcastAddress2, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}

void loop() {
  if (Serial.available() > 0) {
    data = Serial.read();
    digitalWrite(REF_PIN, !digitalRead(REF_PIN));
    // Send message via ESP-NOW

    esp_now_send(broadcastAddress0, (uint8_t *) &data, sizeof(data));
    //esp_now_send(broadcastAddress1, (uint8_t *) &data, sizeof(data));
    //esp_now_send(broadcastAddress2, (uint8_t *) &data, sizeof(data));

    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }

Receiver code:

void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {

  digitalWrite(REF_PIN, !digitalRead(REF_PIN));
  data =  incomingData[0];
  uint8_t cmd[] = {
        (uint8_t)0xAB,
        (uint8_t)data,           // Use the received eight_bit_value here
        1, 2, 3, 4, 5, 6, 7, 8,  // Example placeholders for additional data
        0xDE, 0xAD, 0xBE, 0xEF   // Example hex values as requested
      };
  Serial.write(cmd, sizeof(cmd));
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  // setup GPIO pin
  pinMode(REF_PIN, OUTPUT);

  // 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() {

}

so you don't broadcast, your use unicast.

what's this ?

(when you share code, share it all... here we don't have the variables definition etc)

Unicast communication may introduce slightly more latency compared to broadcast due to peer registration, targeted transmission, and potential acknowledgment mechanisms. The sender could use the OnDataSent callback to track the success or failure of the transmission based on whether the peer successfully receives the packet

1 Like

Thanks for the feedback!

As I mentioned in my question, I am using broadcast. Here is my broadcast mac address variable:

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

LED_BULLETIN as an arduino variable that only refers to the LED pin.

REF_PIN is my GPIO pin that I use to measure the transmission latency.

The only part of my codes left are these 3 variable and header files. Sorry if that created some confusion.

About retires: as far as I know there are no retires in ESP now broadcast.

Could you post the rest of the code so we can better analyze it?

1 Like

Ok
I thought it was unicast because you had multiple peers added

You are right that there is no ack for broadcast.

Not sure what might create the lag…

1 Like

Thanks! Here is the full code. I am measuring the GPIO pins of sender and receivers to measure transmission delay.

Here is my full code:

Sender Code:

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

uint8_t broadcastAddress0[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
#define REF_PIN 5
byte data;
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() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(REF_PIN, OUTPUT);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);

  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(OnDataSent);
  esp_now_add_peer(broadcastAddress0, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
 
void loop() {
  if (Serial.available() > 0) {
    data = Serial.read();
    digitalWrite(REF_PIN, !digitalRead(REF_PIN));
    esp_now_send(broadcastAddress0, (uint8_t *) &data, sizeof(data));
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }
}

Receiver Code:

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

// ***this example is tested for both ESP8266 and adafruit esp8266***

uint8_t data;
#define REF_PIN 5 

void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {

  digitalWrite(REF_PIN, !digitalRead(REF_PIN));
  data =  incomingData[0];
  uint8_t cmd[] = {
        0xAB,
        (uint8_t)data,
        0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
        0xAB, 0xAB, 0xAB, 0xAB
      };
  Serial.write(cmd, sizeof(cmd));
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
 
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(REF_PIN, OUTPUT);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
  esp_now_register_recv_cb(OnDataRecv);
}

void loop() {
}

Please see the code above. Thanks in advance!

1 Like

Got it thanks.

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