#include <esp_now.h>
#include <WiFi.h>

// Enter the MAC address of each receiver here.
uint8_t slave1_Address[] = {0x00, 0x70, 0x07, 0xa3, 0x78, 0x78};
uint8_t slave2_Address[] = {0x88, 0x57, 0x21, 0xc2, 0xc0, 0xb4};
uint8_t slave3_Address[] = {0x8c, 0x94, 0xdf, 0x61, 0xda, 0x04};

// The data structure to be sent (must be the same for both sending and receiving).
typedef struct struct_message {
  char msg[32];
  int value;
} struct_message;

struct_message myData;

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);

  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  //Register receiver 1.
  esp_now_peer_info_t peerInfo1 = {};
  memcpy(peerInfo1.peer_addr, slave1_Address, 6);
  peerInfo1.channel = 0;  
  peerInfo1.encrypt = false;
  esp_now_add_peer(&peerInfo1);

  // Register receiver 2.
  esp_now_peer_info_t peerInfo2 = {};
  memcpy(peerInfo2.peer_addr, slave2_Address, 6);
  peerInfo2.channel = 0;
  peerInfo2.encrypt = false;
  esp_now_add_peer(&peerInfo2);
  //Register receiver 3.
  esp_now_peer_info_t peerInfo3 = {};
  memcpy(peerInfo3.peer_addr, slave3_Address, 6);
  peerInfo3.channel = 0;
  peerInfo3.encrypt = false;
  esp_now_add_peer(&peerInfo3);
  
}

void loop() {
  strcpy(myData.msg, "Hello from Master!");
  myData.value = random(1, 100);

  // Send to everyone (NULL means send to all registered peers)
  esp_err_t result = esp_now_send(0, (uint8_t *) &myData, sizeof(myData));
  
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  } else {
    Serial.println("Error sending the data");
  }
  
  delay(2000);
}
