# 导入必要的库
import machine
from machine import Pin, RTC
import time
# 初始化 RTC
rtc = RTC()
# 设置当前时间(需要根据实际情况设置)
rtc.datetime((2024, 4, 7, 3, 10, 0, 0, 0))
# 初始化控制灯光的引脚
LED_PIN = 2 # 这里假设灯光连接到GPIO 2
led = Pin(LED_PIN, Pin.OUT)
# 设定灯光关闭时间(小时、分钟)
light_off_hour = 21
light_off_minute = 30
# 循环检测时间并控制灯光
while True:
# 获取当前时间
year, month, day, hour, minute, second, _, _ = rtc.datetime()
# 判断是否到达设定的关闭时间
if hour == light_off_hour and minute == light_off_minute:
# 关闭灯光
led.value(0)
print("灯光已关闭")
break # 可以选择退出循环,节省资源
# 每隔一段时间检测一次
time.sleep(60) # 每分钟检测一次