DHT

Fork of DHT by Wim De Roeve

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DHT.cpp Source File

DHT.cpp

00001 /*
00002  *  DHT Library for  Digital-output Humidity and Temperature sensors
00003  *
00004  *  Works with DHT11, DHT22
00005  *             SEN11301P,  Grove - Temperature&Humidity Sensor     (Seeed Studio)
00006  *             SEN51035P,  Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
00007  *             AM2302   ,  temperature-humidity sensor
00008  *             HM2303   ,  Digital-output humidity and temperature sensor
00009  *
00010  *  Copyright (C) Wim De Roeve
00011  *                based on DHT22 sensor library by HO WING KIT
00012  *                Arduino DHT11 library 
00013  *
00014  * Permission is hereby granted, free of charge, to any person obtaining a copy
00015  * of this software and associated documnetation files (the "Software"), to deal
00016  * in the Software without restriction, including without limitation the rights
00017  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00018  * copies of the Software, and to permit persons to  whom the Software is
00019  * furished to do so, subject to the following conditions:
00020  *
00021  * The above copyright notice and this permission notice shall be included in
00022  * all copies or substantial portions of the Software.
00023  *
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00025  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00026  * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00027  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00028  * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00029  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00030  * THE SOFTWARE.
00031  */
00032 
00033 #include "DHT.h"
00034 #define DHT_DATA_BIT_COUNT 41
00035 DigitalOut myled1(LED2);
00036 
00037 
00038 DHT::DHT(PinName pin,int DHTtype) {
00039     _pin = pin;
00040     _DHTtype = DHTtype;
00041     _firsttime=true;
00042 }
00043 
00044 DHT::~DHT() {
00045 }
00046 
00047 int DHT::readData() {
00048     int i, j, retryCount,b;
00049     unsigned int bitTimes[DHT_DATA_BIT_COUNT];
00050 
00051     
00052     //myled1=0;
00053     eError err = ERROR_NONE;
00054     time_t currentTime = time(NULL);
00055 
00056     DigitalInOut DHT_io(_pin);
00057 
00058     for (i = 0; i < DHT_DATA_BIT_COUNT; i++) {
00059         bitTimes[i] = 0;
00060     }
00061 
00062 //    if (!_firsttime) {
00063 //        if (int(currentTime - _lastReadTime) < 2) {
00064 //            err = ERROR_NO_PATIENCE;
00065 //            return err;
00066 //        }
00067 //    } else {
00068 //        _firsttime=false;
00069 //        _lastReadTime=currentTime;
00070 //    }
00071     retryCount = 0;
00072 
00073     do {
00074         if (retryCount > 125) {
00075             err = BUS_BUSY;
00076             return err;
00077         }
00078         retryCount ++;
00079         wait_us(2);
00080     } while ((DHT_io==0));
00081 
00082 
00083     DHT_io.output();
00084     DHT_io = 0;
00085     
00086     
00087     
00088 //    wait(.1);
00089 //    DHT_io=1;
00090 //    wait(.1);
00091 //    DHT_io=0;
00092 //    return 0;
00093     
00094     
00095     
00096     
00097     
00098     
00099     
00100     wait_ms(18);
00101     DHT_io = 1;
00102     wait_us(40);
00103     DHT_io.input();
00104 
00105     retryCount = 0;
00106     do {
00107         if (retryCount > 40)  {
00108             err = ERROR_NOT_PRESENT;
00109             return err;
00110         }
00111         retryCount++;
00112         wait_us(1);
00113     } while ((DHT_io==1));
00114 
00115     if (err != ERROR_NONE) {
00116         return err;
00117     }
00118 
00119     wait_us(80);
00120 
00121     for (i = 0; i < 5; i++) {
00122         for (j = 0; j < 8; j++) {
00123 
00124             retryCount = 0;
00125             do {
00126                 if (retryCount > 75)  {
00127                     err = ERROR_DATA_TIMEOUT;
00128                     return err;
00129                 }
00130                 retryCount++;
00131                 wait_us(1);
00132             } while (DHT_io == 0);
00133             wait_us(40);
00134             bitTimes[i*8+j]=DHT_io;
00135 
00136             int count = 0;
00137             while (DHT_io == 1 && count < 100) {
00138                 wait_us(1);
00139                 count++;
00140             }
00141         }
00142     }
00143     DHT_io.output();
00144     DHT_io = 1;
00145     for (i = 0; i < 5; i++) {
00146         b=0;
00147         for (j=0; j<8; j++) {
00148             if (bitTimes[i*8+j+1] > 0) {
00149                 b |= ( 1 << (7-j));
00150             }
00151         }
00152         DHT_data[i]=b;
00153     }
00154 
00155    if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) {
00156         _lastReadTime = currentTime;
00157         _lastTemperature=CalcTemperature();
00158         _lastHumidity=CalcHumidity();
00159 
00160     } else {
00161         err = ERROR_CHECKSUM;
00162    }
00163 
00164     return err;
00165 
00166 }
00167 
00168 float DHT::CalcTemperature() {
00169     int v;
00170 
00171     switch (_DHTtype) {
00172         case DHT11:
00173             v = DHT_data[2];
00174             return float(v);
00175         case DHT22:
00176             v = DHT_data[2] & 0x7F;
00177             v *= 256;
00178             v += DHT_data[3];
00179             v /= 10;
00180             if (DHT_data[2] & 0x80)
00181                 v *= -1;
00182             return float(v);
00183     }
00184     return 0;
00185 }
00186 
00187 float DHT::ReadHumidity() {
00188     return _lastHumidity;
00189 }
00190 
00191 float DHT::ConvertCelciustoFarenheit(float celsius) {
00192     return celsius * 9 / 5 + 32;
00193 }
00194 
00195 float DHT::ConvertCelciustoKelvin(float celsius) {
00196     return celsius + 273.15;
00197 }
00198 
00199 // dewPoint function NOAA
00200 // reference: http://wahiduddin.net/calc/density_algorithms.htm
00201 float DHT::CalcdewPoint(float celsius, float humidity) {
00202     float A0= 373.15/(273.15 + celsius);
00203     float SUM = -7.90298 * (A0-1);
00204     SUM += 5.02808 * log10(A0);
00205     SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
00206     SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
00207     SUM += log10(1013.246);
00208     float VP = pow(10, SUM-3) * humidity;
00209     float T = log(VP/0.61078);   // temp var
00210     return (241.88 * T) / (17.558-T);
00211 }
00212 
00213 // delta max = 0.6544 wrt dewPoint()
00214 // 5x faster than dewPoint()
00215 // reference: http://en.wikipedia.org/wiki/Dew_point
00216 float DHT::CalcdewPointFast(float celsius, float humidity)
00217 {
00218         float a = 17.271;
00219         float b = 237.7;
00220         float temp = (a * celsius) / (b + celsius) + log(humidity/100);
00221         float Td = (b * temp) / (a - temp);
00222         return Td;
00223 }
00224 
00225 float DHT::ReadTemperature(eScale Scale) {
00226     if (Scale == FARENHEIT)
00227         return ConvertCelciustoFarenheit(_lastTemperature);
00228     else if (Scale == KELVIN)
00229         return ConvertCelciustoKelvin(_lastTemperature);
00230     else
00231         return _lastTemperature;
00232 }
00233 
00234 float DHT::CalcHumidity() {
00235     int v;
00236 
00237     switch (_DHTtype) {
00238         case DHT11:
00239             v = DHT_data[0];
00240             return float(v);
00241         case DHT22:
00242             v = DHT_data[0];
00243             v *= 256;
00244             v += DHT_data[1];
00245             v /= 10;
00246             return float(v);
00247     }
00248     return 0;
00249 }
00250 
00251