dht11

注意:如果报错: TypeError: expecting a pin,说明引脚号写错了,注意使用 dht11.read(pin1) 而不是 dht11.read(1)

from microbit import *
import dht11

while True:
  temp,hum=dht11.read(pin1)
  print("temp=%d C"%temp)
  print("hum=",hum)
  sleep(1000)

第五行:读取dht11的温度和湿度,dht11.read()的参数为连接的引脚。注意:如果使用了P0有蜂鸣器的扩展板,需要关闭蜂鸣器开关

第六行和第七行:将读取的值显示出来。

dht11Filesystem

# https://bxy.dfrobot.com.cn/bit_dht11
from microbit import *
import microbit
import dht11

limitTemp=30    
highTemp=0     
num=0

fileHandle=None
try:
    while True:
        temp,hum=dht11.read(pin1)
        if temp >= limitTemp:
            if highTemp==0:
                fileHandle=open("alarmTemp.py","w")
                fileHandle.write("time to start=%dms        "%microbit.running_time())

        if temp>=highTemp:
            highTemp=temp

        if highTemp !=0 and temp < limitTemp:
            sleep(50)
            fileHandle.write("Highest temp=%d C        "%highTemp)
            sleep(50)
            fileHandle.write("time to end=%dms"%microbit.running_time())
            fileHandle.close()
            highTemp=0
            break

        if num==60:
            if highTemp != 0:
                sleep(50)
                fileHandle.write("Highest temp=%d C        "%highTemp)
                sleep(50)
                fileHandle.write("time to end=%dms"%microbit.running_time())
                fileHandle.close()
                highTemp=0
            break
        num+=1
        sleep(1000)
except:
    if fileHandle != None:
        fileHandle.close()

此示例程序运行后在microbit文件系统中写入信息,运行一段时间后打开文件系统即可看到输出的文件。