DHT11/DHT22 library fixes.
Fixed temperature below zero. Fixed checksum overflow verification. Fixed inconsistent reading of DHT11.
This commit is contained in:
parent
a41407e3d1
commit
6ff78f802d
2 changed files with 8 additions and 7 deletions
|
@ -15,7 +15,7 @@
|
||||||
* to read and print a new temperature and humidity measurement
|
* to read and print a new temperature and humidity measurement
|
||||||
* from a sensor attached to GPIO pin 4.
|
* from a sensor attached to GPIO pin 4.
|
||||||
*/
|
*/
|
||||||
uint8_t const dht_gpio = 12;
|
uint8_t const dht_gpio = 4;
|
||||||
|
|
||||||
void dhtMeasurementTask(void *pvParameters)
|
void dhtMeasurementTask(void *pvParameters)
|
||||||
{
|
{
|
||||||
|
@ -29,9 +29,9 @@ void dhtMeasurementTask(void *pvParameters)
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
if (dht_read_data(dht_gpio, &humidity, &temperature)) {
|
if (dht_read_data(dht_gpio, &humidity, &temperature)) {
|
||||||
printf("Humidity: %d.%d%% Temp: %d.%dC\n",
|
printf("Humidity: %d%% Temp: %dC\n",
|
||||||
humidity / 10, humidity % 10,
|
humidity / 10,
|
||||||
temperature / 10, abs(temperature) % 10);
|
temperature / 10);
|
||||||
} else {
|
} else {
|
||||||
printf("Could not read data from sensor\n");
|
printf("Could not read data from sensor\n");
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,13 +64,14 @@ static bool dht_await_pin_state(uint8_t pin, uint32_t timeout,
|
||||||
bool expected_pin_state, uint32_t *duration)
|
bool expected_pin_state, uint32_t *duration)
|
||||||
{
|
{
|
||||||
for (uint32_t i = 0; i < timeout; i += DHT_TIMER_INTERVAL) {
|
for (uint32_t i = 0; i < timeout; i += DHT_TIMER_INTERVAL) {
|
||||||
|
// need to wait at least a single interval to prevent reading a jitter
|
||||||
|
sdk_os_delay_us(DHT_TIMER_INTERVAL);
|
||||||
if (gpio_read(pin) == expected_pin_state) {
|
if (gpio_read(pin) == expected_pin_state) {
|
||||||
if (duration) {
|
if (duration) {
|
||||||
*duration = i;
|
*duration = i;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
sdk_os_delay_us(DHT_TIMER_INTERVAL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -135,7 +136,7 @@ static inline int16_t dht_convert_data(uint8_t msb, uint8_t lsb)
|
||||||
data = msb & 0x7F;
|
data = msb & 0x7F;
|
||||||
data <<= 8;
|
data <<= 8;
|
||||||
data |= lsb;
|
data |= lsb;
|
||||||
if (msb & BIT(15)) {
|
if (msb & BIT(7)) {
|
||||||
data = 0 - data; // convert it to negative
|
data = 0 - data; // convert it to negative
|
||||||
}
|
}
|
||||||
#elif DHT_TYPE == DHT11
|
#elif DHT_TYPE == DHT11
|
||||||
|
@ -169,7 +170,7 @@ bool dht_read_data(uint8_t pin, int16_t *humidity, int16_t *temperature)
|
||||||
data[i/8] |= bits[i];
|
data[i/8] |= bits[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data[4] != (data[0] + data[1] + data[2] + data[3])) {
|
if (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
|
||||||
debug("Checksum failed, invalid data received from sensor\n");
|
debug("Checksum failed, invalid data received from sensor\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue