half done - can add slave mac to peers, but can’t add master mac to peers
would glad any halp =^-^=
i think there should be a straight forward example for such common stuff, but i struggle to find it
master
#include <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>
const uint8_t encryptionKey[16] = {0x30, 0x32, 0x30, 0x34, 0x30, 0x32, 0x30, 0x34, 0x30, 0x32, 0x30, 0x34, 0x30, 0x32, 0x30, 0x34}; // Example key
uint8_t slaveMACAddress[6];
void printMAC(const uint8_t *mac, const char *label)
{
Serial.print(label);
for (int i = 0; i < 6; i++)
{
Serial.printf("%02X", mac[i]);
if (i < 5)
Serial.print(":");
}
Serial.println();
}
void addPeer(const uint8_t *peer_addr)
{
esp_now_peer_info_t peerInfo;
memset(&peerInfo, 0, sizeof(peerInfo));
memcpy(peerInfo.peer_addr, peer_addr, 6);
peerInfo.channel = 0; // Default channel
peerInfo.encrypt = true;
memcpy(peerInfo.lmk, encryptionKey, 16);
esp_err_t result = esp_now_add_peer(&peerInfo);
if (result != ESP_OK)
{
Serial.printf("Failed to add peer: %d\n", result);
}
else
{
Serial.println("Peer added successfully");
}
}
void sendMACAddress(const uint8_t *mac_addr)
{
uint8_t myMAC[6];
esp_wifi_get_mac(WIFI_IF_STA, myMAC);
printMAC(myMAC, "Sending back our MAC address: ");
esp_err_t result = esp_now_send(mac_addr, myMAC, 6);
if (result == ESP_OK)
{
Serial.println("Sent back our MAC address");
}
else
{
Serial.printf("Error sending data: %d\n", result);
}
}
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int len)
{
Serial.println("Received data:");
printMAC(mac_addr, " From MAC: ");
Serial.print(" Data: ");
for (int i = 0; i < len; i++)
{
Serial.printf("%02X ", data[i]);
}
Serial.println();
if (len == 16 && memcmp(data, encryptionKey, 16) == 0)
{
memcpy(slaveMACAddress, mac_addr, 6);
printMAC(slaveMACAddress, "Slave MAC Address: ");
if (!esp_now_is_peer_exist(slaveMACAddress))
{
addPeer(slaveMACAddress);
}
else
{
Serial.println("Slave is already added as a peer");
}
sendMACAddress(slaveMACAddress);
}
else
{
Serial.println("Received data does not match the encryption key");
}
}
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv);
Serial.println("Master is ready to receive broadcast data");
}
void loop()
{
// Master is primarily waiting for data and sending responses, so nothing needed in loop.
}
slave
#include <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
const uint8_t encryptionKey[16] = {0x30, 0x32, 0x30, 0x34, 0x30, 0x32, 0x30, 0x34, 0x30, 0x32, 0x30, 0x34, 0x30, 0x32, 0x30, 0x34}; // Example key
uint8_t masterMACAddress[6];
bool isMasterAdded = false;
void printMAC(const uint8_t *mac, const char *label)
{
Serial.print(label);
for (int i = 0; i < 6; i++)
{
Serial.printf("%02X", mac[i]);
if (i < 5)
Serial.print(":");
}
Serial.println();
}
void addPeer(const uint8_t *peer_addr)
{
esp_now_peer_info_t peerInfo;
memset(&peerInfo, 0, sizeof(peerInfo));
memcpy(peerInfo.peer_addr, peer_addr, 6);
peerInfo.channel = 0; // Default channel
peerInfo.encrypt = true;
memcpy(peerInfo.lmk, encryptionKey, 16);
esp_err_t result = esp_now_add_peer(&peerInfo);
if (result != ESP_OK)
{
Serial.printf("Failed to add peer: %d\n", result);
}
else
{
Serial.println("Peer added successfully");
}
}
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int len)
{
Serial.println("Received data:");
printMAC(mac_addr, " From MAC: ");
Serial.print(" Data: ");
for (int i = 0; i < len; i++)
{
Serial.printf("%02X ", data[i]);
}
Serial.println();
if (len == 6)
{
memcpy(masterMACAddress, data, 6);
isMasterAdded = true;
printMAC(masterMACAddress, "Master MAC Address: ");
if (!esp_now_is_peer_exist(masterMACAddress))
{
addPeer(masterMACAddress);
}
else
{
Serial.println("Master is already added as a peer");
}
}
else
{
Serial.println("Received data length does not match expected MAC address length");
}
}
void sendBroadcastMessage()
{
uint8_t message[16];
memcpy(message, encryptionKey, 16);
esp_err_t result = esp_now_send(broadcastAddress, message, sizeof(message));
if (result == ESP_OK)
{
Serial.println("Sent broadcast message with encryption key");
}
else
{
Serial.printf("Error sending data: %d\n", result);
}
}
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv);
esp_now_peer_info_t peerInfo;
memset(&peerInfo, 0, sizeof(peerInfo));
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0; // Default channel
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add broadcast peer");
return;
}
Serial.println("Slave is ready to send broadcast messages");
}
void loop()
{
sendBroadcastMessage();
delay(3000); // Send data every 3 seconds
if (!isMasterAdded)
{
Serial.println("Master is not added yet!");
}
if (isMasterAdded)
{
printMAC(masterMACAddress, "Master MAC Address: ");
}
}
isMasterAdded
never matched