winlinvip / SimpleDHT

Simple, Stable and Fast Arduino Temp & Humidity Sensors for DHT11 and DHT22. http://learn.adafruit.com/dht
MIT License
144 stars 61 forks source link

Negative temperatures #14

Closed danielfaust closed 6 years ago

danielfaust commented 6 years ago

Have you tested this library with negative temperatures?

With a DHT22 I'm getting values like -3276.3°C for values slightly below 0°C (possibly for -0.7°C).

-3277.0°C is not reached, that one is then 0.0°C

winlinvip commented 6 years ago

Nop, I didn't test it. I'll try it in our refrigerator.

danielfaust commented 6 years ago

Here is the code I'm using to work around this issue.

As I'm sending the data over BLE to an Android phone and the USB port of the ESP32 NodeMCU broke off (I'm using the library with that), making an on-device correction somewhat more laborious, i fixed it in the app which receives the data.

Before sending it to the phone I multiply with 100 and then cast to int, that makes the BLE decoding somewhat easier.

In the app I'm using the following to correct the issue:

int iTemp = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT32, 4);
if (iTemp < 0) {
    iTemp = -iTemp - 327700;
}
float fTemp = iTemp / 100.0;
sulicz commented 6 years ago

Hi, solution si:

    if (ptemperature) {
        if ( (temperature & 0x8000 )) {
          *ptemperature = (float) (temperature & 0x7FFF)/ -10.0;
        } else {
          *ptemperature = (float) temperature / 10.0;
        }
    }

in function SimpleDHT22::read2 and I changed all datatype short to uint16_t but datatype is may not a reason of problem.

winlinvip commented 6 years ago

Could u please file a PullRequest?

danielfaust commented 6 years ago

I will look into it

danielfaust commented 6 years ago

BTW, using a compressed gas duster (holding it upside down) can give you temps below the freezing point very quickly, then you can heat it up again with the fingers.

winlinvip commented 6 years ago

Merged

winlinvip commented 6 years ago

Please fix it for DHT11.

danielfaust commented 6 years ago

I don't have a DHT11 to test with. But since the DHT11 only works with temperatures equal to or above 0 °C, I think that sign flag never gets set. I preferred not to touch that code.

Good for 0-50°C temperature readings ±2°C accuracy

winlinvip commented 6 years ago

👌

mapmelad commented 6 years ago

Unfortunately still not displayed correctly :/

https://puu.sh/z23v3/7b8f96b37f.jpg

danielfaust commented 6 years ago

@mapmelad on what device are you running it? For me this works on an ESP32, maybe this is device dependent? When you use a previous commit, what does it show?

mapmelad commented 6 years ago

First, I obtained the values of the similarity -3277.0°C. I used the fix:

if (ptemperature) {
        if ( (temperature & 0x8000 )) {
          *ptemperature = (float) (temperature & 0x7FFF)/ -10.0;
        } else {
          *ptemperature = (float) temperature / 10.0;
        }
    }

Then, after the fix I started getting the values 255, 254 and so on If I delete in the code the minus sign *ptemperature = (float) (temperature & 0x7FFF)/ 10.0; , it displays the temperature correctly, but there is no minus sign in the output.

danielfaust commented 6 years ago

Maybe this is due to the ArduinoBT -- which you're apparently using, judging from the screenshot -- being an 8 bit mcu while the ESP32 has a 32 bit cpu.

winlinvip commented 6 years ago

Should we reopen this issue? @danielfaust

winlinvip commented 6 years ago

Please test 1.0.9

winlinvip commented 6 years ago

image

I put DHT11 into refrigerator, its data is:

Sample DHT11 with RAW bits...
Sample RAW Bits: 0010 1100 0000 0000 0000 0001 0000 0000 0010 1101 
Sample OK: 1 *C, 44 H

Always above 0C.

winlinvip commented 6 years ago

For DHT22:

Sample DHT22 with RAW bits...
Sample RAW Bits: 0000 0011 1110 0111 1000 0000 0101 1010 1100 0100 
Sample OK: -9.00 *C, 99.90 RH%
Sample DHT22 with RAW bits...
Sample RAW Bits: 0000 0010 1101 1101 1000 0000 1000 1000 1110 0111 
Sample OK: -13.60 *C, 73.30 RH%

image