While NOT connected to WLAN #10557
-
MicroPython v1.19.1-831-g4f3780a15 on 2023-01-20; Raspberry Pi Pico W with RP2040 I'm trying to learn micropython in small snippets. I've succeeded in completing a few projects but, by copying someone else's code and modifying them for my needs. Currently working on While loops. I want to check if my Pico W is connected to my WLAN, and if not, halt everything until connected. This is what I have: # *****IMPORT NECESSARY MODULES******** #
import machine
from machine import Pin
import utime
import network
#** CHECK IF CONNECTED TO WLAN
while not wlan.isconnected(): THIS LINE DOESN'T WORK
while wlan !=wlan.isconnected() THIS DOESN'T WORK EITHER
#** LOOP UNTIL CONNECTED
#** THIS WORKS PERFECTLY FINE WITHOUT THE ABOVE
#** I just want to know how: if it's not connected, to halt it until it IS connected
#**ACTIVATE WIFI, CONNECT TO ROUTER
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.isconnected()
print("Connecting to router...")
wlan.connect('MYSSID', 'MYPASSWORD')
print("Connected")
print("Retreiving Network Configuration")
print("")
utime.sleep(1) # Give it a second to get an IP address (assigned by the router)
print (wlan.ifconfig()) Thanks in advance (Edited by @jimmo to format code snippets) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
A good example of a function to connect to an AP is in the documentation at https://docs.micropython.org/en/latest/esp8266/tutorial/network_basics.html?highlight=do_connect. The device will not connect until you call wlan.connect(), and then it might take a few moments until wlan.is_connected() returns True. |
Beta Was this translation helpful? Give feedback.
-
I think I got it right this time: #IMPORT NECESSARY MODULES
import machine
from machine import Pin
import utime
import network
#ACTIVATE WIFI, CONNECT TO ROUTER
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.isconnected()
print("Connecting to router...")
wlan.connect('MYSSID', 'MYPASSWORD')
#CHECK IF CONNECTED TO WLAN
while not wlan.isconnected():
pass# LOOP UNTIL CONNECTED
print("Connected")
print("Retrieving Network Configuration")
print("")
utime.sleep(1) # Give it a second to get an IP address (assigned by the router)
print (wlan.ifconfig())
#DO SOMETHING |
Beta Was this translation helpful? Give feedback.
-
@marinehm Please see the Welcome Post for information on how to format code in posts. |
Beta Was this translation helpful? Give feedback.
I think I got it right this time: