Pico W won't connect to wifi from main.py

I have tried several versions, of this; right now it looks like:

import…

wlan=network.WLAN(network.STA_IF)
wlan.active(True)
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_P4, rotate=0)
display.set_font(“bitmap8”)

def setScreen(m):
Display message ‘m’ on screen…

def connect():
global wlan
max_wait=30
while max_wait > 0:
wlan.connect(ssid,password)
if wlan.status() == 3:
break
max_wait-=1
time.sleep(1)
if wlan.status() != 3:
raise Exception(“Connection Failed”)

setScreen(“start”)
try:
connect()
setScreen(“connected”)
except:
setScreen(“failed”)

When I run it through Thonny it connects without a problem, but if it just runs naturally as main.py it fails to connect. Any suggestions?

1 Like