mstenta / atmospi

Atmospheric monitoring app for logging and graphing temperatures and humidities over time using a Raspberry Pi and DS18B20, DHT11, DHT22, and AM2302 sensor(s).
31 stars 9 forks source link

Humidity and Temprature of DHT22 are switched #31

Closed tvnieuwenhuijzen closed 6 years ago

tvnieuwenhuijzen commented 6 years ago

Hi,

Thank you for this, just what i was looking for. I only have one issue and that is that of my DHT22 sensor it seems like the humidity and temprature data are switched. Is there an easy way to fix this?

mstenta commented 6 years ago

Hi @tvnieuwenhuijzen

Huh. That's strange. I'm not sure what could be causing that. Are you 100% sure they are switched? Maybe the values are just similar so it's hard to tell? Are you using default configuration (Fahrenheit)? Or did you create a settings.py to use Celcius?

The code that gathers the temperature and humidity values from the DHT22 sensor is using the Adafruit DHT code provided here: https://github.com/adafruit/Adafruit_Python_DHT

I wonder if maybe that code changed recently? And now it is reading them in a different order?

mstenta commented 6 years ago

Hmm - looking at the Adafruit_Python_DHT example code... it looks like you're right! The Atmospi script seems to be reading them in the opposite order from the example! That is very surprising.

I'm amazed no one noticed this until now. Maybe no one is using DHT sensors... I have a few set up, but don't look at them very often. I'm looking back at their data now to see if that makes sense... maybe I didn't notice because I'm converting Celcius to Fahrenheit, so it hid the issue a little more...

I will investigate further to confirm if this is truly a bug...

mstenta commented 6 years ago

In the meantime, you can test to see if this fixes it for you...

In Atmospi/measure-dht.py, line 26... (https://github.com/mstenta/atmospi/blob/master/Atmospi/measure-dht.py#L26)

Change:

tc, h = Adafruit_DHT.read_retry(sensor_types[type], int(pin))

To:

h, tc = Adafruit_DHT.read_retry(sensor_types[type], int(pin))

Let me know if the data looks better for you after that.

mstenta commented 6 years ago

Ok nevermind... I'm looking at my old data, and my humidity and temperature appear to be correct. For example, I have an Atmospi in my basement that is currently showing:

Basement (temperature): 66.38 °F (Oct 12, 2017 - 08:30) Basement (humidity): 66.5 % (Oct 12, 2017 - 08:30)

Temperature is being converted from Celcius to Fahrenheit, so if I was accidentally reading humidity as temperature, that would mean the humidity of the basement is actually 18%... which I know is not the case (I have a dehumidifier running which shows ~60% on it's LED screen).

But... I set up my Atmospis (4 of them) about 3 years ago... so they are using older versions of the Adafruit DHT code, and maybe it has changed since then... and the new code is reading temperature/humidity in the opposite order... but I can't find any evidence of that in the Adafruit_Python_DHT codebase...

I'll continue digging to see what's going on... it does look like the code should be changed, but I'm not sure why my old sensors are working properly...

mstenta commented 6 years ago

Alright - so I think what happened was: the Adafruit_Python_DHT changed after I set up my devices.

If I SSH into my Atmospi, and look at the date of the file ~/atmospi/Atmospi/dhtreader.so, it is January 17th, 2014.

However, if I look at the initial commit of the Adafruit_Python_DHT library, it is June 7th, 2014: https://github.com/adafruit/Adafruit_Python_DHT/commit/5ead51ddc270b3941c9e988a6f4b3a468b4ea844

So Adafruit must have removed their original codebase and replaced it with a new one, with a different Git history... after I set up my devices.

Wow. Sneaky! Good job @tvnieuwenhuijzen for discovering this bug! I would not have noticed it because all my Atmospis are using the old version of the library.

mstenta commented 6 years ago

Ah ha - I think I see where this may have occurred... it was actually in the most recent commit to this repo:

https://github.com/mstenta/atmospi/commit/e4dacdae052a00e85d41fb686b80ce8fafc1dc20

I committed a fix: https://github.com/mstenta/atmospi/commit/e4bb0ebdc922968728393ba7f428c9ac1a312159

That should fix it!

tvnieuwenhuijzen commented 6 years ago

Thanks for the quick solve. For me it was quite easy to see, because i use the Celsius so the difference is quite big between humidity and temprature. i will change my code manually

mstenta commented 6 years ago

Great! I also just made a new release, that includes the change: https://github.com/mstenta/atmospi/releases/tag/v0.3.0