Skip to content

Commit f16f003

Browse files
Add documentation for the new espnow component (#4086)
Co-authored-by: Jesse Hills <[email protected]>
1 parent a91c4cc commit f16f003

File tree

3 files changed

+217
-0
lines changed

3 files changed

+217
-0
lines changed

components/espnow.rst

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
ESPNow communication Component
2+
==============================
3+
4+
.. seo::
5+
:description: Instructions for setting up the basic ESPNow component in ESPHome.
6+
:image: esp-now.svg
7+
8+
This component allows ESPHome to communicate with esp32 devices in a simple and unrestricted way.
9+
It enables the option to interact with other esp32 devices over the Espressif's ESP-NOW protocol, see
10+
`documentation <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/network/esp_now.html>`__
11+
12+
.. note::
13+
14+
Broadcasting data is not recommended, this will also reach devices not controlled by you that use the esp-now protocol.
15+
The best solution is to minimize the broadcasting as much as possible and use it only for identification purposes.
16+
17+
.. code-block:: yaml
18+
19+
# Example configuration entry
20+
espnow:
21+
22+
Configuration variables:
23+
------------------------
24+
25+
- **channel** (*Optional*, int): The Wi-Fi channel that the esp-now communication will use to send/receive data packets.
26+
Cannot be set when the :doc:`wifi` is used, as it will use the same channel as the wifi network.
27+
- **auto_add_peer** (*Optional*, boolean): This will allow the esp-now component to automatically add any new incoming device as a peer.
28+
See :ref:`espnow-peers` below. Defaults to ``false``.
29+
- **enable_on_boot** (*Optional*, boolean): Enable the esp-now component on boot. Defaults to ``true``.
30+
- **peers** (*Optional*, list): A peer is the name for devices that use esp-now. The list will have all MAC addresses from
31+
the devices where this device may communicate with. See :ref:`espnow-peers` below.
32+
33+
Automations:
34+
35+
- **on_receive** (*Optional*, :ref:`Automation <automation>`): An automation to perform when data is received. See :ref:`espnow-on_receive`.
36+
- **on_unknown_peer** (*Optional*, :ref:`Automation <automation>`): An automation to perform when data is received from an unknown peer.
37+
Cannot be used when ``auto_add_peer`` is set to ``true``. See :ref:`espnow-on_unknown_peer`.
38+
- **on_broadcast** (*Optional*, :ref:`Automation <automation>`): An automation to perform when a broadcast packet is received.
39+
See :ref:`espnow-on_broadcast`.
40+
41+
Automations
42+
-----------
43+
44+
There will be 3 variables available to automations. Their memory will be cleaned up after the automation is
45+
done and will not be available if there are any `delay` actions or others that do work "asynchronously" in the automation.
46+
47+
- **info**: :apistruct:`espnow::ESPNowRecvInfo` with information about the received packet.
48+
- **data**: A `const uint8_t *` - pointer to the data.
49+
- **size**: The size of the data in bytes.
50+
51+
.. code-block:: yaml
52+
53+
espnow:
54+
on_...:
55+
- logger.log:
56+
format: "Sent to %s from %s: %s RSSI: %ddBm"
57+
args:
58+
- format_mac_address_pretty(info.des_addr).c_str()
59+
- format_mac_address_pretty(info.src_addr).c_str()
60+
- format_hex_pretty(data, size).c_str()
61+
- info.rx_ctrl->rssi
62+
63+
.. _espnow-on_receive:
64+
65+
``on_receive``
66+
**************
67+
68+
This automation will be triggered when data is received from a registered peer.
69+
70+
Configuration variables:
71+
72+
- **address** (*Optional*, MAC Address): Filter this trigger to packets where the source address matches. If not set, it will match any device.
73+
74+
.. _espnow-on_unknown_peer:
75+
76+
``on_unknown_peer``
77+
*******************
78+
79+
This automation will be triggered when data is received from a peer that is not in the list of known peers.
80+
81+
.. _espnow-on_broadcast:
82+
83+
``on_broadcast``
84+
****************
85+
86+
This automation will be triggered when a broadcast packet is received.
87+
88+
Configuration variables:
89+
90+
- **address** (*Optional*, MAC Address): Filter this trigger to packets where the source address matches. If not set, it will match any device.
91+
92+
93+
.. _espnow-send-action:
94+
95+
``espnow.send`` Action
96+
***********************
97+
98+
This is an :ref:`Action <config-action>` for sending a data packet over the espnow protocol.
99+
100+
.. code-block:: yaml
101+
102+
on_...:
103+
- espnow.send:
104+
address: 11:22:33:44:55:66
105+
data: "The big angry wolf awakens"
106+
- espnow.send:
107+
address: 11:22:33:44:55:66
108+
data: !lambda "return {0x00, 0x00, 0x34, 0x5d};"
109+
- espnow.send:
110+
address: !lambda "return {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};"
111+
data: [0x00, 0x00, 0x34, 0x5d]
112+
- espnow.send:
113+
address: !lambda "return {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};"
114+
data: !lambda "return {0x00, 0x00, 0x34, 0x5d};"
115+
116+
117+
Configuration variables:
118+
119+
- **address** (**Required**, :ref:`templatable <config-templatable>`, MAC Address): The MAC address of the receiving device to send to.
120+
- **data** (**Required**, :ref:`templatable <config-templatable>`, string or list of bytes): The data to be sent.
121+
- **on_sent** (*Optional*, :ref:`Automation <automation>`): An automation to perform when the data is sent successfully.
122+
- **wait_for_sent** (*Optional*, boolean): The automation will wait for the data to be sent and for the ``on_sent`` or ``on_error``
123+
actions to be finished before continuing with the next action.
124+
Defaults to ``true``.
125+
- **on_error** (*Optional*, :ref:`Automation <automation>`): An automation to perform when the data could not be sent.
126+
- **continue_on_error** (*Optional*, boolean): If set to ``false``, the next action will not be triggered if the data could not be sent.
127+
Defaults to ``true``.
128+
129+
130+
.. _espnow-broadcast-action:
131+
132+
``espnow.broadcast`` Action
133+
***************************
134+
135+
This is an :ref:`Action <config-action>` for sending a data packet over the espnow protocol to any device that is listening.
136+
137+
.. code-block:: yaml
138+
139+
on_...:
140+
- espnow.broadcast:
141+
data: "The big angry wolf awakens"
142+
- espnow.broadcast:
143+
data: !lambda "return {0x00, 0x00, 0x34, 0x5d};"
144+
- espnow.broadcast:
145+
data: [0x00, 0x00, 0x34, 0x5d]
146+
147+
Configuration variables:
148+
149+
- **data** (**Required**, :ref:`templatable <config-templatable>`, string or list of bytes): The data to be sent.
150+
151+
152+
.. _espnow-peer_add-action:
153+
154+
``espnow.peer.add`` Action
155+
**************************
156+
157+
This is an :ref:`Action <config-action>` to add a new peer to the internal allowed peers list.
158+
159+
.. code-block:: yaml
160+
161+
on_...:
162+
- espnow.peer.add:
163+
address: 11:22:33:44:55:66
164+
- espnow.peer.add:
165+
address: !lambda "return {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};"
166+
167+
168+
Configuration variables:
169+
170+
- **address** (**Required**, MAC Address): The Peer address that needs to be added to the list of allowed peers.
171+
172+
173+
.. _espnow-peer_delete-action:
174+
175+
``espnow.peer.delete`` Action
176+
*****************************
177+
178+
This is an :ref:`Action <config-action>` to remove a known peer from the internal allowed peers list.
179+
180+
.. code-block:: yaml
181+
182+
on_...:
183+
- espnow.peer.delete:
184+
address: 11:22:33:44:55:66
185+
- espnow.peer.delete:
186+
address: !lambda "return {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};"
187+
188+
Configuration variables:
189+
190+
- **address** (**Required**, MAC Address): The Peer address that needs to be removed from the list of allowed peers.
191+
192+
193+
.. _espnow-peers:
194+
195+
Peers
196+
-----
197+
198+
A peer is a device that this device is allowed to send to. Broadcast and unencrypted unicast data can be received from
199+
any device without explicitly adding it as a peer.
200+
201+
If ``auto_add_peer`` is set to ``false`` and you have not added any peers, then only broadcasts can be sent and there
202+
will be an error when trying to send data to a peer.
203+
204+
Setting ``auto_add_peer`` to ``true`` will allow the component to automatically add any incoming device as a peer, and will
205+
automatically add any peer that data is sent to.
206+
207+
See Also
208+
--------
209+
210+
- :apiref:`espnow/espnow.h`
211+
- :ghedit:`Edit`
212+
213+
.. toctree::
214+
:maxdepth: 1
215+
:glob:

components/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Network Protocols
103103
Network Core, components/network, server-network.svg, dark-invert
104104
Native API, components/api, server-network.svg, dark-invert
105105
MQTT, components/mqtt, mqtt.png
106+
ESP-NOW, components/espnow, esp-now.svg
106107
HTTP Request, components/http_request, connection.svg, dark-invert
107108
mDNS, components/mdns, radio-tower.svg, dark-invert
108109
WireGuard, components/wireguard, wireguard_custom_logo.svg, dark-invert

images/esp-now.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)