Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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); // for DHT22 minimum 800µs but for DHT11 etc. 18ms are required 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 00137 00138 00139 00140 float DHT::CalcTemperature() 00141 { 00142 float v; 00143 00144 switch (_DHTtype) { 00145 case DHT11: 00146 v = DHT_data[2]; 00147 return float(v); 00148 case DHT22: 00149 // to calculate temperture of DHT22 in Celsius = (DHT_data[2]*256 + DHT_data[3]) / 10 00150 // if first bit of DHT_data[2] is 1 then the temperature is negative. 00151 00152 00153 v = DHT_data[2] & 0x7F; // mask of the bit for negative temperture 00154 v *= 256; 00155 v += DHT_data[3]; 00156 v /= 10; 00157 if (DHT_data[2] & 0x80) // if first bit of DHT_data[2] is 1 then the temperature is negative. 00158 v *= -1; 00159 v=v/28; 00160 return float(v); 00161 } 00162 return 0; 00163 } 00164 00165 float DHT::ReadHumidity() 00166 { 00167 return _lastHumidity; 00168 } 00169 00170 float DHT::ConvertCelciustoFarenheit(float const celsius) 00171 { 00172 return celsius * 9 / 5 + 32; 00173 } 00174 00175 float DHT::ConvertCelciustoKelvin(float const celsius) 00176 { 00177 return celsius + 273.15f; 00178 } 00179 00180 // dewPoint function NOAA 00181 // reference: http://wahiduddin.net/calc/density_algorithms.htm 00182 float DHT::CalcdewPoint(float const celsius, float const humidity) 00183 { 00184 float A0= 373.15f/(273.15f + celsius); 00185 float SUM = -7.90298 * (A0-1); 00186 SUM += 5.02808f * log10(A0); 00187 SUM += -1.3816e-7 * (pow(10, (11.344f*(1-1/A0)))-1) ; 00188 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; 00189 SUM += log10(1013.246); 00190 float VP = pow(10, SUM-3) * humidity; 00191 float T = log(VP/0.61078f); // temp var 00192 return (241.88f * T) / (17.558f-T); 00193 } 00194 00195 // delta max = 0.6544 wrt dewPoint() 00196 // 5x faster than dewPoint() 00197 // reference: http://en.wikipedia.org/wiki/Dew_point 00198 float DHT::CalcdewPointFast(float const celsius, float const humidity) 00199 { 00200 float a = 17.271; 00201 float b = 237.7; 00202 float temp = (a * celsius) / (b + celsius) + log(humidity/100); 00203 float Td = (b * temp) / (a - temp); 00204 return Td; 00205 } 00206 00207 float DHT::ReadTemperature(eScale Scale) 00208 { 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 { 00219 float v; // needs to be float for DHT22 otherwise only zero after decimal point 00220 switch (_DHTtype) { 00221 case DHT11: 00222 v = DHT_data[0]; 00223 return float(v); 00224 case DHT22: 00225 // to calculate humidity of DHT22 = (DHT_data[0]*256 + DHT_data[1]) / 10 00226 v = DHT_data[0]; 00227 v *= 256; 00228 v += DHT_data[1]; 00229 v /= 10; 00230 return float(v); 00231 } 00232 return 0; 00233 } 00234 00235
Generated on Sun Sep 11 2022 14:22:12 by
1.7.2