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
cda7992c
Commit
cda7992c
authored
Jun 20, 2015
by
vowstar
Browse files
Fix DHTLIB bug of DHT22, DHT21, DHT33, DHT44.
parent
c56659e8
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/dhtlib/Makefile
View file @
cda7992c
...
@@ -42,6 +42,7 @@ INCLUDES += -I ./
...
@@ -42,6 +42,7 @@ INCLUDES += -I ./
INCLUDES
+=
-I
./include
INCLUDES
+=
-I
./include
INCLUDES
+=
-I
../include
INCLUDES
+=
-I
../include
INCLUDES
+=
-I
../../include
INCLUDES
+=
-I
../../include
INCLUDES
+=
-I
../libc
INCLUDES
+=
-I
../platform
INCLUDES
+=
-I
../platform
PDIR
:=
../
$(PDIR)
PDIR
:=
../
$(PDIR)
sinclude
$(PDIR)Makefile
sinclude
$(PDIR)Makefile
...
...
app/dhtlib/dht.c
View file @
cda7992c
...
@@ -29,6 +29,7 @@
...
@@ -29,6 +29,7 @@
#include "user_interface.h"
#include "user_interface.h"
#include "platform.h"
#include "platform.h"
#include "c_stdio.h"
#include "dht.h"
#include "dht.h"
#ifndef LOW
#ifndef LOW
...
@@ -39,12 +40,12 @@
...
@@ -39,12 +40,12 @@
#define HIGH 1
#define HIGH 1
#endif
/* ifndef HIGH */
#endif
/* ifndef HIGH */
#define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) ((byte_high << 8) | (byte_low))
#define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) ((
(
byte_high
)
<< 8) | (byte_low))
static
double
dht_humidity
;
static
double
dht_humidity
;
static
double
dht_temperature
;
static
double
dht_temperature
;
static
uint8_t
dht_b
it
s
[
5
];
// buffer to receive data
static
uint8_t
dht_b
yte
s
[
5
];
// buffer to receive data
static
int
dht_readSensor
(
uint8_t
pin
,
uint8_t
wakeupDelay
);
static
int
dht_readSensor
(
uint8_t
pin
,
uint8_t
wakeupDelay
);
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
...
@@ -81,19 +82,28 @@ int dht_read_universal(uint8_t pin)
...
@@ -81,19 +82,28 @@ int dht_read_universal(uint8_t pin)
return
rv
;
// propagate error value
return
rv
;
// propagate error value
}
}
#if defined(DHT_DEBUG_BYTES)
int
i
;
for
(
i
=
0
;
i
<
5
;
i
++
)
{
DHT_DEBUG
(
"%02X
\n
"
,
dht_bytes
[
i
]);
}
#endif // defined(DHT_DEBUG_BYTES)
// Assume it is DHT11
// Assume it is DHT11
// If it is DHT11, both bit[1] and bit[3] is 0
// If it is DHT11, both bit[1] and bit[3] is 0
if
((
dht_b
it
s
[
1
]
==
0
)
&&
(
dht_b
it
s
[
3
]
==
0
))
if
((
dht_b
yte
s
[
1
]
==
0
)
&&
(
dht_b
yte
s
[
3
]
==
0
))
{
{
// It may DHT11
// It may DHT11
// CONVERT AND STORE
// CONVERT AND STORE
dht_humidity
=
dht_bits
[
0
];
// dht_bits[1] == 0;
DHT_DEBUG
(
"DHT11 method
\n
"
);
dht_temperature
=
dht_bits
[
2
];
// dht_bits[3] == 0;
dht_humidity
=
dht_bytes
[
0
];
// dht_bytes[1] == 0;
dht_temperature
=
dht_bytes
[
2
];
// dht_bytes[3] == 0;
// TEST CHECKSUM
// TEST CHECKSUM
// dht_b
it
s[1] && dht_b
it
s[3] both 0
// dht_b
yte
s[1] && dht_b
yte
s[3] both 0
uint8_t
sum
=
dht_b
it
s
[
0
]
+
dht_b
it
s
[
2
];
uint8_t
sum
=
dht_b
yte
s
[
0
]
+
dht_b
yte
s
[
2
];
if
(
dht_b
it
s
[
4
]
!=
sum
)
if
(
dht_b
yte
s
[
4
]
!=
sum
)
{
{
// It may not DHT11
// It may not DHT11
dht_humidity
=
DHTLIB_INVALID_VALUE
;
// invalid value, or is NaN prefered?
dht_humidity
=
DHTLIB_INVALID_VALUE
;
// invalid value, or is NaN prefered?
...
@@ -108,16 +118,17 @@ int dht_read_universal(uint8_t pin)
...
@@ -108,16 +118,17 @@ int dht_read_universal(uint8_t pin)
// Assume it is not DHT11
// Assume it is not DHT11
// CONVERT AND STORE
// CONVERT AND STORE
dht_humidity
=
(
double
)
COMBINE_HIGH_AND_LOW_BYTE
(
dht_bits
[
0
],
dht_bits
[
1
])
*
0
.
1
;
DHT_DEBUG
(
"DHTxx method
\n
"
);
dht_temperature
=
(
double
)
COMBINE_HIGH_AND_LOW_BYTE
(
dht_bits
[
2
]
&
0x7F
,
dht_bits
[
3
])
*
0
.
1
;
dht_humidity
=
(
double
)
COMBINE_HIGH_AND_LOW_BYTE
(
dht_bytes
[
0
],
dht_bytes
[
1
])
*
0
.
1
;
if
(
dht_bits
[
2
]
&
0x80
)
// negative dht_temperature
dht_temperature
=
(
double
)
COMBINE_HIGH_AND_LOW_BYTE
(
dht_bytes
[
2
]
&
0x7F
,
dht_bytes
[
3
])
*
0
.
1
;
if
(
dht_bytes
[
2
]
&
0x80
)
// negative dht_temperature
{
{
dht_temperature
=
-
dht_temperature
;
dht_temperature
=
-
dht_temperature
;
}
}
// TEST CHECKSUM
// TEST CHECKSUM
uint8_t
sum
=
dht_b
it
s
[
0
]
+
dht_b
it
s
[
1
]
+
dht_b
it
s
[
2
]
+
dht_b
it
s
[
3
];
uint8_t
sum
=
dht_b
yte
s
[
0
]
+
dht_b
yte
s
[
1
]
+
dht_b
yte
s
[
2
]
+
dht_b
yte
s
[
3
];
if
(
dht_b
it
s
[
4
]
!=
sum
)
if
(
dht_b
yte
s
[
4
]
!=
sum
)
{
{
return
DHTLIB_ERROR_CHECKSUM
;
return
DHTLIB_ERROR_CHECKSUM
;
}
}
...
@@ -140,13 +151,13 @@ int dht_read11(uint8_t pin)
...
@@ -140,13 +151,13 @@ int dht_read11(uint8_t pin)
}
}
// CONVERT AND STORE
// CONVERT AND STORE
dht_humidity
=
dht_b
it
s
[
0
];
// dht_b
it
s[1] == 0;
dht_humidity
=
dht_b
yte
s
[
0
];
// dht_b
yte
s[1] == 0;
dht_temperature
=
dht_b
it
s
[
2
];
// dht_b
it
s[3] == 0;
dht_temperature
=
dht_b
yte
s
[
2
];
// dht_b
yte
s[3] == 0;
// TEST CHECKSUM
// TEST CHECKSUM
// dht_b
it
s[1] && dht_b
it
s[3] both 0
// dht_b
yte
s[1] && dht_b
yte
s[3] both 0
uint8_t
sum
=
dht_b
it
s
[
0
]
+
dht_b
it
s
[
2
];
uint8_t
sum
=
dht_b
yte
s
[
0
]
+
dht_b
yte
s
[
2
];
if
(
dht_b
it
s
[
4
]
!=
sum
)
return
DHTLIB_ERROR_CHECKSUM
;
if
(
dht_b
yte
s
[
4
]
!=
sum
)
return
DHTLIB_ERROR_CHECKSUM
;
return
DHTLIB_OK
;
return
DHTLIB_OK
;
}
}
...
@@ -168,16 +179,16 @@ int dht_read(uint8_t pin)
...
@@ -168,16 +179,16 @@ int dht_read(uint8_t pin)
}
}
// CONVERT AND STORE
// CONVERT AND STORE
dht_humidity
=
(
double
)
COMBINE_HIGH_AND_LOW_BYTE
(
dht_b
it
s
[
0
],
dht_b
it
s
[
1
])
*
0
.
1
;
dht_humidity
=
(
double
)
COMBINE_HIGH_AND_LOW_BYTE
(
dht_b
yte
s
[
0
],
dht_b
yte
s
[
1
])
*
0
.
1
;
dht_temperature
=
(
double
)
COMBINE_HIGH_AND_LOW_BYTE
(
dht_b
it
s
[
2
]
&
0x7F
,
dht_b
it
s
[
3
])
*
0
.
1
;
dht_temperature
=
(
double
)
COMBINE_HIGH_AND_LOW_BYTE
(
dht_b
yte
s
[
2
]
&
0x7F
,
dht_b
yte
s
[
3
])
*
0
.
1
;
if
(
dht_b
it
s
[
2
]
&
0x80
)
// negative dht_temperature
if
(
dht_b
yte
s
[
2
]
&
0x80
)
// negative dht_temperature
{
{
dht_temperature
=
-
dht_temperature
;
dht_temperature
=
-
dht_temperature
;
}
}
// TEST CHECKSUM
// TEST CHECKSUM
uint8_t
sum
=
dht_b
it
s
[
0
]
+
dht_b
it
s
[
1
]
+
dht_b
it
s
[
2
]
+
dht_b
it
s
[
3
];
uint8_t
sum
=
dht_b
yte
s
[
0
]
+
dht_b
yte
s
[
1
]
+
dht_b
yte
s
[
2
]
+
dht_b
yte
s
[
3
];
if
(
dht_b
it
s
[
4
]
!=
sum
)
if
(
dht_b
yte
s
[
4
]
!=
sum
)
{
{
return
DHTLIB_ERROR_CHECKSUM
;
return
DHTLIB_ERROR_CHECKSUM
;
}
}
...
@@ -231,7 +242,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
...
@@ -231,7 +242,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
// volatile uint8_t *PIR = portInputRegister(port);
// volatile uint8_t *PIR = portInputRegister(port);
// EMPTY BUFFER
// EMPTY BUFFER
for
(
i
=
0
;
i
<
5
;
i
++
)
dht_b
it
s
[
i
]
=
0
;
for
(
i
=
0
;
i
<
5
;
i
++
)
dht_b
yte
s
[
i
]
=
0
;
// REQUEST SAMPLE
// REQUEST SAMPLE
// pinMode(pin, OUTPUT);
// pinMode(pin, OUTPUT);
...
@@ -283,7 +294,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
...
@@ -283,7 +294,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
if
((
system_get_time
()
-
t
)
>
40
)
if
((
system_get_time
()
-
t
)
>
40
)
{
{
dht_b
it
s
[
idx
]
|=
mask
;
dht_b
yte
s
[
idx
]
|=
mask
;
}
}
mask
>>=
1
;
mask
>>=
1
;
if
(
mask
==
0
)
// next byte?
if
(
mask
==
0
)
// next byte?
...
...
app/dhtlib/dht.h
View file @
cda7992c
...
@@ -30,6 +30,8 @@
...
@@ -30,6 +30,8 @@
#define DHTLIB_DHT_WAKEUP 1
#define DHTLIB_DHT_WAKEUP 1
#define DHTLIB_DHT_UNI_WAKEUP 18
#define DHTLIB_DHT_UNI_WAKEUP 18
#define DHT_DEBUG
// max timeout is 100 usec.
// max timeout is 100 usec.
// For a 16 Mhz proc 100 usec is 1600 clock cycles
// For a 16 Mhz proc 100 usec is 1600 clock cycles
// loops using DHTLIB_TIMEOUT use at least 4 clock cycli
// loops using DHTLIB_TIMEOUT use at least 4 clock cycli
...
...
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