AA

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 
00035 #define DHT_DATA_BIT_COUNT 41
00036 Serial xbee1(A0,A1);
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     eError err = ERROR_NONE;
00052     
00053    time_t currentTime = time(NULL);
00054  
00055     DigitalInOut DHT_io(_pin);
00056 
00057     for (i = 0; i < DHT_DATA_BIT_COUNT; i++) {
00058         bitTimes[i] = 0;
00059     }
00060 
00061     if (!_firsttime) {
00062         if (int(currentTime - _lastReadTime) < 2) {
00063             err = ERROR_NO_PATIENCE;
00064             return err;
00065         }
00066     } else {
00067         _firsttime=false;
00068         _lastReadTime=currentTime;
00069     }
00070     retryCount = 0;
00071 
00072     do {
00073         if (retryCount > 125) {
00074             err = BUS_BUSY;
00075             return err;
00076         }
00077         retryCount ++;
00078         wait_us(2);
00079     } while ((DHT_io==0));
00080 
00081 
00082     DHT_io.output();
00083     DHT_io = 0;
00084     wait_ms(18);
00085     DHT_io = 1;
00086     wait_us(40);
00087     DHT_io.input();
00088 
00089     retryCount = 0;
00090     do {
00091         if (retryCount > 40)  {
00092             err = ERROR_NOT_PRESENT;
00093             return err;
00094         }
00095         retryCount++;
00096         wait_us(1);
00097     } while ((DHT_io==1));
00098 
00099     if (err != ERROR_NONE) {
00100         return err;
00101     }
00102 
00103     wait_us(80);
00104 
00105     for (i = 0; i < 5; i++) {
00106         for (j = 0; j < 8; j++) {
00107 
00108             retryCount = 0;
00109             do {
00110                 if (retryCount > 75)  {
00111                     err = ERROR_DATA_TIMEOUT;
00112                     return err;
00113                 }
00114                 retryCount++;
00115                 wait_us(1);
00116             } while (DHT_io == 0);
00117             wait_us(40);
00118             bitTimes[i*8+j]=DHT_io;
00119 
00120             int count = 0;
00121             while (DHT_io == 1 && count < 100) {
00122                 wait_us(1);
00123                 count++;
00124             }
00125         }
00126     }
00127     DHT_io.output();
00128     DHT_io = 1;
00129     for (i = 0; i < 5; i++) {
00130         b=0;
00131         for (j=0; j<8; j++) {
00132             if (bitTimes[i*8+j+1] > 0) {
00133                 b |= ( 1 << (7-j));
00134             }
00135         }
00136         DHT_data[i]=b;
00137     }
00138 
00139     if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) {
00140         _lastReadTime = currentTime;
00141         _lastTemperature=CalcTemperature();
00142         _lastHumidity=CalcHumidity();
00143 
00144     } else {
00145         err = ERROR_CHECKSUM;
00146     }
00147 
00148     return err;
00149 
00150 }
00151 
00152 float DHT::CalcTemperature() {
00153     int v;
00154 
00155     switch (_DHTtype) {
00156         case DHT11:
00157             v = DHT_data[2];
00158             return float(v);
00159         case DHT22:
00160             v = DHT_data[2] & 0x7F;
00161             v *= 256;
00162             v += DHT_data[3];
00163             v /= 10;
00164             if (DHT_data[2] & 0x80)
00165                 v *= -1;
00166             return float(v);
00167     }
00168     return 0;
00169 }
00170 
00171 float DHT::ReadHumidity() {
00172     return _lastHumidity;
00173 }
00174 
00175 float DHT::ConvertCelciustoFarenheit(float celsius) {
00176     return celsius * 9 / 5 + 32;
00177 }
00178 
00179 float DHT::ConvertCelciustoKelvin(float celsius) {
00180     return celsius + 273.15;
00181 }
00182 
00183 // dewPoint function NOAA
00184 // reference: http://wahiduddin.net/calc/density_algorithms.htm
00185 float DHT::CalcdewPoint(float celsius, float humidity) {
00186     float A0= 373.15/(273.15 + celsius);
00187     float SUM = -7.90298 * (A0-1);
00188     SUM += 5.02808 * log10(A0);
00189     SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
00190     SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
00191     SUM += log10(1013.246);
00192     float VP = pow(10, SUM-3) * humidity;
00193     float T = log(VP/0.61078);   // temp var
00194     return (241.88 * T) / (17.558-T);
00195 }
00196 
00197 // delta max = 0.6544 wrt dewPoint()
00198 // 5x faster than dewPoint()
00199 // reference: http://en.wikipedia.org/wiki/Dew_point
00200 float DHT::CalcdewPointFast(float celsius, float humidity)
00201 {
00202         float a = 17.271;
00203         float b = 237.7;
00204         float temp = (a * celsius) / (b + celsius) + log(humidity/100);
00205         float Td = (b * temp) / (a - temp);
00206         return Td;
00207 }
00208 
00209 float DHT::ReadTemperature(eScale Scale) {
00210     if (Scale == FARENHEIT)
00211         return ConvertCelciustoFarenheit(_lastTemperature);
00212     else if (Scale == KELVIN)
00213         return ConvertCelciustoKelvin(_lastTemperature);
00214     else
00215         return _lastTemperature;
00216 }
00217 
00218 float DHT::CalcHumidity() {
00219     int v;
00220 
00221     switch (_DHTtype) {
00222         case DHT11:
00223             v = DHT_data[0];
00224             return float(v);
00225         case DHT22:
00226             v = DHT_data[0];
00227             v *= 256;
00228             v += DHT_data[1];
00229             v /= 10;
00230             return float(v);
00231     }
00232     return 0;
00233 }
00234 
00235