> For the complete documentation index, see [llms.txt](https://docs.consentiumiot.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.consentiumiot.com/code-usage/consentiumnow-library-api.md).

# 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., a `struct`) that represents the data to be sent/received. Must be serializable (e.g., no pointers or virtual members).

***

### **Public Methods**

#### **Constructor**

```cpp
ConsentiumNow();
```

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**:

  ```cpp
  consentiumNowInstance.receiveBegin();
  ```
* **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**:

  ```cpp
  uint8_t receiverMac[] = {0x24, 0x6F, 0x28, 0x11, 0x22, 0x33};
  consentiumNowInstance.sendBegin(receiverMac);
  ```
* **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**:

  ```cpp
  uint8_t senderMac[] = {0x24, 0x6F, 0x28, 0xAA, 0xBB, 0xCC};
  bool success = consentiumNowInstance.addPeer(senderMac);
  ```
* **Output**: Prints success/failure status to `Serial`.

***

#### **void sendData(const DataType \&data)**

Sends data to the predefined receiver.

* **Parameters**:
  * `data`: A reference to the `DataType` object containing the data to be sent.
* **Usage**:

  ```cpp
  SensorData sensorData = {25.5, 60.0};
  consentiumNowInstance.sendData(sensorData);
  ```
* **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**:

  ```cpp
  if (consentiumNowInstance.isDataAvailable()) {
      // Handle received data
  }
  ```

***

#### **DataType getReceivedData()**

Retrieves the most recently received data.

* **Returns**:
  * The received data of type `DataType`.
* **Usage**:

  ```cpp
  SensorData receivedData = consentiumNowInstance.getReceivedData();
  Serial.printf("Temperature: %.2f, Humidity: %.2f\n", 
                receivedData.temperature, receivedData.humidity);
  ```
* **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**:

  ```cpp
  consentiumNowInstance.readMacAddress();
  ```

***

### **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 the `dataAvailable` 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**

```cpp
#include "ConsentiumNow.h"

struct SensorData {
    float temperature;
    float humidity;
};

ConsentiumNow<SensorData> receiver;

void setup() {
    Serial.begin(115200);
    receiver.receiveBegin();
}

void loop() {
    if (receiver.isDataAvailable()) {
        SensorData data = receiver.getReceivedData();
        Serial.printf("Temperature: %.2f, Humidity: %.2f\n", 
                      data.temperature, data.humidity);
    }
}
```

#### **Sender Code**

```cpp
#include "ConsentiumNow.h"

struct SensorData {
    float temperature;
    float humidity;
};

ConsentiumNow<SensorData> sender;

uint8_t receiverMac[] = {0x24, 0x6F, 0x28, 0x11, 0x22, 0x33};

void setup() {
    Serial.begin(115200);
    sender.sendBegin(receiverMac);
}

void loop() {
    SensorData data = {random(200, 300) / 10.0, random(400, 600) / 10.0};
    sender.sendData(data);
    delay(2000);
}
```

***

### **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**

1. **Peer Management**: Must manually add peers to the network using `addPeer()`.
2. **Static Data Buffer**: Only one instance of incoming data can be stored.
