from machine import Pin, I2C, ADC, RTC
from ssd1306 import SSD1306_I2C
import dht
import time
# 硬件初始化(Wokwi 仿真环境)
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
dht_sensor = dht.DHT22(Pin(15))
potentiometer = ADC(Pin(34))
ir_pin = Pin(14, Pin.IN)
output_led = Pin(2, Pin.OUT)
# 全局变量(模拟 RTC 时间)
NAME = "zhangsan" # 姓名
STUDENT_ID = "2023XXXX" # 学号
TEMP_THRESHOLD = 30 # 温度阈值
ir_key = 0 # 红外按键状态
# 手动设置起始时间(替换为当前真实时间)
START_YEAR = 2025
START_MONTH = 6
START_DAY = 10
START_HOUR = 14
START_MINUTE = 30
START_SECOND = 0
START_WEEKDAY = 2 # 0=周一, 6=周日(2025-6-10 是周二,所以填 1?需确认 Wokwi 映射)
# 模拟 RTC 时间(每秒递增)
current_time = [START_YEAR, START_MONTH, START_DAY, START_HOUR, START_MINUTE, START_SECOND, START_WEEKDAY]
# 红外中断回调
def ir_callback(pin):
global ir_key
ir_key = 1 if pin.value() == 1 else 0
ir_pin.irq(trigger=Pin.IRQ_RISING, handler=ir_callback)
# 递增时间(模拟 RTC 走时)
def update_time():
global current_time
# 秒 +1
current_time[5] += 1
# 处理进位
if current_time[5] >= 60:
current_time[5] = 0
current_time[4] += 1
if current_time[4] >= 60:
current_time[4] = 0
current_time[3] += 1
if current_time[3] >= 24:
current_time[3] = 0
current_time[2] += 1
# 月份和年份进位(简化处理,实际需更完善)
if current_time[2] > 31:
current_time[2] = 1
current_time[1] += 1
if current_time[1] > 12:
current_time[1] = 1
current_time[0] += 1
# 开机画面(模拟实时时间)
def show_splash():
for _ in range(3):
oled.fill(0)
oled.text(NAME, 0, 0)
oled.text(STUDENT_ID, 0, 10)
# 格式化时间
year, month, day, hour, minute, second, weekday = current_time
weekdays = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] # 0=周一
date_str = f"{year}-{month:02d}-{day:02d} {weekdays[weekday]}"
time_str = f"{hour:02d}:{minute:02d}:{second:02d}"
oled.text(date_str, 0, 20)
oled.text(time_str, 0, 30)
oled.show()
update_time() # 递增时间
time.sleep(1) # 模拟 1 秒间隔
# 显示传感器数据
def show_sensor_data():
oled.fill(0)
# 温湿度
try:
dht_sensor.measure()
oled.text(f"T: {dht_sensor.temperature()}°C", 0, 0)
oled.text(f"H: {dht_sensor.humidity()}%", 0, 10)
except:
oled.text("DHT Err", 0, 0)
# 电位器
pot_value = potentiometer.read() // 41
oled.text(f"Pot: {pot_value}", 0, 20)
# 红外状态
oled.text(f"IR: {ir_key}", 0, 30)
# 实时时间(模拟)
year, month, day, hour, minute, second, weekday = current_time
time_str = f"{hour:02d}:{minute:02d}:{second:02d}"
oled.text(time_str, 0, 40)
oled.show()
update_time() # 递增时间
time.sleep(2) # 每 2 秒刷新
# 阈值控制
def check_threshold():
try:
dht_sensor.measure()
output_led.value(1 if dht_sensor.temperature() > TEMP_THRESHOLD else 0)
except:
output_led.value(0)
# 红外设置阈值
def set_threshold_with_ir():
global TEMP_THRESHOLD, ir_key
in_setting = False
while True:
if ir_key == 1 and not in_setting:
in_setting = True
oled.fill(0)
oled.text("Threshold Set", 0, 0)
oled.text(f"Temp: {TEMP_THRESHOLD}", 0, 10)
oled.show()
ir_key = 0
elif in_setting:
if ir_key == 1:
TEMP_THRESHOLD += 1
oled.fill(0)
oled.text("Threshold Set", 0, 0)
oled.text(f"Temp: {TEMP_THRESHOLD}", 0, 10)
oled.show()
ir_key = 0
if TEMP_THRESHOLD % 2 == 0:
oled.fill(0)
oled.text("Threshold Set", 0, 0)
oled.text(f"Temp: {TEMP_THRESHOLD}", 0, 10)
oled.text("Successful!", 0, 20)
oled.show()
time.sleep(2)
in_setting = False
break
time.sleep(0.1)
# 主程序
def main():
show_splash()
set_threshold_with_ir()
while True:
show_sensor_data()
check_threshold()
if __name__ == "__main__":
main()