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:
Tue Dec 03 09:58:19 2013 +0000
Revision:
6:f7a2ac66d45a
Parent:
2:4d307eacba27
Child:
9:71d7f0caaa68
just formatting updates and small comment changes

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 2:4d307eacba27 41 if(tempsign) *temp = *temp * -1;
harrypowers 0:92ba381d5432 42 return 0;
harrypowers 0:92ba381d5432 43 }
harrypowers 1:d41fcc541836 44
harrypowers 2:4d307eacba27 45 int DTH22::testing(bool *bits,signed short int *data,int *temp,int *humidity)
harrypowers 2:4d307eacba27 46 {
harrypowers 2:4d307eacba27 47 signed short int bytes[5];
harrypowers 2:4d307eacba27 48 int checksumtest = 0;
harrypowers 2:4d307eacba27 49 bool tempsign = false;
harrypowers 2:4d307eacba27 50 DTH22::getraw();
harrypowers 2:4d307eacba27 51 DTH22::getrawbits();
harrypowers 2:4d307eacba27 52 for(int i = 0; i < 40; i ++) {
harrypowers 2:4d307eacba27 53 bits[i]=rawdatabits[i];
harrypowers 2:4d307eacba27 54 }
harrypowers 2:4d307eacba27 55 if(transmissionErrors()!=0) return transmissionErrors();
harrypowers 2:4d307eacba27 56 // group bits into bytes
harrypowers 2:4d307eacba27 57 for(int j = 0; j < 5; j++) {
harrypowers 2:4d307eacba27 58 bytes[j] = 0;
harrypowers 2:4d307eacba27 59 for(int i = 0; i < 8; i++) {
harrypowers 2:4d307eacba27 60 if(rawdatabits[(i+(j*8))]==true) bytes[j]=bytes[j] | 1;
harrypowers 2:4d307eacba27 61 if(i!=7) bytes[j]=bytes[j] << 1;
harrypowers 2:4d307eacba27 62 }
harrypowers 2:4d307eacba27 63 }
harrypowers 2:4d307eacba27 64 // checksum test
harrypowers 6:f7a2ac66d45a 65 for(int i = 0; i < 4; i++) {
harrypowers 2:4d307eacba27 66 checksumtest += bytes[i];
harrypowers 2:4d307eacba27 67 }
harrypowers 2:4d307eacba27 68 checksumtest = checksumtest & 255; // only want the last byte for test
harrypowers 2:4d307eacba27 69 if(checksumtest!=bytes[4]) return CHECKSUMFAIL ;
harrypowers 2:4d307eacba27 70 // get rH
harrypowers 2:4d307eacba27 71 *humidity = (int)((bytes[0] << 8) | bytes[1]);
harrypowers 6:f7a2ac66d45a 72 // get temp
harrypowers 2:4d307eacba27 73 if((bytes[2]&128)!=0) tempsign = true;
harrypowers 2:4d307eacba27 74 bytes[2] = bytes[2] & 127;
harrypowers 2:4d307eacba27 75 *temp = (int)((bytes[2] << 8) | bytes[3]);
harrypowers 2:4d307eacba27 76 if(tempsign) *temp = *temp * -1;
harrypowers 2:4d307eacba27 77 for(int i = 0; i < 5; i++) {
harrypowers 2:4d307eacba27 78 data[i]=bytes[i];
harrypowers 2:4d307eacba27 79 }
harrypowers 2:4d307eacba27 80 return 0;
harrypowers 2:4d307eacba27 81 }
harrypowers 2:4d307eacba27 82 void DTH22::getraw()
harrypowers 1:d41fcc541836 83 {
harrypowers 1:d41fcc541836 84 int counter = 0;
harrypowers 1:d41fcc541836 85 for(int i = 0; i < 100; i++) {
harrypowers 2:4d307eacba27 86 timingData[i] = 0;
harrypowers 1:d41fcc541836 87 }
harrypowers 1:d41fcc541836 88 DTH22pin.mode(OpenDrain);
harrypowers 0:92ba381d5432 89 DTH22pin.output();
harrypowers 0:92ba381d5432 90 DTH22pin = 0;
harrypowers 1:d41fcc541836 91 wait_ms(18);
harrypowers 0:92ba381d5432 92 DTH22pin = 1;
harrypowers 0:92ba381d5432 93 wait_us(40);
harrypowers 0:92ba381d5432 94 DTH22pin.input();
harrypowers 1:d41fcc541836 95 for (int i = 0; i < MAXRAWDATA; i++) {
harrypowers 1:d41fcc541836 96 int junk = DTH22pin.read();
harrypowers 1:d41fcc541836 97 if(junk==0) rawdata[i] = false;
harrypowers 1:d41fcc541836 98 if(junk==1) rawdata[i] = true;
harrypowers 1:d41fcc541836 99 wait_us(BITREADTIME);
harrypowers 1:d41fcc541836 100 }
harrypowers 1:d41fcc541836 101 for (int j = 0; j < 100; j++) {
harrypowers 1:d41fcc541836 102 if(counter < MAXRAWDATA) {
harrypowers 2:4d307eacba27 103 timingData[98] = counter ;
harrypowers 2:4d307eacba27 104 timingData[j] = transistionCount(counter);
harrypowers 2:4d307eacba27 105 if(timingData[j]>=MAXBITCOUNT) {
harrypowers 1:d41fcc541836 106 j = 101;
harrypowers 1:d41fcc541836 107 } else {
harrypowers 2:4d307eacba27 108 counter = counter + timingData[j] ;
harrypowers 1:d41fcc541836 109 }
harrypowers 1:d41fcc541836 110 } else {
harrypowers 2:4d307eacba27 111 //timingData[j] = counter;
harrypowers 1:d41fcc541836 112 j = 101;
harrypowers 1:d41fcc541836 113 }
harrypowers 0:92ba381d5432 114 }
harrypowers 0:92ba381d5432 115 }
harrypowers 1:d41fcc541836 116
harrypowers 1:d41fcc541836 117 int DTH22::transistionCount(int index)
harrypowers 1:d41fcc541836 118 {
harrypowers 1:d41fcc541836 119 int count = 0;
harrypowers 1:d41fcc541836 120 bool endbit = !rawdata[index];
harrypowers 1:d41fcc541836 121
harrypowers 1:d41fcc541836 122 for (int i = 1; i < MAXBITCOUNT; i++) {
harrypowers 1:d41fcc541836 123 if(rawdata[index + i]==endbit) {
harrypowers 1:d41fcc541836 124 count = i;
harrypowers 1:d41fcc541836 125 i = MAXBITCOUNT;
harrypowers 1:d41fcc541836 126 }
harrypowers 1:d41fcc541836 127 }
harrypowers 1:d41fcc541836 128 return count;
harrypowers 1:d41fcc541836 129 }
harrypowers 1:d41fcc541836 130
harrypowers 2:4d307eacba27 131 int DTH22::transmissionErrors()
harrypowers 1:d41fcc541836 132 {
harrypowers 2:4d307eacba27 133 if(timingData[83]!=0) {
harrypowers 2:4d307eacba27 134 return DATANOISE ;
harrypowers 2:4d307eacba27 135 }
harrypowers 2:4d307eacba27 136 for(int i = 2; i < 81; i += 2) {
harrypowers 2:4d307eacba27 137 if(!((timingData[i]>=(STARTTRANSBITSIZE - 2))||(timingData[i]<=(STARTTRANSBITSIZE + 2)))) {
harrypowers 2:4d307eacba27 138 return STIMINGFAIL ;
harrypowers 2:4d307eacba27 139 }
harrypowers 2:4d307eacba27 140 }
harrypowers 2:4d307eacba27 141 if(timingData[98]>=(MAXRAWDATA - MAXBITCOUNT)) {
harrypowers 2:4d307eacba27 142 return DATANOISE2 ;
harrypowers 2:4d307eacba27 143 }
harrypowers 2:4d307eacba27 144 return 0;
harrypowers 2:4d307eacba27 145 }
harrypowers 2:4d307eacba27 146
harrypowers 2:4d307eacba27 147 void DTH22::getrawbits()
harrypowers 2:4d307eacba27 148 {
harrypowers 2:4d307eacba27 149 int counter = 0;
harrypowers 2:4d307eacba27 150 bool bitvalue = false;
harrypowers 2:4d307eacba27 151 for(int i=2; i < 81; i += 2) {
harrypowers 2:4d307eacba27 152 if(timingData[i+1]>=timingData[i]) bitvalue = true;
harrypowers 2:4d307eacba27 153 rawdatabits[counter++] = bitvalue;
harrypowers 2:4d307eacba27 154 bitvalue = false;
harrypowers 2:4d307eacba27 155 }
harrypowers 2:4d307eacba27 156 }