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 01 23:21:20 2013 +0000
Revision:
0:92ba381d5432
Child:
1:d41fcc541836
first commit ... its a start anyways!

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 0:92ba381d5432 5 //DigitalInOut DTH22pin(DATAsignal);
harrypowers 0:92ba381d5432 6 }
harrypowers 0:92ba381d5432 7
harrypowers 0:92ba381d5432 8 DTH22::~DTH22(){}
harrypowers 0:92ba381d5432 9
harrypowers 0:92ba381d5432 10 int DTH22::getTH(int *temp,int *humidity){
harrypowers 0:92ba381d5432 11 return 0;
harrypowers 0:92ba381d5432 12 }
harrypowers 0:92ba381d5432 13
harrypowers 0:92ba381d5432 14 void DTH22::testit(bool *data){
harrypowers 0:92ba381d5432 15 DTH22pin.output();
harrypowers 0:92ba381d5432 16 DTH22pin = 0;
harrypowers 0:92ba381d5432 17 wait_ms(1);
harrypowers 0:92ba381d5432 18 DTH22pin = 1;
harrypowers 0:92ba381d5432 19 wait_us(40);
harrypowers 0:92ba381d5432 20 DTH22pin.input();
harrypowers 0:92ba381d5432 21 for (int i = 0; i < 1500; i++){
harrypowers 0:92ba381d5432 22 rawdata[i] = (bool)DTH22pin.read();
harrypowers 0:92ba381d5432 23 }
harrypowers 0:92ba381d5432 24 data = &rawdata[0];
harrypowers 0:92ba381d5432 25 }