Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Select ESP32 Wi-Fi protocol mode during WLAN init #281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/library/network.WLAN.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ Methods
- ``channel`` a number in the range 1-11. Only needed when mode is ``WLAN.AP``.
- ``antenna`` selects between the internal and the external antenna. Can be either
``WLAN.INT_ANT`` or ``WLAN.EXT_ANT``.
- ``protocol_mode`` selects Wi-Fi protocol mode. Defaults to 802.11BGN (``WLAN.PROTOCOL_11B|WLAN.PROTOCOL_11G|WLAN.PROTOCOL_11N``). Here you can activate Espressif-patented long range mode with ``WLAN.PROTOCOL_LR``.
- ``power_save`` enables or disables power save functions in STA mode.


For example, you can do::

# create and configure as an access point
Expand Down Expand Up @@ -194,3 +196,10 @@ Constants
WLAN.EXT_ANT

selects the antenna type

.. data:: WLAN.PROTOCOL_11B
WLAN.PROTOCOL_11G
WLAN.PROTOCOL_11N
WLAN.PROTOCOL_LR

selects the Wi-Fi protocol mode
25 changes: 23 additions & 2 deletions esp32/mods/modwlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void wlan_pre_init (void) {
wlan_obj.base.type = (mp_obj_t)&mod_network_nic_type_wlan;
}

void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, bool hidden) {
void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, bool hidden, uint32_t protocol_mode) {

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
Expand Down Expand Up @@ -198,6 +198,21 @@ void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key,

// start the servers before returning
wlan_servers_start();

// set wifi protocol mode
int32_t wifi_interface = 0;
switch( mode ) {
case WIFI_MODE_STA:
wifi_interface = ESP_IF_WIFI_STA;
break;
case WIFI_MODE_AP:
wifi_interface = ESP_IF_WIFI_AP;
break;
default:
wifi_interface = ESP_IF_WIFI_STA;
}
ESP_ERROR_CHECK(esp_wifi_set_protocol(wifi_interface, protocol_mode));

}

void wlan_get_mac (uint8_t *macAddress) {
Expand Down Expand Up @@ -664,6 +679,7 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) {

wlan_obj.pwrsave = args[5].u_bool;
bool hidden = args[6].u_bool;
int32_t protocol_mode = args[7].u_int;

if (mode != WIFI_MODE_STA) {
if (ssid == NULL) {
Expand All @@ -675,7 +691,7 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) {
}

// initialize the wlan subsystem
wlan_setup(mode, (const char *)ssid, auth, (const char *)key, channel, antenna, false, hidden);
wlan_setup(mode, (const char *)ssid, auth, (const char *)key, channel, antenna, false, hidden, protocol_mode);
mod_network_register_nic(&wlan_obj);

return mp_const_none;
Expand All @@ -690,6 +706,7 @@ STATIC const mp_arg_t wlan_init_args[] = {
{ MP_QSTR_antenna, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_power_save, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_hidden, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_protocol_mode,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N} },
};
STATIC mp_obj_t wlan_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) {
// parse args
Expand Down Expand Up @@ -1172,6 +1189,10 @@ STATIC const mp_map_elem_t wlan_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_WPA2_ENT), MP_OBJ_NEW_SMALL_INT(WIFI_AUTH_WPA2_ENTERPRISE) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_INT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_INTERNAL) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_EXT_ANT), MP_OBJ_NEW_SMALL_INT(ANTENNA_TYPE_EXTERNAL) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_11B), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_11B) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_11G), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_11G) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_11N), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_11N) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROTOCOL_LR), MP_OBJ_NEW_SMALL_INT(WIFI_PROTOCOL_LR) },
// { MP_OBJ_NEW_QSTR(MP_QSTR_ANY_EVENT), MP_OBJ_NEW_SMALL_INT(MODWLAN_WIFI_EVENT_ANY) },
};
STATIC MP_DEFINE_CONST_DICT(wlan_locals_dict, wlan_locals_dict_table);
Expand Down
2 changes: 1 addition & 1 deletion esp32/mods/modwlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ extern wlan_obj_t wlan_obj;
DECLARE PUBLIC FUNCTIONS
******************************************************************************/
extern void wlan_pre_init (void);
extern void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, bool ssid_hidden);
extern void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, bool ssid_hidden, uint32_t protocol_mode);
extern void wlan_update(void);
extern void wlan_get_mac (uint8_t *macAddress);
extern void wlan_get_ip (uint32_t *ip);
Expand Down
3 changes: 2 additions & 1 deletion esp32/mptask.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,8 @@ STATIC void mptask_enable_wifi_ap (void) {
uint8_t wifi_pwd[64];
config_get_wifi_pwd(wifi_pwd);
wlan_setup (WIFI_MODE_AP, DEFAULT_AP_SSID, WIFI_AUTH_WPA2_PSK, DEFAULT_AP_PASSWORD ,
DEFAULT_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, true, false);
DEFAULT_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, true, false, WIFI_PROTOCOL_11B |
WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N );
mod_network_register_nic(&wlan_obj);
}

Expand Down