from machine import Pin, ADC, I2C, PWM
from ssd1306 import SSD1306_I2C
import time # 用于生成模拟传感器数据
# 初始化硬件
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# 传感器模拟输入(实际使用时替换为真实传感器读取函数)
light_sensor = ADC(Pin(25))
light_sensor.atten(ADC.ATTN_11DB)
# 控制引脚定义
red_led = Pin(4, Pin.OUT, value=1) # 初始关闭
green_led = Pin(15, Pin.OUT, value=1) # 新增绿灯
buzzer = Pin(4, Pin.OUT, value=1) # 初始关闭
# 红外遥控器按键定义(需根据实际遥控器修改)
IR_KEY_UP = 1
IR_KEY_DOWN = 2
IR_KEY_LEFT = 3
IR_KEY_RIGHT = 4
IR_KEY_OK = 5
# 模拟传感器数据(实际使用时替换为真实传感器数据)
class SensorData:
def __init__(self):
self.temp = 25.0 # 温度 (°C)
self.humidity = 60.0 # 湿度 (%RH)
self.light = 2000 # 光照强度 (ADC值)
self.gas = 100 # 气体浓度 (ppm)
def update_simulation(self):
# 模拟传感器数据微小波动
self.temp += random.uniform(-0.5, 0.5)
self.humidity += random.uniform(-1.0, 1.0)
self.light += random.randint(-100, 100)
self.gas += random.randint(-5, 5)
# 保持在合理范围内
self.temp = max(10.0, min(40.0, self.temp))
self.humidity = max(20.0, min(95.0, self.humidity))
self.light = max(0, min(4095, self.light))
self.gas = max(0, min(500, self.gas))
# 阈值设置
class Thresholds:
def __init__(self):
self.temp = 30.0 # 温度阈值 (°C)
self.humidity = 80.0 # 湿度阈值 (%RH)
self.light = 3000 # 光照阈值 (ADC值)
self.gas = 300 # 气体浓度阈值 (ppm)
# 系统状态
class SystemState:
def __init__(self):
self.mode = 0 # 0: 监控模式, 1-4: 设置模式
self.error_flags = [False, False, False, False] # 分别对应温度、湿度、光照、气体超标
# 初始化对象
sensor = SensorData()
thresholds = Thresholds()
state = SystemState()
# 模拟红外遥控输入(实际使用时替换为真实红外接收代码)
def read_ir_input():
# 模拟按键输入,实际使用时替换为红外接收模块的读取逻辑
key = 0 # 默认无按键
# 示例:按开发板上的物理按键或通过串口输入模拟遥控器按键
# 这里仅作演示,实际应使用红外接收模块
try:
if uart.any():
key = int(uart.read(1))
except:
pass
return key
# 显示主界面
def display_main_screen():
oled.fill(0)
oled.text("Env Monitor", 35, 0)
# 显示当前传感器数据
oled.text(f"Temp: {sensor.temp:.1f}°C", 10, 15)
oled.text(f"Hum: {sensor.humidity:.1f}%", 10, 25)
oled.text(f"Light: {sensor.light}", 10, 35)
oled.text(f"Gas: {sensor.gas}ppm", 10, 45)
# 显示阈值(带颜色区分)
oled.text("TH:", 90, 15)
oled.text(f"{thresholds.temp:.1f}", 105, 15, 1 if sensor.temp <= thresholds.temp else 2)
oled.text("TH:", 90, 25)
oled.text(f"{thresholds.humidity:.1f}", 105, 25, 1 if sensor.humidity <= thresholds.humidity else 2)
oled.text("TH:", 90, 35)
oled.text(f"{thresholds.light}", 105, 35, 1 if sensor.light <= thresholds.light else 2)
oled.text("TH:", 90, 45)
oled.text(f"{thresholds.gas}", 105, 45, 1 if sensor.gas <= thresholds.gas else 2)
# 显示超标信息
if any(state.error_flags):
oled.fill_rect(0, 55, 128, 9, 1)
oled.text("ALARM:", 5, 55, 0)
errors = []
if state.error_flags[0]: errors.append("T")
if state.error_flags[1]: errors.append("H")
if state.error_flags[2]: errors.append("L")
if state.error_flags[3]: errors.append("G")
oled.text("".join(errors), 50, 55, 0)
oled.show()
# 显示设置界面
def display_settings_screen():
oled.fill(0)
if state.mode == 1:
oled.text("Set Temperature Threshold", 5, 5)
oled.text(f"{thresholds.temp:.1f}°C", 50, 30)
elif state.mode == 2:
oled.text("Set Humidity Threshold", 5, 5)
oled.text(f"{thresholds.humidity:.1f}%", 50, 30)
elif state.mode == 3:
oled.text("Set Light Threshold", 5, 5)
oled.text(f"{thresholds.light}", 50, 30)
elif state.mode == 4:
oled.text("Set Gas Threshold", 5, 5)
oled.text(f"{thresholds.gas}ppm", 50, 30)
oled.text("UP/DOWN: Adjust OK: Exit", 5, 55)
oled.show()
# 检查是否超标
def check_thresholds():
state.error_flags[0] = sensor.temp > thresholds.temp
state.error_flags[1] = sensor.humidity > thresholds.humidity
state.error_flags[2] = sensor.light > thresholds.light
state.error_flags[3] = sensor.gas > thresholds.gas
# 控制指示灯和蜂鸣器
if any(state.error_flags):
red_led.value(0) # 红灯亮
green_led.value(1) # 绿灯灭
buzzer.value(0) # 蜂鸣器响
else:
red_led.value(1) # 红灯灭
green_led.value(0) # 绿灯亮
buzzer.value(1) # 蜂鸣器停
# 处理红外遥控输入
def handle_ir_input(key):
if key == IR_KEY_OK:
if state.mode > 0: # 退出设置模式
state.mode = 0
else: # 进入设置模式(循环切换)
state.mode = (state.mode % 4) + 1
if state.mode > 0: # 在设置模式下
if key == IR_KEY_UP:
if state.mode == 1: thresholds.temp += 0.5
elif state.mode == 2: thresholds.humidity += 1.0
elif state.mode == 3: thresholds.light += 100
elif state.mode == 4: thresholds.gas += 10
if key == IR_KEY_DOWN:
if state.mode == 1: thresholds.temp = max(10.0, thresholds.temp - 0.5)
elif state.mode == 2: thresholds.humidity = max(20.0, thresholds.humidity - 1.0)
elif state.mode == 3: thresholds.light = max(500, thresholds.light - 100)
elif state.mode == 4: thresholds.gas = max(50, thresholds.gas - 10)
# 主循环
while True:
# 更新模拟传感器数据(实际使用时替换为真实传感器读取)
sensor.update_simulation()
# 检查阈值
check_thresholds()
# 读取红外遥控器输入
ir_key = read_ir_input()
handle_ir_input(ir_key)
# 显示界面
if state.mode == 0:
display_main_screen()
else:
display_settings_screen()
time.sleep(1) # 控制刷新频率