from dt22 import DHT22
from machine import Pin, I2C,PWM
import oled
import time
pwm0 = PWM(Pin(4))
pwm0.duty_u16(32768)
pwm0.deinit() #初始化pwm用于对蜂鸣器的控制
def interruput(line):
if p18.value()==1:
led_red.off();
led_yellow.off();
pwm0.deinit();
time.sleep(10) #按键的中断函数
p18=machine.Pin(18,machine.Pin.IN,machine.Pin.PULL_UP); #初始化按键
p18.irq(trigger=machine.Pin.IRQ_RISING,handler=interruput) #按键中断
DHT22 = DHT22(2)
resultList = [0, 0, 0, 0, 0] #初始化DHT22
i2c = I2C(1, scl=Pin(27), sda=Pin(26))
oled_width = 128
oled_height = 64
oled = oled.SSD1306_I2C(oled_width, oled_height, i2c) #初始化Oled
led_red=Pin(14,Pin.OUT)
led_yellow=Pin(15,Pin.OUT) #初始化led
while True:
oled.fill(0) #清屏
time.sleep(3)
DHT22.read_bytes(resultList) #DHt22数据的采集
HumData = ((resultList[0] << 8) + resultList[1]) / 10 #湿度
TemData = ((resultList[2] << 8) + resultList[3]) / 10 #温度
oled.text('RH={}% T={}C'.format(HumData,TemData), 0, 0) #数据处理
oled.show() #显示到oled
if TemData>20: #对温度进行判断
pwm0.freq(1000)
led_red.on()
if HumData>30: #对湿度进行判断
pwm0.freq(1000)
led_yellow.on()