ESP32-S2 wifi radio: check whether already connected before trying to connect#4017
Conversation
|
I'm offering my approval based on testing comments in the issue. Someone else should review the code. |
…rol point for esp_wifi_set_mode()
|
I didn't mean to merge that yet. A couple of small clean-ups to Test code for checking enabled before scan: ITERATIONS = 10
DELAY = 0
for _ in range(ITERATIONS):
print("Enabled? ", wifi.radio.enabled)
try:
for network in wifi.radio.start_scanning_networks():
print("{0:02X}:{1:02X}:{2:02X}:{3:02X}:{4:02X}:{5:02X}".format(*network.bssid), end=" ")
print("{:>2d}".format(network.channel), end=" ")
print(network.rssi, network.authmode, network.country, network.ssid)
wifi.radio.stop_scanning_networks()
except (RuntimeError) as e:
print("RuntimeError:", e)
wifi.radio.enabled = not wifi.radio.enabled
time.sleep(DELAY)
print("Done.") |
|
Do you mean the commit was premature? Just push another commit when you're done. No need to start over. It's not merged yet. If it's in progress, you can mark it as a draft PR. |
|
It's done, it's just a little tangential to the original intent of this PR. The latest commit is independent of the earlier commits except for an error string. It's ready to go once the checks are done if you want me to leave it in this PR. |
|
failing on xtensa builds, which if I'm reading Discord right, is dependent on the rp2040 merge |
|
right, I will re-run these failing PR's after the #4031 merge (which is having a small issue with the size of one build). |
|
Oh, I misunderstood. No problem. Does it fix another issue, or is just an improvement? |

Fixes #3735 by checking to see if the station has wifi enabled and is not already connected before trying to connect. If wifi is not enabled, an exception is raised. If the station is already connected, connect returns immediately. Some early notes are in the issue comments.
This is fine now:
Since there's no harm now to try to connect when already connected, using
wifi.radio.enabled =to toggle wifi is a little more resilient. This is (still) fine:There are no changes to socketpool / socket, or to ping in this PR. Disconnections have the following consequences:
wifi.radio.enabled = Falsestops wifi (disconnecting in the process), so attempts to connect() when not enabled will result in variousadafruit_requestsRuntimeError, BrokenPipeError, and OSError exceptions. But after re-enabling withwifi.radio.enabled = Trueand connecting again, requests will work again.Similarly,
pingreturnsNonewhen wifi is disconnected / not enabled, but will work again after enabled and connected. There is an occasionalpingIDF error even when connected that I'll try to characterize.Addendum for the second commit:
Turns out
esp_wifi_connect()is not resilient to being called while wifi is stopped, so we also need to check enabled state (whether wifi is started or stopped), and not allow connect calls when wifi is stopped (raise exception):ESP_ERROR_CHECK failed: esp_err_t 0x3002 (ESP_ERR_WIFI_NOT_STARTED) at 0x400303f8Since connect() can now be called any time by user code, regardless of whether wifi is enabled or whether the station is already connected to an AP, it also becomes easy for user code to re-connect after a case where the AP goes away after connection (in this case, no disconnection exception was previously raised, but sockets would fail).
Addendum for the final commit:
The check to make sure wifi is enabled is added before starting a scan. If wifi isn't enabled,
common_hal_wifi_radio_start_scanning_network()will raise an exception.