Connect to wifi asynchronously and use LED to indicate wifi status

This commit is contained in:
Lexi / Zoe 2024-01-26 19:01:46 +01:00
parent aa0a04b76f
commit 9136fb5c97
Signed by: binaryDiv
GPG Key ID: F8D4956E224DA232
3 changed files with 35 additions and 5 deletions

6
misc/chip_pinout.txt Normal file
View File

@ -0,0 +1,6 @@
Board: ESP32 NodeMCU
Pins:
- Neopixels: GPIO26
- Status LED: GPIO4
- Button: GPIO21 (against GND)

View File

@ -1,10 +1,12 @@
# boot.py -- This file is executed on every boot (including wake-boot from deepsleep) # boot.py -- This file is executed on every boot (including wake-boot from deepsleep)
from lightbar import wlan_manager import uasyncio
import webrepl import webrepl
# Connect to WLAN as defined in wlan_cfg.py from lightbar import wlan_manager
wlan_manager.connect()
# 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 # Start WebREPL on default port 8266 with password defined in webrepl_cfg.PASS
webrepl.start() webrepl.start()

View File

@ -1,6 +1,8 @@
import network
import time import time
import network
from machine import Pin
def connect() -> network.WLAN: def connect() -> network.WLAN:
""" """
@ -19,13 +21,21 @@ def connect() -> network.WLAN:
wlan = network.WLAN(network.STA_IF) wlan = network.WLAN(network.STA_IF)
wlan.active(True) wlan.active(True)
status_led = Pin(4, Pin.OUT)
status_led.off()
if not wlan.isconnected(): if not wlan.isconnected():
wlan.connect(wlan_cfg.SSID, wlan_cfg.PASSWORD) wlan.connect(wlan_cfg.SSID, wlan_cfg.PASSWORD)
print('[wlan_manager] Connecting to WLAN "{}" '.format(wlan_cfg.SSID), end='') print('[wlan_manager] Connecting to WLAN "{}" '.format(wlan_cfg.SSID), end='')
while not wlan.isconnected(): while not wlan.isconnected():
print('.', end='') 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!') print('\n[wlan_manager] Connected!')
else: else:
print('[wlan_manager] Already connected to WLAN "{}"'.format(wlan.config('essid'))) print('[wlan_manager] Already connected to WLAN "{}"'.format(wlan.config('essid')))
@ -35,4 +45,16 @@ def connect() -> network.WLAN:
wlan.ifconfig(wlan_cfg.IFCONFIG) wlan.ifconfig(wlan_cfg.IFCONFIG)
print('[wlan_manager] IP config: {}'.format(wlan.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 return wlan
async def connect_async():
connect()