|
2 | 2 | ESP-NOW
|
3 | 3 | #######
|
4 | 4 |
|
5 |
| -ESP-NOW is a fast, connectionless communication technology featuring a short packet transmission. |
| 5 | +About |
| 6 | +----- |
| 7 | + |
| 8 | +ESP-NOW is a communication protocol designed for low-power, low-latency, and high-throughput communication between ESP32 devices without the need for an access point (AP). |
| 9 | +It is ideal for scenarios where devices need to communicate directly with each other in a local network. |
6 | 10 | ESP-NOW is ideal for smart lights, remote control devices, sensors and other applications.
|
7 | 11 |
|
8 |
| -.. note:: This is a work in progress project and this section is still missing. If you want to contribute, please see the `Contributions Guide <../contributing.html>`_. |
| 12 | +This library provides an easy-to-use interface for setting up ESP-NOW communication, adding and removing peers, and sending and receiving data packets. |
| 13 | + |
| 14 | +Arduino-ESP32 ESP-NOW API |
| 15 | +------------------------- |
| 16 | + |
| 17 | +ESP-NOW Class |
| 18 | +************* |
| 19 | + |
| 20 | +The `ESP_NOW_Class` is the main class used for managing ESP-NOW communication. |
| 21 | + |
| 22 | +begin |
| 23 | +^^^^^ |
| 24 | + |
| 25 | +Initialize the ESP-NOW communication. This function must be called before using any other ESP-NOW functionalities. |
| 26 | + |
| 27 | +.. code-block:: cpp |
| 28 | +
|
| 29 | + bool begin(const uint8_t *pmk = NULL); |
| 30 | +
|
| 31 | +* ``pmk``: Optional. Pass the pairwise master key (PMK) if encryption is enabled. |
| 32 | + |
| 33 | +Returns ``true`` if initialization is successful, ``false`` otherwise. |
| 34 | + |
| 35 | +end |
| 36 | +^^^ |
| 37 | + |
| 38 | +End the ESP-NOW communication. This function releases all resources used by the ESP-NOW library. |
| 39 | + |
| 40 | +.. code-block:: cpp |
| 41 | +
|
| 42 | + bool end(); |
| 43 | +
|
| 44 | +Returns ``true`` if the operation is successful, ``false`` otherwise. |
| 45 | + |
| 46 | +getTotalPeerCount |
| 47 | +^^^^^^^^^^^^^^^^^ |
| 48 | + |
| 49 | +Get the total number of peers currently added. |
| 50 | + |
| 51 | +.. code-block:: cpp |
| 52 | +
|
| 53 | + int getTotalPeerCount(); |
| 54 | +
|
| 55 | +Returns the total number of peers, or ``-1`` if an error occurs. |
| 56 | + |
| 57 | +getEncryptedPeerCount |
| 58 | +^^^^^^^^^^^^^^^^^^^^^ |
| 59 | + |
| 60 | +Get the number of peers using encryption. |
| 61 | + |
| 62 | +.. code-block:: cpp |
| 63 | +
|
| 64 | + int getEncryptedPeerCount(); |
| 65 | +
|
| 66 | +Returns the number of peers using encryption, or ``-1`` if an error occurs. |
| 67 | + |
| 68 | +onNewPeer |
| 69 | +^^^^^^^^^ |
| 70 | + |
| 71 | +You can register a callback function to handle incoming data from new peers using the `onNewPeer` function. |
| 72 | + |
| 73 | +.. code-block:: cpp |
| 74 | +
|
| 75 | + void onNewPeer(void (*cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg), void *arg); |
| 76 | +
|
| 77 | +* ``cb``: Pointer to the callback function. |
| 78 | +* ``arg``: Optional. Pointer to user-defined argument to be passed to the callback function. |
| 79 | + |
| 80 | +``cb`` function signature: |
| 81 | + |
| 82 | +.. code-block:: cpp |
| 83 | +
|
| 84 | + void cb(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg); |
| 85 | +
|
| 86 | +``info``: Information about the received packet. |
| 87 | +``data``: Pointer to the received data. |
| 88 | +``len``: Length of the received data. |
| 89 | +``arg``: User-defined argument passed to the callback function. |
| 90 | + |
| 91 | +ESP-NOW Peer Class |
| 92 | +****************** |
| 93 | + |
| 94 | +The `ESP_NOW_Peer` class represents a peer device in the ESP-NOW network. |
| 95 | + |
| 96 | +Constructor |
| 97 | +^^^^^^^^^^^ |
| 98 | + |
| 99 | +Create an instance of the `ESP_NOW_Peer` class. |
| 100 | + |
| 101 | +.. code-block:: cpp |
| 102 | +
|
| 103 | + ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk); |
| 104 | +
|
| 105 | +* ``mac_addr``: MAC address of the peer device. |
| 106 | +* ``channel``: Communication channel. |
| 107 | +* ``iface``: WiFi interface. |
| 108 | +* ``lmk``: Optional. Pass the local master key (LMK) if encryption is enabled. |
| 109 | + |
| 110 | +add |
| 111 | +^^^ |
| 112 | + |
| 113 | +Add the peer to the ESP-NOW network. |
| 114 | + |
| 115 | +.. code-block:: cpp |
| 116 | +
|
| 117 | + bool add(); |
| 118 | +
|
| 119 | +Returns ``true`` if the peer is added successfully, ``false`` otherwise. |
| 120 | + |
| 121 | +remove |
| 122 | +^^^^^^ |
| 123 | + |
| 124 | +Remove the peer from the ESP-NOW network. |
| 125 | + |
| 126 | +.. code-block:: cpp |
| 127 | +
|
| 128 | + bool remove(); |
| 129 | +
|
| 130 | +Returns ``true`` if the peer is removed successfully, ``false`` otherwise. |
| 131 | + |
| 132 | +send |
| 133 | +^^^^ |
| 134 | + |
| 135 | +Send data to the peer. |
| 136 | + |
| 137 | +.. code-block:: cpp |
| 138 | +
|
| 139 | + size_t send(const uint8_t *data, int len); |
| 140 | +
|
| 141 | +* ``data``: Pointer to the data to be sent. |
| 142 | +* ``len``: Length of the data in bytes. |
| 143 | + |
| 144 | +Returns the number of bytes sent, or ``0`` if an error occurs. |
| 145 | + |
| 146 | +addr |
| 147 | +^^^^ |
| 148 | + |
| 149 | +Get the MAC address of the peer. |
| 150 | + |
| 151 | +.. code-block:: cpp |
| 152 | +
|
| 153 | + const uint8_t * addr() const; |
| 154 | +
|
| 155 | +Returns a pointer to the MAC address. |
| 156 | + |
| 157 | +addr |
| 158 | +^^^^ |
| 159 | + |
| 160 | +Set the MAC address of the peer. |
| 161 | + |
| 162 | +.. code-block:: cpp |
| 163 | +
|
| 164 | + void addr(const uint8_t *mac_addr); |
| 165 | +
|
| 166 | +* ``mac_addr``: MAC address of the peer. |
| 167 | + |
| 168 | +getChannel |
| 169 | +^^^^^^^^^^ |
| 170 | + |
| 171 | +Get the communication channel of the peer. |
| 172 | + |
| 173 | +.. code-block:: cpp |
| 174 | +
|
| 175 | + uint8_t getChannel() const; |
| 176 | +
|
| 177 | +Returns the communication channel. |
| 178 | + |
| 179 | +setChannel |
| 180 | +^^^^^^^^^^ |
| 181 | + |
| 182 | +Set the communication channel of the peer. |
| 183 | + |
| 184 | +.. code-block:: cpp |
| 185 | +
|
| 186 | + void setChannel(uint8_t channel); |
| 187 | +
|
| 188 | +* ``channel``: Communication channel. |
| 189 | + |
| 190 | +getInterface |
| 191 | +^^^^^^^^^^^^ |
| 192 | + |
| 193 | +Get the WiFi interface of the peer. |
| 194 | + |
| 195 | +.. code-block:: cpp |
| 196 | +
|
| 197 | + wifi_interface_t getInterface() const; |
| 198 | +
|
| 199 | +Returns the WiFi interface. |
| 200 | + |
| 201 | +setInterface |
| 202 | +^^^^^^^^^^^^ |
| 203 | + |
| 204 | +Set the WiFi interface of the peer. |
| 205 | + |
| 206 | +.. code-block:: cpp |
| 207 | +
|
| 208 | + void setInterface(wifi_interface_t iface); |
| 209 | +
|
| 210 | +* ``iface``: WiFi interface. |
| 211 | + |
| 212 | +isEncrypted |
| 213 | +^^^^^^^^^^^ |
| 214 | + |
| 215 | +Check if the peer is using encryption. |
| 216 | + |
| 217 | +.. code-block:: cpp |
| 218 | +
|
| 219 | + bool isEncrypted() const; |
| 220 | +
|
| 221 | +Returns ``true`` if the peer is using encryption, ``false`` otherwise. |
| 222 | + |
| 223 | +setKey |
| 224 | +^^^^^^ |
| 225 | + |
| 226 | +Set the local master key (LMK) for the peer. |
| 227 | + |
| 228 | +.. code-block:: cpp |
| 229 | +
|
| 230 | + void setKey(const uint8_t *lmk); |
| 231 | +
|
| 232 | +* ``lmk``: Local master key. |
| 233 | + |
| 234 | +_onReceive |
| 235 | +^^^^^^^^^^ |
| 236 | + |
| 237 | +Callback function to handle incoming data from the peer, must be implemented by the upper class. |
| 238 | + |
| 239 | +.. code-block:: cpp |
| 240 | +
|
| 241 | + void _onReceive(const uint8_t *data, int len, bool broadcast); |
| 242 | +
|
| 243 | +* ``data``: Pointer to the received data. |
| 244 | +* ``len``: Length of the received data. |
| 245 | +* ``broadcast``: ``true`` if the data is broadcasted, ``false`` otherwise. |
| 246 | + |
| 247 | +_onSent |
| 248 | +^^^^^^^ |
| 249 | + |
| 250 | +Callback function to handle the completion of sending data to the peer, must be implemented by the upper class. |
| 251 | + |
| 252 | +.. code-block:: cpp |
| 253 | +
|
| 254 | + void _onSent(bool success); |
| 255 | +
|
| 256 | +* ``success``: ``true`` if the data is sent successfully, ``false`` otherwise. |
9 | 257 |
|
10 | 258 | Examples
|
11 | 259 | --------
|
12 | 260 |
|
13 |
| -ESP-NOW Master |
14 |
| -************** |
| 261 | +Set of 2 examples of the ESP-NOW library to send and receive data between multiple ESP32 devices (1 master, multiple slaves). |
15 | 262 |
|
16 |
| -.. literalinclude:: ../../../libraries/ESP32/examples/ESPNow/ESPNow_Basic_Master/ESPNow_Basic_Master.ino |
17 |
| - :language: arduino |
| 263 | +1. ESP-NOW Broadcast Master Example: |
18 | 264 |
|
19 |
| -ESP-NOW Slave |
20 |
| -************* |
| 265 | +.. literalinclude:: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Master/ESP_NOW_Broadcast_Master.ino |
| 266 | + :language: cpp |
21 | 267 |
|
22 |
| -.. literalinclude:: ../../../libraries/ESP32/examples/ESPNow/ESPNow_Basic_Slave/ESPNow_Basic_Slave.ino |
23 |
| - :language: arduino |
| 268 | +2. ESP-NOW Broadcast Slave Example: |
24 | 269 |
|
25 |
| -Resources |
26 |
| ---------- |
| 270 | +.. literalinclude:: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Slave/ESP_NOW_Broadcast_Slave.ino |
| 271 | + :language: cpp |
27 | 272 |
|
28 |
| -* `ESP-NOW`_ (User Guide) |
| 273 | +Example of the ESP-NOW Serial library to send and receive data between 2 ESP32 devices using the serial port: |
29 | 274 |
|
30 |
| -.. _ESP-NOW: https://www.espressif.com/sites/default/files/documentation/esp-now_user_guide_en.pdf |
| 275 | +.. literalinclude:: libraries/ESP_NOW/examples/ESP_NOW_Serial/ESP_NOW_Serial.ino |
| 276 | + :language: cpp |
0 commit comments