Hi,
I have two esp32 talking one two each other. Because i do not know in advance the MAC address of each, i put on each of them the same MAC address at start.
Now i want to change with the real MAC address of the two ESP i have, but i do not know which is the receiver MAC and the sendre MAC. I will attach the little code from one so you can pinpoint where is sender MAC and and where is Receiver MAC. Here is the piece of code :
// send data
bool sendData(char *Data, int Length) {
char macStr[19];
snprintf(macStr, sizeof(macStr), “%02x:%02x:%02x:%02x:%02x:%02x”,
KeypadMAC[0], KeypadMAC[1], KeypadMAC[2], KeypadMAC[3], KeypadMAC[4], KeypadMAC[5]);
//const uint8_t *peer_addr = slave.peer_addr;
Serial.print(“Sending: “); Serial.print(Data); Serial.print(” To “); Serial.println(macStr);
esp_err_t result = esp_now_send(KeypadMAC, (uint8_t *)Data, Length);//strlen(Data));
Serial.print(“Send Status: “);
if (result == ESP_OK) {
Serial.println(“Success”);
} else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
// How did we get so far!!
Serial.println(“ESPNOW not Init.”);
} else if (result == ESP_ERR_ESPNOW_ARG) {
Serial.println(“Invalid Argument”);
} else if (result == ESP_ERR_ESPNOW_INTERNAL) {
Serial.println(“Internal Error”);
} else if (result == ESP_ERR_ESPNOW_NO_MEM) {
Serial.println(“ESP_ERR_ESPNOW_NO_MEM”);
} else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
Serial.println(“Peer not found.”);
} else {
Serial.println(“Not sure what happened”);
}
if (result == ESP_OK) return true; else return false;
}
// callback when data is recv from Master
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len)
{
char macStr[18];
uint16_t CommandLength;
snprintf(macStr, sizeof(macStr), “%02x:%02x:%02x:%02x:%02x:%02x”,
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
//Serial.print(“Last Packet Recv from: “); Serial.println(macStr);
//Serial.print(“Last Packet Recv Data: “); Serial.println(*data);
//Serial.print(” of data length “); Serial.println(data_len);
Serial.print(“data: “);
Serial.println((char *)data);
//Check for command or initial invitation
if (strstr((char *)data, “Keypad is here”) != NULL) {
doConnect = true;
doConfirmed = false;
Step = 0;
Serial.println(“Get invitation”);
} else if (strstr((char *)data, “Code received”) != NULL) {
doConfirmed = true;
} else {
//Decoding the command
CommandLength = Decode((char *)data, (size_t)data_len, Command);
Serial.print(“Decoded “), Serial.print(CommandLength); Serial.print(“: “); Serial.println(Command);
HaveCommand = true;
}
}
//Encryption …….
Thank you
Hi.
I’m not sure that I understand your question.
To change the MAC address, you can use this tutorial:
Here is a discussion that shows how to uniquely identify the boards (other than the current MAC address) and how to get the default MAC address using esp_efuse_mac_get_default():
Alternatively, to identify each board, each board can also send something unique in their messages. For example, one board sends “#1” at the end of the messages, and the other sends “#2” so that you can identify each board. You can save that “id” permanently in the flash memory, for example, and then read it every time you want to identify the board. For that, you can use EEPROM or the Preferences library.
I hope this helps.
Regards,
Sara