ESP32 - ESPNOW: Peer interface is invalid

Pcborges
Posts: 37
Joined: Thu Aug 09, 2018 9:56 pm

ESP32 - ESPNOW: Peer interface is invalid

Postby Pcborges » Sun May 02, 2021 2:14 pm

Hi, I am trying to implement ESP NOW in a way that I only need to enter at my master the MAC address of the peer.
Then I send the master’s MAC to the peer so the peer can register the master’s received MAC.
I succeeded to send the Master’s MAC to the peer but so far I failed to have the peer register the master MAC.
Console printout and the code of the peer is as below, the Master works fine.

Code: Select all

0:56:40.557 -> INCOMING READINGS
10:56:40.557 -> Temperature: 26.00 ºC
10:56:40.557 -> Humidity: 77.00 %
10:56:40.557 -> Pressure: 1033.00 hPa
10:56:40.557 -> Master's Mac address: 24:6F:28:46:95:B0
10:56:40.886 -> Bytes received: 32
10:56:40.886 -> Temporary buffer: 24:6F:28:46:95:B0
10:56:40.886 -> |---> MAC broadcastAddress: 24:6F:28:46:95:B0
10:56:40.886 -> E (12358) ESPNOW: Peer interface is invalid
10:56:40.886 -> Failed to add peer
Code:

Code: Select all

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

// INIT MAC Address of your receiver 
uint8_t broadcastAddress[6] = { 0 };

// Define variables to be sent
float temperature;
float humidity;
float pressure;
char  localmac[18];

// Define variables to store incoming readings
float incomingTemp;
float incomingHum;
float incomingPres;
char  incomingMac[18];

// Variable to store if sending data was successful
String success;
int getMAC_OK;

//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
    float temp;
    float hum;
    float pres;
    char  hmac[18];
} struct_message;

// Create a struct_message called variablesToBeSent to hold sensor readings
struct_message variablesToBeSent;

// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;

// 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");
  if (status ==0){
    success = "Delivery Success";
  }
  else{
    success = "Delivery Fail";
  }
}

// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  incomingTemp = incomingReadings.temp;
  incomingHum  = incomingReadings.hum;
  incomingPres = incomingReadings.pres;
  strcpy(incomingMac, incomingReadings.hmac);
  registerPier(incomingMac);
}

int str2mac(const char* str, uint8_t* broadcastAddress) {
  char buffer[18];
  strcpy(buffer, str);
  Serial.print("Temporary buffer: ");
  Serial.println(buffer);
  const char* delims = ":";
  char* tok = strtok(buffer, delims);
  for (int i = 0; i < 6; i++) {
    broadcastAddress[i] = strtol(tok, NULL, 16);
    tok = strtok(NULL, delims);
  }
  return 1;
}

 
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 for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
}

void registerPier(const char* serverMAC){
  static bool peerRegOk = false;
  if(peerRegOk){return;}
  
  getMAC_OK = str2mac(serverMAC, broadcastAddress);

  Serial.print("|---> MAC broadcastAddress: ");
  if (getMAC_OK) {
    for (int i=0; i<6; i++) {
      Serial.print(broadcastAddress[i],HEX);
      if(i<5){Serial.print(":");}
    }
    Serial.println("");
  }
  
  // Register peer
  esp_now_peer_info_t peerInfo;
  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;
  }else{
    peerRegOk = true;
  }
}


void loop() {
  // Set values to send
  variablesToBeSent.temp = 26;
  variablesToBeSent.hum  = 77;
  variablesToBeSent.pres = 1033;
  strcpy(variablesToBeSent.hmac, "22:33:44:55:66");   //send my own mac here

  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &variablesToBeSent, sizeof(variablesToBeSent));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  updateDisplay();
  delay(10000);
}

void updateDisplay(){
  // Display Readings in Serial Monitor
  Serial.println("INCOMING READINGS");
  Serial.print("Temperature: ");
  Serial.print(incomingReadings.temp);
  Serial.println(" ºC");
  Serial.print("Humidity: ");
  Serial.print(incomingReadings.hum);
  Serial.println(" %");
  Serial.print("Pressure: ");
  Serial.print(incomingReadings.pres);
  Serial.println(" hPa");
  Serial.print("Master's Mac address:  ");
  Serial.print(incomingReadings.hmac);  
  Serial.println();
}

Who is online

Users browsing this forum: No registered users and 327 guests