Skip to content
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 AP MAC address #5593

Merged
merged 1 commit into from
Nov 17, 2021
Merged

Set AP MAC address #5593

merged 1 commit into from
Nov 17, 2021

Conversation

anecdata
Copy link
Member

@anecdata anecdata commented Nov 16, 2021

Allow setting the Access Point mac_address. Like #5571, but for AP. Also, when hostname is set, set it for AP as well as Station.

Example code to demonstrate the feature, with results in the comments:

import time
import wifi
from secrets import secrets

# after init, wifi is started/enabled, wifi mode is station
# hostname is default, mac_addresses are default
print("# Defaults:")
print(wifi.radio.hostname)
print(f'{wifi.radio.mac_address[5]:02X}')
print(f'{wifi.radio.mac_address_ap[5]:02X}')

# set a new hostname
print("# New hostname:")
wifi.radio.hostname = "hostname24".encode()
print(wifi.radio.hostname)

# set new Station MAC address and connect to an external AP
print("# Station MAC address:")
wifi.radio.mac_address = bytes((0x7A, 0xDF, 0xA1, 0xFF, 0xFF, 0x24))
print(":".join("%02X" % _ for _ in wifi.radio.mac_address))
wifi.radio.connect(secrets["ssid"], secrets["password"])
if wifi.radio.ipv4_address:
    print("Connected.")
# router shows Station as:
# hostname24 7A:DF:A1:FF:FF:24

# disable / stop wifi (Station will be disconnected from its external AP)
print("# Disable / stop wifi")
wifi.radio.enabled = False

# start an on-board AP and set its MAC address
print("AP MAC address:")
wifi.radio.start_ap("ZZZ_📶", "wqiugdf23894756", authmode=[wifi.AuthMode.WPA2, wifi.AuthMode.PSK])
wifi.radio.mac_address_ap = bytes((0x7A, 0xDF, 0xA1, 0xFF, 0xFF, 0x25))  # Local MAC address
print(":".join("%02X" % _ for _ in wifi.radio.mac_address_ap))

# enable / restart wifi & re-check the AP MAC address
print("# Enable / restart wifi:")
wifi.radio.enabled = True
print("AP Gateway", wifi.radio.ipv4_gateway_ap)
print("AP Subnet ", wifi.radio.ipv4_subnet_ap)
print("AP IPv4   ", wifi.radio.ipv4_address_ap)
print(":".join("%02X" % _ for _ in wifi.radio.mac_address_ap))
print("# Check external wifi utility...")
time.sleep(15)  # allow time to check external wifi utility...
# wifi utility shows AP as:
# SSID BSSID             RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
# ZZZ_📶 7a:df:a1:ff:ff:25 -23  1,+1    Y  CN WPA2(PSK/AES,TKIP/TKIP)

# stop AP and try to set MAC address
wifi.radio.stop_ap()
wifi.radio.mac_address_ap = bytes((0x7A, 0xDF, 0xA1, 0xFF, 0xFF, 0x26))  # Local MAC address
# exception, as expected:
# RuntimeError: Interface must be started
print(":".join("%02X" % _ for _ in wifi.radio.mac_address_ap))

@anecdata anecdata added enhancement espressif applies to multiple Espressif chips network labels Nov 16, 2021
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Cool. Thank you!

@tannewt tannewt merged commit 3b7d2f1 into adafruit:main Nov 17, 2021
@anecdata anecdata deleted the ap_extras branch November 17, 2021 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement espressif applies to multiple Espressif chips network
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants