Mengirim Date ke Banyak Board dengan ESP_NOW

Mencari alamat MAC penerima

#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif

void setup(){
  Serial.begin(115200);
  Serial.println();
  Serial.print("ESP Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

Pengirim Data

#include <esp_now.h>
#include <WiFi.h>

// Ganti dengan MAC address penerima
uint8_t broadcastAddress1[] = {0x3C, 0x71, 0xBF, 0xC3, 0xBF, 0xB0};
uint8_t broadcastAddress2[] = {0x24, 0x0A, 0xC4, 0xAE, 0xAE, 0x44};
uint8_t broadcastAddress3[] = {0x80, 0x7D, 0x3A, 0x58, 0xB4, 0xB0};

typedef struct test_struct {
  int x;
  int y;
} test_struct;

test_struct test1;
test_struct test2;
test_struct test3;

// callback saat pengiriman data
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  char macStr[18];
  Serial.print("Ditujukan : ");
  // Copies the sender mac address to a string
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.print(macStr);
  Serial.print(" status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Sukses" : "Gagal");
}
 
void setup() {
  Serial.begin(115200);
 
  WiFi.mode(WIFI_STA);
 
  if (esp_now_init() != ESP_OK) {
    Serial.println("Gagal inisialisasi ESP-NOW");
    return;
  }
  
  esp_now_register_send_cb(OnDataSent);
   
  // pendaftaran peer
  esp_now_peer_info_t peerInfo;
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // peer  pertama
  memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
  if (esp_now_add_peer(&amp;peerInfo) != ESP_OK){
    Serial.println("Gagal menambahkan peer");
    return;
  }
  //  peer kedua 
  memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  if (esp_now_add_peer(&amp;peerInfo) != ESP_OK){
    Serial.println("Gagal menambahkan peer");
    return;
  }
  /// peer ketiga
  memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
  if (esp_now_add_peer(&amp;peerInfo) != ESP_OK){
    Serial.println("Gagal menambahkan peer");
    return;
  }
}
 
void loop() {
  //masukkan data padda struktur
  test1.x = random(0,20);
  test1.y = random(0,20);
  test2.x = random(0,20);
  test2.y = random(0,20);
  test3.x = random(0,20);
  test3.y = random(0,20);
 
  //kirim paket data test1 ke semua board
  esp_err_t result = esp_now_send(0, (uint8_t *) &amp;test1, sizeof(test_struct));

  if (result == ESP_OK) {
    Serial.println("Terkirim ke semua board");
  }
  else {
    Serial.println("Gagal mengirim");
  }

  //kirim paket data test1,test2, test3 ke maing-masing board
  result = esp_now_send(broadcastAddress1, (uint8_t *) &amp;test1, sizeof(test_struct));
  if (result == ESP_OK) {
    Serial.println("Terkirim ke board 1");
  }
  else {
    Serial.println("Gagal mengirim board 1");
  }

  result = esp_now_send(broadcastAddress2, (uint8_t *) &amp;test2, sizeof(test_struct));
  if (result == ESP_OK) {
    Serial.println("Terkirim ke board 2");
  }
  else {
    Serial.println("Gagal mengirim board 2");
  }

  result = esp_now_send(broadcastAddress3, (uint8_t *) &amp;test3, sizeof(test_struct));
   
  if (result == ESP_OK) {
    Serial.println("Terkirim ke board 3");
  }
  else {
    Serial.println("Gagal mengirim board 3");
  }
  
  delay(2000);
}

Penerima Data

#include <esp_now.h>
#include <WiFi.h>

typedef struct test_struct {
  int x;
  int y;
} test_struct;

test_struct myData;

void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&amp;myData, incomingData, sizeof(myData));
  Serial.print("Bytes diterima: ");
  Serial.println(len);
  Serial.print("x: ");
  Serial.println(myData.x);
  Serial.print("y: ");
  Serial.println(myData.y);
  Serial.println();
}
 
void setup() {
  Serial.begin(115200);
  
  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Gagal inisialisasi ESP-NOW");
    return;
  }
  //daftarkan callback
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {

}

sumber: https://randomnerdtutorials.com/esp-now-one-to-many-esp32-esp8266/