Skip to content

Commit de2ace7

Browse files
projectgusdpgeorge
authored andcommitted
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>
1 parent be8d5fc commit de2ace7

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

ports/esp32/modespnow.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ static void send_cb(const esp_now_send_info_t *tx_info, esp_now_send_status_t st
187187

188188
static void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, int msg_len);
189189

190+
// Return the current wifi mode, or raise ValueError
191+
static wifi_mode_t get_wifi_mode(void) {
192+
wifi_mode_t mode;
193+
if (esp_wifi_get_mode(&mode) != ESP_OK || mode == WIFI_MODE_NULL) {
194+
// network.WLAN STA or AP must be set active(True) before ESP-NOW can be used
195+
mp_raise_OSError(MP_ENOENT);
196+
}
197+
return mode;
198+
}
199+
190200
// ESPNow.init(): Initialise the data buffers and ESP-NOW functions.
191201
// Initialise the Espressif ESPNOW software stack, register callbacks and
192202
// allocate the recv data buffers.
@@ -197,7 +207,6 @@ static mp_obj_t espnow_init(mp_obj_t _) {
197207
self->recv_buffer = m_new_obj(ringbuf_t);
198208
ringbuf_alloc(self->recv_buffer, self->recv_buffer_size);
199209

200-
esp_initialise_wifi(); // Call the wifi init code in network_wlan.c
201210
check_esp_err(esp_now_init());
202211
check_esp_err(esp_now_register_recv_cb(recv_cb));
203212
check_esp_err(esp_now_register_send_cb(send_cb));
@@ -260,9 +269,13 @@ static mp_obj_t espnow_config(size_t n_args, const mp_obj_t *pos_args, mp_map_t
260269
self->recv_timeout_ms = args[ARG_timeout_ms].u_int;
261270
}
262271
if (args[ARG_rate].u_int >= 0) {
263-
esp_initialise_wifi(); // Call the wifi init code in network_wlan.c
264-
check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_STA, args[ARG_rate].u_int));
265-
check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_AP, args[ARG_rate].u_int));
272+
wifi_mode_t mode = get_wifi_mode();
273+
if (mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) {
274+
check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_STA, args[ARG_rate].u_int));
275+
}
276+
if (mode == WIFI_MODE_AP || mode == WIFI_MODE_APSTA) {
277+
check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_AP, args[ARG_rate].u_int));
278+
}
266279
}
267280
if (args[ARG_get].u_obj == MP_OBJ_NULL) {
268281
return mp_const_none;

0 commit comments

Comments
 (0)