from machine import Pin, I2C
from time import sleep
import ds3231
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# I2C Setup
i2c_rtc = I2C(1, scl=Pin(3), sda=Pin(2))
i2c_lcd = I2C(0, scl=Pin(1), sda=Pin(0))
# RTC Setup
rtc = DS3231(i2c_rtc)
# LCD Setup
lcd_addr = i2c_lcd.scan()[0]
lcd = I2cLcd(i2c_lcd, lcd_addr, 2, 16)
# Relais / LED an GPIO 15
output = Pin(4, Pin.OUT)
# Zeitschaltuhr-Zeitpunkte
on_hour = 8
on_minute = 0
off_hour = 20
off_minute = 0
while True:
year, month, day, weekday, hour, minute, second = rtc.datetime()
# LCD Anzeige
lcd.clear()
lcd.putstr("Zeit: {:02d}:{:02d}:{:02d}".format(hour, minute, second))
lcd.move_to(0, 1)
lcd.putstr("{:02d}.{:02d}.{:04d}".format(day, month, year))
# Zeitschaltlogik
current_time = hour * 60 + minute
on_time = on_hour * 60 + on_minute
off_time = off_hour * 60 + off_minute
if on_time <= current_time < off_time:
output.value(1)
else:
output.value(0)
sleep(1)