Wlan timeout error



  • Hi all!

    I have an issue regarding connecting to a wifi network when the wifi network is not available.

    My application flow is (LoPy4 + Pysense) : wake from deep sleep -> get data from sensors -> connect to wifi network -> send data -> go to deep sleep.

    A part of my code:

    wlan = WLAN(mode=WLAN.STA)
    wlan.connect(WIFI_SSID, auth=(WLAN.WPA2, WIFI_KEY), timeout=5000)
    

    Results in:

    TimeoutError: Connection to AP Timeout!
    Pycom MicroPython 1.20.2.rc6 [v1.11-01f49f7] on 2020-02-28; LoPy4 with ESP32
    Pybytes Version: 1.3.1
    

    The problem is, that this stops execution of main.py.
    I would like to have control of what should happen after the connection timeout

    Something like:

    if (timeout error occurs):
    go back to deep sleep

    and then after deep sleep, LoPy4 boots up and application tries again.

    The point is for the timeout error NOT to stop main.py.

    example: I am enjoyng holiday somewhere at the beach sipping drinks and stuff. I also have battery powered LoPy+Pysense measuring stuff back home. Lightning strucks, power goes down and with it my home AP. Now, during this blackout my LoPy4 tries to send out new data, but ends up with the TimeoutError (which if OK so far, can happen). Now, after power is restored to my home, LoPy4 will never send out new data since it stayed stuck idling after the TimeoutError and I am f..ked (NOT OK, battery drains fast and someone has to physically acces the device and reset it).

    Anyone know how to achieve this? Is it possible? (I guess it is)

    Thank you,
    j_mor



  • @jcaron said in Wlan timeout error:

    @j_mor Given the error message in your first post, I'd guess it would be TimeoutError?

    I thought that too, tried

    except TimeoutError:
    

    gave me "NameError: name 'TimeoutError' isn't defined"

    Also, you may want to add a second layer of security with a watchdog timer (WDT) so if ever things go bad and the board gets stuck it reboots after the given delay. IIRC it's not 100% foolproof, but every little helps.

    great idea, thank you



  • This post is deleted!


  • @j_mor Given the error message in your first post, I'd guess it would be TimeoutError?

    Also, you may want to add a second layer of security with a watchdog timer (WDT) so if ever things go bad and the board gets stuck it reboots after the given delay. IIRC it's not 100% foolproof, but every little helps.



  • Figured it out!

    Using try: except: works as desired.

    try:
        wlan.connect(WIFI_SSID, auth=(WLAN.WPA2, WIFI_KEY), timeout=5000)
        print('Connecting to: ' + WIFI_SSID)
        while not wlan.isconnected():
            machine.idle() # energy saving while waiting
        print('WLAN connection succeeded!')
    except Exception:
        print('Could not connect to '+ WIFI_SSID +', going back to sleep.')
        py.setup_sleep(5)
        py.go_to_sleep()
    

    I am only not sure how exactly is the exception raised by connection timeout called.
    Documentation on micropython states about 30 different exceptions (IOError, RuntimeError, SystemError etc..),
    but "except Exception:" can be used in general for all of them.


Log in to reply
 

Pycom on Twitter