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 41 00036 00037 DHT::DHT(PinName pin,int DHTtype) 00038 { 00039 _pin = pin; 00040 _DHTtype = DHTtype; 00041 _firsttime=true; 00042 } 00043 00044 DHT::~DHT() 00045 { 00046 } 00047 00048 int DHT::readData() 00049 { 00050 int i, j, retryCount,b; 00051 unsigned int bitTimes[DHT_DATA_BIT_COUNT]; 00052 00053 eError err = ERROR_NONE; 00054 time_t currentTime = time(NULL); 00055 00056 DigitalInOut DHT_io(_pin); 00057 00058 for (i = 0; i < DHT_DATA_BIT_COUNT; i++) { 00059 bitTimes[i] = 0; 00060 } 00061 00062 if (!_firsttime) { 00063 if (int(currentTime - _lastReadTime) < 2) { 00064 err = ERROR_NO_PATIENCE; 00065 return err; 00066 } 00067 } else { 00068 _firsttime=false; 00069 _lastReadTime=currentTime; 00070 } 00071 retryCount = 0; 00072 00073 do { 00074 if (retryCount > 125) { 00075 err = BUS_BUSY; 00076 return err; 00077 } 00078 retryCount ++; 00079 wait_us(2); 00080 } while ((DHT_io==0)); 00081 00082 00083 DHT_io.output(); 00084 DHT_io = 0; 00085 wait_ms(18); 00086 DHT_io = 1; 00087 wait_us(40); 00088 DHT_io.input(); 00089 00090 retryCount = 0; 00091 do { 00092 if (retryCount > 40) { 00093 err = ERROR_NOT_PRESENT; 00094 return err; 00095 } 00096 retryCount++; 00097 wait_us(1); 00098 } while ((DHT_io==1)); 00099 00100 if (err != ERROR_NONE) { 00101 return err; 00102 } 00103 00104 wait_us(80); 00105 00106 for (i = 0; i < 5; i++) { 00107 for (j = 0; j < 8; j++) { 00108 00109 retryCount = 0; 00110 do { 00111 if (retryCount > 75) { 00112 err = ERROR_DATA_TIMEOUT; 00113 return err; 00114 } 00115 retryCount++; 00116 wait_us(1); 00117 } while (DHT_io == 0); 00118 wait_us(40); 00119 bitTimes[i*8+j]=DHT_io; 00120 00121 int count = 0; 00122 while (DHT_io == 1 && count < 100) { 00123 wait_us(1); 00124 count++; 00125 } 00126 } 00127 } 00128 DHT_io.output(); 00129 DHT_io = 1; 00130 for (i = 0; i < 5; i++) { 00131 b=0; 00132 for (j=0; j<8; j++) { 00133 if (bitTimes[i*8+j+1] > 0) { 00134 b |= ( 1 << (7-j)); 00135 } 00136 } 00137 DHT_data[i]=b; 00138 } 00139 00140 if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { 00141 _lastReadTime = currentTime; 00142 _lastTemperature=CalcTemperature(); 00143 _lastHumidity=CalcHumidity(); 00144 00145 } else { 00146 err = ERROR_CHECKSUM; 00147 } 00148 00149 return err; 00150 00151 } 00152 00153 float DHT::CalcTemperature() 00154 { 00155 int v; 00156 00157 switch (_DHTtype) { 00158 case DHT11: 00159 v = DHT_data[2]; 00160 return float(v); 00161 case DHT22: 00162 v = DHT_data[2] & 0x7F; 00163 v *= 256; 00164 v += DHT_data[3]; 00165 v /= 10; 00166 if (DHT_data[2] & 0x80) 00167 v *= -1; 00168 return float(v); 00169 } 00170 return 0; 00171 } 00172 00173 float DHT::ReadHumidity() 00174 { 00175 return _lastHumidity; 00176 } 00177 00178 float DHT::ConvertCelciustoFarenheit(float celsius) 00179 { 00180 return celsius * 9 / 5 + 32; 00181 } 00182 00183 float DHT::ConvertCelciustoKelvin(float celsius) 00184 { 00185 return celsius + (float)273.15; 00186 } 00187 00188 // dewPoint function NOAA 00189 // reference: http://wahiduddin.net/calc/density_algorithms.htm 00190 float DHT::CalcdewPoint(float celsius, float humidity) 00191 { 00192 float A0= (float)373.15/((float)273.15 + celsius); 00193 float SUM = (float)-7.90298 * (A0-1); 00194 SUM += (float)5.02808 * log10(A0); 00195 SUM += -1.3816e-7 * (pow(10, ((float)11.344*(1-1/A0)))-1) ; 00196 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; 00197 SUM += log10(1013.246); 00198 float VP = pow(10, SUM-3) * humidity; 00199 float T = log(VP/(float)0.61078); // temp var 00200 return ((float)241.88 * T) / ((float)17.558-T); 00201 } 00202 00203 // delta max = 0.6544 wrt dewPoint() 00204 // 5x faster than dewPoint() 00205 // reference: http://en.wikipedia.org/wiki/Dew_point 00206 float DHT::CalcdewPointFast(float celsius, float humidity) 00207 { 00208 float a = 17.271; 00209 float b = 237.7; 00210 float temp = (a * celsius) / (b + celsius) + log(humidity/100); 00211 float Td = (b * temp) / (a - temp); 00212 return Td; 00213 } 00214 00215 float DHT::ReadTemperature(eScale Scale) 00216 { 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 { 00227 int v; 00228 00229 switch (_DHTtype) { 00230 case DHT11: 00231 v = DHT_data[0]; 00232 return float(v); 00233 case DHT22: 00234 v = DHT_data[0]; 00235 v *= 256; 00236 v += DHT_data[1]; 00237 v /= 10; 00238 return float(v); 00239 } 00240 return 0; 00241 }
Generated on Wed Jul 13 2022 01:56:24 by
1.7.2