Python Forum
[ESP32 Micropython]Total noob here, I don't understand why this while loop won't run.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[ESP32 Micropython]Total noob here, I don't understand why this while loop won't run.
#1
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).
Reply
#2
If you are using python 3, this line print "Waiting for Wifi..." should be print("Waiting for Wifi...")
wh33t likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Omg lol.

I see those brackets aren't optional like they are in PHP. Cheers!
Reply
#4
in python 2 print was a statement, in python3 it's a function and you need them for function call
wh33t likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply
#6
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?
wh33t likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#7
(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.
Reply
#8
(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...
Reply
#9
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#10
(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():
wh33t likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  micropython-stepper 1.0.3 trix 2 554 Nov-20-2023, 06:00 AM
Last Post: trix
  Read 2 Value with bleak via BLE notify from ESP32 Nietzsche 8 2,619 May-04-2023, 09:31 PM
Last Post: Nietzsche
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,318 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Noob Alert! Wrong result using loop and if statemnent GJG 7 2,794 Dec-19-2020, 05:18 PM
Last Post: buran
  How do I understand "and" in a loop leoahum 2 1,631 Apr-24-2020, 05:57 AM
Last Post: leoahum
  Please Help , Total Noob need help to run Scrip on Windows ClicksCode 2 1,708 Mar-06-2020, 01:59 PM
Last Post: DeaD_EyE
  geting started with Micropython on ESP 32 - do i need to have a virtual environment? apollo 0 1,764 Sep-02-2019, 04:25 PM
Last Post: apollo
  Python on Linux, Total Noob to Both halcyon 4 3,007 May-27-2019, 10:40 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020