Skip to content

Fixed DHT timing error, Issue #5848 #6044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions drivers/dht/dht.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
#define mp_hal_pin_od_high_dht mp_hal_pin_od_high
#endif

STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
// dht_version should be 22 for DHT22 and 11 for DHT11
STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in, mp_obj_t dht_version) {
mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(pin_in);
mp_hal_pin_open_drain(pin);

Expand All @@ -52,7 +53,15 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
mp_hal_pin_od_high_dht(pin);
mp_hal_delay_ms(250);
mp_hal_pin_od_low(pin);
mp_hal_delay_ms(18);

// we have to use non-interruptable delay here, because otherwise
// on high system load, we get DHT timeout errors: see Issue #5848
// DHT22 needs only 1-3 milliseconds low, DHT11 needs 18 milliseconds low
if(mp_obj_get_int(dht_version) == 22){
mp_hal_delay_us_fast(3000);
}else{
mp_hal_delay_us_fast(18000);
}

mp_uint_t irq_state = mp_hal_quiet_timing_enter();

Expand Down Expand Up @@ -91,4 +100,4 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
mp_hal_quiet_timing_exit(irq_state);
mp_raise_OSError(MP_ETIMEDOUT);
}
MP_DEFINE_CONST_FUN_OBJ_2(dht_readinto_obj, dht_readinto);
MP_DEFINE_CONST_FUN_OBJ_3(dht_readinto_obj, dht_readinto);
2 changes: 1 addition & 1 deletion drivers/dht/dht.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include "py/obj.h"

MP_DECLARE_CONST_FUN_OBJ_2(dht_readinto_obj);
MP_DECLARE_CONST_FUN_OBJ_3(dht_readinto_obj);

#endif // MICROPY_INCLUDED_DRIVERS_DHT_DHT_H
10 changes: 9 additions & 1 deletion drivers/dht/dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ def __init__(self, pin):

def measure(self):
buf = self.buf
dht_readinto(self.pin, buf)
dht_readinto(self.pin, buf, self.dht_version)
if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xFF != buf[4]:
raise Exception("checksum error")


class DHT11(DHTBase):
def __init__(self, pin):
DHTBase.__init__(self, pin)
self.dht_version = 11

def humidity(self):
return self.buf[0]

Expand All @@ -28,6 +32,10 @@ def temperature(self):


class DHT22(DHTBase):
def __init__(self, pin):
DHTBase.__init__(self, pin)
self.dht_version = 22

def humidity(self):
return (self.buf[0] << 8 | self.buf[1]) * 0.1

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy