Skip to content

Instantly share code, notes, and snippets.

@iBobX
Forked from dybber/connectToWifi.py
Created May 17, 2022 19:13
Show Gist options
  • Save iBobX/cc17ae9fe58d2792ecc9e0f5bab34a43 to your computer and use it in GitHub Desktop.
Save iBobX/cc17ae9fe58d2792ecc9e0f5bab34a43 to your computer and use it in GitHub Desktop.
MicroPython wifi connection w. timeout
import machine
import time
import network
import urequests
# Function to connect to a specific wifi network
def connectToWifi(wifi, essid, password, timeout):
if not wifi.isconnected():
print("Connecting to WiFi network...")
wifi.connect(essid, password)
# Wait until connected
t = time.ticks_ms()
while not wifi.isconnected():
if time.ticks_diff(time.ticks_ms(), t) > timeout:
wifi.disconnect()
print("Timeout. Could not connect.")
return False
print("Successfully connected to " + essid)
return True
else:
print("Already connected")
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment