A library that provides automatic pairing functionality using ESP-NOW between ESP32 devices.
- Selectable Master/Slave roles at instantiation
- Automatic saving of pairing results to non-volatile memory (EEPROM)
- Functions for reading and clearing pairing data
- Simple API for ESP-NOW communication
- Copy this library folder to the Arduino IDE libraries folder
- Restart Arduino IDE
#include <ESPNowAutoPairing.h>// For Master use
ESPNowAutoPairing espNow(MASTER);
// For Slave use
ESPNowAutoPairing espNow(SLAVE);void setup() {
Serial.begin(115200);
espNow.begin();
}ESPNowAutoPairing(DeviceRole role)role: SpecifyMASTERorSLAVE
Initialize the library. Call this in setup().
Start pairing mode.
Stop pairing mode.
Check if pairing is complete.
Return value:
true: Pairedfalse: Not paired
Get the paired MAC address.
Return value:
- Pointer to paired MAC address (6 bytes)
Clear saved pairing data.
Send data to paired device.
Parameters:
data: Pointer to data to sendlen: Data size
Set user-defined receive callback function.
Parameters:
callback: Function pointer called on data reception
- Call
startPairingMode()to enter pairing mode - Broadcast PAIR_REQUEST
- Receive PAIR_RESPONSE from Slave
- Send PAIR_CONFIRM to complete pairing
- Call
startPairingMode()to enter pairing mode - Wait for PAIR_REQUEST from Master
- Send PAIR_RESPONSE when PAIR_REQUEST is received
- Receive PAIR_CONFIRM from Master to complete pairing
typedef struct {
uint8_t type; // Message type
uint8_t mac[6]; // Sender's MAC address
uint8_t data[8]; // Additional data
} pairing_message_t;PAIR_REQUEST (0x01): Pairing requestPAIR_RESPONSE (0x02): Pairing responsePAIR_CONFIRM (0x03): Pairing confirmationNES_COMMAND (0x10): Normal data communication
- ESP-NOW callback functions execute in interrupt context
- M5.Lcd drawing operations internally perform task switching and memory allocation
- SPI communication conflicts may occur
// Manage data with global variables
pairing_message_t msg;
bool RCVD = false;
// Only minimal processing in callback function
void OnDataRecvUser(const uint8_t *mac_addr, const uint8_t *incomingData, int data_len) {
if (data_len >= (int)sizeof(pairing_message_t)) {
RCVD = true;
memcpy(&msg, incomingData, sizeof(msg));
}
}
// Safe drawing operations in main loop
void loop() {
if(RCVD == true && PAIR_STATUS == PAIRED) {
if (msg.type == NES_COMMAND) {
// Safe drawing operations here
M5.Lcd.printf("Received: %02X %02X...", msg.data[0], msg.data[1]);
}
RCVD = false; // Reset flag
}
}See the examples folder for detailed usage examples:
MasterAtomS3_M5Unified: Master side implementation using AtomS3SlaveM5StackS3_M5Unified: Slave side implementation using M5StackS3
- Appropriate screen display according to pairing status
- Screen clearing only on status changes (prevents flickering)
- Visual display of MAC addresses and transmitted/received data
- Master/Slave Common: Long press Button A (1 second) to start pairing mode
- Automatic pairing data clear function
- Real-time data transmission/reception display
- MAC address transmission every 2 seconds
- Communication in 8-byte data format
- Detailed logging via serial output
- Uses 7 bytes of EEPROM (6 bytes MAC address + 1 byte pairing status)
- Normal data communication is not performed during pairing mode
- Once pairing is complete, settings are retained even after power off
- Do not perform drawing operations in callback functions (causes memory violations)
- Tested with M5Unified library
This library is released under the MIT License.