Skip to content

Add ESP-NOW Arduino library #9395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2c68fdc
feat(libs): Add ESP-NOW Arduino library
P-R-O-C-H-Y Mar 20, 2024
f1a6b90
Update libraries/ESP_NOW/src/ESP32_NOW.cpp
P-R-O-C-H-Y Mar 21, 2024
26410d9
Update libraries/ESP_NOW/src/ESP32_NOW.cpp
P-R-O-C-H-Y Mar 21, 2024
1f34934
fix(esp-now): Add check if Wifi is started.
P-R-O-C-H-Y Mar 21, 2024
c5ed3b1
Fix ESP_NOW_Serial
me-no-dev Mar 21, 2024
20489b3
Add ESP NOW Serial Example
lucasssvaz Mar 21, 2024
e3b6715
Add comment
lucasssvaz Mar 22, 2024
2b7730c
Skip esp-now example for esp32h2
P-R-O-C-H-Y Mar 22, 2024
6bf369a
Merge branch 'master' into feat/espnow-library
P-R-O-C-H-Y Mar 22, 2024
deacaac
Add broadcast address constant
lucasssvaz Mar 25, 2024
271f94b
Change return value to align with other APIs
lucasssvaz Mar 25, 2024
8751b98
Apply suggested changes
lucasssvaz Mar 25, 2024
6d04c83
Improve example
lucasssvaz Mar 25, 2024
8a6025c
Fix example
lucasssvaz Mar 25, 2024
fac9f95
Merge branch 'master' into feat/espnow-library
lucasssvaz Mar 26, 2024
6bf981e
Improve serial example
lucasssvaz Mar 26, 2024
07f7de0
Add argument to receive callback to know if a message was broadcasted
lucasssvaz Mar 26, 2024
03d386b
Update libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp
lucasssvaz Mar 27, 2024
52c4ec2
Simplify example
lucasssvaz Mar 27, 2024
98a6ae0
Merge branch 'feat/espnow-library' of ssh://github.com/P-R-O-C-H-Y/ar…
lucasssvaz Mar 27, 2024
03e837c
Add broadcast example
lucasssvaz Mar 27, 2024
6a0774c
Change comments
lucasssvaz Mar 27, 2024
34168a1
Change comment
lucasssvaz Mar 27, 2024
ab31ab8
Improve broadcast master example
lucasssvaz Mar 28, 2024
5ca2af1
Remove examples using IDF's API
lucasssvaz Mar 28, 2024
c435a7f
Fix example
lucasssvaz Mar 29, 2024
6ad83cf
Add network example
lucasssvaz Mar 29, 2024
c0b2042
Add skip file
lucasssvaz Mar 29, 2024
4085a2f
Add LMK back
lucasssvaz Mar 29, 2024
f7278fa
Add logs
lucasssvaz Mar 29, 2024
6f6e889
Improve example
lucasssvaz Apr 1, 2024
705b1ee
Fix example
lucasssvaz Apr 1, 2024
5fe5f77
Apply @suglider suggestions from code review
P-R-O-C-H-Y Apr 2, 2024
a815a15
Merge branch 'master' into feat/espnow-library
P-R-O-C-H-Y Apr 2, 2024
9017067
Add documentation
P-R-O-C-H-Y Apr 2, 2024
2b43448
fix examples links in docs
P-R-O-C-H-Y Apr 2, 2024
fc7616e
Apply @lucasssvaz suggestions to docs
P-R-O-C-H-Y Apr 2, 2024
14f19be
Update espnow.rst
P-R-O-C-H-Y Apr 3, 2024
ad193ae
Update examples
lucasssvaz Apr 3, 2024
439840b
Merge branch 'feat/espnow-library' of ssh://github.com/P-R-O-C-H-Y/ar…
lucasssvaz Apr 3, 2024
51ce71f
make onSent optional and remove underscores for virtual functions
P-R-O-C-H-Y Apr 3, 2024
55a6193
Make onRecieve also optional and make constructor protected
P-R-O-C-H-Y Apr 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ set(ARDUINO_ALL_LIBRARIES
DNSServer
EEPROM
ESP_I2S
ESP_NOW
ESP_SR
ESPmDNS
Ethernet
Expand Down Expand Up @@ -127,6 +128,10 @@ set(ARDUINO_LIBRARY_DNSServer_SRCS libraries/DNSServer/src/DNSServer.cpp)
set(ARDUINO_LIBRARY_EEPROM_SRCS libraries/EEPROM/src/EEPROM.cpp)

set(ARDUINO_LIBRARY_ESP_I2S_SRCS libraries/ESP_I2S/src/ESP_I2S.cpp)

set(ARDUINO_LIBRARY_ESP_NOW_SRCS
libraries/ESP_NOW/src/ESP32_NOW.cpp
libraries/ESP_NOW/src/ESP32_NOW_Serial.cpp)

set(ARDUINO_LIBRARY_ESP_SR_SRCS
libraries/ESP_SR/src/ESP_SR.cpp
Expand Down
274 changes: 260 additions & 14 deletions docs/en/api/espnow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,275 @@
ESP-NOW
#######

ESP-NOW is a fast, connectionless communication technology featuring a short packet transmission.
About
-----

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).
It is ideal for scenarios where devices need to communicate directly with each other in a local network.
ESP-NOW is ideal for smart lights, remote control devices, sensors and other applications.

.. 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>`_.
This library provides an easy-to-use interface for setting up ESP-NOW communication, adding and removing peers, and sending and receiving data packets.

Arduino-ESP32 ESP-NOW API
-------------------------

ESP-NOW Class
*************

The `ESP_NOW_Class` is the main class used for managing ESP-NOW communication.

begin
^^^^^

Initialize the ESP-NOW communication. This function must be called before using any other ESP-NOW functionalities.

.. code-block:: cpp

bool begin(const uint8_t *pmk = NULL);

* ``pmk``: Optional. Pass the pairwise master key (PMK) if encryption is enabled.

Returns ``true`` if initialization is successful, ``false`` otherwise.

end
^^^

End the ESP-NOW communication. This function releases all resources used by the ESP-NOW library.

.. code-block:: cpp

bool end();

Returns ``true`` if the operation is successful, ``false`` otherwise.

getTotalPeerCount
^^^^^^^^^^^^^^^^^

Get the total number of peers currently added.

.. code-block:: cpp

int getTotalPeerCount();

Returns the total number of peers, or ``-1`` if an error occurs.

getEncryptedPeerCount
^^^^^^^^^^^^^^^^^^^^^

Get the number of peers using encryption.

.. code-block:: cpp

int getEncryptedPeerCount();

Returns the number of peers using encryption, or ``-1`` if an error occurs.

onNewPeer
^^^^^^^^^

You can register a callback function to handle incoming data from new peers using the `onNewPeer` function.

.. code-block:: cpp

void onNewPeer(void (*cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg), void *arg);

* ``cb``: Pointer to the callback function.
* ``arg``: Optional. Pointer to user-defined argument to be passed to the callback function.

``cb`` function signature:

.. code-block:: cpp

void cb(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg);

``info``: Information about the received packet.
``data``: Pointer to the received data.
``len``: Length of the received data.
``arg``: User-defined argument passed to the callback function.

ESP-NOW Peer Class
******************

The `ESP_NOW_Peer` class represents a peer device in the ESP-NOW network.

Constructor
^^^^^^^^^^^

Create an instance of the `ESP_NOW_Peer` class.

.. code-block:: cpp

ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk);

* ``mac_addr``: MAC address of the peer device.
* ``channel``: Communication channel.
* ``iface``: WiFi interface.
* ``lmk``: Optional. Pass the local master key (LMK) if encryption is enabled.

add
^^^

Add the peer to the ESP-NOW network.

.. code-block:: cpp

bool add();

Returns ``true`` if the peer is added successfully, ``false`` otherwise.

remove
^^^^^^

Remove the peer from the ESP-NOW network.

.. code-block:: cpp

bool remove();

Returns ``true`` if the peer is removed successfully, ``false`` otherwise.

send
^^^^

Send data to the peer.

.. code-block:: cpp

size_t send(const uint8_t *data, int len);

* ``data``: Pointer to the data to be sent.
* ``len``: Length of the data in bytes.

Returns the number of bytes sent, or ``0`` if an error occurs.

addr
^^^^

Get the MAC address of the peer.

.. code-block:: cpp

const uint8_t * addr() const;

Returns a pointer to the MAC address.

addr
^^^^

Set the MAC address of the peer.

.. code-block:: cpp

void addr(const uint8_t *mac_addr);

* ``mac_addr``: MAC address of the peer.

getChannel
^^^^^^^^^^

Get the communication channel of the peer.

.. code-block:: cpp

uint8_t getChannel() const;

Returns the communication channel.

setChannel
^^^^^^^^^^

Set the communication channel of the peer.

.. code-block:: cpp

void setChannel(uint8_t channel);

* ``channel``: Communication channel.

getInterface
^^^^^^^^^^^^

Get the WiFi interface of the peer.

.. code-block:: cpp

wifi_interface_t getInterface() const;

Returns the WiFi interface.

setInterface
^^^^^^^^^^^^

Set the WiFi interface of the peer.

.. code-block:: cpp

void setInterface(wifi_interface_t iface);

* ``iface``: WiFi interface.

isEncrypted
^^^^^^^^^^^

Check if the peer is using encryption.

.. code-block:: cpp

bool isEncrypted() const;

Returns ``true`` if the peer is using encryption, ``false`` otherwise.

setKey
^^^^^^

Set the local master key (LMK) for the peer.

.. code-block:: cpp

void setKey(const uint8_t *lmk);

* ``lmk``: Local master key.

_onReceive
^^^^^^^^^^

Callback function to handle incoming data from the peer, must be implemented by the upper class.

.. code-block:: cpp

void _onReceive(const uint8_t *data, int len, bool broadcast);

* ``data``: Pointer to the received data.
* ``len``: Length of the received data.
* ``broadcast``: ``true`` if the data is broadcasted, ``false`` otherwise.

_onSent
^^^^^^^

Callback function to handle the completion of sending data to the peer, must be implemented by the upper class.

.. code-block:: cpp

void _onSent(bool success);

* ``success``: ``true`` if the data is sent successfully, ``false`` otherwise.

Examples
--------

ESP-NOW Master
**************
Set of 2 examples of the ESP-NOW library to send and receive data between multiple ESP32 devices (1 master, multiple slaves).

.. literalinclude:: ../../../libraries/ESP32/examples/ESPNow/ESPNow_Basic_Master/ESPNow_Basic_Master.ino
:language: arduino
1. ESP-NOW Broadcast Master Example:

ESP-NOW Slave
*************
.. literalinclude:: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Master/ESP_NOW_Broadcast_Master.ino
:language: cpp

.. literalinclude:: ../../../libraries/ESP32/examples/ESPNow/ESPNow_Basic_Slave/ESPNow_Basic_Slave.ino
:language: arduino
2. ESP-NOW Broadcast Slave Example:

Resources
---------
.. literalinclude:: libraries/ESP_NOW/examples/ESP_NOW_Broadcast_Slave/ESP_NOW_Broadcast_Slave.ino
:language: cpp

* `ESP-NOW`_ (User Guide)
Example of the ESP-NOW Serial library to send and receive data between 2 ESP32 devices using the serial port:

.. _ESP-NOW: https://www.espressif.com/sites/default/files/documentation/esp-now_user_guide_en.pdf
.. literalinclude:: libraries/ESP_NOW/examples/ESP_NOW_Serial/ESP_NOW_Serial.ino
:language: cpp
Loading
Loading