Menerima Data dari Banyak Board dengan ESP_NOW

Mendapatkan informasi MAC Address ESP Penerima

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

}

ESP Pengirim Data

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

// Ganti dengan MAC penerima
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

// ID harus unik
const int id = 1;

// struktur untuk pengiriman data harus sama dengan struktur pada penerima
typedef struct struct_message {
    int id; 
    int x;
    int y;
} struct_message;

struct_message myData;

// callback saat data dikirim
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nStatus pengiriman sebelumnya:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Sukses" : "Gagal");
}
 
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;
  }

  // registrasi ke penerima dan cek status paket
  esp_now_register_send_cb(OnDataSent);
  
  // Registrasi peer
  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // Menambahkan diri peer        
  if (esp_now_add_peer(&amp;peerInfo) != ESP_OK){
    Serial.println("Gagal menambahkan diri ke peer");
    return;
  }
}
 
void loop() {
  // Nilai untuk dikirim
  myData.id = 1;
  myData.x = random(0,50);
  myData.y = random(0,50);

  // Kirim pesan
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &amp;myData, sizeof(myData));
   
  if (result == ESP_OK) {
    Serial.println("Terkirim dengan sukses");
  }
  else {
    Serial.println("Gagal mengirim data");
  }
  delay(10000);
}

ESP Penerima data

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

// struktur untuk pengiriman data harus sama dengan struktur pada penerima
typedef struct struct_message {
  int id;
  int x;
  int y;
}struct_message;

struct_message myData;

// Membuat struktur untuk tiap board
struct_message board1;
struct_message board2;
struct_message board3;

struct_message boardsStruct[3] = {board1, board2, board3};

// callback saat data diterima
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
  char macStr[18];
  Serial.print("Paket diterima dari: ");
  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.println(macStr);
  memcpy(&amp;myData, incomingData, sizeof(myData));
  Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
  // Update the structures with the new incoming data
  boardsStruct[myData.id-1].x = myData.x;
  boardsStruct[myData.id-1].y = myData.y;
  Serial.printf("x value: %d \n", boardsStruct[myData.id-1].x);
  Serial.printf("y value: %d \n", boardsStruct[myData.id-1].y);
  Serial.println();
}
 
void setup() {
  Serial.begin(115200);
  
  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  //daftrakan callback
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {
  // Mengakses data dari setiap board
  /*
  int board1X = boardsStruct[0].x;
  int board1Y = boardsStruct[0].y;
  int board2X = boardsStruct[1].x;
  int board2Y = boardsStruct[1].y;
  int board3X = boardsStruct[2].x;
  int board3Y = boardsStruct[2].y;
  */

  delay(1000);  
}

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