Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Wlan timeout error

    Scheduled Pinned Locked Moved Discussion
    5 Posts 3 Posters 191 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      j_mor
      last edited by

      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

      robert-hhR 1 Reply Last reply Reply Quote 0
      • J Offline
        j_mor
        last edited by

        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.

        J 1 Reply Last reply Reply Quote 0
        • J Offline
          jcaron @j_mor
          last edited by

          @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.

          J 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @j_mor
            last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • J Offline
              j_mor @jcaron
              last edited by

              @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

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • 1 / 1
              • First post
                Last post

              Pycom on Twitter

              Pycom