def connect_to_wifi(self, ssid, password, mode, wait_for_ip=0):
import network, time
log.info("Attempting to connect to WiFi '{}' with password '{}'...".format(ssid, password))
n = network.WLAN(mode)
n.active(True)
n.connect(ssid, password)
# Wait for IP address to be provided
count = 0
while not n.isconnected() and count < wait_for_ip:
log.info("Waiting to obtain IP ... ({} sec remaining)".format(str(wait_for_ip - count)))
time.sleep(1)
count += 1
# Get provided IP
ip = n.ifconfig()[0]
if ip == "0.0.0.0":
log.info("Could not obtain IP on '{}'".format(ssid))
else:
log.info("Connected with IP '{}'".format(ip))
return ip
# @timed_function
评论列表
文章目录