FRDM-K64F, Avnet M14A2A, Grove Shield, to create smart home system. In use with AT&Ts M2x & Flow.
Dependencies: mbed FXOS8700CQ MODSERIAL
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 // 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 } 00120 00121 // uncomment to see the checksum error if it exists 00122 //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]); 00123 data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]; 00124 if (DHT_data[4] == data_valid) { 00125 _lastReadTime = currentTime; 00126 _lastTemperature = CalcTemperature(); 00127 _lastHumidity = CalcHumidity(); 00128 00129 } else { 00130 err = ERROR_CHECKSUM; 00131 } 00132 00133 return err; 00134 00135 } 00136 00137 float DHT::CalcTemperature() 00138 { 00139 int v; 00140 00141 switch (_DHTtype) { 00142 case DHT11: 00143 v = DHT_data[2]; 00144 return float(v); 00145 case DHT22: 00146 v = DHT_data[2] & 0x7F; 00147 v *= 256; 00148 v += DHT_data[3]; 00149 v /= 10; 00150 if (DHT_data[2] & 0x80) 00151 v *= -1; 00152 return float(v); 00153 } 00154 return 0; 00155 } 00156 00157 float DHT::ReadHumidity() 00158 { 00159 return _lastHumidity; 00160 } 00161 00162 float DHT::ConvertCelciustoFarenheit(float const celsius) 00163 { 00164 return celsius * 9 / 5 + 32; 00165 } 00166 00167 float DHT::ConvertCelciustoKelvin(float const celsius) 00168 { 00169 return celsius + 273.15; 00170 } 00171 00172 // dewPoint function NOAA 00173 // reference: http://wahiduddin.net/calc/density_algorithms.htm 00174 float DHT::CalcdewPoint(float const celsius, float const humidity) 00175 { 00176 float A0= 373.15/(273.15 + celsius); 00177 float SUM = -7.90298 * (A0-1); 00178 SUM += 5.02808 * log10(A0); 00179 SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; 00180 SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; 00181 SUM += log10(1013.246); 00182 float VP = pow(10, SUM-3) * humidity; 00183 float T = log(VP/0.61078); // temp var 00184 return (241.88 * T) / (17.558-T); 00185 } 00186 00187 // delta max = 0.6544 wrt dewPoint() 00188 // 5x faster than dewPoint() 00189 // reference: http://en.wikipedia.org/wiki/Dew_point 00190 float DHT::CalcdewPointFast(float const celsius, float const humidity) 00191 { 00192 float a = 17.271; 00193 float b = 237.7; 00194 float temp = (a * celsius) / (b + celsius) + log(humidity/100); 00195 float Td = (b * temp) / (a - temp); 00196 return Td; 00197 } 00198 00199 float DHT::ReadTemperature(eScale Scale) 00200 { 00201 if (Scale == FARENHEIT) 00202 return ConvertCelciustoFarenheit(_lastTemperature); 00203 else if (Scale == KELVIN) 00204 return ConvertCelciustoKelvin(_lastTemperature); 00205 else 00206 return _lastTemperature; 00207 } 00208 00209 float DHT::CalcHumidity() 00210 { 00211 int v; 00212 00213 switch (_DHTtype) { 00214 case DHT11: 00215 v = DHT_data[0]; 00216 return float(v); 00217 case DHT22: 00218 v = DHT_data[0]; 00219 v *= 256; 00220 v += DHT_data[1]; 00221 v /= 10; 00222 return float(v); 00223 } 00224 return 0; 00225 } 00226 00227 00228
Generated on Wed Jul 13 2022 15:25:27 by 1.7.2