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

    Connect to wifi

    Scheduled Pinned Locked Moved LoPy
    7 Posts 3 Posters 44 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.
    • E Offline
      ErickM
      last edited by

      Hello, im new on Lopy4 and im doing some little test, with the documentation guide i made some basic functions but when i trie to connect the wifi just dont work, here is the code:

      from network import WLAN
      
      wlan = WLAN(mode=WLAN.STA)
      wlan.connect("my net", auth=(WLAN.WPA2, "my net key"), timeout=5000)
      
      while wlan.isconnected():
          print("network")
      
      while not wlan.isconnected():
          print("no network")
      

      i know that could be better but is just a quick test
      With the code of the documentation, the console say "is connect" but nothing change in the modem page.

      Some suggestion or idea?

      1 Reply Last reply Reply Quote 0
      • G Offline
        g0hww
        last edited by

        Maybe yielding some processor time in your while loops perhaps, by sleeping?

        G 1 Reply Last reply Reply Quote 0
        • G Offline
          g0hww @g0hww
          last edited by g0hww

          Here is my code

          def wificonnect(wlan=WLAN()):
              if machine.reset_cause() != machine.SOFT_RESET:
                  wlan.init(mode=WLAN.STA)
          
              if not wlan.isconnected():
                  wlan.connect(config.wifi_ssid, auth=(WLAN.WPA2, config.wifi_pass), timeout=5000)
                  while not wlan.isconnected():
                      machine.idle()
                  print("Connected to wifi with IP address: " + wlan.ifconfig()[0])
          

          Edit: Actually, it might be the case that when your first while loop is encountered, the wifi has not yet been connected, so it moves on from there to the second loop, says it is not connected and then completes when it is connected. Sorry, it is early here and I don't have much time due to work, so I didn't fully appreciate what you had posted.

          L 1 Reply Last reply Reply Quote 0
          • L Offline
            livius @g0hww
            last edited by livius

            @g0hww
            with your above code, you can hang in not connected state in endless loop.
            You have timeout=5000 but loop is endless:

            while not wlan.isconnected():
                        machine.idle()
            

            i do this in this way, below 5second timeout. Put it after wlan.connect:

            chrono = Timer.Chrono()
            chrono.start()
            while not wlan.isconnected():
            	machine.idle() # save power while waiting
            	if chrono.read()>5: #5sedcond timeout
            		break

            think about people first, then computers

            G 1 Reply Last reply Reply Quote 1
            • G Offline
              g0hww @livius
              last edited by

              @livius Well spotted. In my specific case, that is what I want. But it is useful to point that out to the OP.

              1 Reply Last reply Reply Quote 0
              • E Offline
                ErickM
                last edited by

                Thanks for the anwers, but now i know that my problem is the libraries, for some reason what idk, when i put the network lib, VS say missing imports, im trying to solve that.

                L 1 Reply Last reply Reply Quote 0
                • L Offline
                  livius @ErickM
                  last edited by

                  @ErickM said in Connect to wifi:

                  VS say missing imports

                  VS do not know pycom libraries at all, so do not worry.

                  think about people first, then computers

                  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