Understanding and Utilizing ESP-NOW Peer Lists with Auto ESP

ESP-NOW is a powerful protocol for short-range, low-power communication between ESP32 and ESP8266 microcontrollers. A crucial aspect of ESP-NOW is the ability to manage peer devices, essentially a list of MAC addresses representing connected devices. This article delves into understanding how to access and utilize this peer list, particularly within the context of auto-pairing and retrieving specific MAC addresses using “Aut Esp.”

Accessing the ESP-NOW Peer List

The ESP-NOW peer list is dynamically built during the auto-pairing process. Once a connection is established, the MAC address of the peer device (often referred to as a “slave” in ESP-NOW terminology) is added to this list. Crucially, this list persists and remains accessible even after the initial pairing is complete.

The code snippet below demonstrates how to iterate through the peer list:

if((pn.total_num) > 0){ 
  boolean from_head = true; 
  int i = 1; 
  while (esp_now_fetch_peer(from_head, &slave) == ESP_OK){ 
    from_head = false; 
    Serial.print("MAC ADDRESS "); 
    Serial.print(i); 
    Serial.print(": "); 
    printMAC(slave.peer_addr); 
    i++; 
  } 
}

The esp_now_fetch_peer function is key here. The from_head parameter controls where to start fetching from the list. Setting it to true initially retrieves the first peer. Subsequent calls with from_head set to false iterate through the remaining peers. The slave parameter is a pointer to an esp_now_peer_info_t structure, which will be populated with the peer’s information, including its MAC address. The while loop continues as long as esp_now_fetch_peer returns ESP_OK, indicating a successful retrieval. Each iteration prints the MAC address of a connected peer. Importantly, this list reflects the currently connected devices; if a device disconnects, its MAC address will be removed.

Creating a Standalone Function to Retrieve MAC Addresses

To improve code modularity, it’s beneficial to encapsulate the MAC address retrieval logic within a standalone function. This function would accept a target index as input and return the corresponding MAC address.

esp_now_peer_info_t getPeerAddress(int targetIndex){
  if(targetIndex > 0 && targetIndex <= pn.total_num){
      boolean from_head = true;
      esp_now_peer_info_t slave;
      int i = 1;
      while (esp_now_fetch_peer(from_head, &slave) == ESP_OK){
          from_head = false;
          if(i == targetIndex) return slave;
          i++;
      }    
  }
  return {}; //return empty struct if index is out of range
}

This getPeerAddress function takes an integer targetIndex as input, representing the desired position in the peer list. It iterates through the list using esp_now_fetch_peer, similar to the previous example. However, it only returns the esp_now_peer_info_t structure of the peer when the current index i matches the targetIndex. This allows for targeted retrieval of specific MAC addresses from the list. Note that error handling, such as checking for valid targetIndex values, is crucial in a production environment. Furthermore, you can modify this function to randomly select a peer by generating a random number within the range of available peers.

Conclusion

Managing and accessing the ESP-NOW peer list is essential for effective device communication. By understanding how esp_now_fetch_peer works and implementing functions like getPeerAddress, developers can build robust and flexible ESP-NOW applications that can easily identify and interact with specific connected devices, leveraging the power of auto ESP for seamless network formation.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *