ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
espnow_component.cpp
Go to the documentation of this file.
1#include "espnow_component.h"
2
3#ifdef USE_ESP32
4
5#include "espnow_err.h"
6
7#include <cinttypes>
8
12#include "esphome/core/log.h"
13
14#include <esp_event.h>
15#include <esp_mac.h>
16#include <esp_netif.h>
17#include <esp_now.h>
18#include <esp_random.h>
19#include <esp_wifi.h>
20#include <cstring>
21#include <memory>
22
23#ifdef USE_WIFI
25#endif
26
27namespace esphome::espnow {
28
29static constexpr const char *TAG = "espnow";
30
31static const esp_err_t CONFIG_ESPNOW_WAKE_WINDOW = 50;
32static const esp_err_t CONFIG_ESPNOW_WAKE_INTERVAL = 100;
33
34ESPNowComponent *global_esp_now = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
35
36static const LogString *espnow_error_to_str(esp_err_t error) {
37 switch (error) {
38 case ESP_ERR_ESPNOW_FAILED:
39 return LOG_STR("ESPNow is in fail mode");
40 case ESP_ERR_ESPNOW_OWN_ADDRESS:
41 return LOG_STR("Message to your self");
42 case ESP_ERR_ESPNOW_DATA_SIZE:
43 return LOG_STR("Data size to large");
44 case ESP_ERR_ESPNOW_PEER_NOT_SET:
45 return LOG_STR("Peer address not set");
46 case ESP_ERR_ESPNOW_PEER_NOT_PAIRED:
47 return LOG_STR("Peer address not paired");
48 case ESP_ERR_ESPNOW_NOT_INIT:
49 return LOG_STR("Not init");
50 case ESP_ERR_ESPNOW_ARG:
51 return LOG_STR("Invalid argument");
52 case ESP_ERR_ESPNOW_INTERNAL:
53 return LOG_STR("Internal Error");
54 case ESP_ERR_ESPNOW_NO_MEM:
55 return LOG_STR("Our of memory");
56 case ESP_ERR_ESPNOW_NOT_FOUND:
57 return LOG_STR("Peer not found");
58 case ESP_ERR_ESPNOW_IF:
59 return LOG_STR("Interface does not match");
60 case ESP_OK:
61 return LOG_STR("OK");
62 case ESP_NOW_SEND_FAIL:
63 return LOG_STR("Failed");
64 default:
65 return LOG_STR("Unknown Error");
66 }
67}
68
69#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
70void on_send_report(const esp_now_send_info_t *info, esp_now_send_status_t status)
71#else
72void on_send_report(const uint8_t *mac_addr, esp_now_send_status_t status)
73#endif
74{
75 // Allocate an event from the pool
77 if (packet == nullptr) {
78 // No events available - queue is full or we're out of memory
79 global_esp_now->receive_packet_queue_.increment_dropped_count();
80 return;
81 }
82
83// Load new packet data (replaces previous packet)
84#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
85 packet->load_sent_data(info->des_addr, status);
86#else
87 packet->load_sent_data(mac_addr, status);
88#endif
89
90 // Push the packet to the queue
92 // Push always succeeds: pool is sized to queue capacity (SIZE-1), so if
93 // allocate() returned non-null, the queue cannot be full.
94
95 // Wake main loop immediately to process ESP-NOW send event
97}
98
99void on_data_received(const esp_now_recv_info_t *info, const uint8_t *data, int size) {
100 // Allocate an event from the pool
102 if (packet == nullptr) {
103 // No events available - queue is full or we're out of memory
104 global_esp_now->receive_packet_queue_.increment_dropped_count();
105 return;
106 }
107
108 // Load new packet data (replaces previous packet)
109 packet->load_received_data(info, data, size);
110
111 // Push the packet to the queue
113 // Push always succeeds: pool is sized to queue capacity (SIZE-1), so if
114 // allocate() returned non-null, the queue cannot be full.
115
116 // Wake main loop immediately to process ESP-NOW receive event
118}
119
121
123 uint32_t version = 0;
124 esp_now_get_version(&version);
125
126 ESP_LOGCONFIG(TAG, "espnow:");
127 if (this->is_disabled()) {
128 ESP_LOGCONFIG(TAG, " Disabled");
129 return;
130 }
131 char own_addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
132 format_mac_addr_upper(this->own_address_, own_addr_buf);
133 ESP_LOGCONFIG(TAG,
134 " Own address: %s\n"
135 " Version: v%" PRIu32 "\n"
136 " Wi-Fi channel: %d",
137 own_addr_buf, version, this->wifi_channel_);
138#ifdef USE_WIFI
139 ESP_LOGCONFIG(TAG, " Wi-Fi enabled: %s", YESNO(this->is_wifi_enabled()));
140#endif
141}
142
144#ifdef USE_WIFI
146#else
147 return false;
148#endif
149}
150
152 if (this->enable_on_boot_) {
153 this->enable_();
154 } else {
156 }
157}
158
160 if (this->state_ == ESPNOW_STATE_ENABLED)
161 return;
162
163 ESP_LOGD(TAG, "Enabling");
164 this->state_ = ESPNOW_STATE_OFF;
165
166 this->enable_();
167}
168
170 if (!this->is_wifi_enabled()) {
171 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
172
173 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
174 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
175 ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
176 ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
177 ESP_ERROR_CHECK(esp_wifi_start());
178 ESP_ERROR_CHECK(esp_wifi_disconnect());
179
180 this->apply_wifi_channel();
181 }
182 this->get_wifi_channel();
183
184 esp_err_t err = esp_now_init();
185 if (err != ESP_OK) {
186 ESP_LOGE(TAG, "esp_now_init failed: %s", esp_err_to_name(err));
187 this->mark_failed();
188 return;
189 }
190
191 err = esp_now_register_recv_cb(on_data_received);
192 if (err != ESP_OK) {
193 ESP_LOGE(TAG, "esp_now_register_recv_cb failed: %s", esp_err_to_name(err));
194 this->mark_failed();
195 return;
196 }
197
198 err = esp_now_register_send_cb(on_send_report);
199 if (err != ESP_OK) {
200 ESP_LOGE(TAG, "esp_now_register_recv_cb failed: %s", esp_err_to_name(err));
201 this->mark_failed();
202 return;
203 }
204
205 esp_wifi_get_mac(WIFI_IF_STA, this->own_address_);
206
207#ifdef USE_DEEP_SLEEP
208 esp_now_set_wake_window(CONFIG_ESPNOW_WAKE_WINDOW);
209 esp_wifi_connectionless_module_set_wake_interval(CONFIG_ESPNOW_WAKE_INTERVAL);
210#endif
211
213
214 for (auto peer : this->peers_) {
215 this->add_peer(peer.address);
216 }
217}
218
220 if (this->state_ == ESPNOW_STATE_DISABLED)
221 return;
222
223 ESP_LOGD(TAG, "Disabling");
225
226 esp_now_unregister_recv_cb();
227 esp_now_unregister_send_cb();
228
229 esp_err_t err = esp_now_deinit();
230 if (err != ESP_OK) {
231 ESP_LOGE(TAG, "esp_now_deinit failed! 0x%x", err);
232 }
233}
234
236 if (this->state_ == ESPNOW_STATE_DISABLED) {
237 ESP_LOGE(TAG, "Cannot set channel when ESPNOW disabled");
238 this->mark_failed();
239 return;
240 }
241
242 if (this->is_wifi_enabled()) {
243 ESP_LOGE(TAG, "Cannot set channel when Wi-Fi enabled");
244 this->mark_failed();
245 return;
246 }
247
248 ESP_LOGI(TAG, "Channel set to %d.", this->wifi_channel_);
249 esp_wifi_set_promiscuous(true);
250 esp_wifi_set_channel(this->wifi_channel_, WIFI_SECOND_CHAN_NONE);
251 esp_wifi_set_promiscuous(false);
252}
253
255#ifdef USE_WIFI
256 if (wifi::global_wifi_component != nullptr && wifi::global_wifi_component->is_connected()) {
257 int32_t new_channel = wifi::global_wifi_component->get_wifi_channel();
258 if (new_channel != this->wifi_channel_) {
259 ESP_LOGI(TAG, "Wifi Channel is changed from %d to %" PRId32 ".", this->wifi_channel_, new_channel);
260 this->wifi_channel_ = new_channel;
261 }
262 }
263#endif
264 // Process received packets
265 ESPNowPacket *packet = this->receive_packet_queue_.pop();
266 while (packet != nullptr) {
267 switch (packet->type_) {
269 const ESPNowRecvInfo info = packet->get_receive_info();
270 if (!esp_now_is_peer_exist(info.src_addr)) {
271 bool handled = false;
272 for (auto *handler : this->unknown_peer_handlers_) {
273 if (handler->on_unknown_peer(info, packet->packet_.receive.data, packet->packet_.receive.size)) {
274 handled = true;
275 break; // If a handler returns true, stop processing further handlers
276 }
277 }
278 if (!handled && this->auto_add_peer_) {
279 this->add_peer(info.src_addr);
280 }
281 }
282 // Intentionally left as if instead of else in case the peer is added above
283 if (esp_now_is_peer_exist(info.src_addr)) {
284#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
285 char src_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
286 char dst_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
287 char hex_buf[format_hex_pretty_size(ESP_NOW_MAX_DATA_LEN)];
288 format_mac_addr_upper(info.src_addr, src_buf);
289 format_mac_addr_upper(info.des_addr, dst_buf);
290 ESP_LOGV(TAG, "<<< [%s -> %s] %s", src_buf, dst_buf,
291 format_hex_pretty_to(hex_buf, packet->packet_.receive.data, packet->packet_.receive.size));
292#endif
293 if (memcmp(info.des_addr, ESPNOW_BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0) {
294 for (auto *handler : this->broadcast_handlers_) {
295 if (handler->on_broadcast(info, packet->packet_.receive.data, packet->packet_.receive.size))
296 break; // If a handler returns true, stop processing further handlers
297 }
298 } else {
299 for (auto *handler : this->receive_handlers_) {
300 if (handler->on_receive(info, packet->packet_.receive.data, packet->packet_.receive.size))
301 break; // If a handler returns true, stop processing further handlers
302 }
303 }
304 }
305 break;
306 }
307 case ESPNowPacket::SENT: {
308#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
309 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
310 format_mac_addr_upper(packet->packet_.sent.address, addr_buf);
311 ESP_LOGV(TAG, ">>> [%s] %s", addr_buf, LOG_STR_ARG(espnow_error_to_str(packet->packet_.sent.status)));
312#endif
313 if (this->current_send_packet_ != nullptr) {
314 this->current_send_packet_->callback_(packet->packet_.sent.status);
315 this->send_packet_pool_.release(this->current_send_packet_);
316 this->current_send_packet_ = nullptr; // Reset current packet after sending
317 }
318 break;
319 }
320 default:
321 break;
322 }
323 // Return the packet to the pool
324 this->receive_packet_pool_.release(packet);
325 packet = this->receive_packet_queue_.pop();
326 }
327
328 // Process sending packet queue
329 if (this->current_send_packet_ == nullptr) {
330 this->send_();
331 }
332
333 // Log dropped received packets periodically
334 uint16_t received_dropped = this->receive_packet_queue_.get_and_reset_dropped_count();
335 if (received_dropped > 0) {
336 ESP_LOGW(TAG, "Dropped %u received packets due to buffer overflow", received_dropped);
337 }
338
339 // Log dropped send packets periodically
340 uint16_t send_dropped = this->send_packet_queue_.get_and_reset_dropped_count();
341 if (send_dropped > 0) {
342 ESP_LOGW(TAG, "Dropped %u send packets due to buffer overflow", send_dropped);
343 }
344}
345
347 wifi_second_chan_t dummy;
348 esp_wifi_get_channel(&this->wifi_channel_, &dummy);
349 return this->wifi_channel_;
350}
351
352esp_err_t ESPNowComponent::send(const uint8_t *peer_address, const uint8_t *payload, size_t size,
353 const send_callback_t &callback) {
354 if (this->state_ != ESPNOW_STATE_ENABLED) {
355 return ESP_ERR_ESPNOW_NOT_INIT;
356 } else if (this->is_failed()) {
357 return ESP_ERR_ESPNOW_FAILED;
358 } else if (peer_address == 0ULL) {
359 return ESP_ERR_ESPNOW_PEER_NOT_SET;
360 } else if (memcmp(peer_address, this->own_address_, ESP_NOW_ETH_ALEN) == 0) {
361 return ESP_ERR_ESPNOW_OWN_ADDRESS;
362 } else if (size > ESP_NOW_MAX_DATA_LEN) {
363 return ESP_ERR_ESPNOW_DATA_SIZE;
364 } else if (!esp_now_is_peer_exist(peer_address)) {
365 if (memcmp(peer_address, ESPNOW_BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0 || this->auto_add_peer_) {
366 esp_err_t err = this->add_peer(peer_address);
367 if (err != ESP_OK) {
368 return err;
369 }
370 } else {
371 return ESP_ERR_ESPNOW_PEER_NOT_PAIRED;
372 }
373 }
374 // Allocate a packet from the pool
375 ESPNowSendPacket *packet = this->send_packet_pool_.allocate();
376 if (packet == nullptr) {
377 this->send_packet_queue_.increment_dropped_count();
378 ESP_LOGE(TAG, "Failed to allocate send packet from pool");
379 this->status_momentary_warning("send-packet-pool-full");
380 return ESP_ERR_ESPNOW_NO_MEM;
381 }
382 // Load the packet data
383 packet->load_data(peer_address, payload, size, callback);
384 // Push the packet to the send queue
385 this->send_packet_queue_.push(packet);
386 return ESP_OK;
387}
388
390 ESPNowSendPacket *packet = this->send_packet_queue_.pop();
391 if (packet == nullptr) {
392 return; // No packets to send
393 }
394
395 this->current_send_packet_ = packet;
396 esp_err_t err = esp_now_send(packet->address_, packet->data_, packet->size_);
397 if (err != ESP_OK) {
398 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
399 format_mac_addr_upper(packet->address_, addr_buf);
400 ESP_LOGE(TAG, "Failed to send packet to %s - %s", addr_buf, LOG_STR_ARG(espnow_error_to_str(err)));
401 if (packet->callback_ != nullptr) {
402 packet->callback_(err);
403 }
404 this->status_momentary_warning("send-failed");
405 this->send_packet_pool_.release(packet);
406 this->current_send_packet_ = nullptr; // Reset current packet
407 return;
408 }
409}
410
411esp_err_t ESPNowComponent::add_peer(const uint8_t *peer) {
412 if (this->state_ != ESPNOW_STATE_ENABLED || this->is_failed()) {
413 return ESP_ERR_ESPNOW_NOT_INIT;
414 }
415
416 if (memcmp(peer, this->own_address_, ESP_NOW_ETH_ALEN) == 0) {
417 this->status_momentary_warning("peer-add-failed");
418 return ESP_ERR_INVALID_MAC;
419 }
420
421 if (!esp_now_is_peer_exist(peer)) {
422 esp_now_peer_info_t peer_info = {};
423 memset(&peer_info, 0, sizeof(esp_now_peer_info_t));
424 peer_info.ifidx = WIFI_IF_STA;
425 memcpy(peer_info.peer_addr, peer, ESP_NOW_ETH_ALEN);
426 esp_err_t err = esp_now_add_peer(&peer_info);
427
428 if (err != ESP_OK) {
429 char peer_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
430 format_mac_addr_upper(peer, peer_buf);
431 ESP_LOGE(TAG, "Failed to add peer %s - %s", peer_buf, LOG_STR_ARG(espnow_error_to_str(err)));
432 this->status_momentary_warning("peer-add-failed");
433 return err;
434 }
435 }
436 bool found = false;
437 for (auto &it : this->peers_) {
438 if (it == peer) {
439 found = true;
440 break;
441 }
442 }
443 if (!found) {
444 ESPNowPeer new_peer;
445 memcpy(new_peer.address, peer, ESP_NOW_ETH_ALEN);
446 this->peers_.push_back(new_peer);
447 }
448
449 return ESP_OK;
450}
451
452esp_err_t ESPNowComponent::del_peer(const uint8_t *peer) {
453 if (this->state_ != ESPNOW_STATE_ENABLED || this->is_failed()) {
454 return ESP_ERR_ESPNOW_NOT_INIT;
455 }
456 if (esp_now_is_peer_exist(peer)) {
457 esp_err_t err = esp_now_del_peer(peer);
458 if (err != ESP_OK) {
459 char peer_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
460 format_mac_addr_upper(peer, peer_buf);
461 ESP_LOGE(TAG, "Failed to delete peer %s - %s", peer_buf, LOG_STR_ARG(espnow_error_to_str(err)));
462 this->status_momentary_warning("peer-del-failed");
463 return err;
464 }
465 }
466 for (auto it = this->peers_.begin(); it != this->peers_.end(); ++it) {
467 if (*it == peer) {
468 this->peers_.erase(it);
469 break;
470 }
471 }
472 return ESP_OK;
473}
474
475} // namespace esphome::espnow
476
477#endif // USE_ESP32
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_momentary_warning(const char *name, uint32_t length=5000)
Set warning status flag and automatically clear it after a timeout.
std::vector< ESPNowBroadcastHandler * > broadcast_handlers_
esp_err_t send(const uint8_t *peer_address, const std::vector< uint8_t > &payload, const send_callback_t &callback=nullptr)
Queue a packet to be sent to a specific peer address.
LockFreeQueue< ESPNowSendPacket, MAX_ESP_NOW_SEND_QUEUE_SIZE > send_packet_queue_
void add_peer(peer_address_t address)
std::vector< ESPNowUnknownPeerHandler * > unknown_peer_handlers_
std::vector< ESPNowPeer > peers_
friend void on_send_report(const esp_now_send_info_t *info, esp_now_send_status_t status)
friend void on_data_received(const esp_now_recv_info_t *info, const uint8_t *data, int size)
esp_err_t del_peer(const uint8_t *peer)
std::vector< ESPNowReceivedPacketHandler * > receive_handlers_
EventPool< ESPNowPacket, MAX_ESP_NOW_RECEIVE_QUEUE_SIZE - 1 > receive_packet_pool_
LockFreeQueue< ESPNowPacket, MAX_ESP_NOW_RECEIVE_QUEUE_SIZE > receive_packet_queue_
EventPool< ESPNowSendPacket, MAX_ESP_NOW_SEND_QUEUE_SIZE - 1 > send_packet_pool_
uint8_t own_address_[ESP_NOW_ETH_ALEN]
void load_sent_data(const uint8_t *mac_addr, esp_now_send_status_t status)
struct esphome::espnow::ESPNowPacket::@85::received_data receive
void load_received_data(const esp_now_recv_info_t *info, const uint8_t *data, int size)
union esphome::espnow::ESPNowPacket::@85 packet_
esp_now_packet_type_t type_
const ESPNowRecvInfo & get_receive_info() const
struct esphome::espnow::ESPNowPacket::@85::sent_data sent
void load_data(const uint8_t *peer_address, const uint8_t *payload, size_t size, const send_callback_t &callback)
uint8_t address_[ESP_NOW_ETH_ALEN]
uint8_t data_[ESP_NOW_MAX_DATA_LEN]
@ ESPNOW_STATE_ENABLED
ESPNOW is enabled.
@ ESPNOW_STATE_OFF
Nothing has been initialized yet.
@ ESPNOW_STATE_DISABLED
ESPNOW is disabled.
void on_send_report(const esp_now_send_info_t *info, esp_now_send_status_t status) void on_send_report(const uint8_t *mac_addr
void on_data_received(const esp_now_recv_info_t *info, const uint8_t *data, int size)
ESPNowComponent * global_esp_now
void esp_now_send_status_t status
std::function< void(esp_err_t)> send_callback_t
WiFiComponent * global_wifi_component
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:340
uint16_t size
Definition helpers.cpp:25
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1386
Application App
Global storage of Application pointer - only one Application can exist.
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:1453
static void uint32_t
uint8_t address[ESP_NOW_ETH_ALEN]
uint8_t des_addr[ESP_NOW_ETH_ALEN]
Destination address of ESPNOW packet.
uint8_t src_addr[ESP_NOW_ETH_ALEN]
Source address of ESPNOW packet.