Skip to content
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

Getting high negative values on Sensor DHT22 with negative Temperatures #3494

Closed
tomcatxx opened this issue Feb 6, 2021 · 3 comments · Fixed by #4166
Closed

Getting high negative values on Sensor DHT22 with negative Temperatures #3494

tomcatxx opened this issue Feb 6, 2021 · 3 comments · Fixed by #4166

Comments

@tomcatxx
Copy link
Contributor

tomcatxx commented Feb 6, 2021

If you use the DHT22 Plugin you get values like -3276,8 for Temperatures <=0°C

The way I fixed it was taking this example: https://github.com/RobTillaart/Arduino/blob/master/libraries/DHTstable/dht.cpp
But I still got -3276.80 at 0.0 °C. For this I just found a dirty way.
Here the way I fixed the whole problem. Maybe someone with more C++ experiance can fix it cleaner....

_P005_DHT.ino:

    case P005_DHT22:
      int16_t temp_temperature;
      temp_temperature   = (dht_dat[2] * 256 + dht_dat[3]);
      temperature = temp_temperature * 0.1;
      if (temperature < -3000) { temperature = 0; } // Dirty way to prevent -3276.80 at 0.0 °C.
      humidity = ((dht_dat[0] * 256) + dht_dat[1])/10; 	  
      break;
@TD-er
Copy link
Member

TD-er commented Feb 6, 2021

My first impression is that you may need to convert 2-complement values.
Isn't it just something like this?

    case P005_DHT22:
      int16_t temp_temperature = ((dht_dat[2] & 0x7f) * 256 + dht_dat[3]);
      if (dht_dat[2] & 0x80) {
        temp_temperature  *= -1;
      }
      temperature = temp_temperature * 0.1f;
      humidity = ((dht_dat[0] * 256) + dht_dat[1])/10; 	  
      break;

Otherwise you're throwing away negative readings.

@tomcatxx
Copy link
Contributor Author

tomcatxx commented Feb 6, 2021

    case P005_DHT22:
      int16_t temp_temperature = (dht_dat[2] & 0x7f) * 256 + dht_dat[3]);
      if (dht_dat[2] & 0x80) {
        temp_temperature  *= -1;
      }
      temperature = temp_temperature * 0.1f;
      humidity = ((dht_dat[0] * 256) + dht_dat[1])/10; 	  
      break;

Gives compile error.


    case P005_DHT22:
      int16_t temp_temperature;
      temp_temperature = (dht_dat[2] & 0x7f) * 256 + dht_dat[3]);
      if (dht_dat[2] & 0x80) {
        temp_temperature  *= -1;
      }
      temperature = temp_temperature * 0.1f;
      humidity = ((dht_dat[0] * 256) + dht_dat[1])/10; 	  
      break;

Is compileable but result in high negative number like without the patch.
The way I changed the code result in correct positive, 0, and negatie Temperaturevalues.

@Barracuda09
Copy link

Hi @tomcatxx

Is dht_dat by any chance an unsigned integer type (uint8_t), then you should cast that first so you don't need the multiplication with -1.

Probably there is no sign extension this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants