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 if(ValidCheckSum()){ 00108 _lastReadTime = currentTime; 00109 } 00110 else{ 00111 err = ERROR_CHECKSUM; 00112 } 00113 00114 return err; 00115 } 00116 00117 float DHT::CalcTemperature() { 00118 int v; 00119 00120 switch (_DHTtype) { 00121 case DHT11: 00122 v = DHT_data[2]; 00123 return float(v); 00124 case DHT22: 00125 v = DHT_data[2] & 0x7F; 00126 v *= 256; 00127 v += DHT_data[3]; 00128 v /= 10; 00129 if (DHT_data[2] & 0x80) 00130 v *= -1; 00131 return float(v); 00132 } 00133 return 0; 00134 } 00135 00136 float DHT::ReadHumidity() { 00137 return _lastHumidity; 00138 } 00139 00140 float DHT::ConvertCelciustoFarenheit(float celsius) { 00141 return celsius * 9 / 5 + 32; 00142 } 00143 00144 float DHT::ConvertCelciustoKelvin(float celsius) { 00145 return celsius + 273.15; 00146 } 00147 00148 // dewPoint function NOAA 00149 // reference: http://wahiduddin.net/calc/density_algorithms.htm 00150 float DHT::CalcdewPoint(float celsius, float humidity) { 00151 float A0= 373.15/(273.15 + celsius); 00152 float SUM = -7.90298 * (A0-1); 00153 SUM += 5.02808 * log10(A0); 00154 SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; 00155 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; 00156 SUM += log10(1013.246); 00157 float VP = pow(10, SUM-3) * humidity; 00158 float T = log(VP/0.61078); // temp var 00159 return (241.88 * T) / (17.558-T); 00160 } 00161 00162 // delta max = 0.6544 wrt dewPoint() 00163 // 5x faster than dewPoint() 00164 // reference: http://en.wikipedia.org/wiki/Dew_point 00165 float DHT::CalcdewPointFast(float celsius, float humidity) 00166 { 00167 float a = 17.271; 00168 float b = 237.7; 00169 float temp = (a * celsius) / (b + celsius) + log(humidity/100); 00170 float Td = (b * temp) / (a - temp); 00171 return Td; 00172 } 00173 00174 float DHT::ReadTemperature(eScale Scale) { 00175 if (Scale == FARENHEIT) 00176 return ConvertCelciustoFarenheit(_lastTemperature); 00177 else if (Scale == KELVIN) 00178 return ConvertCelciustoKelvin(_lastTemperature); 00179 else 00180 return _lastTemperature; 00181 } 00182 00183 float DHT::CalcHumidity() { 00184 int v; 00185 00186 switch (_DHTtype) { 00187 case DHT11: 00188 v = DHT_data[0]; 00189 return float(v); 00190 case DHT22: 00191 v = DHT_data[0]; 00192 v *= 256; 00193 v += DHT_data[1]; 00194 v /= 10; 00195 return float(v); 00196 } 00197 return 0; 00198 } 00199 00200 void DHT::BuildRxBytes(unsigned int * bitTimes ){ 00201 int byteValue = 0; 00202 for (int byteCount = 0; byteCount < 5; byteCount++) { 00203 byteValue = 0; 00204 for (int bit = 0; bit < 8; bit++) { 00205 if ( bitTimes[ (byteCount * 8)+ bit + 1] > 0) { 00206 byteValue |= ( 1 << (7 - bit) ); 00207 } 00208 } 00209 DHT_data[byteCount] = byteValue; 00210 } 00211 } 00212 00213 bool DHT::ValidCheckSum(void){ 00214 bool CorrectCheckSum = false; 00215 00216 if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { 00217 _lastTemperature = CalcTemperature(); 00218 _lastHumidity = CalcHumidity(); 00219 CorrectCheckSum = true; 00220 } 00221 00222 return CorrectCheckSum; 00223 }
Generated on Tue Jul 12 2022 18:25:25 by
1.7.2