def connect():
global wifi_cfg, _wlan
_ap.active(False)
_wlan.active(True)
# removed scan of networks to allow connect to hidden essid
# Try to connect
_wlan.connect(wifi_cfg.name, wifi_cfg.password)
tries=15
for i in range(tries):
print("%d/%d. Trying to connect." %(i+1, tries))
machine.idle()
time.sleep(1)
# if _wlan.isconnected(): break
if _wlan.status() == network.STAT_GOT_IP: break
if _wlan.isconnected() and _wlan.status() == network.STAT_GOT_IP:
print('Wifi: connection succeeded!')
print(_wlan.ifconfig())
else:
print('Wifi: connection failed, starting accesspoint!')
accesspoint()
nr.start(nostop=True)
python类idle()的实例源码
def sleep_from_until (start, delay):
## while time.ticks_diff(start, time.ticks_ms()) < delay:
while time.ticks_diff(time.ticks_ms(),start) < delay:
machine.idle()
return start + delay
def connect(self):
self.wlan = network.WLAN(mode=network.WLAN.STA)
if not self.wlan.isconnected() or self.ssid() != self.SSID:
for net in self.wlan.scan():
if net.ssid == self.SSID:
self.wlan.connect(self.SSID, auth=(network.WLAN.WPA2,
self.password))
while not self.wlan.isconnected():
machine.idle() # save power while waiting
break
else:
raise Exception("Cannot find network '{}'".format(SSID))
else:
# Already connected to the correct WiFi
pass
def connect(self):
self.wlan = network.WLAN(mode=network.WLAN.STA)
if not self.wlan.isconnected() or self.ssid() != self.SSID:
for net in self.wlan.scan():
if net.ssid == self.SSID:
self.wlan.connect(self.SSID, auth=(network.WLAN.WPA2,
self.password))
while not self.wlan.isconnected():
machine.idle() # save power while waiting
break
else:
raise Exception("Cannot find network '{}'".format(SSID))
else:
# Already connected to the correct WiFi
pass
def oflush(self, n=255):
t = 5000
while t:
self.i2c.readfrom_into(self.i2c_addr + 1, self.buf1)
r = self.buf1[0]
if r >= n:
return
t -= 1
machine.idle()
raise OSError(uerrno.ETIMEDOUT)
def setup():
global d
d = dht.DHT22(Pin(2))
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
cfg = load_config()
ap = cfg['ap']
wlan.connect(ap['ssid'], ap['pwd'])
while not wlan.isconnected():
machine.idle()
wifi(cfg['mqtt'])