feat: Join an existing WiFi network

Add ssid and passphrase to config to configure the network to join. If
empty SSID is configured, it will create the "TonberryPicoAP..." network
in AP mode as before.

Holding the button 1 during startup will revert to AP mode regardless of
the current configuration.

Signed-off-by: Matthias Blankertz <matthias@blankertz.org>
This commit is contained in:
2025-12-23 11:46:32 +01:00
parent d3aef1be32
commit 58f8526d7e
3 changed files with 36 additions and 9 deletions

View File

@@ -384,6 +384,12 @@
'root.LED_COUNT': { 'root.LED_COUNT': {
'input-type': 'number' 'input-type': 'number'
}, },
'root.WLAN.SSID': {
'input-type': 'text'
},
'root.WLAN.PASSPHRASE': {
'input-type': 'text'
}
}; };
function renderObject(obj, path) { function renderObject(obj, path) {

View File

@@ -37,14 +37,22 @@ hwconfig.board_init()
machine.mem32[0x40030000 + 0x00] = 0x10 machine.mem32[0x40030000 + 0x00] = 0x10
def setup_wifi(): def setup_wifi(ssid='', passphrase='', sec=network.WLAN.SEC_WPA2_WPA3):
network.hostname("TonberryPico") network.hostname("TonberryPico")
wlan = network.WLAN(network.WLAN.IF_AP) if ssid is None or ssid == '':
wlan.config(ssid=f"TonberryPicoAP_{machine.unique_id().hex()}", security=wlan.SEC_OPEN) apname = f"TonberryPicoAP_{machine.unique_id().hex()}"
wlan.active(True) print(f"Create AP {apname}")
wlan = network.WLAN(network.WLAN.IF_AP)
wlan.config(ssid=apname, security=wlan.SEC_OPEN)
wlan.active(True)
else:
print(f"Connect to SSID {ssid} with passphrase {passphrase}...")
wlan = network.WLAN()
wlan.active(True)
wlan.connect(ssid, passphrase if passphrase is not None else '', security=sec)
# disable power management # configure power management
wlan.config(pm=network.WLAN.PM_NONE) wlan.config(pm=network.WLAN.PM_PERFORMANCE)
mac = ubinascii.hexlify(network.WLAN().config('mac'), ':').decode() mac = ubinascii.hexlify(network.WLAN().config('mac'), ':').decode()
print(f" mac: {mac}") print(f" mac: {mac}")
@@ -71,8 +79,11 @@ def run():
# Setup LEDs # Setup LEDs
np = NeoPixel(hwconfig.LED_DIN, config.get_led_count(), sm=1) np = NeoPixel(hwconfig.LED_DIN, config.get_led_count(), sm=1)
# Wifi with default config if machine.Pin(hwconfig.BUTTONS[1], machine.Pin.IN, machine.Pin.PULL_UP).value() == 0:
setup_wifi() # Force default access point
setup_wifi('', '')
else:
setup_wifi(config.get_wifi_ssid(), config.get_wifi_passphrase())
# Setup MP3 player # Setup MP3 player
with SDContext(mosi=hwconfig.SD_DI, miso=hwconfig.SD_DO, sck=hwconfig.SD_SCK, ss=hwconfig.SD_CS, with SDContext(mosi=hwconfig.SD_DI, miso=hwconfig.SD_DO, sck=hwconfig.SD_SCK, ss=hwconfig.SD_CS,

View File

@@ -22,7 +22,11 @@ class Configuration:
'PREV': None, 'PREV': None,
'NEXT': 1, 'NEXT': 1,
}, },
'TAGMODE': 'tagremains' 'TAGMODE': 'tagremains',
'WIFI': {
'SSID': '',
'PASSPHRASE': '',
}
} }
def __init__(self, config_path='/config.json'): def __init__(self, config_path='/config.json'):
@@ -87,6 +91,12 @@ class Configuration:
def get_tagmode(self) -> str: def get_tagmode(self) -> str:
return self._get('TAGMODE') return self._get('TAGMODE')
def get_wifi_ssid(self) -> str:
return self._get('WIFI')['SSID']
def get_wifi_passphrase(self) -> str:
return self._get('WIFI')['PASSPHRASE']
# For the web API # For the web API
def get_config(self) -> Mapping[str, Any]: def get_config(self) -> Mapping[str, Any]:
return self.config return self.config