from machine import Pin,PWM,I2C
import time,dht,ssd1306
data= dht.DHT22(Pin(15))#DHT22温湿度检测模块初始化管教为15
i2c = I2C(0,scl = Pin(22),sda = Pin(21))#调用函数,设置I2C通信的管脚
oled_width= 128
oled_height = 64 #OLED屏大小
oled=ssd1306.SSD1306_I2C(oled_width,oled_height,i2c)#调用类
tempmax= -40
tempmin=80
humimax=0
humimin=1
oled.fill(0)#清屏
oled.text("Please adjust the temperature and humidity for the first time",10,10)
oled.text("the temperature",10,20)#将world显示再横坐标为10纵坐标为10的位置上
oled.text("and humidity for",10,30)#将world显示再横坐标为10纵坐标为10的位置上
oled.text("the first time",10,40)#将world显示再横坐标为10纵坐标为10的位置上
oled.show()#开始显示
time.sleep(1)
# oled.fill(0)
def Fun_DHT22(): # 测量DHT22的数值
data.measure()#测量获取温湿度
global temp,tempmax,tempmin #定义的是全局变量
temp = data.temperature()
global humi,humimax,humimin
humi = data.humidity()
# print(temp)
# print(humi)
oled.fill(0)#清屏
oled.text("temp:"+str(temp),10,10)
oled.text("tempmax:"+str(tempmax),10,20)
oled.text("tempmmin:"+str(tempmin),10,30)
oled.text("humi:"+str(humi),10,40)
oled.show()#开始显示
time.sleep(1)
oled.fill(0)
# Fun_DHT22()
while True:
Fun_DHT22()
if temp >=tempmax:
tempmax= temp
elif temp < tempmin:
tempmin=temp
elif humi>=humimax:
humimax= humi
else:
humimin=humi
time.sleep(0.5)