NodeMCU DHT

DHT Module

dht.read()

原型: dht.read(pin)
作用: 读取所有的DHT传感器,包括 DHT11, 21, 22, 33, 44温湿度传感器
参数: DHT传感器的针脚
返回值:

  1. status DHT的状态,有dht.OK, dht.ERROR_CHECKSUM, dht.ERROR_TIMEOUT三种值
  2. temp 传感器测得的温度
  3. humi 传感器测得的湿度
  4. temp_dec 温度小数部分
  5. humi_dec 湿度小数部分
    例子:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    pin = 1
    status, temp, humi, temp_dec, humi_dec = dht.read(pin)
    if status == dht.OK then
    -- Integer firmware using this example
    print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n",
    math.floor(temp),
    temp_dec,
    math.floor(humi),
    humi_dec
    ))
    -- Float firmware using this example
    print("DHT Temperature:"..temp..";".."Humidity:"..humi)
    elseif status == dht.ERROR_CHECKSUM then
    print( "DHT Checksum error." )
    elseif status == dht.ERROR_TIMEOUT then
    print( "DHT timed out." )
    end