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

    Lopy can't reconnect to access point after connection is lost.

    Scheduled Pinned Locked Moved MicroPython
    1 Posts 1 Posters 855 Views 1 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.
    • F Offline
      Feiko
      last edited by

      Hi I wrote some code for my Lopy that should reconnect to my access point after the connection is lost. I'm using the latest firmware version 1.5.0.b2.

      Here's my code:

      import network
      from network import WLAN
      import utime
      
      class Wlan():
          
          _ssid = "ssid"
          _passw = "secret"
          wlan = network.WLAN(mode=WLAN.STA)
          connected = False
          
          
          def __init__(self, ssid, passw):
              self._ssid = ssid
              self._passw = passw
              self.connected = self.wlan.isconnected()
              self.connect_to_hotspot()
              
          def connect_to_hotspot(self):
              while not self.connected:
                  print('scanning: ')
                  networks = self.wlan.scan()
                  for net in networks:
                      print(net.ssid)
                      
                      if net.ssid == self._ssid:  
                          print('found %s' % self._ssid)
                          self.wlan.connect(self._ssid, auth=(WLAN.WPA2, self._passw))
                          while not self.wlan.isconnected():
                              utime.sleep_ms(50)
                          print(self.wlan.ifconfig())
                          self.connected = True;
                  
                  if not self.connected:
                      utime.sleep_ms(1000)
                      print('not connected')
                      
              print('connected')
              
              
      
      wlan = Wlan("secret",  "ssid")
      
      while wlan.wlan.isconnected():
          pass
      
      print('wlan failed try to reconnect')
      wlan.wlan.deinit()
      wlan.connected = False
      utime.sleep(5)
      wlan.wlan.init(mode=WLAN.STA)
      wlan.connect_to_hotspot()
      

      The code runs and connects to my access point. After the connection is lost. I see that the Lopy is trying to reconnect and lists all the wlans. However, the Lopy can't find the network it previous connected to doesn’t show up. Even after disabling the wlan and enabling it again, it still can't find the right wireless network.

      If I cut the power for a view seconds and reboot, the wireless network is found and the Lopy connects.

      What’s going on? Please help.

      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