cvb
Dependents: DHT11-HelloWorld1 DHT11-HelloWorld1 STMNucleoF401RE_ExampleCode_08_DHT11
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 Serial out1(USBTX,USBRX); 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 // only 500uS for DHT22 but 18ms for DHT11 00081 (_DHTtype == 11) ? wait_ms(18) : wait(1); 00082 DHT_io = 1; 00083 wait_us(30); 00084 DHT_io.input(); 00085 // wait till the sensor grabs the bus 00086 if (ERROR_NONE != stall(DHT_io, 1, 40)) { 00087 return ERROR_NOT_PRESENT; 00088 } 00089 // sensor should signal low 80us and then hi 80us 00090 if (ERROR_NONE != stall(DHT_io, 0, 100)) { 00091 return ERROR_SYNC_TIMEOUT; 00092 } 00093 if (ERROR_NONE != stall(DHT_io, 1, 100)) { 00094 return ERROR_NO_PATIENCE; 00095 } 00096 // capture the data 00097 for (i = 0; i < 5; i++) { 00098 for (j = 0; j < 8; j++) { 00099 if (ERROR_NONE != stall(DHT_io, 0, 75)) { 00100 return ERROR_DATA_TIMEOUT; 00101 } 00102 // logic 0 is 28us max, 1 is 70us 00103 wait_us(40); 00104 bit_value[i*8+j] = DHT_io; 00105 if (ERROR_NONE != stall(DHT_io, 1, 50)) { 00106 return ERROR_DATA_TIMEOUT; 00107 } 00108 } 00109 } 00110 // store the data 00111 for (i = 0; i < 5; i++) { 00112 b=0; 00113 for (j=0; j<8; j++) { 00114 if (bit_value[i*8+j] == 1) { 00115 b |= (1 << (7-j)); 00116 } 00117 } 00118 DHT_data[i]=b; 00119 out1.printf("data %d",DHT_data[i]); 00120 } 00121 00122 // uncomment to see the checksum error if it exists 00123 //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]); 00124 data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]; 00125 if (DHT_data[4] == data_valid) { 00126 _lastReadTime = currentTime; 00127 _lastTemperature = CalcTemperature(); 00128 _lastHumidity = CalcHumidity(); 00129 00130 } else { 00131 err = ERROR_CHECKSUM; 00132 } 00133 00134 return err; 00135 00136 } 00137 00138 float DHT::CalcTemperature() 00139 { 00140 int v; 00141 00142 switch (_DHTtype) { 00143 case DHT11: 00144 v = DHT_data[2]; 00145 return float(v); 00146 case DHT22: 00147 v = DHT_data[2] & 0x7F; 00148 v *= 256; 00149 v += DHT_data[3]; 00150 v /= 10; 00151 if (DHT_data[2] & 0x80) 00152 v *= -1; 00153 return float(v); 00154 } 00155 return 0; 00156 } 00157 00158 float DHT::ReadHumidity() 00159 { 00160 return _lastHumidity; 00161 } 00162 00163 float DHT::ConvertCelciustoFarenheit(float const celsius) 00164 { 00165 return celsius * 9 / 5 + 32; 00166 } 00167 00168 float DHT::ConvertCelciustoKelvin(float const celsius) 00169 { 00170 return celsius + 273.15; 00171 } 00172 00173 // dewPoint function NOAA 00174 // reference: http://wahiduddin.net/calc/density_algorithms.htm 00175 float DHT::CalcdewPoint(float const celsius, float const humidity) 00176 { 00177 float A0= 373.15/(273.15 + celsius); 00178 float SUM = -7.90298 * (A0-1); 00179 SUM += 5.02808 * log10(A0); 00180 SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; 00181 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; 00182 SUM += log10(1013.246); 00183 float VP = pow(10, SUM-3) * humidity; 00184 float T = log(VP/0.61078); // temp var 00185 return (241.88 * T) / (17.558-T); 00186 } 00187 00188 // delta max = 0.6544 wrt dewPoint() 00189 // 5x faster than dewPoint() 00190 // reference: http://en.wikipedia.org/wiki/Dew_point 00191 float DHT::CalcdewPointFast(float const celsius, float const humidity) 00192 { 00193 float a = 17.271; 00194 float b = 237.7; 00195 float temp = (a * celsius) / (b + celsius) + log(humidity/100); 00196 float Td = (b * temp) / (a - temp); 00197 return Td; 00198 } 00199 00200 float DHT::ReadTemperature(eScale Scale) 00201 { 00202 if (Scale == FARENHEIT) 00203 return ConvertCelciustoFarenheit(_lastTemperature); 00204 else if (Scale == KELVIN) 00205 return ConvertCelciustoKelvin(_lastTemperature); 00206 else 00207 return _lastTemperature; 00208 } 00209 00210 float DHT::CalcHumidity() 00211 { 00212 int v; 00213 00214 switch (_DHTtype) { 00215 case DHT11: 00216 v = DHT_data[0]; 00217 return float(v); 00218 case DHT22: 00219 v = DHT_data[0]; 00220 v *= 256; 00221 v += DHT_data[1]; 00222 v /= 10; 00223 return float(v); 00224 } 00225 return 0; 00226 } 00227 00228
Generated on Sun Jul 17 2022 21:04:00 by 1.7.2