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:
Mon Dec 02 08:48:29 2013 +0000
Revision:
1:d41fcc541836
Parent:
0:92ba381d5432
Child:
2:4d307eacba27
got plans for how to get the final data but for now this produces the timing data to allow extraction of the 40 bits of data!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
harrypowers 0:92ba381d5432 1 #include "DTH22.h"
harrypowers 0:92ba381d5432 2
harrypowers 0:92ba381d5432 3 DTH22::DTH22(PinName DATAsignal ) : DTH22pin(DATAsignal)
harrypowers 0:92ba381d5432 4 {
harrypowers 1:d41fcc541836 5 for(int i = 0; i < MAXRAWDATA; i++) {
harrypowers 1:d41fcc541836 6 rawdata[i] = false;
harrypowers 1:d41fcc541836 7 }
harrypowers 1:d41fcc541836 8
harrypowers 0:92ba381d5432 9 }
harrypowers 0:92ba381d5432 10
harrypowers 1:d41fcc541836 11 DTH22::~DTH22() {}
harrypowers 0:92ba381d5432 12
harrypowers 1:d41fcc541836 13 int DTH22::getTH(int *temp,int *humidity)
harrypowers 1:d41fcc541836 14 {
harrypowers 0:92ba381d5432 15 return 0;
harrypowers 0:92ba381d5432 16 }
harrypowers 1:d41fcc541836 17
harrypowers 1:d41fcc541836 18 void DTH22::getraw(int *data)
harrypowers 1:d41fcc541836 19 {
harrypowers 1:d41fcc541836 20 int counter = 0;
harrypowers 1:d41fcc541836 21 for(int i = 0; i < 100; i++) {
harrypowers 1:d41fcc541836 22 data[i] = 0;
harrypowers 1:d41fcc541836 23 }
harrypowers 1:d41fcc541836 24 DTH22pin.mode(OpenDrain);
harrypowers 0:92ba381d5432 25 DTH22pin.output();
harrypowers 0:92ba381d5432 26 DTH22pin = 0;
harrypowers 1:d41fcc541836 27 wait_ms(18);
harrypowers 0:92ba381d5432 28 DTH22pin = 1;
harrypowers 0:92ba381d5432 29 wait_us(40);
harrypowers 0:92ba381d5432 30 DTH22pin.input();
harrypowers 1:d41fcc541836 31 for (int i = 0; i < MAXRAWDATA; i++) {
harrypowers 1:d41fcc541836 32 int junk = DTH22pin.read();
harrypowers 1:d41fcc541836 33 if(junk==0) rawdata[i] = false;
harrypowers 1:d41fcc541836 34 if(junk==1) rawdata[i] = true;
harrypowers 1:d41fcc541836 35 wait_us(BITREADTIME);
harrypowers 1:d41fcc541836 36 }
harrypowers 1:d41fcc541836 37
harrypowers 1:d41fcc541836 38 for (int j = 0; j < 100; j++) {
harrypowers 1:d41fcc541836 39 if(counter < MAXRAWDATA) {
harrypowers 1:d41fcc541836 40 data[98] = counter ;
harrypowers 1:d41fcc541836 41 data[j] = transistionCount(counter);
harrypowers 1:d41fcc541836 42 if(data[j]>=MAXBITCOUNT) {
harrypowers 1:d41fcc541836 43 j = 101;
harrypowers 1:d41fcc541836 44 } else {
harrypowers 1:d41fcc541836 45 counter = counter + data[j] ;
harrypowers 1:d41fcc541836 46 }
harrypowers 1:d41fcc541836 47 } else {
harrypowers 1:d41fcc541836 48 j = 101;
harrypowers 1:d41fcc541836 49 }
harrypowers 0:92ba381d5432 50 }
harrypowers 0:92ba381d5432 51 }
harrypowers 1:d41fcc541836 52
harrypowers 1:d41fcc541836 53 int DTH22::transistionCount(int index)
harrypowers 1:d41fcc541836 54 {
harrypowers 1:d41fcc541836 55 int count = 0;
harrypowers 1:d41fcc541836 56 bool endbit = !rawdata[index];
harrypowers 1:d41fcc541836 57
harrypowers 1:d41fcc541836 58 for (int i = 1; i < MAXBITCOUNT; i++) {
harrypowers 1:d41fcc541836 59 if(rawdata[index + i]==endbit) {
harrypowers 1:d41fcc541836 60 count = i;
harrypowers 1:d41fcc541836 61 i = MAXBITCOUNT;
harrypowers 1:d41fcc541836 62 }
harrypowers 1:d41fcc541836 63 }
harrypowers 1:d41fcc541836 64 return count;
harrypowers 1:d41fcc541836 65 }
harrypowers 1:d41fcc541836 66
harrypowers 1:d41fcc541836 67 int DTH22::currentpin()
harrypowers 1:d41fcc541836 68 {
harrypowers 1:d41fcc541836 69 DTH22pin.input();
harrypowers 1:d41fcc541836 70 return DTH22pin;
harrypowers 1:d41fcc541836 71 }