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
706ce8a1
Commit
706ce8a1
authored
Jan 21, 2015
by
Vowstar
Browse files
Merge pull request #119 from javieryanez/patch-1
Add DHT22 module by dvv
parents
b6a40301
75904ed9
Changes
1
Hide whitespace changes
Inline
Side-by-side
lua_modules/dht22/dht22.lua
0 → 100644
View file @
706ce8a1
-- ***************************************************************************
-- DHT22 module for ESP8266 with nodeMCU
--
-- Written by Javier Yanez
-- but based on a script of Pigs Fly from ESP8266.com forum
--
-- MIT license, http://opensource.org/licenses/MIT
-- ***************************************************************************
local
moduleName
=
...
local
M
=
{}
_G
[
moduleName
]
=
M
local
humidity
local
temperature
local
checksum
local
checksumTest
function
M
.
read
(
pin
)
humidity
=
0
temperature
=
0
checksum
=
0
-- Use Markus Gritsch trick to speed up read/write on GPIO
gpio_read
=
gpio
.
read
gpio_write
=
gpio
.
write
bitStream
=
{}
for
j
=
1
,
40
,
1
do
bitStream
[
j
]
=
0
end
bitlength
=
0
-- Step 1: send out start signal to DHT22
gpio
.
mode
(
pin
,
gpio
.
OUTPUT
)
gpio
.
write
(
pin
,
gpio
.
HIGH
)
tmr
.
delay
(
100
)
gpio
.
write
(
pin
,
gpio
.
LOW
)
tmr
.
delay
(
20000
)
gpio
.
write
(
pin
,
gpio
.
HIGH
)
gpio
.
mode
(
pin
,
gpio
.
INPUT
)
-- Step 2: DHT22 send response signal
-- bus will always let up eventually, don't bother with timeout
while
(
gpio_read
(
pin
)
==
0
)
do
end
c
=
0
while
(
gpio_read
(
pin
)
==
1
and
c
<
100
)
do
c
=
c
+
1
end
-- bus will always let up eventually, don't bother with timeout
while
(
gpio_read
(
pin
)
==
0
)
do
end
c
=
0
while
(
gpio_read
(
pin
)
==
1
and
c
<
100
)
do
c
=
c
+
1
end
-- Step 3: DHT22 send data
for
j
=
1
,
40
,
1
do
while
(
gpio_read
(
pin
)
==
1
and
bitlength
<
10
)
do
bitlength
=
bitlength
+
1
end
bitStream
[
j
]
=
bitlength
bitlength
=
0
-- bus will always let up eventually, don't bother with timeout
while
(
gpio_read
(
pin
)
==
0
)
do
end
end
--DHT data acquired, process.
for
i
=
1
,
16
,
1
do
if
(
bitStream
[
i
+
0
]
>
2
)
then
humidity
=
humidity
+
2
^
(
16
-
i
)
end
end
for
i
=
1
,
16
,
1
do
if
(
bitStream
[
i
+
16
]
>
2
)
then
temperature
=
temperature
+
2
^
(
16
-
i
)
end
end
for
i
=
1
,
8
,
1
do
if
(
bitStream
[
i
+
32
]
>
2
)
then
checksum
=
checksum
+
2
^
(
8
-
i
)
end
end
checksumTest
=
((
humidity
/
256
)
+
(
humidity
%
256
)
+
(
temperature
/
256
)
+
(
temperature
%
256
))
%
256
if
temperature
>
0x8000
then
-- convert to negative format
temperature
=
-
(
temperature
-
0x8000
)
end
if
checksum
~=
checksumTest
then
humidity
=
-
1
end
end
function
M
.
getTemperature
()
return
temperature
end
function
M
.
getHumidity
()
return
humidity
end
return
M
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