SOLVED! I've amended this post, but left it in place in case it's useful.
The solution was to make sure esp_wifi_set_mode() was called before the call to esp_wifi_set_protocol().
In micropython, this means a call to wlan.active(True)
Code: Select all
>>>
>>> import network as nw
>>> wlan = nw.WLAN(nw.STA_IF)
>>> wlan.active(True)
W (18387343) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration
I (18387473) phy: phy_version: 4102, 2fa7a43, Jul 15 2019, 13:06:06, 0, 2
I (18387483) wifi: mode : sta (b4:e6:2d:d9:0f:ed)
True
I (18387483>)> >w ifi: STA_START
>>>
>>> nw.phy_mode(nw.STA_IF, nw.MODE_LR)
(True, I (18406273) network: event 3
0)
I (18406273) wifi: STA_START
>>>
>>>
>>> nw.phy_mode(nw.STA_IF)
(True, 8)
>>>
This question was answered by jimmo at the
micropython forum
=============================================== original post from here
Hi,
I'm trying to get wifi LR mode from Micropython. I can use C code along the lines of
this example, but it's easier to use Micropython at present.
I've added this rough implementation of phy_mode() to modnetwork.c (with a view to enabling LR mode) and flashed it to a sparkfun thing, but I get ESP_ERR_INVALID_ARG. Calling phy_mode() with no args just returns the error values, (0, 12289, 12292, 258), so I can decode them.
Code: Select all
STATIC mp_obj_t esp_phy_mode(size_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
mp_obj_t tuple[4] = {
mp_obj_new_int(ESP_OK),
mp_obj_new_int(ESP_ERR_WIFI_NOT_INIT),
mp_obj_new_int(ESP_ERR_WIFI_IF),
mp_obj_new_int(ESP_ERR_INVALID_ARG),
};
return mp_obj_new_tuple(4, tuple);
}
uint8_t protocol_bitmap = -1;
esp_err_t esp_err = ESP_OK;
wifi_interface_t ifx = WIFI_IF_STA;
switch(n_args)
{
case 1:
ifx = mp_obj_get_int(args[0]);
//case 0:
esp_err = esp_wifi_get_protocol(ifx, &protocol_bitmap);
if(esp_err ==ESP_OK)
{
mp_obj_t tuple[2] = {
mp_const_true,
mp_obj_new_int(protocol_bitmap),
};
return mp_obj_new_tuple(2, tuple);
}
break;
case 2:
ifx = mp_obj_get_int(args[0]);
esp_err = esp_wifi_set_protocol(ifx, mp_obj_get_int(args[1]));
break;
}
mp_obj_t tuple[2] = {
mp_obj_new_bool(esp_err ==ESP_OK),
mp_obj_new_int(esp_err),
};
return mp_obj_new_tuple(2, tuple);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_phy_mode_obj, 0, 2, esp_phy_mode);
I also added ..
Code: Select all
STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = {
.
{ MP_ROM_QSTR(MP_QSTR_MODE_LR), MP_ROM_INT(WIFI_PROTOCOL_LR) },
.
after reboot..
Code: Select all
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5064
load:0x40078000,len:9656
load:0x40080400,len:6252
entry 0x400806f4
I (440) cpu_start: Pro cpu up.
I (440) cpu_start: Application information:
I (441) cpu_start: Compile time: Nov 5 2019 12:51:12
I (444) cpu_start: ELF file SHA256: 0000000000000000...
I (450) cpu_start: ESP-IDF: v3.3
I (454) cpu_start: Starting app cpu, entry point is 0x40083168
I (0) cpu_start: App cpu up.
I (465) heap_init: Initializing. RAM available for dynamic allocation:
I (472) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (478) heap_init: At 3FFBA488 len 00025B78 (150 KiB): DRAM
I (484) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (490) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (497) heap_init: At 40092A60 len 0000D5A0 (53 KiB): IRAM
I (503) cpu_start: Pro cpu start user code
I (74) cpu_start: Chip Revision: 1
I (74) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (470) modsocket: Initializing
I call it like this..
Code: Select all
>>>
>>>
>>>
>>>
>>> import network as nw
>>> nw.phy_mode()
(0, 12289, 12292, 258)
>>>
>>> nw.phy_mode(nw.STA_IF, nw.MODE_LR)
(False, 12289)
>>>
>>>
>>> wlan = nw.WLAN(nw.STA_IF)
I (372060) wifi: wifi driver task: 3ffe2e9c, prio:23, stack:3584, core=0
I (386295) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (386305) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (386355) wifi: wifi firmware version: aeed694
I (386355) wifi: config NVS flash: enabled
I (386355) wifi: config nano formating: disabled
I (386355) wifi: Init dynamic tx buffer num: 32
I (386355) wifi: Init data frame dynamic rx buffer num: 32
I (386365) wifi: Init management frame dynamic rx buffer num: 32
I (386365) wifi: Init management short buffer num: 32
I (386375) wifi: Init static rx buffer size: 1600
I (386375) wifi: Init static rx buffer num: 10
I (386385) wifi: Init dynamic rx buffer num: 32
>>>
>>> nw.phy_mode(nw.STA_IF, nw.MODE_LR)
(False, 258)
>>>
>>>
>>> nw.phy_mode(nw.STA_IF)
(True, 7)
>>>
>>> nw.phy_mode(nw.STA_IF, 7)
(False, 258)
>>>
>>>
Note: before I call nw.WLAN(nw.STA_IF), the error I get is from phy_mode() is ESP_ERR_WIFI_NOT_INIT. After wlan is initialized, I get ESP_ERR_INVALID_ARG, which seems to suggest my call to esp_wifi_set_protocol() is executing actual code and not some stub.
Note: that phy_mode() returns the (presumably) correct mode value: 7 (ie, 11b|11g|11n == 1|2|4)
But note: setting mode to 7 also fails with ESP_ERR_INVALID_ARG.
This is compiled against idf v3.3
Can anyone suggest what might be missing to set phy mode. Has anyone out there successfully used ESP32 LR mode on a thing?