Temp and Humidity sensor device driver.
Dependents: temp_hum IAC_send Smart_Home_sensors
Fork of DHT by
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 40 00036 00037 DHT::DHT(PinName pin, eType DHTtype) 00038 { 00039 _pin = pin; 00040 _DHTtype = DHTtype; 00041 _firsttime = true; 00042 } 00043 00044 DHT::~DHT() 00045 { 00046 00047 } 00048 00049 eError DHT::stall(DigitalInOut &io, int const level, int const max_time) 00050 { 00051 int cnt = 0; 00052 while (level == io) { 00053 if (cnt > max_time) { 00054 return ERROR_NO_PATIENCE; 00055 } 00056 cnt++; 00057 wait_us(1); 00058 } 00059 return ERROR_NONE; 00060 } 00061 00062 eError DHT::readData() 00063 { 00064 uint8_t i = 0, j = 0, b = 0, data_valid = 0; 00065 uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0}; 00066 00067 eError err = ERROR_NONE; 00068 time_t currentTime = time(NULL); 00069 00070 DigitalInOut DHT_io(_pin); 00071 00072 // IO must be in hi state to start 00073 if (ERROR_NONE != stall(DHT_io, 0, 250)) { 00074 return BUS_BUSY; 00075 } 00076 00077 // start the transfer 00078 DHT_io.output(); 00079 DHT_io = 0; 00080 wait_ms(18); 00081 DHT_io = 1; 00082 wait_us(30); 00083 DHT_io.input(); 00084 // wait till the sensor grabs the bus 00085 if (ERROR_NONE != stall(DHT_io, 1, 100)) { 00086 return ERROR_NOT_PRESENT; 00087 } 00088 // sensor should signal low 80us and then hi 80us 00089 if (ERROR_NONE != stall(DHT_io, 0, 100)) { 00090 return ERROR_SYNC_TIMEOUT; 00091 } 00092 if (ERROR_NONE != stall(DHT_io, 1, 100)) { 00093 return ERROR_NO_PATIENCE; 00094 } 00095 // capture the data 00096 for (i = 0; i < 5; i++) { 00097 for (j = 0; j < 8; j++) { 00098 if (ERROR_NONE != stall(DHT_io, 0, 75)) { 00099 return ERROR_DATA_TIMEOUT; 00100 } 00101 // logic 0 is 28us max, 1 is 70us 00102 wait_us(40); 00103 bit_value[i*8+j] = DHT_io; 00104 if (ERROR_NONE != stall(DHT_io, 1, 50)) { 00105 return ERROR_DATA_TIMEOUT; 00106 } 00107 } 00108 } 00109 // store the data 00110 for (i = 0; i < 5; i++) { 00111 b=0; 00112 for (j=0; j<8; j++) { 00113 if (bit_value[i*8+j] == 1) { 00114 b |= (1 << (7-j)); 00115 } 00116 } 00117 DHT_data[i]=b; 00118 } 00119 00120 // uncomment to see the checksum error if it exists 00121 //printf(" 0x%02x + 0x%02x + 0x%02x + 0x%02x = 0x%02x \n", DHT_data[0], DHT_data[1], DHT_data[2], DHT_data[3], DHT_data[4]); 00122 data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]; 00123 if (DHT_data[4] == data_valid) { 00124 _lastReadTime = currentTime; 00125 _lastTemperature = CalcTemperature(); 00126 _lastHumidity = CalcHumidity(); 00127 00128 } else { 00129 err = ERROR_CHECKSUM; 00130 } 00131 00132 return err; 00133 00134 } 00135 00136 float DHT::CalcTemperature() 00137 { 00138 int v; 00139 00140 switch (_DHTtype) { 00141 case DHT11: 00142 v = DHT_data[2]; 00143 return float(v); 00144 case DHT22: 00145 v = DHT_data[2] & 0x7F; 00146 v *= 256; 00147 v += DHT_data[3]; 00148 v /= 10; 00149 if (DHT_data[2] & 0x80) 00150 v *= -1; 00151 return float(v); 00152 } 00153 return 0; 00154 } 00155 00156 float DHT::ReadHumidity() 00157 { 00158 return _lastHumidity; 00159 } 00160 00161 float DHT::ConvertCelciustoFarenheit(float const celsius) 00162 { 00163 return celsius * 9 / 5 + 32; 00164 } 00165 00166 float DHT::ConvertCelciustoKelvin(float const celsius) 00167 { 00168 return celsius + 273.15f; 00169 } 00170 00171 // dewPoint function NOAA 00172 // reference: http://wahiduddin.net/calc/density_algorithms.htm 00173 float DHT::CalcdewPoint(float const celsius, float const humidity) 00174 { 00175 float A0= 373.15f/(273.15f + celsius); 00176 float SUM = -7.90298 * (A0-1); 00177 SUM += 5.02808f * log10(A0); 00178 SUM += -1.3816e-7 * (pow(10, (11.344f*(1-1/A0)))-1) ; 00179 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; 00180 SUM += log10(1013.246); 00181 float VP = pow(10, SUM-3) * humidity; 00182 float T = log(VP/0.61078f); // temp var 00183 return (241.88f * T) / (17.558f-T); 00184 } 00185 00186 // delta max = 0.6544 wrt dewPoint() 00187 // 5x faster than dewPoint() 00188 // reference: http://en.wikipedia.org/wiki/Dew_point 00189 float DHT::CalcdewPointFast(float const celsius, float const humidity) 00190 { 00191 float a = 17.271; 00192 float b = 237.7; 00193 float temp = (a * celsius) / (b + celsius) + log(humidity/100); 00194 float Td = (b * temp) / (a - temp); 00195 return Td; 00196 } 00197 00198 float DHT::ReadTemperature(eScale Scale) 00199 { 00200 if (Scale == FARENHEIT) 00201 return ConvertCelciustoFarenheit(_lastTemperature); 00202 else if (Scale == KELVIN) 00203 return ConvertCelciustoKelvin(_lastTemperature); 00204 else 00205 return _lastTemperature; 00206 } 00207 00208 float DHT::CalcHumidity() 00209 { 00210 int v; 00211 00212 switch (_DHTtype) { 00213 case DHT11: 00214 v = DHT_data[0]; 00215 return float(v); 00216 case DHT22: 00217 v = DHT_data[0]; 00218 v *= 256; 00219 v += DHT_data[1]; 00220 v /= 10; 00221 return float(v); 00222 } 00223 return 0; 00224 } 00225 00226
Generated on Wed Jul 13 2022 09:36:27 by
