ESP_NOW ESP8266 esp_now_peer_info_t missing

Hi In ESP32 we have the code

esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    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
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}
How can we do this with the ESP8266

The implementation of ESP-Now for the ESP8266 differs from the implementation for the ESP32.

This starts with the fact that the names for the header files are different: esp_now.h (ESP32) vs. espnow.h (ESP8266).

The return type ‘esp_err_t’ is not available on the ESP8266. Accordingly, the ESP_ERR_xxx constants are also not available.

The data type esp_now_peer_info_t is not defined for ESP8266.

The function signatures are different. Example:

ESP32:
esp_err_t esp_now_add_peer(const esp_now_peer_info_t *peer);

ESP8266:
int esp_now_add_peer(u8 *mac_addr, u8 role, u8 channel, u8 *key, u8 key_len);

You can find an example of ESP-Now for 8266 here: Getting Started with ESP-NOW (ESP8266 NodeMCU with Arduino IDE) | Random Nerd Tutorials

If the project is to be compiled for both the ESP32 and the ESP8266, I would solve it by writing a small wrapper library.

A common header file (e.g. my_espnow.h) and two different implementation files (e.g. my_espnow8266.cpp and my_espnow32.cpp). The implementation files must then be provided with corresponding #ifdef ESP8266 / #ifdef ESP32 blocks.