esp32/modespnow: Fix espnow rate setting.

Only set the rate on interfaces that are active.

It seems ESP-IDF 5.4.x or so added checks that the interface is enabled,
whereas previous versions silently did nothing.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
This commit is contained in:
Angus Gratton
2025-11-19 17:59:25 +11:00
committed by Damien George
parent be8d5fc27b
commit de2ace72e9

View File

@@ -187,6 +187,16 @@ static void send_cb(const esp_now_send_info_t *tx_info, esp_now_send_status_t st
static void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, int msg_len);
// Return the current wifi mode, or raise ValueError
static wifi_mode_t get_wifi_mode(void) {
wifi_mode_t mode;
if (esp_wifi_get_mode(&mode) != ESP_OK || mode == WIFI_MODE_NULL) {
// network.WLAN STA or AP must be set active(True) before ESP-NOW can be used
mp_raise_OSError(MP_ENOENT);
}
return mode;
}
// ESPNow.init(): Initialise the data buffers and ESP-NOW functions.
// Initialise the Espressif ESPNOW software stack, register callbacks and
// allocate the recv data buffers.
@@ -197,7 +207,6 @@ static mp_obj_t espnow_init(mp_obj_t _) {
self->recv_buffer = m_new_obj(ringbuf_t);
ringbuf_alloc(self->recv_buffer, self->recv_buffer_size);
esp_initialise_wifi(); // Call the wifi init code in network_wlan.c
check_esp_err(esp_now_init());
check_esp_err(esp_now_register_recv_cb(recv_cb));
check_esp_err(esp_now_register_send_cb(send_cb));
@@ -260,10 +269,14 @@ static mp_obj_t espnow_config(size_t n_args, const mp_obj_t *pos_args, mp_map_t
self->recv_timeout_ms = args[ARG_timeout_ms].u_int;
}
if (args[ARG_rate].u_int >= 0) {
esp_initialise_wifi(); // Call the wifi init code in network_wlan.c
wifi_mode_t mode = get_wifi_mode();
if (mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) {
check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_STA, args[ARG_rate].u_int));
}
if (mode == WIFI_MODE_AP || mode == WIFI_MODE_APSTA) {
check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_AP, args[ARG_rate].u_int));
}
}
if (args[ARG_get].u_obj == MP_OBJ_NULL) {
return mp_const_none;
}