DHT library
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 //extern Serial pc; 00036 // was 41 00037 #define DHT_DATA_BIT_COUNT 40 00038 00039 DHT::DHT(PinName pin,int DHTtype) { 00040 _pin = pin; 00041 _DHTtype = DHTtype; 00042 _firsttime=true; 00043 } 00044 00045 DHT::~DHT() { 00046 } 00047 00048 int DHT::readData() { 00049 int i, j, retryCount,b; 00050 unsigned int bitTimes[DHT_DATA_BIT_COUNT]; 00051 00052 eError err = ERROR_NONE; 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 DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]-1) & 0xFF)) { 00141 _lastReadTime = currentTime; 00142 _lastTemperature=CalcTemperature(); 00143 _lastHumidity=CalcHumidity(); 00144 00145 } else { 00146 /*for( int i=0; i < 5; i++ ) { 00147 pc.printf("%02X ",DHT_data[i]); 00148 } 00149 pc.printf("\n"); 00150 pc.printf("%04X\n",(DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3])); 00151 */ 00152 err = ERROR_CHECKSUM; 00153 } 00154 00155 return err; 00156 00157 } 00158 00159 float DHT::CalcTemperature() { 00160 float v; 00161 00162 switch (_DHTtype) { 00163 case DHT11: 00164 v = DHT_data[2]; 00165 return v; 00166 case DHT22: 00167 v = DHT_data[2] & 0x7F; 00168 v *= 256; 00169 v += DHT_data[3]; 00170 v /= 10; 00171 if (DHT_data[2] & 0x80) 00172 v *= -1; 00173 return v; 00174 } 00175 return 0; 00176 } 00177 00178 float DHT::ReadHumidity() { 00179 return _lastHumidity; 00180 } 00181 00182 float DHT::ConvertCelciustoFarenheit(float celsius) { 00183 return celsius * 9 / 5 + 32.0f; 00184 } 00185 00186 float DHT::ConvertCelciustoKelvin(float celsius) { 00187 return celsius + 273.15f; 00188 } 00189 00190 // dewPoint function NOAA 00191 // reference: http://wahiduddin.net/calc/density_algorithms.htm 00192 float DHT::CalcdewPoint(float celsius, float humidity) { 00193 float A0= 373.15f/(273.15f + celsius); 00194 float SUM = -7.90298f * (A0-1); 00195 SUM += 5.02808f * log10(A0); 00196 SUM += -1.3816e-7 * (pow(10, (11.344f*(1-1/A0)))-1) ; 00197 SUM += 8.1328e-3f * (pow(10,(-3.49149f*(A0-1.0f)))-1) ; 00198 SUM += log10(1013.246); 00199 float VP = pow(10, SUM-3) * humidity; 00200 float T = log(VP/0.61078f); // temp var 00201 return (241.88f * T) / (17.558f-T); 00202 } 00203 00204 // delta max = 0.6544 wrt dewPoint() 00205 // 5x faster than dewPoint() 00206 // reference: http://en.wikipedia.org/wiki/Dew_point 00207 float DHT::CalcdewPointFast(float celsius, float humidity) 00208 { 00209 float a = 17.271; 00210 float b = 237.7; 00211 float temp = (a * celsius) / (b + celsius) + log(humidity/100); 00212 float Td = (b * temp) / (a - temp); 00213 return Td; 00214 } 00215 00216 float DHT::ReadTemperature(eScale Scale) { 00217 if (Scale == FARENHEIT) 00218 return ConvertCelciustoFarenheit(_lastTemperature); 00219 else if (Scale == KELVIN) 00220 return ConvertCelciustoKelvin(_lastTemperature); 00221 else 00222 return _lastTemperature; 00223 } 00224 00225 float DHT::CalcHumidity() { 00226 float v; 00227 00228 switch (_DHTtype) { 00229 case DHT11: 00230 v = DHT_data[0]; 00231 return v; 00232 case DHT22: 00233 v = DHT_data[0]; 00234 v *= 256; 00235 v += DHT_data[1]; 00236 v /= 10; 00237 return v; 00238 } 00239 return 0; 00240 } 00241 00242
Generated on Tue Jul 19 2022 13:39:29 by 1.7.2