Connect to wifi
-
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?
-
Maybe yielding some processor time in your while loops perhaps, by sleeping?
-
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.
-
@g0hww
with your above code, you can hang in not connected state in endless loop.
You havetimeout=5000but 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 -
@livius Well spotted. In my specific case, that is what I want. But it is useful to point that out to the OP.
-
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.
-
@ErickM said in Connect to wifi:
VS say missing imports
VS do not know pycom libraries at all, so do not worry.
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