move wifi to boot.py

This commit is contained in:
Fusselkater 2022-08-10 23:58:39 +02:00
parent 816b83f6fd
commit 1fa59a0ca5
2 changed files with 26 additions and 19 deletions

18
boot.py
View File

@ -1,2 +1,18 @@
import esp import esp
esp.osdebug(None) import webrepl
import config
def connect_wifi(ssid, psk):
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
sta_if.active(True)
sta_if.connect(ssid, psk)
while not sta_if.isconnected():
pass
return(sta_if.ifconfig())
esp.osdebug(None)
connect_wifi(config.WIFI_SSID, config.WIFI_PSK)
webrepl.start()

27
main.py
View File

@ -3,7 +3,6 @@ import neopixel
import config import config
import ntptime import ntptime
import time import time
import webrepl
def set_time(hour, minute): def set_time(hour, minute):
np_hour = neopixel.NeoPixel(machine.Pin(23), 24, bpp=4) np_hour = neopixel.NeoPixel(machine.Pin(23), 24, bpp=4)
@ -14,18 +13,6 @@ def set_time(hour, minute):
np_minute[int(minute / 2)] = config.MINUTE_COLOR np_minute[int(minute / 2)] = config.MINUTE_COLOR
np_minute.write() np_minute.write()
def connect_wifi(ssid, psk):
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
sta_if.active(True)
sta_if.connect(ssid, psk)
while not sta_if.isconnected():
pass
webrepl.start()
return(sta_if.ifconfig())
def sync_ntptime(): def sync_ntptime():
try: try:
print("Syncing time...") print("Syncing time...")
@ -51,6 +38,11 @@ def check_daylight_saving(current_time):
return True return True
return False return False
def check_wifi_connection():
import network
sta_if = network.WLAN(network.STA_IF)
return sta_if.isconnected
# Test LEDs before startup # Test LEDs before startup
for hour in range(0, 11): for hour in range(0, 11):
set_time(hour, 0) set_time(hour, 0)
@ -62,9 +54,10 @@ set_time(0, 0)
# Main Loop # Main Loop
while True: while True:
# Call connect_wifi to reconnect if connection is lost if not check_wifi_connection():
connect_wifi(config.WIFI_SSID, config.WIFI_PSK) machine.reset()
current_time = time.localtime() current_time = time.localtime()
# Sync time if we are on full hour # Sync time if we are on full hour
@ -77,8 +70,6 @@ while True:
else: else:
current_time = time.localtime(time.mktime(current_time) + config.TIMEZONE_OFFSET) current_time = time.localtime(time.mktime(current_time) + config.TIMEZONE_OFFSET)
print("Set Time:", current_time) print("Set Time:", current_time)
set_time(current_time[3], current_time[4]) set_time(current_time[3], current_time[4])
time.sleep(1) time.sleep(1)