Controller B updates from A: link_peer (pairing over the wire, encrypted credential handoff, B status + commands), ESP-NOW receive/unicast, Updates page with two cards; shared header maintenance frames; docs + wiki
Firmware / build (push) Successful in 7m1s
Firmware / build (push) Successful in 7m1s
This commit is contained in:
@@ -16,6 +16,13 @@ static const uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
static bool g_ready = false;
|
||||
static uint32_t g_sent = 0;
|
||||
static uint32_t g_fail = 0;
|
||||
static receive_handler_t g_rx_handler = NULL;
|
||||
|
||||
// IDF 5.x receive callback: sender MAC is in the info struct.
|
||||
static void onReceive(const esp_now_recv_info_t* info, const uint8_t* data, int len) {
|
||||
if (g_rx_handler == NULL || info == NULL || len <= 0) return;
|
||||
g_rx_handler(info->src_addr, data, (size_t)len);
|
||||
}
|
||||
|
||||
// IDF 5.5 (pioarduino / Arduino 3.3) passes wifi_tx_info_t*; older cores passed the MAC.
|
||||
static void onSent(const wifi_tx_info_t* info, esp_now_send_status_t status) {
|
||||
@@ -45,6 +52,7 @@ bool init() {
|
||||
return false;
|
||||
}
|
||||
esp_now_register_send_cb(onSent);
|
||||
esp_now_register_recv_cb(onReceive);
|
||||
|
||||
esp_now_peer_info_t peer = {};
|
||||
memcpy(peer.peer_addr, BROADCAST_MAC, 6);
|
||||
@@ -93,4 +101,26 @@ bool isReady() { return g_ready; }
|
||||
uint32_t sentCount() { return g_sent; }
|
||||
uint32_t failCount() { return g_fail; }
|
||||
|
||||
void setReceiveHandler(receive_handler_t handler) { g_rx_handler = handler; }
|
||||
|
||||
bool sendTo(const uint8_t mac[6], const uint8_t* frame, size_t len) {
|
||||
if (len == 0 || len > ESP_NOW_MAX_DATA_LEN) return false;
|
||||
if (!g_ready) {
|
||||
reinitIfDue();
|
||||
if (!g_ready) return false;
|
||||
}
|
||||
if (!esp_now_is_peer_exist(mac)) {
|
||||
esp_now_peer_info_t peer = {};
|
||||
memcpy(peer.peer_addr, mac, 6);
|
||||
peer.channel = 0; // current channel
|
||||
peer.encrypt = false; // SECURE frames carry their own encryption
|
||||
peer.ifidx = WIFI_IF_STA;
|
||||
if (esp_now_add_peer(&peer) != ESP_OK) {
|
||||
ESP_LOGE("EspNow", "esp_now_add_peer(B) failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return esp_now_send(mac, frame, len) == ESP_OK;
|
||||
}
|
||||
|
||||
} // namespace EspNowLink
|
||||
|
||||
Reference in New Issue
Block a user