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 00003 #include "DHT.h" 00004 00005 #define DHT_DATA_BIT_COUNT 41 00006 00007 DHT::DHT(PinName pin,int DHTtype) { 00008 _pin = pin; 00009 _DHTtype = DHTtype; 00010 _firsttime=true; 00011 } 00012 00013 DHT::~DHT() { 00014 } 00015 00016 int DHT::readData() { 00017 int i, j, retryCount,b; 00018 unsigned int bitTimes[DHT_DATA_BIT_COUNT]; 00019 00020 eError err = ERROR_NONE; 00021 uint32_t currentTime = us_ticker_read() / 1000000; 00022 00023 DigitalInOut DHT_io(_pin); 00024 00025 //Clear the bit buffer. 00026 for (i = 0; i < DHT_DATA_BIT_COUNT; i++) { 00027 bitTimes[i] = 0; 00028 } 00029 00030 if (!_firsttime) { 00031 if (int(currentTime - _lastReadTime) < 2) { 00032 err = ERROR_NO_PATIENCE; 00033 return err; 00034 } 00035 } else { 00036 _firsttime = false; 00037 _lastReadTime = currentTime; 00038 } 00039 retryCount = 0; 00040 00041 do { 00042 if (retryCount > 125) { 00043 err = BUS_BUSY; 00044 return err; 00045 } 00046 retryCount++; 00047 wait_us(2); 00048 } while (DHT_io == 0); 00049 00050 00051 //Set pin as output 00052 DHT_io.output(); 00053 //Put the Line as High impedance 00054 DHT_io = 1; 00055 wait_ms(250); 00056 00057 //Start of message. 00058 DHT_io = 0; 00059 wait_ms(20); 00060 00061 DHT_io = 1; 00062 wait_us(40); 00063 //Set line as input 00064 DHT_io.input(); 00065 00066 wait_us(10); 00067 00068 retryCount = 0; 00069 00070 do { 00071 if (retryCount > 40) { 00072 err = ERROR_NOT_PRESENT; 00073 return err; 00074 } 00075 retryCount++; 00076 wait_us(1); 00077 } while ( DHT_io == 1 ); 00078 00079 if (err != ERROR_NONE) { 00080 return err; 00081 } 00082 00083 for (i = 0; i < 5; i++) { 00084 for (j = 0; j < 8; j++) { 00085 retryCount = 0; 00086 do { 00087 if (retryCount > 78) { 00088 err = ERROR_DATA_TIMEOUT; 00089 return err; 00090 } 00091 retryCount++; 00092 wait_us(1); 00093 } while (DHT_io == 0); 00094 wait_us(30); 00095 bitTimes[i*8 + j] = DHT_io; 00096 int count = 0; 00097 while (DHT_io == 1 && count < 100) { 00098 wait_us(1); 00099 count++; 00100 } 00101 } 00102 } 00103 DHT_io.output(); 00104 DHT_io = 1; 00105 00106 BuildRxBytes(bitTimes); 00107 /*for (i = 0; i < 5; i++) { 00108 b = 0; 00109 for (j = 0; j< 8; j++) { 00110 if ( bitTimes[ i*8+j+1 ] > 0) { 00111 b |= ( 1 << (7-j)); 00112 } 00113 } 00114 DHT_data[i]=b; 00115 }*/ 00116 00117 /*if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { 00118 _lastReadTime = currentTime; 00119 _lastTemperature = CalcTemperature(); 00120 _lastHumidity = CalcHumidity(); 00121 00122 } else { 00123 err = ERROR_CHECKSUM; 00124 }*/ 00125 00126 if(ValidCheckSum()){ 00127 _lastReadTime = currentTime; 00128 } 00129 else{ 00130 err = ERROR_CHECKSUM; 00131 } 00132 00133 return err; 00134 } 00135 00136 float DHT::CalcTemperature() { 00137 int v; 00138 00139 switch (_DHTtype) { 00140 case DHT11: 00141 v = DHT_data[2]; 00142 return float(v); 00143 case DHT22: 00144 v = DHT_data[2] & 0x7F; 00145 v *= 256; 00146 v += DHT_data[3]; 00147 v /= 10; 00148 if (DHT_data[2] & 0x80) 00149 v *= -1; 00150 return float(v); 00151 } 00152 return 0; 00153 } 00154 00155 float DHT::ReadHumidity() { 00156 return _lastHumidity; 00157 } 00158 00159 float DHT::ConvertCelciustoFarenheit(float celsius) { 00160 return celsius * 9 / 5 + 32; 00161 } 00162 00163 float DHT::ConvertCelciustoKelvin(float celsius) { 00164 return celsius + 273.15; 00165 } 00166 00167 // dewPoint function NOAA 00168 // reference: http://wahiduddin.net/calc/density_algorithms.htm 00169 float DHT::CalcdewPoint(float celsius, float humidity) { 00170 float A0= 373.15/(273.15 + celsius); 00171 float SUM = -7.90298 * (A0-1); 00172 SUM += 5.02808 * log10(A0); 00173 SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; 00174 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; 00175 SUM += log10(1013.246); 00176 float VP = pow(10, SUM-3) * humidity; 00177 float T = log(VP/0.61078); // temp var 00178 return (241.88 * T) / (17.558-T); 00179 } 00180 00181 // delta max = 0.6544 wrt dewPoint() 00182 // 5x faster than dewPoint() 00183 // reference: http://en.wikipedia.org/wiki/Dew_point 00184 float DHT::CalcdewPointFast(float celsius, float humidity) 00185 { 00186 float a = 17.271; 00187 float b = 237.7; 00188 float temp = (a * celsius) / (b + celsius) + log(humidity/100); 00189 float Td = (b * temp) / (a - temp); 00190 return Td; 00191 } 00192 00193 float DHT::ReadTemperature(eScale Scale) { 00194 if (Scale == FARENHEIT) 00195 return ConvertCelciustoFarenheit(_lastTemperature); 00196 else if (Scale == KELVIN) 00197 return ConvertCelciustoKelvin(_lastTemperature); 00198 else 00199 return _lastTemperature; 00200 } 00201 00202 float DHT::CalcHumidity() { 00203 int v; 00204 00205 switch (_DHTtype) { 00206 case DHT11: 00207 v = DHT_data[0]; 00208 return float(v); 00209 case DHT22: 00210 v = DHT_data[0]; 00211 v *= 256; 00212 v += DHT_data[1]; 00213 v /= 10; 00214 return float(v); 00215 } 00216 return 0; 00217 } 00218 00219 void DHT::BuildRxBytes(unsigned int * bitTimes ){ 00220 int byteValue = 0; 00221 for (int byteCount = 0; byteCount < 5; byteCount++) { 00222 byteValue = 0; 00223 for (int bit = 0; bit < 8; bit++) { 00224 if ( bitTimes[ (byteCount * 8)+ bit + 1] > 0) { 00225 byteValue |= ( 1 << (7 - bit) ); 00226 } 00227 } 00228 DHT_data[byteCount] = byteValue; 00229 } 00230 } 00231 00232 bool DHT::ValidCheckSum(void){ 00233 bool CorrectCheckSum = false; 00234 00235 if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { 00236 _lastTemperature = CalcTemperature(); 00237 _lastHumidity = CalcHumidity(); 00238 CorrectCheckSum = true; 00239 } 00240 00241 return CorrectCheckSum; 00242 }
Generated on Fri Jul 22 2022 16:05:23 by
1.7.2
