Created
January 2, 2023 21:54
-
-
Save RyanFleck/ad24ac9346768cbca8107dd580e5e16e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin, Timer, I2C | |
from pico_i2c_lcd import I2cLcd | |
from network import WLAN, STA_IF | |
# from urequests import post | |
import gc | |
import errno | |
import utime | |
import ntptime | |
MOUNTAIN_OFFSET = -7 * 60 * 60 | |
DAYLIGHT_OFFSET = 1 * 60 * 60 | |
led = Pin("LED", Pin.OUT) | |
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000) | |
I2C_ADDR = i2c.scan()[0] | |
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16) | |
# Add your own credentials here | |
ssid=' ' | |
password=' ' | |
led = Pin("LED", Pin.OUT) | |
led.low() | |
wlan = WLAN(STA_IF) | |
wlan.active(True) | |
# for network in wlan.scan(): | |
# print(str(network[0])) | |
def is_dst(t): | |
return False | |
def MST(t): | |
if is_dst(t): | |
return utime.localtime(t + MOUNTAIN_OFFSET + DAYLIGHT_OFFSET) | |
else: | |
return utime.localtime(t + MOUNTAIN_OFFSET) | |
def connect(): | |
print("Connecting to network.") | |
lcd.clear() | |
lcd.putstr("Connecting...") | |
led.low() | |
wlan.connect(ssid, password) | |
retry_wait = 0 | |
while not wlan.isconnected(): | |
print("Waiting for network... {}".format(retry_wait)) | |
retry_wait += 1 | |
if retry_wait == 10: | |
break | |
utime.sleep(1) | |
if wlan.isconnected(): | |
lcd.clear() | |
lcd.putstr("Connected!") | |
utime.sleep(1) | |
ntptime.settime() | |
led.high() # Set LED to high if network is active | |
lcd.clear() | |
print(utime.localtime()) | |
def main(): | |
if not wlan.isconnected(): | |
print("Disconnected! Reconnecting...") | |
connect() | |
else: | |
now = MST(utime.time()) | |
now = list(map(lambda x: '{:0>{w}}'.format(str(x), w=2), now)) | |
lcd.move_to(0,0) | |
lcd.putstr(str("{}-{}-{}\n{}:{}:{}").format(*now)) | |
# Sync (Busy Wait) | |
while MST(utime.time())[5] == now[5]: | |
utime.utime.sleep_ms(1) | |
while True: | |
main() | |
# Run main on boot and repeat. | |
while True: | |
try: | |
main() | |
except Exception as err: | |
lcd.clear() | |
print(str(err)) | |
lcd.putstr("Error: \n{}".format(str(err))) | |
utime.sleep(0.5) | |
print("Exception thrown.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment