Skip to content

Commit 1a80829

Browse files
P-R-O-C-H-Ylucasssvazme-no-devSuGlider
authored
Add ESP-NOW Arduino library (#9395)
* feat(libs): Add ESP-NOW Arduino library * Update libraries/ESP_NOW/src/ESP32_NOW.cpp Co-authored-by: Lucas Saavedra Vaz <[email protected]> * Update libraries/ESP_NOW/src/ESP32_NOW.cpp Co-authored-by: Lucas Saavedra Vaz <[email protected]> * fix(esp-now): Add check if Wifi is started. * Fix ESP_NOW_Serial * Add ESP NOW Serial Example * Add comment * Skip esp-now example for esp32h2 * Add broadcast address constant * Change return value to align with other APIs * Apply suggested changes * Improve example * Fix example * Improve serial example * Add argument to receive callback to know if a message was broadcasted * Update libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp * Simplify example * Add broadcast example * Change comments * Change comment * Improve broadcast master example * Remove examples using IDF's API * Fix example * Add network example * Add skip file * Add LMK back * Add logs * Improve example * Fix example * Apply @SuGlider suggestions from code review Co-authored-by: Rodrigo Garcia <[email protected]> * Add documentation * fix examples links in docs * Apply @lucasssvaz suggestions to docs Co-authored-by: Lucas Saavedra Vaz <[email protected]> * Update espnow.rst * Update examples * make onSent optional and remove underscores for virtual functions * Make onRecieve also optional and make constructor protected --------- Co-authored-by: Lucas Saavedra Vaz <[email protected]> Co-authored-by: me-no-dev <[email protected]> Co-authored-by: Rodrigo Garcia <[email protected]>
1 parent 6c6666a commit 1a80829

File tree

19 files changed

+1731
-707
lines changed

19 files changed

+1731
-707
lines changed

Diff for: CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ set(ARDUINO_ALL_LIBRARIES
8383
DNSServer
8484
EEPROM
8585
ESP_I2S
86+
ESP_NOW
8687
ESP_SR
8788
ESPmDNS
8889
Ethernet
@@ -127,6 +128,10 @@ set(ARDUINO_LIBRARY_DNSServer_SRCS libraries/DNSServer/src/DNSServer.cpp)
127128
set(ARDUINO_LIBRARY_EEPROM_SRCS libraries/EEPROM/src/EEPROM.cpp)
128129

129130
set(ARDUINO_LIBRARY_ESP_I2S_SRCS libraries/ESP_I2S/src/ESP_I2S.cpp)
131+
132+
set(ARDUINO_LIBRARY_ESP_NOW_SRCS
133+
libraries/ESP_NOW/src/ESP32_NOW.cpp
134+
libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp)
130135

131136
set(ARDUINO_LIBRARY_ESP_SR_SRCS
132137
libraries/ESP_SR/src/ESP_SR.cpp

Diff for: docs/en/api/espnow.rst

+261-15
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,275 @@
22
ESP-NOW
33
#######
44

5-
ESP-NOW is a fast, connectionless communication technology featuring a short packet transmission.
6-
ESP-NOW is ideal for smart lights, remote control devices, sensors and other applications.
5+
About
6+
-----
77

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>`_.
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.
10+
ESP-NOW can be used for smart lights, remote control devices, sensors and many other applications.
11+
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. It is an abstract class that must be inherited by a child class that properly handles the peer connections and implements the `_onReceive` and `_onSent` methods.
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. This is a virtual method can be implemented by the upper class for custom handling.
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. This is a virtual method can be implemented by the upper class for custom handling.
251+
252+
.. code-block:: cpp
253+
254+
void onSent(bool success);
255+
256+
* ``success``: ``true`` if the data is sent successfully, ``false`` otherwise.
9257

10258
Examples
11259
--------
12260

13-
ESP-NOW Master
14-
**************
261+
Set of 2 examples of the ESP-NOW library to send and receive data using broadcast messages between multiple ESP32 devices (multiple masters, multiple slaves).
15262

16-
.. literalinclude:: ../../../libraries/ESP32/examples/ESPNow/ESPNow_Basic_Master/ESPNow_Basic_Master.ino
17-
:language: arduino
263+
1. ESP-NOW Broadcast Master Example:
18264

19-
ESP-NOW Slave
20-
*************
265+
.. literalinclude:: ../../../libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Master/ESP_NOW_Broadcast_Master.ino
266+
:language: cpp
21267

22-
.. literalinclude:: ../../../libraries/ESP32/examples/ESPNow/ESPNow_Basic_Slave/ESPNow_Basic_Slave.ino
23-
:language: arduino
268+
2. ESP-NOW Broadcast Slave Example:
24269

25-
Resources
26-
---------
270+
.. literalinclude:: ../../../libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Slave/ESP_NOW_Broadcast_Slave.ino
271+
:language: cpp
27272

28-
* `ESP-NOW`_ (User Guide)
273+
Example of the ESP-NOW Serial library to send and receive data as a stream between 2 ESP32 devices using the serial monitor:
29274

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

Comments
 (0)