From 1fa59a0ca5fcb912e4b6b15222fd7568ed7d759e Mon Sep 17 00:00:00 2001 From: Fusselkater Date: Wed, 10 Aug 2022 23:58:39 +0200 Subject: [PATCH] move wifi to boot.py --- boot.py | 18 +++++++++++++++++- main.py | 27 +++++++++------------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/boot.py b/boot.py index 09288fb..3bc8e35 100644 --- a/boot.py +++ b/boot.py @@ -1,2 +1,18 @@ import esp -esp.osdebug(None) \ No newline at end of file +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() \ No newline at end of file diff --git a/main.py b/main.py index d12581f..8c2c663 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,6 @@ import neopixel import config import ntptime import time -import webrepl def set_time(hour, minute): 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.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(): try: print("Syncing time...") @@ -51,6 +38,11 @@ def check_daylight_saving(current_time): return True return False +def check_wifi_connection(): + import network + sta_if = network.WLAN(network.STA_IF) + return sta_if.isconnected + # Test LEDs before startup for hour in range(0, 11): set_time(hour, 0) @@ -62,9 +54,10 @@ set_time(0, 0) # Main Loop while True: - - # Call connect_wifi to reconnect if connection is lost - connect_wifi(config.WIFI_SSID, config.WIFI_PSK) + + if not check_wifi_connection(): + machine.reset() + current_time = time.localtime() # Sync time if we are on full hour @@ -77,8 +70,6 @@ while True: else: current_time = time.localtime(time.mktime(current_time) + config.TIMEZONE_OFFSET) - - print("Set Time:", current_time) set_time(current_time[3], current_time[4]) time.sleep(1) \ No newline at end of file