Merge branch 'master' of https://github.com/nodemcu/nodemcu-firmware into devfloat
This commit is contained in:
commit
04ed036cc1
|
@ -1,5 +1,8 @@
|
||||||
# DHT22 module
|
# DHT22 module
|
||||||
|
|
||||||
|
This module is compatible with DHT22 and DHT21.
|
||||||
|
No need to use a resistor to connect the pin data of DHT22 to ESP8266.
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
```lua
|
```lua
|
||||||
PIN = 4 -- data pin, GPIO2
|
PIN = 4 -- data pin, GPIO2
|
||||||
|
@ -9,14 +12,41 @@ dht22.read(PIN)
|
||||||
t = dht22.getTemperature()
|
t = dht22.getTemperature()
|
||||||
h = dht22.getHumidity()
|
h = dht22.getHumidity()
|
||||||
|
|
||||||
-- temperature in degrees Celsius and Farenheit
|
if h == -1 then
|
||||||
print("Temperature: "..(t/10).."."..(t%10).." deg C")
|
print("Error reading from DHT22")
|
||||||
print("Temperature: "..(9 * t / 50 + 32).."."..(9 * t / 5 % 10).." deg F")
|
else
|
||||||
|
-- temperature in degrees Celsius and Farenheit
|
||||||
|
print("Temperature: "..(t / 10).."."..(t % 10).." deg C")
|
||||||
|
print("Temperature: "..(9 * t / 50 + 32).."."..(9 * t / 5 % 10).." deg F")
|
||||||
|
|
||||||
-- humidity
|
-- humidity
|
||||||
print("Humidity: "..(h/10).."."..(h%10).."%")
|
print("Humidity: "..(h/10).."."..(h%10).."%")
|
||||||
|
end
|
||||||
|
|
||||||
-- release module
|
-- release module
|
||||||
dht22 = nil
|
dht22 = nil
|
||||||
package.loaded["dht22"]=nil
|
package.loaded["dht22"]=nil
|
||||||
```
|
```
|
||||||
|
## Functions
|
||||||
|
### read
|
||||||
|
read(pin)
|
||||||
|
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
|
||||||
|
|
||||||
|
|
|
@ -13,23 +13,22 @@ _G[moduleName] = M
|
||||||
|
|
||||||
local humidity
|
local humidity
|
||||||
local temperature
|
local temperature
|
||||||
local checksum
|
|
||||||
local checksumTest
|
|
||||||
|
|
||||||
function M.read(pin)
|
function M.read(pin)
|
||||||
|
local checksum
|
||||||
|
local checksumTest
|
||||||
humidity = 0
|
humidity = 0
|
||||||
temperature = 0
|
temperature = 0
|
||||||
checksum = 0
|
checksum = 0
|
||||||
|
|
||||||
-- Use Markus Gritsch trick to speed up read/write on GPIO
|
-- Use Markus Gritsch trick to speed up read/write on GPIO
|
||||||
gpio_read = gpio.read
|
local gpio_read = gpio.read
|
||||||
gpio_write = gpio.write
|
|
||||||
|
|
||||||
bitStream = {}
|
local bitStream = {}
|
||||||
for j = 1, 40, 1 do
|
for j = 1, 40, 1 do
|
||||||
bitStream[j] = 0
|
bitStream[j] = 0
|
||||||
end
|
end
|
||||||
bitlength = 0
|
local bitlength = 0
|
||||||
|
|
||||||
-- Step 1: send out start signal to DHT22
|
-- Step 1: send out start signal to DHT22
|
||||||
gpio.mode(pin, gpio.OUTPUT)
|
gpio.mode(pin, gpio.OUTPUT)
|
||||||
|
@ -43,13 +42,13 @@ function M.read(pin)
|
||||||
-- Step 2: DHT22 send response signal
|
-- Step 2: DHT22 send response signal
|
||||||
-- bus will always let up eventually, don't bother with timeout
|
-- bus will always let up eventually, don't bother with timeout
|
||||||
while (gpio_read(pin) == 0 ) do end
|
while (gpio_read(pin) == 0 ) do end
|
||||||
c=0
|
local c=0
|
||||||
while (gpio_read(pin) == 1 and c < 100) do c = c + 1 end
|
while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end
|
||||||
-- bus will always let up eventually, don't bother with timeout
|
-- bus will always let up eventually, don't bother with timeout
|
||||||
while (gpio_read(pin) == 0 ) do end
|
while (gpio_read(pin) == 0 ) do end
|
||||||
c=0
|
c=0
|
||||||
while (gpio_read(pin) == 1 and c < 100) do c = c + 1 end
|
while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end
|
||||||
|
|
||||||
-- Step 3: DHT22 send data
|
-- Step 3: DHT22 send data
|
||||||
for j = 1, 40, 1 do
|
for j = 1, 40, 1 do
|
||||||
while (gpio_read(pin) == 1 and bitlength < 10 ) do
|
while (gpio_read(pin) == 1 and bitlength < 10 ) do
|
||||||
|
@ -63,17 +62,17 @@ function M.read(pin)
|
||||||
|
|
||||||
--DHT data acquired, process.
|
--DHT data acquired, process.
|
||||||
for i = 1, 16, 1 do
|
for i = 1, 16, 1 do
|
||||||
if (bitStream[i + 0] > 2) then
|
if (bitStream[i] > 4) then
|
||||||
humidity = humidity + 2 ^ (16 - i)
|
humidity = humidity + 2 ^ (16 - i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for i = 1, 16, 1 do
|
for i = 1, 16, 1 do
|
||||||
if (bitStream[i + 16] > 2) then
|
if (bitStream[i + 16] > 4) then
|
||||||
temperature = temperature + 2 ^ (16 - i)
|
temperature = temperature + 2 ^ (16 - i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for i = 1, 8, 1 do
|
for i = 1, 8, 1 do
|
||||||
if (bitStream[i + 32] > 2) then
|
if (bitStream[i + 32] > 4) then
|
||||||
checksum = checksum + 2 ^ (8 - i)
|
checksum = checksum + 2 ^ (8 - i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue