Go Down

Topic: Class for DHT11, DHT21 and DHT22 (temperature & humidity) (Read 62 times) previous topic - next topic

robtillaart

updated on the playground to version 0.1.04

+ added the rv != DHTLIB_OK as suggested by jrbenito (thanks)
+ added #define DHTLIB_INVALID_VALUE  -999  to remove that magic number from the lib too.

Rob
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

jrbenito



+ added #define DHTLIB_INVALID_VALUE  -999  to remove that magic number from the lib too.

Rob


Wow! Very good too.

Thanks for this code and best regards,
Benito.

RoseMan

Hi,

Nice library.  However, it does not work correctly for negative temperatures.  I'm using version 0.1.04.  When a negative temperature is measured the class will return DHTLIB_ERROR_CHECKSUM.  The problem is at line 74 in dht.cpp.  Here bits[2] is modified, which makes the checksum calculation incorrect.  There are, of course, many ways to fix the problem.  I have fixed it by changing the code in the following way.

Old Code:
Code: [Select]
        int sign = 1;
        if (bits[2] & 0x80) // negative temperature
        {
                bits[2] = bits[2] & 0x7F;
                sign = -1;
        }
        temperature = sign * word(bits[2], bits[3]) * 0.1;


New Code:
Code: [Select]

        if (bits[2] & 0x80) // negative temperature
        {
                temperature = word(bits[2]&0x7F, bits[3]) * 0.1;
                temperature = -1.0 * temperature;
        }
        else
        {
                temperature = word(bits[2], bits[3]) * 0.1;
        }


Thanks for the library, and with this fix it is just perfect for me!

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

- http://playground.arduino.cc//Main/DHTLib -

updated the playground article to include the patch above.

Thanks again!
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up