micropy-light/code/boot.py

44 lines
945 B
Python
Raw Permalink Normal View History

2020-04-21 19:10:31 +02:00
"""This file is executed on every boot (including wake-boot from deepsleep)"""
# import esp
# esp.osdebug(None)
2018-09-13 17:45:22 +02:00
import gc
2018-11-18 23:15:25 +01:00
import time
import network
2020-05-16 09:06:18 +02:00
# import webrepl
2020-04-21 20:58:14 +02:00
from config import KNOWN_WIFI_AP
2018-11-18 23:15:25 +01:00
2020-05-16 09:06:18 +02:00
# Disable AP
WLAN = network.WLAN(network.AP_IF)
WLAN.active(False)
2018-11-18 23:15:25 +01:00
# Connect to one of the known wifi AP
2020-04-21 17:56:30 +02:00
WLAN = network.WLAN(network.STA_IF)
WLAN.active(True)
2020-04-21 19:10:31 +02:00
AP_LIST = WLAN.scan()
2018-11-18 23:15:25 +01:00
2020-04-21 19:10:31 +02:00
for ap in AP_LIST:
2020-04-21 17:56:30 +02:00
if WLAN.isconnected():
2018-11-18 23:15:25 +01:00
break
ap_ssid = ap[0].decode("utf-8")
2020-04-21 20:58:14 +02:00
if ap_ssid in KNOWN_WIFI_AP.keys():
2018-11-18 23:15:25 +01:00
print("Known wifi network found : {}".format(ap_ssid))
print("Try to connect...")
2020-04-21 20:58:14 +02:00
WLAN.connect(ap_ssid, KNOWN_WIFI_AP[ap_ssid])
2020-04-21 17:56:30 +02:00
DELAY = 0
2018-11-18 23:15:25 +01:00
2020-04-21 17:56:30 +02:00
while not WLAN.isconnected():
print("Waiting for wifi to connect...")
time.sleep(1)
DELAY += 1
if DELAY > 10:
print("Wifi time-out")
break
2018-11-18 23:15:25 +01:00
2020-05-16 09:06:18 +02:00
# webrepl.start()
2018-11-18 23:15:25 +01:00
print("Boot.py : Done")
2018-09-13 17:45:22 +02:00
gc.collect()