cvb
Dependents: DHT11-HelloWorld1 DHT11-HelloWorld1 STMNucleoF401RE_ExampleCode_08_DHT11
Fork of DHT by
DHT.cpp@1:25c96950b6ed, 2013-07-24 (annotated)
- Committer:
- yihui
- Date:
- Wed Jul 24 06:59:28 2013 +0000
- Revision:
- 1:25c96950b6ed
- Parent:
- 0:9b5b3200688f
- Child:
- 2:df22ddf10d75
Seeeduino Arch + Grove - OLED 128x64 + Grove - Temperature & Humidity Sensor Pro to sense the environment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wimpie | 0:9b5b3200688f | 1 | /* |
Wimpie | 0:9b5b3200688f | 2 | * DHT Library for Digital-output Humidity and Temperature sensors |
Wimpie | 0:9b5b3200688f | 3 | * |
Wimpie | 0:9b5b3200688f | 4 | * Works with DHT11, DHT22 |
Wimpie | 0:9b5b3200688f | 5 | * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio) |
Wimpie | 0:9b5b3200688f | 6 | * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio) |
Wimpie | 0:9b5b3200688f | 7 | * AM2302 , temperature-humidity sensor |
Wimpie | 0:9b5b3200688f | 8 | * HM2303 , Digital-output humidity and temperature sensor |
Wimpie | 0:9b5b3200688f | 9 | * |
Wimpie | 0:9b5b3200688f | 10 | * Copyright (C) Wim De Roeve |
Wimpie | 0:9b5b3200688f | 11 | * based on DHT22 sensor library by HO WING KIT |
Wimpie | 0:9b5b3200688f | 12 | * Arduino DHT11 library |
Wimpie | 0:9b5b3200688f | 13 | * |
Wimpie | 0:9b5b3200688f | 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Wimpie | 0:9b5b3200688f | 15 | * of this software and associated documnetation files (the "Software"), to deal |
Wimpie | 0:9b5b3200688f | 16 | * in the Software without restriction, including without limitation the rights |
Wimpie | 0:9b5b3200688f | 17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Wimpie | 0:9b5b3200688f | 18 | * copies of the Software, and to permit persons to whom the Software is |
Wimpie | 0:9b5b3200688f | 19 | * furished to do so, subject to the following conditions: |
Wimpie | 0:9b5b3200688f | 20 | * |
Wimpie | 0:9b5b3200688f | 21 | * The above copyright notice and this permission notice shall be included in |
Wimpie | 0:9b5b3200688f | 22 | * all copies or substantial portions of the Software. |
Wimpie | 0:9b5b3200688f | 23 | * |
Wimpie | 0:9b5b3200688f | 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Wimpie | 0:9b5b3200688f | 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Wimpie | 0:9b5b3200688f | 26 | * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Wimpie | 0:9b5b3200688f | 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Wimpie | 0:9b5b3200688f | 28 | * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Wimpie | 0:9b5b3200688f | 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Wimpie | 0:9b5b3200688f | 30 | * THE SOFTWARE. |
Wimpie | 0:9b5b3200688f | 31 | */ |
Wimpie | 0:9b5b3200688f | 32 | |
Wimpie | 0:9b5b3200688f | 33 | #include "DHT.h" |
Wimpie | 0:9b5b3200688f | 34 | |
Wimpie | 0:9b5b3200688f | 35 | #define DHT_DATA_BIT_COUNT 41 |
Wimpie | 0:9b5b3200688f | 36 | |
Wimpie | 0:9b5b3200688f | 37 | DHT::DHT(PinName pin,int DHTtype) { |
Wimpie | 0:9b5b3200688f | 38 | _pin = pin; |
Wimpie | 0:9b5b3200688f | 39 | _DHTtype = DHTtype; |
Wimpie | 0:9b5b3200688f | 40 | _firsttime=true; |
Wimpie | 0:9b5b3200688f | 41 | } |
Wimpie | 0:9b5b3200688f | 42 | |
Wimpie | 0:9b5b3200688f | 43 | DHT::~DHT() { |
Wimpie | 0:9b5b3200688f | 44 | } |
Wimpie | 0:9b5b3200688f | 45 | |
Wimpie | 0:9b5b3200688f | 46 | int DHT::readData() { |
Wimpie | 0:9b5b3200688f | 47 | int i, j, retryCount,b; |
Wimpie | 0:9b5b3200688f | 48 | unsigned int bitTimes[DHT_DATA_BIT_COUNT]; |
Wimpie | 0:9b5b3200688f | 49 | |
Wimpie | 0:9b5b3200688f | 50 | eError err = ERROR_NONE; |
Wimpie | 0:9b5b3200688f | 51 | time_t currentTime = time(NULL); |
Wimpie | 0:9b5b3200688f | 52 | |
Wimpie | 0:9b5b3200688f | 53 | DigitalInOut DHT_io(_pin); |
Wimpie | 0:9b5b3200688f | 54 | |
Wimpie | 0:9b5b3200688f | 55 | for (i = 0; i < DHT_DATA_BIT_COUNT; i++) { |
Wimpie | 0:9b5b3200688f | 56 | bitTimes[i] = 0; |
Wimpie | 0:9b5b3200688f | 57 | } |
yihui | 1:25c96950b6ed | 58 | #if 0 |
Wimpie | 0:9b5b3200688f | 59 | if (!_firsttime) { |
Wimpie | 0:9b5b3200688f | 60 | if (int(currentTime - _lastReadTime) < 2) { |
Wimpie | 0:9b5b3200688f | 61 | err = ERROR_NO_PATIENCE; |
Wimpie | 0:9b5b3200688f | 62 | return err; |
Wimpie | 0:9b5b3200688f | 63 | } |
Wimpie | 0:9b5b3200688f | 64 | } else { |
Wimpie | 0:9b5b3200688f | 65 | _firsttime=false; |
Wimpie | 0:9b5b3200688f | 66 | _lastReadTime=currentTime; |
Wimpie | 0:9b5b3200688f | 67 | } |
yihui | 1:25c96950b6ed | 68 | #endif |
Wimpie | 0:9b5b3200688f | 69 | retryCount = 0; |
Wimpie | 0:9b5b3200688f | 70 | |
Wimpie | 0:9b5b3200688f | 71 | do { |
Wimpie | 0:9b5b3200688f | 72 | if (retryCount > 125) { |
Wimpie | 0:9b5b3200688f | 73 | err = BUS_BUSY; |
Wimpie | 0:9b5b3200688f | 74 | return err; |
Wimpie | 0:9b5b3200688f | 75 | } |
Wimpie | 0:9b5b3200688f | 76 | retryCount ++; |
Wimpie | 0:9b5b3200688f | 77 | wait_us(2); |
Wimpie | 0:9b5b3200688f | 78 | } while ((DHT_io==0)); |
Wimpie | 0:9b5b3200688f | 79 | |
Wimpie | 0:9b5b3200688f | 80 | |
Wimpie | 0:9b5b3200688f | 81 | DHT_io.output(); |
Wimpie | 0:9b5b3200688f | 82 | DHT_io = 0; |
Wimpie | 0:9b5b3200688f | 83 | wait_ms(18); |
Wimpie | 0:9b5b3200688f | 84 | DHT_io = 1; |
Wimpie | 0:9b5b3200688f | 85 | wait_us(40); |
Wimpie | 0:9b5b3200688f | 86 | DHT_io.input(); |
Wimpie | 0:9b5b3200688f | 87 | |
Wimpie | 0:9b5b3200688f | 88 | retryCount = 0; |
Wimpie | 0:9b5b3200688f | 89 | do { |
Wimpie | 0:9b5b3200688f | 90 | if (retryCount > 40) { |
Wimpie | 0:9b5b3200688f | 91 | err = ERROR_NOT_PRESENT; |
Wimpie | 0:9b5b3200688f | 92 | return err; |
Wimpie | 0:9b5b3200688f | 93 | } |
Wimpie | 0:9b5b3200688f | 94 | retryCount++; |
Wimpie | 0:9b5b3200688f | 95 | wait_us(1); |
Wimpie | 0:9b5b3200688f | 96 | } while ((DHT_io==1)); |
Wimpie | 0:9b5b3200688f | 97 | |
Wimpie | 0:9b5b3200688f | 98 | if (err != ERROR_NONE) { |
Wimpie | 0:9b5b3200688f | 99 | return err; |
Wimpie | 0:9b5b3200688f | 100 | } |
Wimpie | 0:9b5b3200688f | 101 | |
Wimpie | 0:9b5b3200688f | 102 | wait_us(80); |
Wimpie | 0:9b5b3200688f | 103 | |
Wimpie | 0:9b5b3200688f | 104 | for (i = 0; i < 5; i++) { |
Wimpie | 0:9b5b3200688f | 105 | for (j = 0; j < 8; j++) { |
Wimpie | 0:9b5b3200688f | 106 | |
Wimpie | 0:9b5b3200688f | 107 | retryCount = 0; |
Wimpie | 0:9b5b3200688f | 108 | do { |
Wimpie | 0:9b5b3200688f | 109 | if (retryCount > 75) { |
Wimpie | 0:9b5b3200688f | 110 | err = ERROR_DATA_TIMEOUT; |
Wimpie | 0:9b5b3200688f | 111 | return err; |
Wimpie | 0:9b5b3200688f | 112 | } |
Wimpie | 0:9b5b3200688f | 113 | retryCount++; |
Wimpie | 0:9b5b3200688f | 114 | wait_us(1); |
Wimpie | 0:9b5b3200688f | 115 | } while (DHT_io == 0); |
Wimpie | 0:9b5b3200688f | 116 | wait_us(40); |
Wimpie | 0:9b5b3200688f | 117 | bitTimes[i*8+j]=DHT_io; |
Wimpie | 0:9b5b3200688f | 118 | |
Wimpie | 0:9b5b3200688f | 119 | int count = 0; |
Wimpie | 0:9b5b3200688f | 120 | while (DHT_io == 1 && count < 100) { |
Wimpie | 0:9b5b3200688f | 121 | wait_us(1); |
Wimpie | 0:9b5b3200688f | 122 | count++; |
Wimpie | 0:9b5b3200688f | 123 | } |
Wimpie | 0:9b5b3200688f | 124 | } |
Wimpie | 0:9b5b3200688f | 125 | } |
Wimpie | 0:9b5b3200688f | 126 | DHT_io.output(); |
Wimpie | 0:9b5b3200688f | 127 | DHT_io = 1; |
Wimpie | 0:9b5b3200688f | 128 | for (i = 0; i < 5; i++) { |
Wimpie | 0:9b5b3200688f | 129 | b=0; |
Wimpie | 0:9b5b3200688f | 130 | for (j=0; j<8; j++) { |
Wimpie | 0:9b5b3200688f | 131 | if (bitTimes[i*8+j+1] > 0) { |
Wimpie | 0:9b5b3200688f | 132 | b |= ( 1 << (7-j)); |
Wimpie | 0:9b5b3200688f | 133 | } |
Wimpie | 0:9b5b3200688f | 134 | } |
Wimpie | 0:9b5b3200688f | 135 | DHT_data[i]=b; |
Wimpie | 0:9b5b3200688f | 136 | } |
Wimpie | 0:9b5b3200688f | 137 | |
Wimpie | 0:9b5b3200688f | 138 | if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { |
Wimpie | 0:9b5b3200688f | 139 | _lastReadTime = currentTime; |
Wimpie | 0:9b5b3200688f | 140 | _lastTemperature=CalcTemperature(); |
Wimpie | 0:9b5b3200688f | 141 | _lastHumidity=CalcHumidity(); |
Wimpie | 0:9b5b3200688f | 142 | |
Wimpie | 0:9b5b3200688f | 143 | } else { |
Wimpie | 0:9b5b3200688f | 144 | err = ERROR_CHECKSUM; |
Wimpie | 0:9b5b3200688f | 145 | } |
Wimpie | 0:9b5b3200688f | 146 | |
Wimpie | 0:9b5b3200688f | 147 | return err; |
Wimpie | 0:9b5b3200688f | 148 | |
Wimpie | 0:9b5b3200688f | 149 | } |
Wimpie | 0:9b5b3200688f | 150 | |
Wimpie | 0:9b5b3200688f | 151 | float DHT::CalcTemperature() { |
Wimpie | 0:9b5b3200688f | 152 | int v; |
Wimpie | 0:9b5b3200688f | 153 | |
Wimpie | 0:9b5b3200688f | 154 | switch (_DHTtype) { |
Wimpie | 0:9b5b3200688f | 155 | case DHT11: |
Wimpie | 0:9b5b3200688f | 156 | v = DHT_data[2]; |
Wimpie | 0:9b5b3200688f | 157 | return float(v); |
Wimpie | 0:9b5b3200688f | 158 | case DHT22: |
Wimpie | 0:9b5b3200688f | 159 | v = DHT_data[2] & 0x7F; |
Wimpie | 0:9b5b3200688f | 160 | v *= 256; |
Wimpie | 0:9b5b3200688f | 161 | v += DHT_data[3]; |
Wimpie | 0:9b5b3200688f | 162 | v /= 10; |
Wimpie | 0:9b5b3200688f | 163 | if (DHT_data[2] & 0x80) |
Wimpie | 0:9b5b3200688f | 164 | v *= -1; |
Wimpie | 0:9b5b3200688f | 165 | return float(v); |
Wimpie | 0:9b5b3200688f | 166 | } |
Wimpie | 0:9b5b3200688f | 167 | return 0; |
Wimpie | 0:9b5b3200688f | 168 | } |
Wimpie | 0:9b5b3200688f | 169 | |
Wimpie | 0:9b5b3200688f | 170 | float DHT::ReadHumidity() { |
Wimpie | 0:9b5b3200688f | 171 | return _lastHumidity; |
Wimpie | 0:9b5b3200688f | 172 | } |
Wimpie | 0:9b5b3200688f | 173 | |
Wimpie | 0:9b5b3200688f | 174 | float DHT::ConvertCelciustoFarenheit(float celsius) { |
Wimpie | 0:9b5b3200688f | 175 | return celsius * 9 / 5 + 32; |
Wimpie | 0:9b5b3200688f | 176 | } |
Wimpie | 0:9b5b3200688f | 177 | |
Wimpie | 0:9b5b3200688f | 178 | float DHT::ConvertCelciustoKelvin(float celsius) { |
Wimpie | 0:9b5b3200688f | 179 | return celsius + 273.15; |
Wimpie | 0:9b5b3200688f | 180 | } |
Wimpie | 0:9b5b3200688f | 181 | |
Wimpie | 0:9b5b3200688f | 182 | // dewPoint function NOAA |
Wimpie | 0:9b5b3200688f | 183 | // reference: http://wahiduddin.net/calc/density_algorithms.htm |
Wimpie | 0:9b5b3200688f | 184 | float DHT::CalcdewPoint(float celsius, float humidity) { |
Wimpie | 0:9b5b3200688f | 185 | float A0= 373.15/(273.15 + celsius); |
Wimpie | 0:9b5b3200688f | 186 | float SUM = -7.90298 * (A0-1); |
Wimpie | 0:9b5b3200688f | 187 | SUM += 5.02808 * log10(A0); |
Wimpie | 0:9b5b3200688f | 188 | SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; |
Wimpie | 0:9b5b3200688f | 189 | SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; |
Wimpie | 0:9b5b3200688f | 190 | SUM += log10(1013.246); |
Wimpie | 0:9b5b3200688f | 191 | float VP = pow(10, SUM-3) * humidity; |
Wimpie | 0:9b5b3200688f | 192 | float T = log(VP/0.61078); // temp var |
Wimpie | 0:9b5b3200688f | 193 | return (241.88 * T) / (17.558-T); |
Wimpie | 0:9b5b3200688f | 194 | } |
Wimpie | 0:9b5b3200688f | 195 | |
Wimpie | 0:9b5b3200688f | 196 | // delta max = 0.6544 wrt dewPoint() |
Wimpie | 0:9b5b3200688f | 197 | // 5x faster than dewPoint() |
Wimpie | 0:9b5b3200688f | 198 | // reference: http://en.wikipedia.org/wiki/Dew_point |
Wimpie | 0:9b5b3200688f | 199 | float DHT::CalcdewPointFast(float celsius, float humidity) |
Wimpie | 0:9b5b3200688f | 200 | { |
Wimpie | 0:9b5b3200688f | 201 | float a = 17.271; |
Wimpie | 0:9b5b3200688f | 202 | float b = 237.7; |
Wimpie | 0:9b5b3200688f | 203 | float temp = (a * celsius) / (b + celsius) + log(humidity/100); |
Wimpie | 0:9b5b3200688f | 204 | float Td = (b * temp) / (a - temp); |
Wimpie | 0:9b5b3200688f | 205 | return Td; |
Wimpie | 0:9b5b3200688f | 206 | } |
Wimpie | 0:9b5b3200688f | 207 | |
Wimpie | 0:9b5b3200688f | 208 | float DHT::ReadTemperature(eScale Scale) { |
Wimpie | 0:9b5b3200688f | 209 | if (Scale == FARENHEIT) |
Wimpie | 0:9b5b3200688f | 210 | return ConvertCelciustoFarenheit(_lastTemperature); |
Wimpie | 0:9b5b3200688f | 211 | else if (Scale == KELVIN) |
Wimpie | 0:9b5b3200688f | 212 | return ConvertCelciustoKelvin(_lastTemperature); |
Wimpie | 0:9b5b3200688f | 213 | else |
Wimpie | 0:9b5b3200688f | 214 | return _lastTemperature; |
Wimpie | 0:9b5b3200688f | 215 | } |
Wimpie | 0:9b5b3200688f | 216 | |
Wimpie | 0:9b5b3200688f | 217 | float DHT::CalcHumidity() { |
Wimpie | 0:9b5b3200688f | 218 | int v; |
Wimpie | 0:9b5b3200688f | 219 | |
Wimpie | 0:9b5b3200688f | 220 | switch (_DHTtype) { |
Wimpie | 0:9b5b3200688f | 221 | case DHT11: |
Wimpie | 0:9b5b3200688f | 222 | v = DHT_data[0]; |
Wimpie | 0:9b5b3200688f | 223 | return float(v); |
Wimpie | 0:9b5b3200688f | 224 | case DHT22: |
Wimpie | 0:9b5b3200688f | 225 | v = DHT_data[0]; |
Wimpie | 0:9b5b3200688f | 226 | v *= 256; |
Wimpie | 0:9b5b3200688f | 227 | v += DHT_data[1]; |
Wimpie | 0:9b5b3200688f | 228 | v /= 10; |
Wimpie | 0:9b5b3200688f | 229 | return float(v); |
Wimpie | 0:9b5b3200688f | 230 | } |
Wimpie | 0:9b5b3200688f | 231 | return 0; |
Wimpie | 0:9b5b3200688f | 232 | } |
Wimpie | 0:9b5b3200688f | 233 | |
Wimpie | 0:9b5b3200688f | 234 |