You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bugfix for DHT module to avoid timeouts on high system load #5848
I found an error in the DHT module, if the system load is high: I have many Python threads running and the DHT module constantly reports a timeout error. If system load is low, there is no error.
I have a fix for the problem: You should extend the atomic section to include the 18ms waiting. Simply move the mp_hal_quiet_timing_enter() before the mp_hal_delay_ms(18), see below. I see no problem to loose the 18ms because the DHT measurements should only be every 2-3 seconds.
The 18ms may be reduced for DHT22, because the data sheet needs at least 1ms delay. I use a 3ms delay with the DHT22 with no problems. But for DHT11 we need 18ms. So I would let the delay at 18ms.
// issue start command
mp_hal_pin_od_high_dht(pin);
mp_hal_delay_ms(250);
mp_hal_pin_od_low(pin);
// the atomic section should be before the delay of 18ms
// otherwise we have timeout errors on high system load
// Stefan Hammes, 2020-03-31
mp_uint_t irq_state = mp_hal_quiet_timing_enter();
mp_hal_delay_ms(18);
Or with diff:
$ diff dht.c-orig dht.c
55d54
< mp_hal_delay_ms(18);
56a56,58
> // the atomic section should be before the delay of 18ms
> // otherwise we have timeout errors on high system load
> // Stefan Hammes, 2020-03-31
57a60,61
>
> mp_hal_delay_ms(18);
This gives correct DHT readings even with extremly high system load.
It was my understanding that the 18ms delay was a minimum, so allowing it to possibly extend beyond 18ms was OK. But I guess that's not the case.
If this fix is made then the mp_hal_delay_ms(18) would need to be changed to mp_hal_delay_us_fast(18000), because mp_hal_delay_ms may call scheduled Python code.
Hi Damien,
I use Ubuntu 18.04 LTS and an ESP32 port (NodeMCU) Board. I compiled
Micropython successfully and encountered the DHT problem and fixed it ;-).
Yes, the use of |mp_hal_delay_us_fast(18000)| would also fix the problem!
Idea: More elaborate could be: We can use 18ms for the DHT11 and 3ms for
the DHT22 (the information which sensor is used could be passed to the
function).
Many thanks for the good work.
Greetings from Germany and stay healthy!
Stefan Hammes
|
|
Am 05.04.20 um 13:52 schrieb Damien George:
Thanks for the detailed description and fix.
What port/system are you using?
It was my understanding that the 18ms delay was a minimum, so allowing
it to possibly extend beyond 18ms was OK. But I guess that's not the case.
If this fix is made then the |mp_hal_delay_ms(18)| would need to be
changed to |mp_hal_delay_us_fast(18000)|, because |mp_hal_delay_ms|
may call scheduled Python code.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5848 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AO7ZYWBDCA7CNVA5TU5YHS3RLBWJJANCNFSM4LXMLOXQ>.
Idea: More elaborate could be: We can use 18ms for the DHT11 and 3ms for the DHT22 (the information which sensor is used could be passed to the function).
Yes that makes sense. 18ms is a long time (relatively) and reducing it to 3ms for DHT22 would be good.
Activity
dpgeorge commentedon Apr 5, 2020
Thanks for the detailed description and fix.
What port/system are you using?
It was my understanding that the 18ms delay was a minimum, so allowing it to possibly extend beyond 18ms was OK. But I guess that's not the case.
If this fix is made then the
mp_hal_delay_ms(18)would need to be changed tomp_hal_delay_us_fast(18000), becausemp_hal_delay_msmay call scheduled Python code.seiuvwcdhol commentedon Apr 5, 2020
dpgeorge commentedon Apr 6, 2020
Yes that makes sense. 18ms is a long time (relatively) and reducing it to 3ms for DHT22 would be good.
Fixed DHT timing error, Issue micropython#5848
Merge pull request micropython#5848 from prplz/esp32s3-devkits
Andrei-Pozolotin commentedon Apr 2, 2022
Another solution for a similar problem (random timeout errors in DHT):
re-implement driver in viper for total control of DHT protocol parameters.