Skip to content

Commit 7e10d22

Browse files
projectgusdpgeorge
authored andcommitted
esp32,docs: Add constants and documentation for espnow data rates.
Changes are: - Add constants for some of the supported ESP-NOW data rates. - Add constants for switching an ESP32 WLAN radio in/out of Long Range mode. - Document the new constants and their usage. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent de2ace7 commit 7e10d22

4 files changed

Lines changed: 107 additions & 4 deletions

File tree

docs/library/espnow.rst

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,15 @@ Configuration
164164
wait forever. The timeout can also be provided as arg to
165165
`recv()`/`irecv()`/`recvinto()`.
166166

167-
*rate*: (ESP32 only) Set the transmission speed for
168-
ESPNow packets. Must be set to a number from the allowed numeric values
169-
in `enum wifi_phy_rate_t
170-
<https://docs.espressif.com/projects/esp-idf/en/v5.2.3/esp32/
167+
*rate*: (ESP32 only) Set the transmission data rate for ESPNow packets.
168+
The default setting is `espnow.RATE_1M`. It's recommended to use one of
169+
the other ``espnow.RATE_nnn`` constants to set this, but it's also
170+
possible to pass an integer corresponding to the `enum wifi_phy_rate_t
171+
<https://docs.espressif.com/projects/esp-idf/en/v5.5.1/esp32/
171172
api-reference/network/esp_wifi.html#_CPPv415wifi_phy_rate_t>`_. This
172173
parameter is actually *write-only* due to ESP-IDF not providing any
173174
means for querying the radio interface's rate parameter.
175+
See also `espnow-long-range`.
174176

175177
.. data:: Returns:
176178

@@ -574,6 +576,45 @@ Constants
574576
espnow.MAX_TOTAL_PEER_NUM(=20)
575577
espnow.MAX_ENCRYPT_PEER_NUM(=6)
576578

579+
The following constants correspond to different transmit data rates on ESP32
580+
only. Lower data rates are generally more reliable over long distances:
581+
582+
.. data:: espnow.RATE_LORA_250K
583+
espnow.RATE_LORA_500K
584+
585+
See `espnow-long-range`.
586+
587+
.. data:: espnow.RATE_1M
588+
espnow.RATE_2M
589+
espnow.RATE_5M
590+
espnow.RATE_6M
591+
espnow.RATE_11M
592+
espnow.RATE_12M
593+
espnow.RATE_24M
594+
espnow.RATE_54M
595+
596+
Unless using the two proprietary long range data rates, only the sender must
597+
configure the data rate.
598+
599+
.. _espnow-long-range:
600+
601+
Long Range Mode
602+
---------------
603+
604+
(ESP32 Only, except ESP32-C2)
605+
606+
To use the `espnow.RATE_LORA_250K` and `espnow.RATE_LORA_500K` data rates,
607+
first set the `WLAN` interface object to long-range mode, i.e.::
608+
609+
import network, espnow
610+
sta = network.WLAN(network.WLAN.IF_STA)
611+
sta.active(True)
612+
sta.config(channel=6, protocol=WLAN.PROTOCOL_LR) # Set on sender & receiver
613+
e = espnow.ESPNow()
614+
e.config(rate=espnow.RATE_LORA_250K) # Needed on sender only
615+
616+
For more information about the limitations of long-range mode, see `WLAN.PROTOCOL_LR`.
617+
577618
Exceptions
578619
----------
579620

docs/library/network.WLAN.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Methods
145145
reconnects Number of reconnect attempts to make (integer, 0=none, -1=unlimited)
146146
txpower Maximum transmit power in dBm (integer or float)
147147
pm WiFi Power Management setting (see below for allowed values)
148+
protocol (ESP32 Only.) WiFi Low level 802.11 protocol. See `WLAN.PROTOCOL_DEFAULTS`.
148149
============= ===========
149150

150151
Constants
@@ -161,3 +162,34 @@ Constants
161162
* ``PM_POWERSAVE``: enable WiFi power management with additional power
162163
savings and reduced WiFi performance
163164
* ``PM_NONE``: disable wifi power management
165+
166+
167+
ESP32 Protocol Constants
168+
------------------------
169+
170+
The following ESP32-only constants relate to the ``WLAN.config(protocol=...)``
171+
network interface parameter:
172+
173+
.. data:: WLAN.PROTOCOL_DEFAULTS
174+
175+
A bitmap representing all of the default 802.11 Wi-Fi modes supported by
176+
the chip. Consult `ESP-IDF Wi-Fi Protocols`_ documentation for details.
177+
178+
.. data:: WLAN.PROTOCOL_LR
179+
180+
This value corresponds to the `Espressif proprietary "long-range" mode`_,
181+
which is not compatible with standard Wi-Fi devices. By setting this
182+
protocol it's possible for an ESP32 STA in long-range mode to connect to
183+
an ESP32 AP in long-range mode, or to use `ESP-NOW long range modes
184+
<espnow-long-range>`.
185+
186+
This mode can be bitwise ORed with some standard 802.11 protocol bits
187+
(including `WLAN.PROTOCOL_DEFAULTS`) in order to support a mix of standard
188+
Wi-Fi modes as well as LR mode, consult the `Espressif long-range
189+
documentation`_ for more details.
190+
191+
Long range mode is not supported on ESP32-C2.
192+
193+
.. _ESP-IDF Wi-Fi Protocols: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/wifi.html#wi-fi-protocol-mode
194+
.. _Espressif proprietary "long-range" mode:
195+
.. _Espressif long-range documentation: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/wifi.html#long-range-lr

ports/esp32/modespnow.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,22 @@ static const mp_rom_map_elem_t espnow_globals_dict_table[] = {
817817
{ MP_ROM_QSTR(MP_QSTR_KEY_LEN), MP_ROM_INT(ESP_NOW_KEY_LEN)},
818818
{ MP_ROM_QSTR(MP_QSTR_MAX_TOTAL_PEER_NUM), MP_ROM_INT(ESP_NOW_MAX_TOTAL_PEER_NUM)},
819819
{ MP_ROM_QSTR(MP_QSTR_MAX_ENCRYPT_PEER_NUM), MP_ROM_INT(ESP_NOW_MAX_ENCRYPT_PEER_NUM)},
820+
821+
#if !CONFIG_IDF_TARGET_ESP32C2
822+
{ MP_ROM_QSTR(MP_QSTR_RATE_LORA_250K), MP_ROM_INT(WIFI_PHY_RATE_LORA_250K)},
823+
{ MP_ROM_QSTR(MP_QSTR_RATE_LORA_500K), MP_ROM_INT(WIFI_PHY_RATE_LORA_500K)},
824+
#endif
825+
// Note: specifying long preamble versions for the lower bit rates apart
826+
// from the non-802.11b 6Mbit rate, for more robust error correction
827+
{ MP_ROM_QSTR(MP_QSTR_RATE_1M), MP_ROM_INT(WIFI_PHY_RATE_1M_L)},
828+
{ MP_ROM_QSTR(MP_QSTR_RATE_2M), MP_ROM_INT(WIFI_PHY_RATE_2M_L)},
829+
{ MP_ROM_QSTR(MP_QSTR_RATE_5M), MP_ROM_INT(WIFI_PHY_RATE_5M_L)},
830+
{ MP_ROM_QSTR(MP_QSTR_RATE_6M), MP_ROM_INT(WIFI_PHY_RATE_6M)},
831+
{ MP_ROM_QSTR(MP_QSTR_RATE_11M), MP_ROM_INT(WIFI_PHY_RATE_11M_L)},
832+
{ MP_ROM_QSTR(MP_QSTR_RATE_12M), MP_ROM_INT(WIFI_PHY_RATE_12M)},
833+
{ MP_ROM_QSTR(MP_QSTR_RATE_24M), MP_ROM_INT(WIFI_PHY_RATE_24M)},
834+
{ MP_ROM_QSTR(MP_QSTR_RATE_54M), MP_ROM_INT(WIFI_PHY_RATE_54M)},
835+
820836
};
821837
static MP_DEFINE_CONST_DICT(espnow_globals_dict, espnow_globals_dict_table);
822838

ports/esp32/network_wlan.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ static bool mdns_initialised = false;
7979
static uint8_t conf_wifi_sta_reconnects = 0;
8080
static uint8_t wifi_sta_reconnects;
8181

82+
// The rules for this default are defined in the documentation of esp_wifi_set_protocol()
83+
// rather than in code, so we have to recreate them here.
84+
#if CONFIG_SOC_WIFI_HE_SUPPORT
85+
// Note: No Explicit support for 5GHz here, yet
86+
#define WIFI_PROTOCOL_DEFAULT (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_11AX)
87+
#else
88+
#define WIFI_PROTOCOL_DEFAULT (WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N)
89+
#endif
90+
8291
// This function is called by the system-event task and so runs in a different
8392
// thread to the main MicroPython task. It must not raise any Python exceptions.
8493
static void network_wlan_wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
@@ -771,6 +780,11 @@ static const mp_rom_map_elem_t wlan_if_locals_dict_table[] = {
771780
{ MP_ROM_QSTR(MP_QSTR_SEC_WPA_ENT), MP_ROM_INT(WIFI_AUTH_WPA_ENTERPRISE) },
772781
#endif
773782

783+
{ MP_ROM_QSTR(MP_QSTR_PROTOCOL_DEFAULT), MP_ROM_INT(WIFI_PROTOCOL_DEFAULT) },
784+
#if !CONFIG_IDF_TARGET_ESP32C2
785+
{ MP_ROM_QSTR(MP_QSTR_PROTOCOL_LR), MP_ROM_INT(WIFI_PROTOCOL_LR) },
786+
#endif
787+
774788
{ MP_ROM_QSTR(MP_QSTR_PM_NONE), MP_ROM_INT(WIFI_PS_NONE) },
775789
{ MP_ROM_QSTR(MP_QSTR_PM_PERFORMANCE), MP_ROM_INT(WIFI_PS_MIN_MODEM) },
776790
{ MP_ROM_QSTR(MP_QSTR_PM_POWERSAVE), MP_ROM_INT(WIFI_PS_MAX_MODEM) },

0 commit comments

Comments
 (0)