2015-01-21 08:23:06 +01:00
|
|
|
# DHT22 module
|
|
|
|
|
2015-01-25 20:20:48 +01:00
|
|
|
This module is compatible with DHT22 and DHT21.
|
2015-02-02 10:01:24 +01:00
|
|
|
Supports nodemcu with or without floating point.
|
2015-01-25 20:20:48 +01:00
|
|
|
No need to use a resistor to connect the pin data of DHT22 to ESP8266.
|
|
|
|
|
2015-01-21 08:23:06 +01:00
|
|
|
## Example
|
|
|
|
```lua
|
|
|
|
PIN = 4 -- data pin, GPIO2
|
|
|
|
|
|
|
|
dht22 = require("dht22")
|
|
|
|
dht22.read(PIN)
|
|
|
|
t = dht22.getTemperature()
|
|
|
|
h = dht22.getHumidity()
|
|
|
|
|
2015-02-02 10:01:24 +01:00
|
|
|
if h == nil then
|
2015-01-25 20:20:48 +01:00
|
|
|
print("Error reading from DHT22")
|
|
|
|
else
|
|
|
|
-- temperature in degrees Celsius and Farenheit
|
2015-02-02 10:01:24 +01:00
|
|
|
-- floating point and integer version:
|
|
|
|
print("Temperature: "..((t-(t % 10)) / 10).."."..(t % 10).." deg C")
|
|
|
|
-- only integer version:
|
2015-01-25 20:20:48 +01:00
|
|
|
print("Temperature: "..(9 * t / 50 + 32).."."..(9 * t / 5 % 10).." deg F")
|
2015-02-02 10:01:24 +01:00
|
|
|
-- only float point version:
|
|
|
|
print("Temperature: "..(9 * t / 50 + 32).." deg F")
|
|
|
|
|
2015-01-25 20:20:48 +01:00
|
|
|
-- humidity
|
2015-02-02 10:01:24 +01:00
|
|
|
-- floating point and integer version
|
|
|
|
print("Humidity: "..((h - (h % 10)) / 10).."."..(h % 10).."%")
|
2015-01-25 20:20:48 +01:00
|
|
|
end
|
2015-01-21 08:23:06 +01:00
|
|
|
|
|
|
|
-- release module
|
|
|
|
dht22 = nil
|
|
|
|
package.loaded["dht22"]=nil
|
|
|
|
```
|
2015-01-25 20:20:48 +01:00
|
|
|
## Functions
|
|
|
|
### read
|
2015-02-02 10:01:24 +01:00
|
|
|
read(pin)
|
2015-01-25 20:20:48 +01:00
|
|
|
Read humidity and temperature from DHT22.
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
* pin - ESP8266 pin connect to data pin in DHT22
|
|
|
|
|
|
|
|
### getHumidity
|
|
|
|
getHumidity()
|
|
|
|
Returns the humidity of the last reading.
|
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
* last humidity reading in per thousand
|
|
|
|
|
|
|
|
### getTemperature
|
|
|
|
getTemperature()
|
|
|
|
Returns the temperature of the last reading.
|
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
* last temperature reading in 0.1ºC
|
|
|
|
|