Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
b0e34e11
Commit
b0e34e11
authored
Apr 03, 2015
by
Vowstar
Browse files
Merge pull request #328 from MarsTechHAN/patch-1
Add support to DHT11 and add some comments.
parents
d8f2c2ba
4ca371f4
Changes
1
Hide whitespace changes
Inline
Side-by-side
lua_modules/dht22/dht
22
.lua
→
lua_modules/dht22/dht
_lib
.lua
View file @
b0e34e11
-- ***************************************************************************
-- ***************************************************************************
-- DHT22 module for ESP8266 with nodeMCU
-- DHT
xx(11,21,
22
)
module for ESP8266 with nodeMCU
--
--
-- Written by Javier Yanez
-- Written by Javier Yanez
mod by Martin
-- but based on a script of Pigs Fly from ESP8266.com forum
-- but based on a script of Pigs Fly from ESP8266.com forum
--
--
-- MIT license, http://opensource.org/licenses/MIT
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************
-- ***************************************************************************
--Support list:
--DHT11 Tested ->read11
--DHT21 Not Tested->read22
--DHT22 Tested->read22
--==========================Module Part======================
local
moduleName
=
...
local
moduleName
=
...
local
M
=
{}
local
M
=
{}
_G
[
moduleName
]
=
M
_G
[
moduleName
]
=
M
--==========================Local the UMI and TEMP===========
local
humidity
local
humidity
local
temperature
local
temperature
--==========================Local the bitStream==============
local
bitStream
=
{}
function
M
.
read
(
pin
)
---------------------------Read bitStream from DHTXX--------------------------
local
checksum
local
function
read
(
pin
)
local
checksumTest
local
bitlength
=
0
humidity
=
0
humidity
=
0
temperature
=
0
temperature
=
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
local
gpio_read
=
gpio
.
read
local
gpio_read
=
gpio
.
read
local
bitStream
=
{}
for
j
=
1
,
40
,
1
do
for
j
=
1
,
40
,
1
do
bitStream
[
j
]
=
0
bitStream
[
j
]
=
0
end
end
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
)
...
@@ -39,7 +48,7 @@ function M.read(pin)
...
@@ -39,7 +48,7 @@ function M.read(pin)
gpio
.
write
(
pin
,
gpio
.
HIGH
)
gpio
.
write
(
pin
,
gpio
.
HIGH
)
gpio
.
mode
(
pin
,
gpio
.
INPUT
)
gpio
.
mode
(
pin
,
gpio
.
INPUT
)
-- Step 2:
DHT22 send response signal
-- Step 2:
Receive bitStream from DHT11/22
-- 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
local
c
=
0
local
c
=
0
...
@@ -59,8 +68,53 @@ function M.read(pin)
...
@@ -59,8 +68,53 @@ function M.read(pin)
-- 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
end
end
end
---------------------------Convert the bitStream into Number through DHT11 Ways--------------------------
function
M
.
read11
(
pin
)
--As for DHT11 40Bit is consisit of 5Bytes
--First byte->Humidity Data's Int part
--Sencond byte->Humidity Data's Float Part(Which should be empty)
--Third byte->Temp Data;s Intpart
--Forth byte->Temp Data's Float Part(Which should be empty)
--Fifth byte->SUM Byte, Humi+Temp
read
(
pin
)
local
checksum
=
0
local
checksumTest
--DHT data acquired, process.
--DHT data acquired, process.
for
i
=
1
,
8
,
1
do
-- Byte[0]
if
(
bitStream
[
i
]
>
3
)
then
humidity
=
humidity
+
2
^
(
8
-
i
)
end
end
for
i
=
1
,
8
,
1
do
-- Byte[2]
if
(
bitStream
[
i
+
16
]
>
3
)
then
temperature
=
temperature
+
2
^
(
8
-
i
)
end
end
for
i
=
1
,
8
,
1
do
--Byte[4]
if
(
bitStream
[
i
+
32
]
>
3
)
then
checksum
=
checksum
+
2
^
(
8
-
i
)
end
end
if
(
checksum
~=
humidity
+
temperature
)
then
humidity
=
nil
temperature
=
nil
end
end
---------------------------Convert the bitStream into Number through DHT22 Ways--------------------------
function
M
.
read22
(
pin
)
--As for DHT22 40Bit is consisit of 5Bytes
--First byte->Humidity Data's High Bit
--Sencond byte->Humidity Data's Low Bit(And if over 0x8000, use complement)
--Third byte->Temp Data's High Bit
--Forth byte->Temp Data's Low Bit
--Fifth byte->SUM Byte
read
(
pin
)
local
checksum
=
0
local
checksumTest
--DHT data acquired, process.
for
i
=
1
,
16
,
1
do
for
i
=
1
,
16
,
1
do
if
(
bitStream
[
i
]
>
3
)
then
if
(
bitStream
[
i
]
>
3
)
then
humidity
=
humidity
+
2
^
(
16
-
i
)
humidity
=
humidity
+
2
^
(
16
-
i
)
...
@@ -91,6 +145,7 @@ function M.read(pin)
...
@@ -91,6 +145,7 @@ function M.read(pin)
end
end
end
end
---------------------------Check out the data--------------------------
function
M
.
getTemperature
()
function
M
.
getTemperature
()
return
temperature
return
temperature
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment