//espnow2 send_receive  mac add2 0x8c,0x94,0xdf,0x61,0xda,0x04   
 
// my mac add 0x88,0x57,0x21,0xb1,0x28,0x2c
  
#include <esp_now.h>
#include <WiFi.h>

// Enter the MAC address of the second board here.
uint8_t broadcastAddress2[] = {0x8c, 0x94, 0xdf, 0x61, 0xda, 0x04};

// Data structure for receiving and sending.
typedef struct struct_message {
    char text[32];
    int counter;
} struct_message;

struct_message myData;       // Information to send
struct_message incomingData; // Received information

esp_now_peer_info_t peerInfo;

//The function is called when data is sent.
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLatest delivery status: ");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Sent successfully." : "Send failed.");
}

// The function is called when data is submitted.
void OnDataRecv(const esp_now_recv_info *info, const uint8_t *incomingDataRaw, int len) {
	
const uint8_t *mac = info->src_addr;
//------------------------------------- check data incomming -------------
    Serial.println("\n=== [Received a new data frame.] ===");
    
    // Displays the sender's MAC address.
    Serial.printf("from MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    
    // Check the frame size (Frame Length Verification)
    Serial.printf("Size of data received. (Bytes): %d\n", len);
    Serial.printf("Expected structure dimensions(Bytes): %d\n", sizeof(struct_message));

    if (len != sizeof(struct_message)) {
        Serial.println("❌ [Error] Frame Format Incorrect! The data size does not match the structure");
        return;
    }
//----------------------------------------------------------------------- 	
	
	
	
  memcpy(&incomingData, incomingDataRaw, sizeof(incomingData));
  Serial.print("---I received the information from the board.2: ");
  Serial.print(incomingData.text);
  Serial.print(" |number: ");
  Serial.println(incomingData.counter);
}
 
void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);

  // Getting Started with ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("ESP-NOW startup failed.");
    return;
  }

  // Register the Callback function for both incoming and outgoing calls.

esp_now_register_send_cb((esp_now_send_cb_t)OnDataSent);

  

esp_now_register_recv_cb((esp_now_recv_cb_t)OnDataRecv);  
  
  // Register the second board as a Peer.
  memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Adding a peer failed.");
    return;
  }
}
 
void loop() {
  // Generate and send data to board 2 every 2 seconds.
  strcpy(myData.text, "Greetings from Board 3.");
  //myData.counter++;
   myData.counter = random(1, 100);
  esp_err_t result = esp_now_send(broadcastAddress2, (uint8_t *) &myData, sizeof(myData));
  if (result != ESP_OK) {
    Serial.println("An error occurred during data transmission.");
  }

  delay(2000);
}
