DTH22, RHT03 and DTH11 sensors can be read with this code!

This DHT22 sensor reading class works with the mbed LPC1768. The code is time dependent but has been tested with mbed LPC1768 currently. I may add timing defines for other platforms if needed but i currently only use the mbed LPC1768 platform. Please feel free to import the code and modify it for other platforms if needed. BITREADTIME define and STARTTRANSBITSIZE define would be the main items to change for any other platform to operate properly. BITREADTIME is the us time value used in a basic look for a wait value to get next reading. This may work simply on other platforms at other system speeds but it may not. A more general solution maybe to add another calculation that generates these defines based on some platform speed value. At this writing the code performs very well with little to no read failures(in fact i have not seen a read failure yet in testing). The issues that i have seen with other classes and this sensor is the fact that the sensor always produces the correct Temperature and Humidity output values on the io pin but the class reading these values miss many reading causing errors. This class avoids this because it reads the output from the DTH22 sensor completely and then processes the values from a run length bit measurement perspective that i feel is far more accurate. Anyways the results speak for them self.

I have now added a member function for reading the DTH11 sensor as it is read the same way as the DTH22 sensor. The only difference is the final use of the retrieved bytes from the sensor for calculating the temperature and humidity. Note the DTH11 sensor has less range and less accuracy but it also can be found for less money!

Revision:
10:75e2489cecfe
Parent:
8:cee33b2d368e
--- a/DTH22.h	Wed Dec 04 03:17:51 2013 +0000
+++ b/DTH22.h	Sun Dec 08 21:52:47 2013 +0000
@@ -80,16 +80,43 @@
 
     ~DTH22();                      // Destructor
 
-    /** Read Temperature and Humidity
+    /** Read Temperature and Humidity of DTH22 or RHT03 sensors
     *
     * This function will return when it has readings.  Note the DTH22 documents say that the device should not be read more then once every 2 seconds.
-    *   This code should work with DTH11 and RHT03 sensors also.  The RHT03 sensor is the same as the DTH22 i believe and the DTH11 sensor is a less percise version of the DTH22.
+    *   This code should work with RHT03 sensors also.  The RHT03 sensor is the same as the DTH22 i believe.
     *
     * @param temp the temperature returned in this variable. Degrees celsius with one decimal so 253 is 25.3 C. (-40C to +80C)
     * @param humidity the humidity is returned in this variable. Humidity is in % rH with one decimal so 288 is 28.8 % rH. (0% to 100% rH)
     * @param returns 0 for no errors.  Any other number is some type of communication error and data might not be valid!
     */
-    int getTH(int *temp,int *humidity);    // get both temperature and humidity with an error indicator.  Note only do this read no more then once every 2 seconds as per DTH22 documents
+    int getTH(int *temp,int *humidity); // Will read both temperature and humidity with an error indicator.  Note only do this read no more then once every 2 seconds as per DTH22 documents.
+                                        // This will only read the DTH22 or RHT03 sensor.  To read the DTH11 sensor use the getDTH11TH member function.
+                                        
+    /** Read Temperature and Humidity of DTH11 sensor
+    *
+    * This function will return when it has readings.  Note the DTH11 documents say that the device should not be read more then once every 2 seconds.
+    *   DTH11 sensor is a less percise version of the DTH22.
+    *
+    * @param temp the temperature returned in this variable. Degrees celsius with one decimal so 250 is 25.0 C. ( 0C to +50C)
+    * @param humidity the humidity is returned in this variable. Humidity is in % rH with one decimal so 280 is 28.0 % rH. (20% to 90% rH)
+    * @param returns 0 for no errors.  Any other number is some type of communication error and data might not be valid!
+    */    
+    int getDTH11TH(int *temp,int *humidity);    // This gets the temperature and humidity with an error indicator.  Note only do this reading no more then once every 2 seconds as per the DTH11 documents.
+                                                // This will only read the DTH11 sensor.  To read the DTH22 or the RHT03 sensor use getTH member function.     
+
+    /** Read sensor and give the raw data out for testing 
+    *
+    * This function will return when it has data from sensor.  Note the sensors should not be read more then once every 2 seconds.
+    *
+    * @param bits is a bool array that should contain 40 bits of the data from the sensor just read
+    * @param data is an array of 5 bytes that the above bits get compiled into.  These bytes are simply from the above bits read from the device.
+    * @param temp the temperature returned in this variable. Degrees celsius with one decimal so 253 is 25.3 C. (-40C to +80C) ( This is the DTH22 sensor value only)
+    * @param humidity the humidity is returned in this variable. Humidity is in % rH with one decimal so 288 is 28.8 % rH. (0% to 100% rH) ( This is the DTH22 sensor value only)
+    * @param returns 0 for no errors.  Any other number is some type of communication error and data might not be valid!
+    */    
+    int testing(bool *bits,signed short int *data,int *temp,int *humidity); // This will get the raw data from sensor and will produce the results as the data is processed from bits to byts to readings.
+                                                                            // Note the temperature and humidity values that are returned are for the DTH22 or RHT03 sensor only but the raw data is from
+                                                                            // the sensor used.  This means you can see the raw data from either the DTH22 or DTH11 sensor!
 
 protected:
     DigitalInOut DTH22pin;
@@ -97,7 +124,6 @@
     unsigned short int timingData[100];
     bool rawdatabits[DATABITS];
 
-    int testing(bool *bits,signed short int *data,int *temp,int *humidity);
     void getraw();
     void getrawbits();
     int transistionCount(int index);