import utime as time
from machine import I2C, Pin, Timer, PWM
# Drivers for the RTC and the display
# https://github.com/mcauser/micropython-tinyrtc-i2c
# https://github.com/T-622/RPI-PICO-I2C-LCD
import os, network, rp2, time, urequests
from datetime import datetime, timedelta
from machine import Pin, I2C, RTC
from picozero import Speaker
from ds1307 import DS1307
import json
from i2c_lcd import I2cLcd
from time_plus import strptime
time.sleep(0.1) # Wait for USB to become ready
print("Hello, Pi Pico!")
# Sync time and config on boot
SYNC_ON_BOOT = True
# Get rid of the hash if you want it to update: https://stackoverflow.com/a/47175630
JSON_URL = "https://pastebin.com/raw/mfg17UUm"
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = "password not needed for Wokwi-GUEST"
WIFI_COUNTRY = "US"
ONBOARD_LED_PIN = 25
LCD_I2C_SDA_PIN = 12
LCD_I2C_SCL_PIN = 13
DS1307RTC_I2C_SCL_PIN = 15
DS1307RTC_I2C_SDA_PIN = 14
SPEAKER_PIN = 16
conf = {}
onboard_led = Pin(ONBOARD_LED_PIN, Pin.OUT)
internal_rtc = RTC()
lcd_i2c = I2C(0, sda=Pin(LCD_I2C_SDA_PIN), scl=Pin(LCD_I2C_SCL_PIN), freq=400000)
lcd = I2cLcd(lcd_i2c, 0x27, 2, 16)
ds1307rtc = DS1307(I2C(1, scl = Pin(DS1307RTC_I2C_SCL_PIN), sda = Pin(DS1307RTC_I2C_SDA_PIN),freq = 400000))
last_pressed = time.ticks_ms()
speaker = Speaker(SPEAKER_PIN)
def sync():
global conf, JSON_URL, WIFI_COUNTRY, ds1307rtc
# Connect to WiFi
rp2.country(WIFI_COUNTRY)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# set power mode to get WiFi power-saving off (if needed)
wlan.config(pm = 0xa11140)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
while not wlan.isconnected() and wlan.status() >= 0:
# print("Waiting to connect:")
# time.sleep(1)
pass
# print(wlan.ifconfig())
# Download config
try:
os.remove("/config.json")
except OSError:
pass
with open("/config.json", "w") as f:
resp = urequests.request("GET", JSON_URL)
conf = resp.json()
# print(conf)
f.write(resp.text)
# Set external RTC to the internal RTC
url = conf.get("utc_url") or "https://worldtimeapi.org/api/timezone/UTC"
dt_str = urequests.request("GET", url).json()["datetime"]
dt = datetime.fromisoformat(dt_str)
dt_tuple = (dt.year, dt.month, dt.day, dt.weekday(), dt.hour, dt.minute, dt.second)
ds1307rtc.datetime(dt_tuple)
# Turn off WiFi
wlan.active(False)
def configure():
global conf, ds1307rtc, json_url, onboard_led
onboard_led.on()
try:
with open("/config.json", "r") as f:
conf = json.load(f)
# print(conf)
except OSError:
sync()
internal_rtc.datetime(ds1307rtc.datetime())
print(internal_rtc.datetime())
onboard_led.off()
if SYNC_ON_BOOT:
sync()
print("Successful Sync!")
while True:
lcd.clear()
# https://github.com/mcauser/micropython-tinyrtc-i2c/blob/8c9904a7d36e87467637f641e5f6438464076462/ds1307.py#L57
(year, month, day, _, hour, minute, second, _)=internal_rtc.datetime()
# https://stackoverflow.com/a/14043939
dt = datetime(year=year, month=month,day=day, hour=hour, minute=minute, second=second) + timedelta(hours=conf["time_offset"])
# print(dt)
year, month, day, hour, minute, second = dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second
for alarm in conf["alarms"]:
# Parse the target time string
target_time = strptime(alarm, "%H:%M")
# Extract target hour and minute
target_hour = target_time.tm_hour
target_minute = target_time.tm_min
alarm_time = strptime(alarm, "%H:%M")
# Without the second check I think this may trigger multiple times unless alarm_duration is 59 (we sleep before updating the screen too)
if hour == target_hour and minute == target_minute and second==0:
speaker.on()
time.sleep(conf.get("alarm_duration") or 5)
speaker.off()
# https://github.com/panchalnikunj/Raspberry-Pi-Pico-Projects/blob/main/Raspberry%20Pi%20Pico%20DS1307%20RTC/main.py
lcd.move_to(0,0)
lcd.putstr(str(hour) + ":" + str(minute) + ":" + str(second))
lcd.move_to(0,1)
lcd.putstr(str(month) + "/" + str(day) + "/" + str(year))
time.sleep(1)