哈喽,大家好,还记得之前我们学习的使用Wokwi仿真MicroPython on ESP32开发板实现温度和湿度检测并使用屏幕显示的内容吗,这次我们加了一个按键,就是报警后可以手动取消报警。
一、目的
温湿度采集和报警,报警对应的LED灯也亮,而且报警可手动取消。
二、硬件环境
三、代码
main.py
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(3) #按键的中断函数
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()
diagram.json
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{
"type": "wokwi-pi-pico",
"id": "pico",
"top": 0,
"left": 0,
"attrs": { "env": "micropython-20220117-v1.18" }
},
{ "type": "wokwi-dht22", "id": "dht1", "top": -98.59, "left": -113.64, "attrs": {} },
{ "type": "wokwi-ssd1306", "id": "oled1", "top": -71.05, "left": 109.69, "attrs": {} },
{
"type": "wokwi-buzzer",
"id": "bz1",
"top": 56.43,
"left": -148.61,