ConsentiumNow Library API
The ConsentiumNow
library simplifies the implementation of ESP-NOW communication for ESP32 devices. It provides a templated, type-safe interface for transmitting and receiving structured data between devices.
Class: ConsentiumNow
The ConsentiumNow
class is a templated class designed to handle communication using ESP-NOW. DataType
is a user-defined struct or class representing the data to be exchanged.
Template Parameter
DataType
: A user-defined data structure (e.g., astruct
) that represents the data to be sent/received. Must be serializable (e.g., no pointers or virtual members).
Public Methods
Constructor
Initializes the ConsentiumNow
object. Does not perform any hardware initialization.
void receiveBegin()
Initializes ESP-NOW in receive mode.
Purpose: Sets up the ESP32 to receive data over ESP-NOW and register a callback function to handle incoming messages.
Usage:
Output: Prints initialization status to
Serial
.
*void sendBegin(const uint8_t peerMac)
Initializes ESP-NOW in send mode and sets the receiver’s MAC address.
Parameters:
peerMac
: A 6-byte array representing the receiver's MAC address.
Usage:
Output: Prints initialization status to
Serial
.
*bool addPeer(const uint8_t peerMac)
Adds a peer (sender or receiver) to the ESP-NOW network.
Parameters:
peerMac
: A 6-byte array representing the MAC address of the peer to be added.
Returns:
true
if the peer is added successfully.false
if the operation fails.
Usage:
Output: Prints success/failure status to
Serial
.
void sendData(const DataType &data)
Sends data to the predefined receiver.
Parameters:
data
: A reference to theDataType
object containing the data to be sent.
Usage:
Output:
Prints success/failure status to
Serial
.
bool isDataAvailable()
Checks if new data has been received.
Returns:
true
if new data is available.false
if no data is available.
Usage:
DataType getReceivedData()
Retrieves the most recently received data.
Returns:
The received data of type
DataType
.
Usage:
Note: Resets the data availability flag.
void readMacAddress()
Prints the MAC address of the ESP32 to the Serial
monitor.
Purpose: Useful for identifying devices in an ESP-NOW network.
Usage:
Static Callback Methods
void onDataReceive(const esp_now_recv_info_t info, const uint8_t incoming, int len)
Handles incoming ESP-NOW messages. Automatically called when data is received.
Parameters:
info
: Information about the sender (e.g., MAC address).incoming
: Pointer to the incoming data.len
: Length of the received data.
Purpose: Parses and stores incoming data in the
incomingData
buffer and sets thedataAvailable
flag.Usage: Registered automatically by
receiveBegin()
.
Static Variables
DataType incomingData
Holds the latest received data.
Scope: Private (access via
getReceivedData()
).
bool dataAvailable
Indicates whether new data has been received.
Scope: Private (access via
isDataAvailable()
).
Example: Using ConsentiumNow
Receiver Code
Sender Code
Features
Templated Design: Flexibility to handle any structured data.
Static Methods: Callback management for receiving data.
Ease of Use: Abstracts ESP-NOW complexity, focusing on data exchange.
Known Limitations
Peer Management: Must manually add peers to the network using
addPeer()
.Static Data Buffer: Only one instance of incoming data can be stored.
Last updated