adafruit / Adafruit_CircuitPython_DHT

CircuitPython support for DHT11 and DHT22 type temperature/humidity devices
MIT License
179 stars 62 forks source link

Improve error handling #31

Closed michaellass closed 4 years ago

michaellass commented 4 years ago

https://github.com/adafruit/Adafruit_Blinka/issues/210 demonstrated erroneous readings that can easily be avoided:

  1. If an error is detected in the data received from the sensor, temperature and humidity readings should not be updated. Otherwise they will actually be returned to the user if they query data within two seconds after the error occured.
  2. If we receive a humidity value below 0% or above 100% this is clearly bogus data.
ladyada commented 4 years ago

hmm! nice idea - would you mind pylinting? you probably just need to add a '# pylint: disable' line

michaellass commented 4 years ago

We can actually reduce the number of branches by checking for any missing bits first. Then the main decoding logic does not need to be wrapped by an if len(pulses) >= 80. Pylint seems to be happy now.

ladyada commented 4 years ago

ok and ya tested on a pi? which ones? we'll test too :)

michaellass commented 4 years ago

It's running here (in combination with https://github.com/adafruit/libgpiod_pulsein/pull/1) on a Raspberry Pi 4B and taking one measurement each minute. Looks good so far. Of course the most recent version now only runs for an hour or so but I will continue observing it over the next days.

michaellass commented 4 years ago

Just a quick follow-up: The most recent version has been running here for more than three days now, taking over 4000 successful measurements without returning any garbage. Of course it can still return garbage if just the wrong two bits flip in the communication. There's nothing we can do about that.

ladyada commented 4 years ago

thanks, we're a little swamped, but not forgotten!

heroldus commented 4 years ago

@ladyada Could you please merge the fix.

FoamyGuy commented 4 years ago

I tested this with a Trinket M0 and a DHT11. Ran for about an hour taking readings every 2 seconds. I did see the bogus data error a few times. But I think it was my fault in those cases the sensor was not connected properly yet. Since getting connected correctly it's run successfully and been reporting temperature and humidity.

It does need to have some merge conflicts resolved now though. @michaellass if you are able to you can make a new commit here to resolve them.

If not, no worries I will try to work on getting it resolved later this week.

michaellass commented 4 years ago

Thanks for testing! I just rebased the changes onto master.

FoamyGuy commented 4 years ago

Thank you michaellass this contribution and for rebasing.

Following up on testing, my Trinket M0 + DHT11 continued to run all night over night with no serious issues.

It looks like this was originally made to resolve an issue when running under Pi / Blinka. I will try to get it tested that way later this week.