ESP-NOW sscanf( fails to identify MAC address
Posted: 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.
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
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();
}
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