From 81e479ecd1579f334411fb013b7edbbcd353abaa Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 18 May 2026 22:21:08 +0200 Subject: [PATCH] Ignore repeat master discover after slave has joined network. Slave responds and logs only on first discover from a given master MAC; subsequent broadcasts on the same network are silently ignored. Co-authored-by: Cursor --- main/README.md | 2 +- main/esp_now_comm.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/main/README.md b/main/README.md index 8a7563a..1404310 100644 --- a/main/README.md +++ b/main/README.md @@ -90,7 +90,7 @@ WiFi is brought up in STA mode (no AP association). Channel = `app_config.networ **Master:** task `espnow_disc` sends `DISCOVER` every **500 ms** on the configured network. Logs `slave joined id=… mac=… ver=…` when a new slave is seen (up to 16 entries). -**Slave:** on matching `DISCOVER`, unicast `SLAVE_INFO` back to the master source MAC. +**Slave:** on first matching `DISCOVER`, logs `joined network N, master …`, sends `SLAVE_INFO` once, then ignores further discovers from that master (no repeat log or reply). Monitor via USB-JTAG (`/dev/ttyACM0`) while using a USB-serial adapter on **GPIO2/3** (`/dev/ttyUSB0`) for UART — they are different interfaces. diff --git a/main/esp_now_comm.c b/main/esp_now_comm.c index 1ea8aba..bfbd320 100644 --- a/main/esp_now_comm.c +++ b/main/esp_now_comm.c @@ -58,6 +58,8 @@ static app_config_t s_config; static uint8_t s_wifi_channel; static uint8_t s_own_mac[ESP_NOW_ETH_ALEN]; static slave_entry_t s_slaves[ESPNOW_MAX_SLAVES]; +static bool s_slave_joined; +static uint8_t s_master_mac[ESP_NOW_ETH_ALEN]; static uint8_t network_to_channel(uint8_t network) { if (network < 1 || network > 13) { @@ -147,7 +149,18 @@ static void handle_discover(const uint8_t *sender_mac, if (pkt->network != s_config.network) { return; } - ESP_LOGI(TAG, "discover from master on network %u", (unsigned)pkt->network); + + if (s_slave_joined && mac_equal(sender_mac, s_master_mac)) { + return; + } + + memcpy(s_master_mac, sender_mac, ESP_NOW_ETH_ALEN); + s_slave_joined = true; + + char mac_str[18]; + mac_to_str(sender_mac, mac_str, sizeof(mac_str)); + ESP_LOGI(TAG, "joined network %u, master %s", (unsigned)pkt->network, mac_str); + send_slave_info(sender_mac); } @@ -266,6 +279,8 @@ esp_err_t esp_now_comm_init(const app_config_t *config) { memset(&s_config, 0, sizeof(s_config)); memcpy(&s_config, config, sizeof(s_config)); memset(s_slaves, 0, sizeof(s_slaves)); + s_slave_joined = false; + memset(s_master_mac, 0, sizeof(s_master_mac)); s_wifi_channel = network_to_channel(config->network); ESP_ERROR_CHECK(esp_read_mac(s_own_mac, ESP_MAC_WIFI_STA));