-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set Station MAC address & validate connect SSID len #5571
Conversation
uh-oh, now what, |
This code is not included in |
Old bug was in wifi init: station was started manually using ESP API rather than using
So now things should be more robust with respect to wifi starts/enables and stops/disables, station mode starting and stopping, etc.: Adafruit CircuitPython 7.0.0-rc.1-823-ge4f06f69c-dirty on 2021-11-12; Gravitech Cucumber R with ESP32S2
>>> import wifi
>>> from secrets import secrets
>>>
>>> # after init, wifi is started/enabled, wifi mode is station, and there is no connection
>>> wifi.radio.hostname
'cucumber_r'
>>> f'{wifi.radio.mac_address[0]:02X}'
'7C'
>>>
>>> # unlike hostname, wifi mode must be station to set mac_address
>>> wifi.radio.stop_station()
>>> wifi.radio.hostname = "hostname7A".encode()
>>> wifi.radio.hostname
'hostname7A'
>>> wifi.radio.mac_address = bytes((0x7A, 0xDF, 0xA1, 0xFF, 0xFF, 0xFF))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Station must be started
>>>
>>> # put wifi mode back to station
>>> wifi.radio.start_station()
>>>
>>> wifi.radio.mac_address = bytes((0x7A, 0xDF, 0xA1, 0xFF, 0xFF, 0xFF))
>>> f'{wifi.radio.mac_address[0]:02X}'
'7A'
>>>
>>> wifi.radio.connect(secrets["ssid"], secrets["password"])
>>> # ESP32-S2 now shows up in router with hostname "hostname7A" and MAC address 7A:DF:A1:FF:FF:FF
>>>
>>> # now stop wifi and set new hostname and MAC address
>>> wifi.radio.enabled = False
>>> wifi.radio.hostname = "hostname78".encode()
>>> wifi.radio.hostname
'hostname78'
>>> wifi.radio.mac_address = bytes((0x78, 0xDF, 0xA1, 0xFF, 0xFF, 0xFF))
>>> f'{wifi.radio.mac_address[0]:02X}'
'78'
>>>
>>> # now start wifi and connect
>>> wifi.radio.enabled = True
>>> wifi.radio.connect(secrets["ssid"], secrets["password"])
>>> # ESP32-S2 now shows up in router with hostname "hostname78" and MAC address 78:DF:A1:FF:FF:FF |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks @anecdata.
Allow Station MAC address to be set for MAC address randomization, private wifi address, etc., just like the big kids do.
For example,
wifi.radio.mac_address = bytes((0x7E, 0xDF, 0xA1, 0xFF, 0xFF, 0xFF))
changes the MAC address of the Station and it connects to wifi, shows up on the router with the new address, and operates as expected.Tested the following error scenarios successfully:
ValueError: Invalid MAC address
- too short, too longTypeError: object with buffer protocol required
-None
; MAC not of a buffer protocol typeRuntimeError: Invalid multicast MAC address
- any MAC address where bit 0 of the first octet is 1RuntimeError: Can't set MAC address while connected
- wifi can be enabled / started, but station can't be connected to an APRuntimeError: Station must be started
- MAC address can't be set if wifi mode is not STA or STA+APAlso added input validation for SSID length in the
connect()
function (max 32 bytes allowed):wifi.radio.connect("*"*33, secrets["password"])
results in aValueError
[thanks for finding that bug @jepler]