Needed to comment out time limitations
Fork of DHT by
Embed:
(wiki syntax)
Show/hide line numbers
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 /*if (!_firsttime) { 00060 if (int(currentTime - _lastReadTime) < 2) { 00061 err = ERROR_NO_PATIENCE; 00062 return err; 00063 } 00064 } else { 00065 _firsttime=false; 00066 _lastReadTime=currentTime; 00067 }*/ 00068 retryCount = 0; 00069 00070 do { 00071 if (retryCount > 125) { 00072 err = BUS_BUSY; 00073 return err; 00074 } 00075 retryCount ++; 00076 wait_us(2); 00077 } while ((DHT_io==0)); 00078 00079 00080 DHT_io.output(); 00081 DHT_io = 0; 00082 wait_ms(18); 00083 DHT_io = 1; 00084 wait_us(40); 00085 DHT_io.input(); 00086 00087 retryCount = 0; 00088 do { 00089 if (retryCount > 40) { 00090 err = ERROR_NOT_PRESENT; 00091 return err; 00092 } 00093 retryCount++; 00094 wait_us(1); 00095 } while ((DHT_io==1)); 00096 00097 if (err != ERROR_NONE) { 00098 return err; 00099 } 00100 00101 wait_us(80); 00102 00103 for (i = 0; i < 5; i++) { 00104 for (j = 0; j < 8; j++) { 00105 00106 retryCount = 0; 00107 do { 00108 if (retryCount > 75) { 00109 err = ERROR_DATA_TIMEOUT; 00110 return err; 00111 } 00112 retryCount++; 00113 wait_us(1); 00114 } while (DHT_io == 0); 00115 wait_us(40); 00116 bitTimes[i*8+j]=DHT_io; 00117 00118 int count = 0; 00119 while (DHT_io == 1 && count < 100) { 00120 wait_us(1); 00121 count++; 00122 } 00123 } 00124 } 00125 DHT_io.output(); 00126 DHT_io = 1; 00127 for (i = 0; i < 5; i++) { 00128 b=0; 00129 for (j=0; j<8; j++) { 00130 if (bitTimes[i*8+j+1] > 0) { 00131 b |= ( 1 << (7-j)); 00132 } 00133 } 00134 DHT_data[i]=b; 00135 } 00136 00137 if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { 00138 _lastReadTime = currentTime; 00139 _lastTemperature=CalcTemperature(); 00140 _lastHumidity=CalcHumidity(); 00141 00142 } else { 00143 err = ERROR_CHECKSUM; 00144 _lastReadTime = currentTime; 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
Generated on Fri Jul 15 2022 21:39:29 by 1.7.2