ESP-NOW sscanf( fails to identify MAC address

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

ESP-NOW sscanf( fails to identify MAC address

Postby Pcborges » Sat May 01, 2021 10:36 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.
The code of the peer is as below, the Master works fine.

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;

//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);
  success = str2mac(incomingMac, broadcastAddress);          <--gets MAC from master and FAIL to load into broadcastAddress

  Serial.println("broadcastAddress: ");                              <-- see console printout attached
  if (success) {
    for (int i=0; i<6; i++) {
      printf("%02X:",broadcastAddress[i]);
      Serial.println("");
    }
  }
}

int str2mac(const char* mac, uint8_t* broadcastAddress){
  Serial.print("str2mac: #");
  Serial.print(mac);                           <--- MAC from Master OK, sscanf( below fails to parse contents of broadcastAddress
  Serial.print("# lenght: ");
  Serial.println(sizeof(mac));
  if( 6 == sscanf( mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",&broadcastAddress[0], &broadcastAddress[1], &broadcastAddress[2],&broadcastAddress[3], &broadcastAddress[4], &broadcastAddress[5] ) ){
    return 1;
  }else{
    return 0;
  }
}
 
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);

  //int success = str2mac("24:0A:C4:BA:99:4C", broadcastAddress);  //dest COM camera
  //int success = str2mac("24:6F:28:46:95:B0", broadcastAddress);  //dest SEM camera  
  //if (success) {
  //  for (int i=0; i<6; i++) {
  //    printf("%02X:",broadcastAddress[i]);
  //    Serial.println("");
  //  }
  //}  
  registerPier();
/*
  // 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;
  }
*/

  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
}

void registerPier(){
  // 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;
  }  
}


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("Mac: ");
  Serial.print(incomingReadings.hmac);  
  Serial.println();
}
Console printout:
Error sending the data
19:34:40.905 -> INCOMING READINGS
19:34:40.905 -> Temperature: 26.00 ºC
19:34:40.952 -> Humidity: 77.00 %
19:34:40.952 -> Pressure: 1033.00 hPa
19:34:40.952 -> Mac: 24:6F:28:46:95:B0 <-- MAC from master station arrives OK
19:34:42.826 -> Bytes received: 32
19:34:42.826 -> str2mac: #24:6F:28:46:95:B0# lenght: 4 <-- lenght should be 18 not 4 ------------ERROR ERROR
19:34:42.826 -> broadcastAddress:
19:34:42.826 ->
19:34:42.826 ->
19:34:42.826 ->
19:34:42.826 ->
19:34:42.826 ->
19:34:42.826 ->

Thanks

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ESP-NOW sscanf( fails to identify MAC address

Postby WiFive » Sat May 01, 2021 10:42 pm

sizeof does not return the length of a string

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

Re: ESP-NOW sscanf( fails to identify MAC address

Postby Pcborges » Sat May 01, 2021 11:11 pm

"sizeof does not return the length of a string"

OK, I was sure the size of a char array could be found with sizeof, will research, thanks.
But my real problem is why this:

Code: Select all

sscanf( mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",&broadcastAddress[0], &broadcastAddress[1], &broadcastAddress[2],&broadcastAddress[3], &broadcastAddress[4], &broadcastAddress[5] ) 
Returns am empty broadcastAddress.

Thanks

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

Re: ESP-NOW sscanf( fails to identify MAC address

Postby Pcborges » Sun May 02, 2021 12:52 am

Just an update I just cannot explain...

If I leave the sketch to run for a while it eventually prints a big broadcastAddress, please check at the end.
What am I doing wrong?

Code: Select all

21:48:45.243 -> ets Jun  8 2016 00:22:57
21:48:45.243 -> 
21:48:45.243 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
21:48:45.243 -> configsip: 0, SPIWP:0xee
21:48:45.243 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
21:48:45.243 -> mode:DIO, clock div:2
21:48:45.243 -> load:0x3fff0018,len:4
21:48:45.243 -> load:0x3fff001c,len:1216
21:48:45.243 -> ho 0 tail 12 room 4
21:48:45.243 -> load:0x40078000,len:10944
21:48:45.243 -> load:0x40080400,len:6360
21:48:45.243 -> entry 0x400806b4
21:48:46.512 -> E (2003) ESPNOW: Peer interface is invalid
21:48:46.512 -> Failed to add peer
21:48:46.512 -> Error sending the data
21:48:46.512 -> INCOMING READINGS
21:48:46.512 -> Temperature: 0.00 ºC
21:48:46.512 -> Humidity: 0.00 %
21:48:46.512 -> Pressure: 0.00 hPa
21:48:46.512 -> Mac: 
21:48:52.753 -> Bytes received: 32
21:48:52.753 -> -> broadcastAddress: 
21:48:52.753 -> ,
21:48:52.753 -> ,
21:48:52.753 -> ,
21:48:52.753 -> ,
21:48:52.753 -> ,
21:48:52.753 -> ,
21:48:56.556 -> Error sending the data
21:48:56.556 -> INCOMING READINGS
21:48:56.556 -> Temperature: 26.00 ºC
21:48:56.556 -> Humidity: 77.00 %
21:48:56.556 -> Pressure: 1033.00 hPa
21:48:56.556 -> Mac: 24:6F:28:46:95:B0
21:49:02.790 -> Bytes received: 32
21:49:02.790 -> -> broadcastAddress: 
21:49:02.790 -> ,
21:49:02.790 -> ,
21:49:02.790 -> ,
21:49:02.790 -> ,
21:49:02.790 -> ,
21:49:02.790 -> ,
21:49:06.510 -> Error sending the data
21:49:06.510 -> INCOMING READINGS
21:49:06.556 -> Temperature: 26.00 ºC
21:49:06.556 -> Humidity: 77.00 %
21:49:06.556 -> Pressure: 1033.00 hPa
21:49:06.556 -> Mac: 24:6F:28:46:95:B0
21:49:12.763 -> Bytes received: 32
21:49:12.763 -> -> broadcastAddress: 
21:49:12.763 -> ,
21:49:12.763 -> ,
21:49:12.763 -> ,
21:49:12.763 -> ,
21:49:12.763 -> ,
21:49:12.763 -> ,
21:49:16.503 -> Error sending the data
21:49:16.550 -> INCOMING READINGS
21:49:16.550 -> Temperature: 26.00 ºC
21:49:16.550 -> Humidity: 77.00 %
21:49:16.550 -> Pressure: 1033.00 hPa
21:49:16.550 -> Mac: 24:6F:28:46:95:B0
21:49:22.787 -> Bytes received: 32
21:49:22.787 -> -> broadcastAddress: 
21:49:22.787 -> ,
21:49:22.787 -> ,
21:49:22.787 -> ,
21:49:22.787 -> ,
21:49:22.787 -> ,
21:49:22.787 -> ,
21:49:26.523 -> Error sending the data
21:49:26.523 -> INCOMING READINGS
21:49:26.523 -> Temperature: 26.00 ºC
21:49:26.523 -> Humidity: 77.00 %
21:49:26.523 -> Pressure: 1033.00 hPa
21:49:26.523 -> Mac: 24:6F:28:46:95:B0
21:49:32.775 -> Bytes received: 32
21:49:32.775 -> -> broadcastAddress: 
21:49:32.775 -> ,
21:49:32.775 -> ,
21:49:32.775 -> ,
21:49:32.775 -> ,
21:49:32.775 -> ,
21:49:32.775 -> ,
21:49:36.528 -> Error sending the data
21:49:36.528 -> INCOMING READINGS
21:49:36.528 -> Temperature: 26.00 ºC
21:49:36.528 -> Humidity: 77.00 %
21:49:36.528 -> Pressure: 1033.00 hPa
21:49:36.528 -> Mac: 24:6F:28:46:95:B0
21:49:42.752 -> Bytes received: 32
21:49:42.752 -> -> broadcastAddress: 
21:49:42.752 -> ,
21:49:42.752 -> ,
21:49:42.752 -> ,
21:49:42.752 -> ,
21:49:42.752 -> ,
21:49:42.752 -> ,
21:49:46.521 -> Error sending the data
21:49:46.521 -> INCOMING READINGS
21:49:46.521 -> Temperature: 26.00 ºC
21:49:46.521 -> Humidity: 77.00 %
21:49:46.521 -> Pressure: 1033.00 hPa
21:49:46.521 -> Mac: 24:6F:28:46:95:B0
21:49:52.779 -> Bytes received: 32
21:49:52.779 -> -> broadcastAddress: 
21:49:52.779 -> ,
21:49:52.779 -> ,
21:49:52.779 -> ,
21:49:52.779 -> ,
21:49:52.779 -> ,
21:49:52.779 -> ,
21:49:56.541 -> Error sending the data
21:49:56.541 -> INCOMING READINGS
21:49:56.541 -> Temperature: 26.00 ºC
21:49:56.541 -> Humidity: 77.00 %
21:49:56.541 -> Pressure: 1033.00 hPa
21:49:56.541 -> Mac: 24:6F:28:46:95:B0
21:50:02.786 -> Bytes received: 32
21:50:02.786 -> -> broadcastAddress: 
21:50:02.786 -> 24:6F:28:46:95:B0:24:6F:28:46:95:B0:24:6F:28:46:95:B0:24:6F:28:46:95:B0:24:6F:28:46:95:B0:24:6F:28:46:95:B0:24:6F:28:46:95:B0:24,
21:50:02.786 -> ,
21:50:02.786 -> ,
21:50:02.786 -> ,

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

Re: ESP-NOW sscanf( fails to identify MAC address

Postby Pcborges » Sun May 02, 2021 1:35 am

Some changes in the code:

Code: Select all

// 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);
}

void registerPier(const char* serverMAC){
  
  getMAC_OK = str2mac(serverMAC, broadcastAddress);

  Serial.println("|---> broadcastAddress: ");
  if (getMAC_OK) {
    for (int i=0; i<6; i++) {
      printf("%02X:",broadcastAddress[i]);
      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;
  }  
}
Console printout:

Code: Select all

2:30:01.819 -> Error sending the data
22:30:01.819 -> INCOMING READINGS
22:30:01.819 -> Temperature: 0.00 ºC
22:30:01.819 -> Humidity: 0.00 %
22:30:01.819 -> Pressure: 0.00 hPa
22:30:01.819 -> Mac: 
22:30:02.714 -> Bytes received: 32
22:30:02.761 -> |---> broadcastAddress: 
22:30:02.761 -> ,,,,,,
22:30:02.761 -> 24:6F:28:46:95:B0:E (2924) ESPNOW: Peer interface is invalid
22:30:02.761 -> Failed to add peer
22:30:11.849 -> Error sending the data
22:30:11.849 -> INCOMING READINGS
22:30:11.849 -> Temperature: 26.00 ºC
22:30:11.849 -> Humidity: 77.00 %
22:30:11.849 -> Pressure: 1033.00 hPa
22:30:11.849 -> Mac: 24:6F:28:46:95:B0
22:30:12.749 -> Bytes received: 32
22:30:12.749 -> |---> broadcastAddress: 
22:30:12.749 -> ,,,,,,                                                        <----- Why empty?
22:30:12.749 -> 24:6F:28:46:95:B0:E (12923) ESPNOW: Peer interface is invalid         //(:E) Where does it come from?
22:30:12.749 -> Failed to add peer
22:30:21.845 -> Error sending the data
22:30:21.845 -> INCOMING READINGS
22:30:21.845 -> Temperature: 26.00 ºC
22:30:21.845 -> Humidity: 77.00 %
22:30:21.845 -> Pressure: 1033.00 hPa
22:30:21.845 -> Mac: 24:6F:28:46:95:B0
22:30:22.783 -> Bytes received: 32
22:30:22.783 -> |---> broadcastAddress:

ESP_Sprite
Posts: 9723
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-NOW sscanf( fails to identify MAC address

Postby ESP_Sprite » Mon May 10, 2021 6:55 am

liyon01: Suggest you make that into its own separate topic, as your issue seems to have nothing to do with the one talked about here.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 105 guests