From 9136fb5c97018d5ffdc4343fc346ad5ae3862938 Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Fri, 26 Jan 2024 19:01:46 +0100 Subject: [PATCH] Connect to wifi asynchronously and use LED to indicate wifi status --- misc/chip_pinout.txt | 6 ++++++ src/boot.py | 8 +++++--- src/lightbar/wlan_manager.py | 26 ++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 misc/chip_pinout.txt diff --git a/misc/chip_pinout.txt b/misc/chip_pinout.txt new file mode 100644 index 0000000..3b58a34 --- /dev/null +++ b/misc/chip_pinout.txt @@ -0,0 +1,6 @@ +Board: ESP32 NodeMCU + +Pins: +- Neopixels: GPIO26 +- Status LED: GPIO4 +- Button: GPIO21 (against GND) diff --git a/src/boot.py b/src/boot.py index ef06500..8c9ce8f 100644 --- a/src/boot.py +++ b/src/boot.py @@ -1,10 +1,12 @@ # boot.py -- This file is executed on every boot (including wake-boot from deepsleep) -from lightbar import wlan_manager +import uasyncio import webrepl -# Connect to WLAN as defined in wlan_cfg.py -wlan_manager.connect() +from lightbar import wlan_manager + +# Asynchronously connect to WLAN as defined in wlan_cfg.py +uasyncio.create_task(wlan_manager.connect_async()) # Start WebREPL on default port 8266 with password defined in webrepl_cfg.PASS webrepl.start() diff --git a/src/lightbar/wlan_manager.py b/src/lightbar/wlan_manager.py index 9b2c0bd..0c8af3c 100644 --- a/src/lightbar/wlan_manager.py +++ b/src/lightbar/wlan_manager.py @@ -1,6 +1,8 @@ -import network import time +import network +from machine import Pin + def connect() -> network.WLAN: """ @@ -19,13 +21,21 @@ def connect() -> network.WLAN: wlan = network.WLAN(network.STA_IF) wlan.active(True) + status_led = Pin(4, Pin.OUT) + status_led.off() + if not wlan.isconnected(): wlan.connect(wlan_cfg.SSID, wlan_cfg.PASSWORD) print('[wlan_manager] Connecting to WLAN "{}" '.format(wlan_cfg.SSID), end='') while not wlan.isconnected(): print('.', end='') - time.sleep(1) + + # Let the LED blink to indicate the connection attempt + status_led.on() + time.sleep(0.5) + status_led.off() + time.sleep(0.5) print('\n[wlan_manager] Connected!') else: print('[wlan_manager] Already connected to WLAN "{}"'.format(wlan.config('essid'))) @@ -35,4 +45,16 @@ def connect() -> network.WLAN: wlan.ifconfig(wlan_cfg.IFCONFIG) print('[wlan_manager] IP config: {}'.format(wlan.ifconfig())) + + # Let the LED blink 2 times quickly to indicate that the WLAN is connected + for _ in range(2): + time.sleep(0.125) + status_led.on() + time.sleep(0.125) + status_led.off() + return wlan + + +async def connect_async(): + connect()