From 15cad37d5e1b042df2d4be858e25708e11ffee4e Mon Sep 17 00:00:00 2001 From: iosullivan Date: Sun, 11 Oct 2026 18:33:01 +1000 Subject: [PATCH] Changes to be committed: modified: examples/src/micropython-webserver/main.py --- examples/src/micropython-webserver/main.py | 110 +++++++++++---------- 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/examples/src/micropython-webserver/main.py b/examples/src/micropython-webserver/main.py index adc1c46..c8492a5 100755 --- a/examples/src/micropython-webserver/main.py +++ b/examples/src/micropython-webserver/main.py @@ -52,62 +52,20 @@ async def save(request): async def index(request): return send_file('portal.html') -# Setup Neopixel -p = machine.Pin(20) # connected on pin 20 -wled = neopixel.NeoPixel(p,2) -wled[0] = (0,0,0) -wled[1] = (0,0,0) -wled.write() - -# Check if already configured and load config for Wifi, API Key etc. -try: - with open("config.json", "r") as f: - config = json.load(f) - SSID = config["SSID"] - PASS = config["PASS"] - - wlan = network.WLAN(network.STA_IF) - wlan.active(True) - while not wlan.isconnected(): - try: - wlan.connect(SSID, PASS) - max_wait = 10 - while max_wait > 0: - if wlan.isconnected(): - break - max_wait -= 1 - print('Waiting for connection...') - sleep(1) - if wlan.isconnected(): - print('Connected to Wi-Fi: ', SSID) - print('Device IP Address:', wlan.ifconfig()[0]) - break - except Exception as e: - print('Connection error:', e) - print('Retrying connection...') - sleep(2) -except: - # Failed to open config file, launch AP mode and disable trains - demo = True - wlan = network.WLAN(network.AP_IF) - wlan.active(True) - - wlan.config( - essid="CTA-Setup", - authmode=network.AUTH_OPEN - ) - - print("AP active for initial setup: ", wlan.active()) - print("AP IP address:", wlan.ifconfig()[0]) - - -async def CTA_Stations(config): +async def CTA_Stations(config): # Allocate first and second station stations = {} stations[0] = { 'id':config["LStation"], 'eta':[] } stations[1] = { 'id':config["RStation"], 'eta':[] } APIKEY = config["APIKEY"] - + + # Setup Neopixel + p = machine.Pin(20) # connected on pin 20 + wled = neopixel.NeoPixel(p,2) + wled[0] = (0,0,0) + wled[1] = (0,0,0) + wled.write() + # Synchronize time of RTC with NTP last_ntp_sync = -3700 # Init as more than an hour ago last_api_query = mktime([2000,1,1,0,0,0,0,0]) # Set last query default (will run on first run) @@ -181,4 +139,52 @@ async def main(): await server -asyncio.run(main()) \ No newline at end of file +# Check if already configured and load config for Wifi, API Key etc. +try: + with open("config.json", "r") as f: + config = json.load(f) + SSID = config["SSID"] + PASS = config["PASS"] + + wlan = network.WLAN(network.STA_IF) + wlan.active(True) + while not wlan.isconnected(): + try: + wlan.connect(SSID, PASS) + max_wait = 10 + while max_wait > 0: + if wlan.isconnected(): + break + max_wait -= 1 + print('Waiting for connection...') + sleep(1) + if wlan.isconnected(): + print('Connected to Wi-Fi: ', SSID) + print('Device IP Address:', wlan.ifconfig()[0]) + break + except Exception as e: + print('Connection error:', e) + print('Retrying connection...') + sleep(2) +except: + # Failed to open config file, launch AP mode and disable trains + demo = True + wlan = network.WLAN(network.AP_IF) + wlan.active(True) + + wlan.config( + essid="CTA-Setup", + authmode=network.AUTH_OPEN + ) + + print("AP active for initial setup: ", wlan.active()) + print("AP IP address:", wlan.ifconfig()[0]) + + +asyncio.run(main()) + + + + + +