Changes to be committed:

modified:   examples/src/micropython-webserver/main.py
This commit is contained in:
iosullivan committed 2026-10-11 18:33:01 +10:00
1 parent 658adc9baf
commit 15cad37d5e
1 file changed
+55 -49
+55 -49
View File
@@ -52,55 +52,6 @@ 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):
# Allocate first and second station
stations = {}
@@ -108,6 +59,13 @@ async def CTA_Stations(config):
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
# 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())