KL25Z internal RTC doesn't work. Original DHT library throws "no patience error"

Dependents:   meteo_station

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