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!

Committer:
harrypowers
Date:
Sun Dec 08 21:52:47 2013 +0000
Revision:
10:75e2489cecfe
Parent:
8:cee33b2d368e
Changed the class to now have the testing member function be public.  Added the getDTH11TH member function for reading the DTH11 sensor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
harrypowers 0:92ba381d5432 1 #ifndef DTH22_h
harrypowers 0:92ba381d5432 2 #define DTH22_h
harrypowers 0:92ba381d5432 3
harrypowers 5:3a5bdf8bd962 4 /* DTH22 sensor class for use with Mbed LPC1768 and other platforms
harrypowers 0:92ba381d5432 5 * Copyright (c) 2013 Philip King Smith
harrypowers 0:92ba381d5432 6 *
harrypowers 0:92ba381d5432 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
harrypowers 0:92ba381d5432 8 * of this software and associated documentation files (the "Software"), to deal
harrypowers 0:92ba381d5432 9 * in the Software without restriction, including without limitation the rights
harrypowers 0:92ba381d5432 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
harrypowers 0:92ba381d5432 11 * copies of the Software, and to permit persons to whom the Software is
harrypowers 0:92ba381d5432 12 * furnished to do so, subject to the following conditions:
harrypowers 0:92ba381d5432 13 *
harrypowers 0:92ba381d5432 14 * The above copyright notice and this permission notice shall be included in
harrypowers 0:92ba381d5432 15 * all copies or substantial portions of the Software.
harrypowers 0:92ba381d5432 16 *
harrypowers 0:92ba381d5432 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
harrypowers 0:92ba381d5432 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
harrypowers 0:92ba381d5432 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
harrypowers 0:92ba381d5432 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
harrypowers 0:92ba381d5432 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
harrypowers 0:92ba381d5432 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
harrypowers 0:92ba381d5432 23 * THE SOFTWARE.
harrypowers 0:92ba381d5432 24 *
harrypowers 0:92ba381d5432 25 * A DTH22/RHT03 sensor data sheet is at: http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Weather/RHT03.pdf
harrypowers 0:92ba381d5432 26 * I made this software because the classes that others have made seemed to suffer from many errors.
harrypowers 0:92ba381d5432 27 * I have used my rasbpery Pi code writen in Forth as the basis for this code.
harrypowers 0:92ba381d5432 28 * Like the other code i have found my first attempt in Forth to get this too work suffered from the same errors!
harrypowers 0:92ba381d5432 29 * I have since worked out the following code that works with the idea of a run lenght timing method thus removing
harrypowers 0:92ba381d5432 30 * the time sensitive error issues that seem to plauge this low cost sensor.
harrypowers 0:92ba381d5432 31 */
harrypowers 0:92ba381d5432 32
harrypowers 0:92ba381d5432 33 #include "mbed.h"
harrypowers 0:92ba381d5432 34
harrypowers 7:afc91051f2ee 35 #define MAXRAWDATA 1300 // the total times the DTH22 io pin is read via the mbed pin
harrypowers 7:afc91051f2ee 36 #define DATABITS 40 // DTH22 sensor only produces 40 data bits to be read
harrypowers 7:afc91051f2ee 37 #define BITREADTIME 3 // the wait time on lpc1768 mbed device in us via wait_us() function
harrypowers 7:afc91051f2ee 38 #define MAXBITCOUNT 30 // the total read cycles that when read as 1 indicate the DTH22 sensor is not sending anymore
harrypowers 7:afc91051f2ee 39 #define STARTTRANSBITSIZE 14 // the median cycle counts of the start transmit bit from the DTH22 sensor note this is based on the BITREADTIME value
harrypowers 7:afc91051f2ee 40 #define DATANOISE 2000 // Error indicating the DTH22 io line must be noisy and data is not valid
harrypowers 7:afc91051f2ee 41 #define STIMINGFAIL 2001 // Error indicating the start bit timing was exceeded on some bit so data could not be properly read
harrypowers 7:afc91051f2ee 42 #define DATANOISE2 2002 // Error indicating the DTH22 io line must be noisy and data is not valid because there was too much data from device that passed earlier error checks
harrypowers 7:afc91051f2ee 43 #define CHECKSUMFAIL 2003 // Error indicating the DTH22 data did not pass the checksum test
harrypowers 7:afc91051f2ee 44
harrypowers 1:d41fcc541836 45 /** DTH22 Digital Temperature Humidity class
harrypowers 0:92ba381d5432 46 *
harrypowers 0:92ba381d5432 47 * Example:
harrypowers 0:92ba381d5432 48 * @code
harrypowers 0:92ba381d5432 49 * // show how the DTH22_RHT03 class works
harrypowers 0:92ba381d5432 50 * #include "mbed.h"
harrypowers 3:57c9abcd9646 51 * #include "DTH22.h"
harrypowers 0:92ba381d5432 52 * int main()
harrypowers 0:92ba381d5432 53 * {
harrypowers 0:92ba381d5432 54 * int temp ;
harrypowers 0:92ba381d5432 55 * int humidity;
harrypowers 0:92ba381d5432 56 * int error ;
harrypowers 3:57c9abcd9646 57 * DTH22 myhumtemp(p24);
harrypowers 0:92ba381d5432 58 * while(1) {
harrypowers 0:92ba381d5432 59 * error = myhumtemp.getTH(&temp,&humidity);
harrypowers 0:92ba381d5432 60 * printf("Temp is %ld\r\n",temp);
harrypowers 0:92ba381d5432 61 * printf("Humidity is %ld\r\n",humidity);
harrypowers 0:92ba381d5432 62 * printf("Error is %d\r\n\r\n",error);
harrypowers 0:92ba381d5432 63 * wait(2);
harrypowers 6:f7a2ac66d45a 64 * }
harrypowers 0:92ba381d5432 65 * }
harrypowers 0:92ba381d5432 66 * @endcode
harrypowers 0:92ba381d5432 67 */
harrypowers 0:92ba381d5432 68
harrypowers 0:92ba381d5432 69 class DTH22
harrypowers 0:92ba381d5432 70 {
harrypowers 0:92ba381d5432 71 public:
harrypowers 0:92ba381d5432 72 /** Create object connected to DTH22 pin ( remember io pin needs pull up resister)
harrypowers 0:92ba381d5432 73 *
harrypowers 1:d41fcc541836 74 * Ensure the pull up resistors are used on this pin.
harrypowers 4:85dd45e5ab32 75 * Any pin can be used but the pin can only be used for one DTH22 sensor and nothing else.
harrypowers 0:92ba381d5432 76 *
harrypowers 8:cee33b2d368e 77 * @param PinName This is the pin that is connected to the IO pin of the DTH22 sensor (pin 2 of DTH22 device called Data-signal)
harrypowers 0:92ba381d5432 78 */
harrypowers 0:92ba381d5432 79 DTH22(PinName DATAsignal ); // Constructor
harrypowers 0:92ba381d5432 80
harrypowers 0:92ba381d5432 81 ~DTH22(); // Destructor
harrypowers 1:d41fcc541836 82
harrypowers 10:75e2489cecfe 83 /** Read Temperature and Humidity of DTH22 or RHT03 sensors
harrypowers 0:92ba381d5432 84 *
harrypowers 4:85dd45e5ab32 85 * 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.
harrypowers 10:75e2489cecfe 86 * This code should work with RHT03 sensors also. The RHT03 sensor is the same as the DTH22 i believe.
harrypowers 0:92ba381d5432 87 *
harrypowers 4:85dd45e5ab32 88 * @param temp the temperature returned in this variable. Degrees celsius with one decimal so 253 is 25.3 C. (-40C to +80C)
harrypowers 4:85dd45e5ab32 89 * @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)
harrypowers 0:92ba381d5432 90 * @param returns 0 for no errors. Any other number is some type of communication error and data might not be valid!
harrypowers 0:92ba381d5432 91 */
harrypowers 10:75e2489cecfe 92 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.
harrypowers 10:75e2489cecfe 93 // This will only read the DTH22 or RHT03 sensor. To read the DTH11 sensor use the getDTH11TH member function.
harrypowers 10:75e2489cecfe 94
harrypowers 10:75e2489cecfe 95 /** Read Temperature and Humidity of DTH11 sensor
harrypowers 10:75e2489cecfe 96 *
harrypowers 10:75e2489cecfe 97 * 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.
harrypowers 10:75e2489cecfe 98 * DTH11 sensor is a less percise version of the DTH22.
harrypowers 10:75e2489cecfe 99 *
harrypowers 10:75e2489cecfe 100 * @param temp the temperature returned in this variable. Degrees celsius with one decimal so 250 is 25.0 C. ( 0C to +50C)
harrypowers 10:75e2489cecfe 101 * @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)
harrypowers 10:75e2489cecfe 102 * @param returns 0 for no errors. Any other number is some type of communication error and data might not be valid!
harrypowers 10:75e2489cecfe 103 */
harrypowers 10:75e2489cecfe 104 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.
harrypowers 10:75e2489cecfe 105 // This will only read the DTH11 sensor. To read the DTH22 or the RHT03 sensor use getTH member function.
harrypowers 10:75e2489cecfe 106
harrypowers 10:75e2489cecfe 107 /** Read sensor and give the raw data out for testing
harrypowers 10:75e2489cecfe 108 *
harrypowers 10:75e2489cecfe 109 * This function will return when it has data from sensor. Note the sensors should not be read more then once every 2 seconds.
harrypowers 10:75e2489cecfe 110 *
harrypowers 10:75e2489cecfe 111 * @param bits is a bool array that should contain 40 bits of the data from the sensor just read
harrypowers 10:75e2489cecfe 112 * @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.
harrypowers 10:75e2489cecfe 113 * @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)
harrypowers 10:75e2489cecfe 114 * @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)
harrypowers 10:75e2489cecfe 115 * @param returns 0 for no errors. Any other number is some type of communication error and data might not be valid!
harrypowers 10:75e2489cecfe 116 */
harrypowers 10:75e2489cecfe 117 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.
harrypowers 10:75e2489cecfe 118 // Note the temperature and humidity values that are returned are for the DTH22 or RHT03 sensor only but the raw data is from
harrypowers 10:75e2489cecfe 119 // the sensor used. This means you can see the raw data from either the DTH22 or DTH11 sensor!
harrypowers 3:57c9abcd9646 120
harrypowers 0:92ba381d5432 121 protected:
harrypowers 0:92ba381d5432 122 DigitalInOut DTH22pin;
harrypowers 1:d41fcc541836 123 bool rawdata[MAXRAWDATA];
harrypowers 2:4d307eacba27 124 unsigned short int timingData[100];
harrypowers 2:4d307eacba27 125 bool rawdatabits[DATABITS];
harrypowers 3:57c9abcd9646 126
harrypowers 2:4d307eacba27 127 void getraw();
harrypowers 2:4d307eacba27 128 void getrawbits();
harrypowers 1:d41fcc541836 129 int transistionCount(int index);
harrypowers 2:4d307eacba27 130 int transmissionErrors();
harrypowers 0:92ba381d5432 131 };
harrypowers 0:92ba381d5432 132
harrypowers 0:92ba381d5432 133 #endif