From 58f8526d7eb79bc9a3437fce85388eaae8a39b5b Mon Sep 17 00:00:00 2001 From: Matthias Blankertz Date: Tue, 23 Dec 2025 11:46:32 +0100 Subject: [PATCH] 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 --- software/frontend/index.html | 6 ++++++ software/src/main.py | 27 +++++++++++++++++++-------- software/src/utils/config.py | 12 +++++++++++- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/software/frontend/index.html b/software/frontend/index.html index cd70a29..7cce847 100644 --- a/software/frontend/index.html +++ b/software/frontend/index.html @@ -384,6 +384,12 @@ 'root.LED_COUNT': { 'input-type': 'number' }, + 'root.WLAN.SSID': { + 'input-type': 'text' + }, + 'root.WLAN.PASSPHRASE': { + 'input-type': 'text' + } }; function renderObject(obj, path) { diff --git a/software/src/main.py b/software/src/main.py index e1e7b15..d3a0ca3 100644 --- a/software/src/main.py +++ b/software/src/main.py @@ -37,14 +37,22 @@ hwconfig.board_init() machine.mem32[0x40030000 + 0x00] = 0x10 -def setup_wifi(): +def setup_wifi(ssid='', passphrase='', sec=network.WLAN.SEC_WPA2_WPA3): network.hostname("TonberryPico") - wlan = network.WLAN(network.WLAN.IF_AP) - wlan.config(ssid=f"TonberryPicoAP_{machine.unique_id().hex()}", security=wlan.SEC_OPEN) - wlan.active(True) + if ssid is None or ssid == '': + apname = f"TonberryPicoAP_{machine.unique_id().hex()}" + 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 - wlan.config(pm=network.WLAN.PM_NONE) + # configure power management + wlan.config(pm=network.WLAN.PM_PERFORMANCE) mac = ubinascii.hexlify(network.WLAN().config('mac'), ':').decode() print(f" mac: {mac}") @@ -71,8 +79,11 @@ def run(): # Setup LEDs np = NeoPixel(hwconfig.LED_DIN, config.get_led_count(), sm=1) - # Wifi with default config - setup_wifi() + if machine.Pin(hwconfig.BUTTONS[1], machine.Pin.IN, machine.Pin.PULL_UP).value() == 0: + # Force default access point + setup_wifi('', '') + else: + setup_wifi(config.get_wifi_ssid(), config.get_wifi_passphrase()) # Setup MP3 player with SDContext(mosi=hwconfig.SD_DI, miso=hwconfig.SD_DO, sck=hwconfig.SD_SCK, ss=hwconfig.SD_CS, diff --git a/software/src/utils/config.py b/software/src/utils/config.py index 0ab4ad2..f5891b6 100644 --- a/software/src/utils/config.py +++ b/software/src/utils/config.py @@ -22,7 +22,11 @@ class Configuration: 'PREV': None, 'NEXT': 1, }, - 'TAGMODE': 'tagremains' + 'TAGMODE': 'tagremains', + 'WIFI': { + 'SSID': '', + 'PASSPHRASE': '', + } } def __init__(self, config_path='/config.json'): @@ -87,6 +91,12 @@ class Configuration: def get_tagmode(self) -> str: 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 def get_config(self) -> Mapping[str, Any]: return self.config