Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
0f203f32
Commit
0f203f32
authored
Apr 20, 2015
by
Martin Han
Browse files
Resuce RAM Usage
parent
399e85f9
Changes
1
Hide whitespace changes
Inline
Side-by-side
lua_modules/dht_lib/dht_lib.lua
View file @
0f203f32
...
@@ -15,8 +15,6 @@
...
@@ -15,8 +15,6 @@
--AM2320 Not Test yet
--AM2320 Not Test yet
--Output format-> Real temperature times 10(or DHT22 will miss it float part in Int Version)
--Output format-> Real temperature times 10(or DHT22 will miss it float part in Int Version)
--For example, the data read form DHT2x is 24.3 degree C, and the output will be 243
---------------the data read form DHT1x is 27 degree C, and the output will be 270
--==========================Module Part======================
--==========================Module Part======================
local
moduleName
=
...
local
moduleName
=
...
local
M
=
{}
local
M
=
{}
...
@@ -42,8 +40,6 @@ local function read(pin)
...
@@ -42,8 +40,6 @@ local function read(pin)
bitStream
[
j
]
=
0
bitStream
[
j
]
=
0
end
end
-- 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
)
gpio
.
write
(
pin
,
gpio
.
HIGH
)
gpio
.
write
(
pin
,
gpio
.
HIGH
)
...
@@ -62,7 +58,7 @@ local function read(pin)
...
@@ -62,7 +58,7 @@ local function read(pin)
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
<
500
)
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
...
@@ -74,70 +70,78 @@ local function read(pin)
...
@@ -74,70 +70,78 @@ local function read(pin)
while
(
gpio_read
(
pin
)
==
0
)
do
end
while
(
gpio_read
(
pin
)
==
0
)
do
end
end
end
end
end
---------------------------Convert the bitStream into Number through DHT11 Ways--------------------------
local
function
bit2DHT11
()
---------------------------Check out the data--------------------------
--As for DHT11 40Bit is consisit of 5Bytes
----Auto Select the DHT11/DHT22 By check the byte[1] && byte[3] -------
--First byte->Humidity Data's Int part
---------------Which is empty when using DHT11-------------------------
--Sencond byte->Humidity Data's Float Part(Which should be empty)
function
M
.
read
(
pin
)
--Third byte->Temp Data;s Intpart
read
(
pin
)
--Forth byte->Temp Data's Float Part(Which should be empty)
--Fifth byte->SUM Byte, Humi+Temp
local
byte_0
=
0
local
checksum
=
0
local
byte_1
=
0
local
checksumTest
local
byte_2
=
0
--DHT data acquired, process.
local
byte_3
=
0
local
byte_4
=
0
for
i
=
1
,
8
,
1
do
-- Byte[0]
for
i
=
1
,
8
,
1
do
-- Byte[0]
if
(
bitStream
[
i
]
>
3
)
then
if
(
bitStream
[
i
]
>
3
)
then
humidity
=
humidity
+
2
^
(
8
-
i
)
byte_0
=
byte_0
+
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
end
end
for
i
=
1
,
8
,
1
do
--Byte[4]
if
(
bitStream
[
i
+
32
]
>
3
)
then
for
i
=
1
,
8
,
1
do
-- Byte[1]
checksum
=
checksum
+
2
^
(
8
-
i
)
if
(
bitStream
[
i
+
8
]
>
3
)
then
byte_1
=
byte_1
+
2
^
(
8
-
i
)
end
end
end
end
if
(
checksum
~=
humidity
+
temperature
)
then
for
i
=
1
,
8
,
1
do
-- Byte[2]
humidity
=
nil
if
(
bitStream
[
i
+
16
]
>
3
)
then
temperature
=
nil
byte_2
=
byte_2
+
2
^
(
8
-
i
)
else
end
humidity
=
humidity
*
10
-- In order to universe the DHT22
temperature
=
temperature
*
10
end
end
for
i
=
1
,
8
,
1
do
-- Byte[3]
if
(
bitStream
[
i
+
24
]
>
3
)
then
end
byte_2
=
byte_2
+
2
^
(
8
-
i
)
---------------------------Convert the bitStream into Number through DHT22 Ways--------------------------
local
function
bit2DHT22
()
--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
local
checksum
=
0
local
checksumTest
--DHT data acquired, process.
for
i
=
1
,
16
,
1
do
if
(
bitStream
[
i
]
>
3
)
then
humidity
=
humidity
+
2
^
(
16
-
i
)
end
end
end
end
for
i
=
1
,
16
,
1
do
if
(
bitStream
[
i
+
16
]
>
3
)
then
for
i
=
1
,
8
,
1
do
-- Byte[4]
temperature
=
temperature
+
2
^
(
16
-
i
)
if
(
bitStream
[
i
+
32
]
>
3
)
then
byte_4
=
byte_4
+
2
^
(
8
-
i
)
end
end
end
end
for
i
=
1
,
8
,
1
do
if
(
bitStream
[
i
+
32
]
>
3
)
then
checksum
=
checksum
+
2
^
(
8
-
i
)
if
byte_1
==
0
and
byte_3
==
0
then
---------------------------Convert the bitStream into Number through DHT11's Way--------------------------
--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
if
(
byte_4
~=
byte_0
+
byte_2
)
then
humidity
=
nil
temperature
=
nil
else
humidity
=
byte_0
*
10
-- In order to universe with the DHT22
temperature
=
byte_2
*
10
end
end
end
else
---------------------------Convert the bitStream into Number through DHT22's Way--------------------------
--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
humidity
=
byte_0
*
256
+
byte_1
temperature
=
byte_2
*
256
+
byte_3
checksum
=
byte_4
checksumTest
=
(
bit
.
band
(
humidity
,
0xFF
)
+
bit
.
rshift
(
humidity
,
8
)
+
bit
.
band
(
temperature
,
0xFF
)
+
bit
.
rshift
(
temperature
,
8
))
checksumTest
=
(
bit
.
band
(
humidity
,
0xFF
)
+
bit
.
rshift
(
humidity
,
8
)
+
bit
.
band
(
temperature
,
0xFF
)
+
bit
.
rshift
(
temperature
,
8
))
checksumTest
=
bit
.
band
(
checksumTest
,
0xFF
)
checksumTest
=
bit
.
band
(
checksumTest
,
0xFF
)
...
@@ -151,35 +155,14 @@ local function bit2DHT22()
...
@@ -151,35 +155,14 @@ local function bit2DHT22()
if
(
checksumTest
-
checksum
>=
1
)
or
(
checksum
-
checksumTest
>=
1
)
then
if
(
checksumTest
-
checksum
>=
1
)
or
(
checksum
-
checksumTest
>=
1
)
then
humidity
=
nil
humidity
=
nil
end
end
end
---------------------------Check out the data--------------------------
----Auto Select the DHT11/DHT22 by checking the byte[1]==0 && byte[3]==0 ---
---------------Which is empty when using DHT11-------------------------
function
M
.
read
(
pin
)
read
(
pin
)
local
byte_1
=
0
local
byte_2
=
0
for
i
=
1
,
8
,
1
do
-- Byte[1]
if
(
bitStream
[
i
+
8
]
>
3
)
then
byte_1
=
byte_1
+
2
^
(
8
-
i
)
end
end
for
i
=
1
,
8
,
1
do
-- Byte[1]
if
(
bitStream
[
i
+
24
]
>
3
)
then
byte_2
=
byte_2
+
2
^
(
8
-
i
)
end
end
end
if
byte_
1
==
0
and
byte_2
==
0
then
byte_
0
=
nil
bit2DHT11
()
byte_1
=
nil
else
byte_2
=
nil
bit2DHT22
()
byte_3
=
nil
end
byte_4
=
nil
end
end
--------------API for geting the data out------------------
--------------API for geting the data out------------------
...
...
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