← index #7179PR #15916
Duplicate · high · value 2.436
QUERY · ISSUE

Missing country settings for ESP WIFI

openby karfasopened 2021-04-28updated 2024-09-13
enhancementport-esp32

In micropython, there is no way to define in which country the ESP WIFI transmitters operate.
This might create legal problems for anyone using the ESP devices and micropython "out of the box".

The ESP-IDF function call is esp_wifi_set_country(), described in https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/wifi.html#wi-fi-country-code.

Current uPy sources have no call to esp_wifi_set_country() - I searched *.[hc] for it.

The issue had been discussed in https://forum.micropython.org/viewtopic.php?f=18&t=10388

CANDIDATE · PULL REQUEST

esp32/network_wlan: Get and set the WLAN country code.

openby agattiopened 2024-09-26updated 2025-05-14
port-esp32

Summary

This provides an implementation for #7179, by allowing to set (and get) the country code for the ESP32's WLAN radio interface.

No user-visible API changes have been introduced as this uses network.country rather than having its own extra method somewhere else (I initially thought about bolting it onto network.WLAN to minimise code changes, but in for a penny, in for a pound). Ports can now properly set the WLAN country code if they have the APIs for it, rather than just keeping track of the code itself.

There are a couple of minor things one should be aware of when using this on an ESP32, and those have been added as notes to network.country's documentation section:

  • ESP-IDF unconditionally stores the chosen country code into the device's non-volatile storage
  • setting the country code with no WLAN interface ever being instantiated will raise an exception
  • the country code will default to "worldwide" if it was never set in the device or if no WLAN interface was instantiated yet.

Testing

Testing was done manually on an ESP32C3 using ESP-IDF 5.0.4 and via tests/ports/esp32/country_wlan.py. This is tricky to write a reliable test for because the board's WLAN interface may not be in a known state when the test runs.

Right now the test only passes if it is run when no WLAN has been instantiated before and if the WLAN country code is set to either XX (the default when the flash is cleared) or to AU. I was tempted to put IT there instead but I managed to resist :)

The provided test cycles between those two known country codes, so it is still independent from the existing device configuration, as long as the first test run was done with an empty NVS.

Trade-offs and Alternatives

This feature slightly increases the code size so maybe it can be put behind a define of its own, if setting the WLAN country code is not needed. The existing implementation doesn't do anything interesting for ESP32, so some code space was already lost to begin with, which makes the extra space taken by this feature a bit easier to let go.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied