75 lines
2 KiB
Python
75 lines
2 KiB
Python
|
import machine
|
||
|
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)
|
||
|
np_hour[(hour % 12) * 2 + int(minute / 30)] = config.HOUR_COLOR
|
||
|
np_hour.write()
|
||
|
|
||
|
np_minute = neopixel.NeoPixel(machine.Pin(32), 30, bpp=4)
|
||
|
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...")
|
||
|
ntptime.settime()
|
||
|
except:
|
||
|
print("Could not sync time")
|
||
|
|
||
|
def check_daylight_saving(current_time):
|
||
|
for day in range(25, 32):
|
||
|
t = time.mktime((current_time[0], 3, day, 2, 0, 0, 0, 0))
|
||
|
if time.localtime(t)[6] == 6:
|
||
|
summer_time = t
|
||
|
break
|
||
|
|
||
|
for day in range(24, 32):
|
||
|
t = time.mktime((current_time[0], 10, day, 2, 0, 0, 0, 0))
|
||
|
if time.localtime(t)[6] == 6:
|
||
|
winter_time = t
|
||
|
break
|
||
|
|
||
|
comp_time = time.mktime(current_time)
|
||
|
if summer_time <= comp_time <= winter_time:
|
||
|
return True
|
||
|
return False
|
||
|
|
||
|
# Main Loop
|
||
|
while True:
|
||
|
|
||
|
# Call connect_wifi to reconnect if connection is lost
|
||
|
connect_wifi(config.WIFI_SSID, config.WIFI_PSK)
|
||
|
current_time = time.localtime()
|
||
|
|
||
|
# Sync time if we are on full hour
|
||
|
if current_time[4] == 0 and current_time[5] == 0 or current_time[0] == 2000:
|
||
|
sync_ntptime()
|
||
|
current_time = time.localtime()
|
||
|
|
||
|
if check_daylight_saving(current_time):
|
||
|
current_time = time.localtime(time.mktime(current_time) + config.TIMEZONE_OFFSET_DAYLIGHT)
|
||
|
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)
|