import network
import time
import secret
from machine import Pin, I2C
from pico_i2c_lcd import I2cLcd
from ds1307 import DS1307
# Set up LED pin
led = Pin(1, Pin.OUT)
# Turn the LED off as an initial state
led.value(0)
# Setting I2C and DS1307
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=400000)
i2c1 = I2C(1, scl=Pin(11), sda=Pin(10), freq=400000)
clock = DS1307(i2c)
lcd = I2cLcd(i2c1, 0x27, 2, 16)
lcd.clear()
def loop():
while True:
led.on()
time_rtc = clock.datetime
y, m, d, H, M, S, WD, IDK = time_rtc
lcd.move_to(0, 0)
lcd.putstr(f"Date: {d}-{m}-{y}")
lcd.move_to(0, 1)
if M < 10:
lcd.putstr(f"Time: {H}:0{M}")
else:
lcd.putstr(f"Time: {H}:{M}")
time.sleep(0.0163)
# Connect to Wi-Fi
print("Connecting to WIFI")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secret.ssid, secret.password)
# Wait for connection to be established
max_wait = 0
while max_wait < 15:
if wlan.isconnected():
print("Connected")
break
max_wait += 1
print(f"Connecting....{max_wait}")
time.sleep(1)
# Control the LED based on the connection status
if wlan.isconnected():
# Display Datetime INFO
print("Wi-Fi is connected, Displaying Date and Time")
loop()
time.sleep(0.016)
else:
# If connection failed, blink the LED
print("Failed to connect to Wi-Fi. Blinking LED.")
while True:
led.toggle()
time.sleep(0.5)