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:
9:71d7f0caaa68
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 #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 0:92ba381d5432 8 }
harrypowers 0:92ba381d5432 9
harrypowers 1:d41fcc541836 10 DTH22::~DTH22() {}
harrypowers 0:92ba381d5432 11
harrypowers 1:d41fcc541836 12 int DTH22::getTH(int *temp,int *humidity)
harrypowers 1:d41fcc541836 13 {
harrypowers 2:4d307eacba27 14 signed short int bytes[5];
harrypowers 2:4d307eacba27 15 int checksumtest = 0;
harrypowers 2:4d307eacba27 16 bool tempsign = false;
harrypowers 2:4d307eacba27 17 DTH22::getraw();
harrypowers 2:4d307eacba27 18 DTH22::getrawbits();
harrypowers 2:4d307eacba27 19 // check transmission type errors
harrypowers 2:4d307eacba27 20 if(transmissionErrors()!=0) return transmissionErrors();
harrypowers 2:4d307eacba27 21 // group bits into bytes
harrypowers 2:4d307eacba27 22 for(int j = 0; j < 5; j++) {
harrypowers 2:4d307eacba27 23 bytes[j] = 0;
harrypowers 2:4d307eacba27 24 for(int i = 0; i < 8; i++) {
harrypowers 2:4d307eacba27 25 if(rawdatabits[(i+(j*8))]==true) bytes[j]=bytes[j] | 1;
harrypowers 2:4d307eacba27 26 if(i!=7) bytes[j]=bytes[j] << 1;
harrypowers 2:4d307eacba27 27 }
harrypowers 2:4d307eacba27 28 }
harrypowers 2:4d307eacba27 29 // checksum error test
harrypowers 6:f7a2ac66d45a 30 for(int i = 0; i < 4; i++) {
harrypowers 2:4d307eacba27 31 checksumtest += bytes[i];
harrypowers 2:4d307eacba27 32 }
harrypowers 2:4d307eacba27 33 checksumtest = checksumtest & 255; // only want the last byte for test
harrypowers 2:4d307eacba27 34 if(checksumtest!=bytes[4]) return CHECKSUMFAIL ;
harrypowers 2:4d307eacba27 35 // get rH
harrypowers 2:4d307eacba27 36 *humidity = (int)((bytes[0] << 8) | bytes[1]);
harrypowers 6:f7a2ac66d45a 37 // get temp
harrypowers 2:4d307eacba27 38 if((bytes[2]&128)!=0) tempsign = true;
harrypowers 2:4d307eacba27 39 bytes[2] = bytes[2] & 127;
harrypowers 2:4d307eacba27 40 *temp = (int)((bytes[2] << 8) | bytes[3]);
harrypowers 9:71d7f0caaa68 41 if(tempsign) *temp = -*temp;
harrypowers 0:92ba381d5432 42 return 0;
harrypowers 0:92ba381d5432 43 }
harrypowers 1:d41fcc541836 44
harrypowers 10:75e2489cecfe 45 int DTH22::getDTH11TH(int *temp,int *humidity){
harrypowers 10:75e2489cecfe 46 signed short int bytes[5];
harrypowers 10:75e2489cecfe 47 int checksumtest = 0;
harrypowers 10:75e2489cecfe 48 // bool tempsign = false;
harrypowers 10:75e2489cecfe 49 DTH22::getraw();
harrypowers 10:75e2489cecfe 50 DTH22::getrawbits();
harrypowers 10:75e2489cecfe 51 // check transmission type errors
harrypowers 10:75e2489cecfe 52 if(transmissionErrors()!=0) return transmissionErrors();
harrypowers 10:75e2489cecfe 53 // group bits into bytes
harrypowers 10:75e2489cecfe 54 for(int j = 0; j < 5; j++) {
harrypowers 10:75e2489cecfe 55 bytes[j] = 0;
harrypowers 10:75e2489cecfe 56 for(int i = 0; i < 8; i++) {
harrypowers 10:75e2489cecfe 57 if(rawdatabits[(i+(j*8))]==true) bytes[j]=bytes[j] | 1;
harrypowers 10:75e2489cecfe 58 if(i!=7) bytes[j]=bytes[j] << 1;
harrypowers 10:75e2489cecfe 59 }
harrypowers 10:75e2489cecfe 60 }
harrypowers 10:75e2489cecfe 61 // checksum error test
harrypowers 10:75e2489cecfe 62 for(int i = 0; i < 4; i++) {
harrypowers 10:75e2489cecfe 63 checksumtest += bytes[i];
harrypowers 10:75e2489cecfe 64 }
harrypowers 10:75e2489cecfe 65 checksumtest = checksumtest & 255; // only want the last byte for test
harrypowers 10:75e2489cecfe 66 if(checksumtest!=bytes[4]) return CHECKSUMFAIL ;
harrypowers 10:75e2489cecfe 67 // get rH
harrypowers 10:75e2489cecfe 68 *humidity = (int)(bytes[0] * 10);
harrypowers 10:75e2489cecfe 69 // get temp
harrypowers 10:75e2489cecfe 70 //if((bytes[2]&128)!=0) tempsign = true;
harrypowers 10:75e2489cecfe 71 //bytes[2] = bytes[2] & 127;
harrypowers 10:75e2489cecfe 72 *temp = (int)(bytes[2] * 10);
harrypowers 10:75e2489cecfe 73 //if(tempsign) *temp = -*temp;
harrypowers 10:75e2489cecfe 74 return 0;
harrypowers 10:75e2489cecfe 75 }
harrypowers 2:4d307eacba27 76 int DTH22::testing(bool *bits,signed short int *data,int *temp,int *humidity)
harrypowers 2:4d307eacba27 77 {
harrypowers 2:4d307eacba27 78 signed short int bytes[5];
harrypowers 2:4d307eacba27 79 int checksumtest = 0;
harrypowers 2:4d307eacba27 80 bool tempsign = false;
harrypowers 2:4d307eacba27 81 DTH22::getraw();
harrypowers 2:4d307eacba27 82 DTH22::getrawbits();
harrypowers 2:4d307eacba27 83 for(int i = 0; i < 40; i ++) {
harrypowers 2:4d307eacba27 84 bits[i]=rawdatabits[i];
harrypowers 2:4d307eacba27 85 }
harrypowers 2:4d307eacba27 86 if(transmissionErrors()!=0) return transmissionErrors();
harrypowers 2:4d307eacba27 87 // group bits into bytes
harrypowers 2:4d307eacba27 88 for(int j = 0; j < 5; j++) {
harrypowers 2:4d307eacba27 89 bytes[j] = 0;
harrypowers 2:4d307eacba27 90 for(int i = 0; i < 8; i++) {
harrypowers 2:4d307eacba27 91 if(rawdatabits[(i+(j*8))]==true) bytes[j]=bytes[j] | 1;
harrypowers 2:4d307eacba27 92 if(i!=7) bytes[j]=bytes[j] << 1;
harrypowers 2:4d307eacba27 93 }
harrypowers 2:4d307eacba27 94 }
harrypowers 2:4d307eacba27 95 // checksum test
harrypowers 6:f7a2ac66d45a 96 for(int i = 0; i < 4; i++) {
harrypowers 2:4d307eacba27 97 checksumtest += bytes[i];
harrypowers 2:4d307eacba27 98 }
harrypowers 2:4d307eacba27 99 checksumtest = checksumtest & 255; // only want the last byte for test
harrypowers 2:4d307eacba27 100 if(checksumtest!=bytes[4]) return CHECKSUMFAIL ;
harrypowers 2:4d307eacba27 101 // get rH
harrypowers 2:4d307eacba27 102 *humidity = (int)((bytes[0] << 8) | bytes[1]);
harrypowers 6:f7a2ac66d45a 103 // get temp
harrypowers 2:4d307eacba27 104 if((bytes[2]&128)!=0) tempsign = true;
harrypowers 2:4d307eacba27 105 bytes[2] = bytes[2] & 127;
harrypowers 2:4d307eacba27 106 *temp = (int)((bytes[2] << 8) | bytes[3]);
harrypowers 9:71d7f0caaa68 107 if(tempsign) *temp = -*temp;
harrypowers 2:4d307eacba27 108 for(int i = 0; i < 5; i++) {
harrypowers 2:4d307eacba27 109 data[i]=bytes[i];
harrypowers 2:4d307eacba27 110 }
harrypowers 2:4d307eacba27 111 return 0;
harrypowers 2:4d307eacba27 112 }
harrypowers 2:4d307eacba27 113 void DTH22::getraw()
harrypowers 1:d41fcc541836 114 {
harrypowers 1:d41fcc541836 115 int counter = 0;
harrypowers 1:d41fcc541836 116 for(int i = 0; i < 100; i++) {
harrypowers 2:4d307eacba27 117 timingData[i] = 0;
harrypowers 1:d41fcc541836 118 }
harrypowers 1:d41fcc541836 119 DTH22pin.mode(OpenDrain);
harrypowers 0:92ba381d5432 120 DTH22pin.output();
harrypowers 0:92ba381d5432 121 DTH22pin = 0;
harrypowers 1:d41fcc541836 122 wait_ms(18);
harrypowers 0:92ba381d5432 123 DTH22pin = 1;
harrypowers 0:92ba381d5432 124 wait_us(40);
harrypowers 0:92ba381d5432 125 DTH22pin.input();
harrypowers 1:d41fcc541836 126 for (int i = 0; i < MAXRAWDATA; i++) {
harrypowers 1:d41fcc541836 127 int junk = DTH22pin.read();
harrypowers 1:d41fcc541836 128 if(junk==0) rawdata[i] = false;
harrypowers 1:d41fcc541836 129 if(junk==1) rawdata[i] = true;
harrypowers 1:d41fcc541836 130 wait_us(BITREADTIME);
harrypowers 1:d41fcc541836 131 }
harrypowers 1:d41fcc541836 132 for (int j = 0; j < 100; j++) {
harrypowers 1:d41fcc541836 133 if(counter < MAXRAWDATA) {
harrypowers 2:4d307eacba27 134 timingData[98] = counter ;
harrypowers 2:4d307eacba27 135 timingData[j] = transistionCount(counter);
harrypowers 2:4d307eacba27 136 if(timingData[j]>=MAXBITCOUNT) {
harrypowers 1:d41fcc541836 137 j = 101;
harrypowers 1:d41fcc541836 138 } else {
harrypowers 2:4d307eacba27 139 counter = counter + timingData[j] ;
harrypowers 1:d41fcc541836 140 }
harrypowers 1:d41fcc541836 141 } else {
harrypowers 1:d41fcc541836 142 j = 101;
harrypowers 1:d41fcc541836 143 }
harrypowers 0:92ba381d5432 144 }
harrypowers 0:92ba381d5432 145 }
harrypowers 1:d41fcc541836 146
harrypowers 1:d41fcc541836 147 int DTH22::transistionCount(int index)
harrypowers 1:d41fcc541836 148 {
harrypowers 1:d41fcc541836 149 int count = 0;
harrypowers 1:d41fcc541836 150 bool endbit = !rawdata[index];
harrypowers 1:d41fcc541836 151
harrypowers 1:d41fcc541836 152 for (int i = 1; i < MAXBITCOUNT; i++) {
harrypowers 1:d41fcc541836 153 if(rawdata[index + i]==endbit) {
harrypowers 1:d41fcc541836 154 count = i;
harrypowers 1:d41fcc541836 155 i = MAXBITCOUNT;
harrypowers 1:d41fcc541836 156 }
harrypowers 1:d41fcc541836 157 }
harrypowers 1:d41fcc541836 158 return count;
harrypowers 1:d41fcc541836 159 }
harrypowers 1:d41fcc541836 160
harrypowers 2:4d307eacba27 161 int DTH22::transmissionErrors()
harrypowers 1:d41fcc541836 162 {
harrypowers 2:4d307eacba27 163 if(timingData[83]!=0) {
harrypowers 2:4d307eacba27 164 return DATANOISE ;
harrypowers 2:4d307eacba27 165 }
harrypowers 2:4d307eacba27 166 for(int i = 2; i < 81; i += 2) {
harrypowers 2:4d307eacba27 167 if(!((timingData[i]>=(STARTTRANSBITSIZE - 2))||(timingData[i]<=(STARTTRANSBITSIZE + 2)))) {
harrypowers 2:4d307eacba27 168 return STIMINGFAIL ;
harrypowers 2:4d307eacba27 169 }
harrypowers 2:4d307eacba27 170 }
harrypowers 2:4d307eacba27 171 if(timingData[98]>=(MAXRAWDATA - MAXBITCOUNT)) {
harrypowers 2:4d307eacba27 172 return DATANOISE2 ;
harrypowers 2:4d307eacba27 173 }
harrypowers 2:4d307eacba27 174 return 0;
harrypowers 2:4d307eacba27 175 }
harrypowers 2:4d307eacba27 176
harrypowers 2:4d307eacba27 177 void DTH22::getrawbits()
harrypowers 2:4d307eacba27 178 {
harrypowers 2:4d307eacba27 179 int counter = 0;
harrypowers 2:4d307eacba27 180 bool bitvalue = false;
harrypowers 2:4d307eacba27 181 for(int i=2; i < 81; i += 2) {
harrypowers 2:4d307eacba27 182 if(timingData[i+1]>=timingData[i]) bitvalue = true;
harrypowers 2:4d307eacba27 183 rawdatabits[counter++] = bitvalue;
harrypowers 2:4d307eacba27 184 bitvalue = false;
harrypowers 2:4d307eacba27 185 }
harrypowers 2:4d307eacba27 186 }