Go Down

Topic: Would someone please add DHT22 to Interrupt driven DHT11 Lib (idDHT11) (Read 2782 times) previous topic - next topic

turgo

Programmer niesteszeck kindly wrote an interrupt driven DHT11 Lib (idDHT11).  I need to use a DHT22 sensor, which would benefit even more from interrupt programming as it takes 2 seconds to initiate and receive a measurement.  Additionally, I need to make measurements from three separate DHT22s.  However, it is beyond my skill level to modify the idDHT11 to add the DHT22.  Would someone please consider upgrading idDHT11 to benefit the Arduino community?

The original thread describing the idDHT11 is at:
http://forum.arduino.cc/index.php?topic=155800.0

You can get the idDHT11 lib at
https://github.com/niesteszeck/idDHT11

pylon

Because the line protocol of the two sensors is almost identical and just the interpretation of the received bytes differs, it should be sufficient to replace the DATA portion of the case statement in the interrupt handler as follows:

Code: [Select]
case DATA:
if(delta<10) {
detachInterrupt(intNumber);
status = IDDHTLIB_ERROR_DELTA;
state = STOPPED;
} else if(60<delta && delta<135) { //valid in timing
bits[idx] <<= 1;
if(delta>90) //is a one
bits[idx] |= 1;
if (cnt == 0) {  // whe have fullfilled the byte, go to next
cnt = 7;    // restart at MSB
if(idx++ == 4) {      // go to next byte, if whe have got 5 bytes stop.
detachInterrupt(intNumber);
// WRITE TO RIGHT VARS
uint16_t rawhum    = bits[0];
rawhum <<= 8;
rawhum |= bits[1];
hum = rawhum * 0.1;
uint16_t rawtemp = bits[2];
rawtemp <<= 8;
rawtemp |= bits[3];
temp = (rawtemp & 0x8000) ? -(int16_t)(rawtemp & 0x7FFF) * 0.1 : rawtemp * 0.1;
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3]; 
if (bits[4] != sum) {
status = IDDHTLIB_ERROR_CHECKSUM;
state = STOPPED;
} else {
status = IDDHTLIB_OK;
state = ACQUIRED;
}
break;
}
} else cnt--;
} else {
detachInterrupt(intNumber);
status = IDDHTLIB_ERROR_TIMEOUT;
state = STOPPED;
}
break;


niesteszeck

#3
Jul 04, 2013, 06:06 am Last Edit: Jul 04, 2013, 06:08 am by niesteszeck Reason: 1
I'll make the functionality to my lib, but I have a question about the proposed code: why you invert the order of the data, as I can see on the DHT22 datasheet it has the same data structure, the only difference is that it has decimal and not only int data.
The part that I have the question about changing it is this part of code
Code: [Select]
else if(60<delta && delta<135) { //valid in timing
bits[idx] <<= 1;
if(delta>90) //is a one
bits[idx] |= 1


anyway, I make a new idDHTLib that you can get from https://github.com/niesteszeck/idDHTLib, Isn't tested yet, but is a good starting point, tomorrow if I have some time I'll test it on my 2 DHT11 sensor  :)

pylon

Quote
I'll make the functionality to my lib, but I have a question about the proposed code: why you invert the order of the data


No, I don't. You are oring the value of every bit separately which produces a lot of shift operations. I just insert every bit in the lowest position and then shift the whole byte one bit to the left. So the most significant bit enters the byte at the 1 position but after 7 shifts it reached it's final and expected position. This is just an optimization, you can leave it as you have it, you just loose performance.

Quote
is this mod working on a DHT22?


I don't know, I just inserted the different byte interpretation from another library into your code.

niesteszeck


No, I don't. You are oring the value of every bit separately which produces a lot of shift operations. I just insert every bit in the lowest position and then shift the whole byte one bit to the left. So the most significant bit enters the byte at the 1 position but after 7 shifts it reached it's final and expected position. This is just an optimization, you can leave it as you have it, you just loose performance.

OH! Thanks for the code, now I'm looking it right (yesterday was too late), I will make that improvement to the lib and put you in the credits!


I don't know, I just inserted the different byte interpretation from another library into your code.

So either one has a DHT22 sensor, but the lib looks ready, can anyone prove it?

turgo

Thanks for your work.  I'm excited to try it, except that I believe that I have damaged my DHT22s, so I'll have to wait until I get new ones.
http://forum.arduino.cc/index.php?topic=176206.0

niesteszeck

Yesterday at night I try the new id lib with my DHT11 sensor, it had a few small typos (id->ID in some methods, and others), I corrected it, repair all the compile time errors and tested it on a Mega 2560 on pin 19 (int 4) and is working.
I'll be waiting for your news sensors to arrive so you can test the DHT22 and tell me how is working (hopefully great!), so I can get out the beta of the lib!

dksofts

i received this errors.

idDHTLib Example program
LIB version: 0.0.3
---------------

Retrieving information from sensor: Read sensor: Error

   Time out error
Humidity (%): -2.00
Temperature (oC): -2.00
Temperature (oF): -2.00
Temperature (K): -2.00
Dew Point (oC): -2.00
Dew Point Slow (oC): -2.00


Please help out.

Go Up