Loading...
Pages: 1 2 [3]   Go Down
Author Topic: Class for both DHT11 and DHT 22 (temperature & humidity)  (Read 18936 times)
0 Members and 2 Guests are viewing this topic.
Netherlands
Offline Offline
Tesla Member
***
Karma: 89
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

updated the code to 0.1.03;

+ added code to reflect faulty reading in temperature (-999)  and humidity (-999)  if there is a timeout. So if a sketch does not test the return value of read11() or read22() you will still see it clearly.

As allways comments still welcome
« Last Edit: July 09, 2012, 01:54:38 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 140
Just Do it (with Arduino of course)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One suggestion for the code is to replace or augment the read11 and read22 routines by one unique read routine that would detect which one it is and return 11 or 22 in chk.

I'm not sure it is _so_ easy, but for example, having both [2] and [4] zero with a DHT22 should almost never happen, right ?

They are the lsB if I get it right, so humidity has to be 25.6, 51.2 or 76.8 for byte[4] to be zero.
Then in that case temperature has to be precisely 25.6 etc.
Pretty unusual.

To be even more confident, there could be a "confidence" variable based on previous measurements:
increase/decrease  a uint8_t counter for every non/zero value with saturation @ +127/-128, so DHT11 goes from zero @reset to -128 and DHT22 to +127.

Another thing is to replace the double by an int in tenths of degrees. So for DHT11 it would return array[0]*10;
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 89
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
One suggestion for the code is to replace or augment the read11 and read22 routines by one unique read routine that would detect which one it is and return 11 or 22 in chk
Don't mix error state with configuration information. That said, one could make a function int determine() that does just that. Typically it is called just once and remembered - you just needs one byte to keep the 11/22 flag of 8 sensors.

Quote
I'm not sure it is _so_ easy, but for example, having both [2] and [4] zero with a DHT22 should almost never happen, right ?
possible but one could only account readings for which [2] and [4] are zero AND the checksum is OK.

Assuming a valid reading, both numbers have one decimal place 0..9 =>10 possible values. This implies that the chance would be about one in hundred for an individual reading to have two 0's as decimal. That is a small chance but too big imho to make a reliable determine() function, it is definitely not deterministic. Multiple readings with will improve this number but as the device needs 2 seconds between readings so determine() function becomes quite time consuming.

Note that especially for the DHT11 one can use a (running) average to get a higher precision - and thus decimal places ).
« Last Edit: October 14, 2012, 07:47:49 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi robtllaart

updated the code to 0.1.03;

+ added code to reflect faulty reading in temperature (-999)  and humidity (-999)  if there is a timeout. So if a sketch does not test the return value of read11() or read22() you will still see it clearly.

As allways comments still welcome

I would change the follow code in read22() and read11() functions from

Code:
int rv = read(pin);
        if (rv != 0)
    {

to

Code:
int rv = read(pin);
        if (rv != DHTLIB_OK)
    {

No functionally change but improves readability.

Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 89
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is very keen eye!  I will change it asap

Thanks,
Rob
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Netherlands
Offline Offline
Tesla Member
***
Karma: 89
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


+ 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.
Logged

Niagara Region
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
        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:
        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!
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 89
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Thanks for this feedback, I'll fix it a.s.a.p.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Netherlands
Offline Offline
Tesla Member
***
Karma: 89
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

updated the playground article to include the patch above.

Thanks again!
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Pages: 1 2 [3]   Go Up
Print
 
Jump to: