Python Forum

Full Version: [ESP32 Micropython]Total noob here, I don't understand why this while loop won't run.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I just went to turn the Wifi chip on, and scan to see which wifi networks are available.

    # wifi_test.py
    import machine
    import sys
    import network
    import utime
    import urequests

    # Create a station object to store our connection
    station = network.WLAN(network.STA_IF)
    station.active("up")

    while len(station.scan()) == 0:
        print "Waiting for Wifi..."
        
    print(station.scan())
I get this error:

Error:
Traceback (most recent call last): File "<stdin>", line 14 SyntaxError: invalid syntax
I am trying to use len(), because station.scan will output an empty list if the wifi is not ready (I think).
If you are using python 3, this line print "Waiting for Wifi..." should be print("Waiting for Wifi...")
Omg lol.

I see those brackets aren't optional like they are in PHP. Cheers!
in python 2 print was a statement, in python3 it's a function and you need them for function call
(Feb-28-2023, 06:05 PM)buran Wrote: [ -> ]in python 2 print was a statement, in python3 it's a function and you need them for function call

Cheers. TY. I am not getting the result I expected, but it is not erroring now.
Not that I've used the libraries that you are using, but would while not station.scan(): be better? That way (in theory, least ways) you're testing for 'truthfulness'. Also, do you not have to update station.scan() in the while: loop? If not, how will any change in state be detected?
(Feb-28-2023, 06:25 PM)rob101 Wrote: [ -> ]Not that I've used the libraries that you are using, but would if not station.scan(): be better? That way (in theory, least ways) you're testing for 'truthfulness'. Also, do you not have to update station.scan() in the while: loop? If not, how will any change in state be detected?

Originally I tried while while !station.scan(): but I don't think that's valid python. I presumed wifi.scan() "forked" out into the background, and eventually wifi.scan() would return something. This seems to be pretty intermittent so maybe you are right, I have to continually update and check to see.
(Feb-28-2023, 06:27 PM)rob101 Wrote: [ -> ]Not that I've used the libraries that you are using, but would while not station.scan(): be better? That way (in theory, least ways) you're testing for 'truthfulness'. Also, do you not have to update station.scan() in the while: loop? If not, how will any change in state be detected?

I now have this code

# wifi_test.py
import network
import time

# Create a station object to store our connection
wifi = network.WLAN(network.STA_IF)
wifi.active(True)

available_wifi_networks = wifi.scan()

while len(available_wifi_networks) == 0:
    print("Waiting for wifi.scan() to return something...")
    time.sleep(1)
    available_wifi_networks = wifi.scan()
else:
    for i in available_wifi_networks:
        print (i)
It just keeps outputting

Output:
Waiting for wifi.scan() to return something...
I've tried the network library, but on my system (Debian based Linux distro) I'm getting builtins.AttributeError: module 'network' has no attribute 'WLAN', so I can't help; sorry.
(Feb-28-2023, 06:27 PM)wh33t Wrote: [ -> ]Originally I tried while while !station.scan(): but I don't think that's valid python.
It should be

while not station.scan():