Skip to content

wifi: let connect() take a scan result - #11309

Open
mikeysklar wants to merge 4 commits into
adafruit:mainfrom
mikeysklar:esp32c5-wifi-band
Open

wifi: let connect() take a scan result#11309
mikeysklar wants to merge 4 commits into
adafruit:mainfrom
mikeysklar:esp32c5-wifi-band

Conversation

@mikeysklar

@mikeysklar mikeysklar commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #11308. Base is esp32c5-wifi-5ghz, so only the top commit is new here.

Body updated: this replaces an earlier CIRCUITPY_WIFI_BAND design, discussed above.

What

connect() accepts a Network from start_scanning_networks() and uses its
bssid and channel, so reaching a specific AP needs no second scan.

Why

On a same-SSID dual-band network an unqualified connect() lands on 2.4 GHz.
Passing the scan result reaches the 5 GHz AP without scanning twice.

Hardware tested

ESP32-C5-DevKitC-1-N8R8, 10.3.0-45-ge3bffa92b4. Connect timings, 10 trials
each, are in the comment below.

AI assistance

Claude wrote the overload and the docstring. I ran the connect timings on
hardware and checked the channel numbers myself.

@dhalbert

dhalbert commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

The reason for a settings.toml value is to specify 2.4 vs 5 when using auto-connect.

I didn't suggest a property necessarily, it could also be an optional arg to connect() or start_ap(), which would limit the way it could change more. I don't know whether Espressif provides simultaneous dual-band connections but we should also look at the Nordic nRF7002, which is dual band.

EDIT: connect(), not start_station(), I think makes more sense.

@mikeysklar
mikeysklar marked this pull request as ready for review September 4, 2026 19:36
@mikeysklar
mikeysklar marked this pull request as draft September 4, 2026 19:42

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I agree with Dan. This should only impact wifi auto-connect. I think connect can be specific by BSSID or channel already. Channel is especially good because it reduces scan time a ton.

We also considered having connect take a scan result to encapsulate this info. Or maybe ScanResult could have connect() itself.

@mikeysklar mikeysklar changed the title wifi: add CIRCUITPY_WIFI_BAND settings.toml override wifi: let connect() take a scan result Sep 5, 2026
@mikeysklar

mikeysklar commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator Author

connect() takes a Network from a scan and uses its bssid and channel.

CIRCUITPY_WIFI_BAND is gone.

As tannewt suggested channel= is much faster (7x). Same for net or bssid=.

ALL_CHANNEL_SCAN is expensive.

10 trials each (connection time measured in seconds):

Call Mean Range Channel
connect(ssid, pw) 11.01 10.77-11.30 5
connect(ssid, pw, channel=149) 1.55 1.14-1.64 149
connect(ssid, pw, bssid=...) 1.62 1.59-1.64 149
connect(net, pw) 1.62 1.59-1.66 149

Scanning to get the Network costs 3.57s, so scan plus connect is 5.18s end to end.

Passing a Network from start_scanning_networks() uses its bssid and
channel, so reaching a specific AP needs no second scan.

Gated behind CIRCUITPY_WIFI_CONNECT_NETWORK, on by default. The
nRF7002 DK runs from 1MB of internal flash with under 800 bytes free
and overflowed by 32, so it opts out in its circuitpython.toml the
way it already does for ulab and radio.ping(). Costs 64 bytes,
measured on the ESP32-C5.

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for these changes. I'd rather have a new network kwarg with it's own comment about what it does.

Is there any way to shrink the 7002 instead? I'd rather not have this not on every board.

connect() takes a network= keyword instead of accepting a Network in the
ssid slot. ssid is now optional, network is keyword only, and passing both
raises TypeError.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@mikeysklar

Copy link
Copy Markdown
Collaborator Author

network= is its own kwarg

Passing both ssid and network raises TypeError.

Tested on the ESP32-C5-DevKitC-1-N8R8, 10.3.0-50-ge51d55ac3f:

Call Result
connect(network=net, password=pw) 1.19 s, ch 149, 192.168.0.79
connect() TypeError, 'ssid' argument required
connect(ssid=w, password=p, network=net) TypeError, Supply either ssid or network, not both
connect(w, p, network=net) same TypeError
connect(w, p) connects, unchanged

ap_info after the first row reports ch 149 rssi -52 bssid b0:19:21:df:d4:02,
matching the scanned Network it was given.

Yes on the 7002. It reserves a 32 KB storage_partition nothing reads: the port's
only settings user is common-hal/_bleio/Adapter.c:351 and the board sets
CONFIG_BT=n. Giving it to slot0 ends exactly at end of flash. #11335.

Comment thread py/circuitpy_mpconfig.h Outdated
mikeysklar and others added 2 commits September 10, 2026 13:02
adafruit#11335 gave the nRF7002 DK the flash it needed, so every board gets
connect(network=). Removes CIRCUITPY_WIFI_CONNECT_NETWORK.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@mikeysklar
mikeysklar changed the base branch from esp32c5-wifi-5ghz to main September 10, 2026 20:25

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants