DHT1
Dependents: greenhouse_proj SmartCampus_DHT OLED_graph_Hongli
Fork of DHT by
DHT.cpp@2:df22ddf10d75, 2014-08-15 (annotated)
- Committer:
- sam_grove
- Date:
- Fri Aug 15 20:55:43 2014 +0000
- Revision:
- 2:df22ddf10d75
- Parent:
- 1:25c96950b6ed
- Child:
- 3:6937e130feca
- Child:
- 4:1e78a462b211
Update reading sensor to eliminate the checksum error (error 6). Many other changes for good coding practices
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 |
sam_grove | 2:df22ddf10d75 | 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 | |
sam_grove | 2:df22ddf10d75 | 35 | #define DHT_DATA_BIT_COUNT 40 |
Wimpie | 0:9b5b3200688f | 36 | |
sam_grove | 2:df22ddf10d75 | 37 | DHT::DHT(PinName pin, eType DHTtype) |
sam_grove | 2:df22ddf10d75 | 38 | { |
Wimpie | 0:9b5b3200688f | 39 | _pin = pin; |
Wimpie | 0:9b5b3200688f | 40 | _DHTtype = DHTtype; |
sam_grove | 2:df22ddf10d75 | 41 | _firsttime = true; |
sam_grove | 2:df22ddf10d75 | 42 | } |
sam_grove | 2:df22ddf10d75 | 43 | |
sam_grove | 2:df22ddf10d75 | 44 | DHT::~DHT() |
sam_grove | 2:df22ddf10d75 | 45 | { |
sam_grove | 2:df22ddf10d75 | 46 | |
Wimpie | 0:9b5b3200688f | 47 | } |
Wimpie | 0:9b5b3200688f | 48 | |
sam_grove | 2:df22ddf10d75 | 49 | eError DHT::stall(DigitalInOut &io, int const level, int const max_time) |
sam_grove | 2:df22ddf10d75 | 50 | { |
sam_grove | 2:df22ddf10d75 | 51 | int cnt = 0; |
sam_grove | 2:df22ddf10d75 | 52 | while (level == io) { |
sam_grove | 2:df22ddf10d75 | 53 | if (cnt > max_time) { |
sam_grove | 2:df22ddf10d75 | 54 | return ERROR_NO_PATIENCE; |
sam_grove | 2:df22ddf10d75 | 55 | } |
sam_grove | 2:df22ddf10d75 | 56 | cnt++; |
sam_grove | 2:df22ddf10d75 | 57 | wait_us(1); |
sam_grove | 2:df22ddf10d75 | 58 | } |
sam_grove | 2:df22ddf10d75 | 59 | return ERROR_NONE; |
Wimpie | 0:9b5b3200688f | 60 | } |
Wimpie | 0:9b5b3200688f | 61 | |
sam_grove | 2:df22ddf10d75 | 62 | eError DHT::readData() |
sam_grove | 2:df22ddf10d75 | 63 | { |
sam_grove | 2:df22ddf10d75 | 64 | uint8_t i = 0, j = 0, b = 0, data_valid = 0; |
sam_grove | 2:df22ddf10d75 | 65 | uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0}; |
Wimpie | 0:9b5b3200688f | 66 | |
Wimpie | 0:9b5b3200688f | 67 | eError err = ERROR_NONE; |
Wimpie | 0:9b5b3200688f | 68 | time_t currentTime = time(NULL); |
Wimpie | 0:9b5b3200688f | 69 | |
Wimpie | 0:9b5b3200688f | 70 | DigitalInOut DHT_io(_pin); |
Wimpie | 0:9b5b3200688f | 71 | |
sam_grove | 2:df22ddf10d75 | 72 | // IO must be in hi state to start |
sam_grove | 2:df22ddf10d75 | 73 | if (ERROR_NONE != stall(DHT_io, 0, 250)) { |
sam_grove | 2:df22ddf10d75 | 74 | return BUS_BUSY; |
Wimpie | 0:9b5b3200688f | 75 | } |
Wimpie | 0:9b5b3200688f | 76 | |
sam_grove | 2:df22ddf10d75 | 77 | // start the transfer |
Wimpie | 0:9b5b3200688f | 78 | DHT_io.output(); |
Wimpie | 0:9b5b3200688f | 79 | DHT_io = 0; |
sam_grove | 2:df22ddf10d75 | 80 | // only 500uS for DHT22 but 18ms for DHT11 |
sam_grove | 2:df22ddf10d75 | 81 | (_DHTtype == 11) ? wait_ms(18) : wait(1); |
Wimpie | 0:9b5b3200688f | 82 | DHT_io = 1; |
sam_grove | 2:df22ddf10d75 | 83 | wait_us(30); |
Wimpie | 0:9b5b3200688f | 84 | DHT_io.input(); |
sam_grove | 2:df22ddf10d75 | 85 | // wait till the sensor grabs the bus |
sam_grove | 2:df22ddf10d75 | 86 | if (ERROR_NONE != stall(DHT_io, 1, 40)) { |
sam_grove | 2:df22ddf10d75 | 87 | return ERROR_NOT_PRESENT; |
Wimpie | 0:9b5b3200688f | 88 | } |
sam_grove | 2:df22ddf10d75 | 89 | // sensor should signal low 80us and then hi 80us |
sam_grove | 2:df22ddf10d75 | 90 | if (ERROR_NONE != stall(DHT_io, 0, 100)) { |
sam_grove | 2:df22ddf10d75 | 91 | return ERROR_SYNC_TIMEOUT; |
sam_grove | 2:df22ddf10d75 | 92 | } |
sam_grove | 2:df22ddf10d75 | 93 | if (ERROR_NONE != stall(DHT_io, 1, 100)) { |
sam_grove | 2:df22ddf10d75 | 94 | return ERROR_NO_PATIENCE; |
sam_grove | 2:df22ddf10d75 | 95 | } |
sam_grove | 2:df22ddf10d75 | 96 | // capture the data |
Wimpie | 0:9b5b3200688f | 97 | for (i = 0; i < 5; i++) { |
Wimpie | 0:9b5b3200688f | 98 | for (j = 0; j < 8; j++) { |
sam_grove | 2:df22ddf10d75 | 99 | if (ERROR_NONE != stall(DHT_io, 0, 75)) { |
sam_grove | 2:df22ddf10d75 | 100 | return ERROR_DATA_TIMEOUT; |
sam_grove | 2:df22ddf10d75 | 101 | } |
sam_grove | 2:df22ddf10d75 | 102 | // logic 0 is 28us max, 1 is 70us |
Wimpie | 0:9b5b3200688f | 103 | wait_us(40); |
sam_grove | 2:df22ddf10d75 | 104 | bit_value[i*8+j] = DHT_io; |
sam_grove | 2:df22ddf10d75 | 105 | if (ERROR_NONE != stall(DHT_io, 1, 50)) { |
sam_grove | 2:df22ddf10d75 | 106 | return ERROR_DATA_TIMEOUT; |
Wimpie | 0:9b5b3200688f | 107 | } |
Wimpie | 0:9b5b3200688f | 108 | } |
Wimpie | 0:9b5b3200688f | 109 | } |
sam_grove | 2:df22ddf10d75 | 110 | // store the data |
Wimpie | 0:9b5b3200688f | 111 | for (i = 0; i < 5; i++) { |
Wimpie | 0:9b5b3200688f | 112 | b=0; |
Wimpie | 0:9b5b3200688f | 113 | for (j=0; j<8; j++) { |
sam_grove | 2:df22ddf10d75 | 114 | if (bit_value[i*8+j] == 1) { |
sam_grove | 2:df22ddf10d75 | 115 | b |= (1 << (7-j)); |
Wimpie | 0:9b5b3200688f | 116 | } |
Wimpie | 0:9b5b3200688f | 117 | } |
Wimpie | 0:9b5b3200688f | 118 | DHT_data[i]=b; |
Wimpie | 0:9b5b3200688f | 119 | } |
Wimpie | 0:9b5b3200688f | 120 | |
sam_grove | 2:df22ddf10d75 | 121 | // uncomment to see the checksum error if it exists |
sam_grove | 2:df22ddf10d75 | 122 | //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]); |
sam_grove | 2:df22ddf10d75 | 123 | data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]; |
sam_grove | 2:df22ddf10d75 | 124 | if (DHT_data[4] == data_valid) { |
Wimpie | 0:9b5b3200688f | 125 | _lastReadTime = currentTime; |
sam_grove | 2:df22ddf10d75 | 126 | _lastTemperature = CalcTemperature(); |
sam_grove | 2:df22ddf10d75 | 127 | _lastHumidity = CalcHumidity(); |
Wimpie | 0:9b5b3200688f | 128 | |
Wimpie | 0:9b5b3200688f | 129 | } else { |
Wimpie | 0:9b5b3200688f | 130 | err = ERROR_CHECKSUM; |
Wimpie | 0:9b5b3200688f | 131 | } |
Wimpie | 0:9b5b3200688f | 132 | |
Wimpie | 0:9b5b3200688f | 133 | return err; |
Wimpie | 0:9b5b3200688f | 134 | |
Wimpie | 0:9b5b3200688f | 135 | } |
Wimpie | 0:9b5b3200688f | 136 | |
sam_grove | 2:df22ddf10d75 | 137 | float DHT::CalcTemperature() |
sam_grove | 2:df22ddf10d75 | 138 | { |
Wimpie | 0:9b5b3200688f | 139 | int v; |
Wimpie | 0:9b5b3200688f | 140 | |
Wimpie | 0:9b5b3200688f | 141 | switch (_DHTtype) { |
Wimpie | 0:9b5b3200688f | 142 | case DHT11: |
Wimpie | 0:9b5b3200688f | 143 | v = DHT_data[2]; |
Wimpie | 0:9b5b3200688f | 144 | return float(v); |
Wimpie | 0:9b5b3200688f | 145 | case DHT22: |
Wimpie | 0:9b5b3200688f | 146 | v = DHT_data[2] & 0x7F; |
Wimpie | 0:9b5b3200688f | 147 | v *= 256; |
Wimpie | 0:9b5b3200688f | 148 | v += DHT_data[3]; |
Wimpie | 0:9b5b3200688f | 149 | v /= 10; |
Wimpie | 0:9b5b3200688f | 150 | if (DHT_data[2] & 0x80) |
Wimpie | 0:9b5b3200688f | 151 | v *= -1; |
Wimpie | 0:9b5b3200688f | 152 | return float(v); |
Wimpie | 0:9b5b3200688f | 153 | } |
Wimpie | 0:9b5b3200688f | 154 | return 0; |
Wimpie | 0:9b5b3200688f | 155 | } |
Wimpie | 0:9b5b3200688f | 156 | |
sam_grove | 2:df22ddf10d75 | 157 | float DHT::ReadHumidity() |
sam_grove | 2:df22ddf10d75 | 158 | { |
Wimpie | 0:9b5b3200688f | 159 | return _lastHumidity; |
Wimpie | 0:9b5b3200688f | 160 | } |
Wimpie | 0:9b5b3200688f | 161 | |
sam_grove | 2:df22ddf10d75 | 162 | float DHT::ConvertCelciustoFarenheit(float const celsius) |
sam_grove | 2:df22ddf10d75 | 163 | { |
Wimpie | 0:9b5b3200688f | 164 | return celsius * 9 / 5 + 32; |
Wimpie | 0:9b5b3200688f | 165 | } |
Wimpie | 0:9b5b3200688f | 166 | |
sam_grove | 2:df22ddf10d75 | 167 | float DHT::ConvertCelciustoKelvin(float const celsius) |
sam_grove | 2:df22ddf10d75 | 168 | { |
Wimpie | 0:9b5b3200688f | 169 | return celsius + 273.15; |
Wimpie | 0:9b5b3200688f | 170 | } |
Wimpie | 0:9b5b3200688f | 171 | |
Wimpie | 0:9b5b3200688f | 172 | // dewPoint function NOAA |
Wimpie | 0:9b5b3200688f | 173 | // reference: http://wahiduddin.net/calc/density_algorithms.htm |
sam_grove | 2:df22ddf10d75 | 174 | float DHT::CalcdewPoint(float const celsius, float const humidity) |
sam_grove | 2:df22ddf10d75 | 175 | { |
Wimpie | 0:9b5b3200688f | 176 | float A0= 373.15/(273.15 + celsius); |
Wimpie | 0:9b5b3200688f | 177 | float SUM = -7.90298 * (A0-1); |
Wimpie | 0:9b5b3200688f | 178 | SUM += 5.02808 * log10(A0); |
Wimpie | 0:9b5b3200688f | 179 | SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; |
Wimpie | 0:9b5b3200688f | 180 | SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; |
Wimpie | 0:9b5b3200688f | 181 | SUM += log10(1013.246); |
Wimpie | 0:9b5b3200688f | 182 | float VP = pow(10, SUM-3) * humidity; |
Wimpie | 0:9b5b3200688f | 183 | float T = log(VP/0.61078); // temp var |
Wimpie | 0:9b5b3200688f | 184 | return (241.88 * T) / (17.558-T); |
Wimpie | 0:9b5b3200688f | 185 | } |
Wimpie | 0:9b5b3200688f | 186 | |
Wimpie | 0:9b5b3200688f | 187 | // delta max = 0.6544 wrt dewPoint() |
Wimpie | 0:9b5b3200688f | 188 | // 5x faster than dewPoint() |
Wimpie | 0:9b5b3200688f | 189 | // reference: http://en.wikipedia.org/wiki/Dew_point |
sam_grove | 2:df22ddf10d75 | 190 | float DHT::CalcdewPointFast(float const celsius, float const humidity) |
Wimpie | 0:9b5b3200688f | 191 | { |
sam_grove | 2:df22ddf10d75 | 192 | float a = 17.271; |
sam_grove | 2:df22ddf10d75 | 193 | float b = 237.7; |
sam_grove | 2:df22ddf10d75 | 194 | float temp = (a * celsius) / (b + celsius) + log(humidity/100); |
sam_grove | 2:df22ddf10d75 | 195 | float Td = (b * temp) / (a - temp); |
sam_grove | 2:df22ddf10d75 | 196 | return Td; |
Wimpie | 0:9b5b3200688f | 197 | } |
Wimpie | 0:9b5b3200688f | 198 | |
sam_grove | 2:df22ddf10d75 | 199 | float DHT::ReadTemperature(eScale Scale) |
sam_grove | 2:df22ddf10d75 | 200 | { |
Wimpie | 0:9b5b3200688f | 201 | if (Scale == FARENHEIT) |
Wimpie | 0:9b5b3200688f | 202 | return ConvertCelciustoFarenheit(_lastTemperature); |
Wimpie | 0:9b5b3200688f | 203 | else if (Scale == KELVIN) |
Wimpie | 0:9b5b3200688f | 204 | return ConvertCelciustoKelvin(_lastTemperature); |
Wimpie | 0:9b5b3200688f | 205 | else |
Wimpie | 0:9b5b3200688f | 206 | return _lastTemperature; |
Wimpie | 0:9b5b3200688f | 207 | } |
Wimpie | 0:9b5b3200688f | 208 | |
sam_grove | 2:df22ddf10d75 | 209 | float DHT::CalcHumidity() |
sam_grove | 2:df22ddf10d75 | 210 | { |
Wimpie | 0:9b5b3200688f | 211 | int v; |
Wimpie | 0:9b5b3200688f | 212 | |
Wimpie | 0:9b5b3200688f | 213 | switch (_DHTtype) { |
Wimpie | 0:9b5b3200688f | 214 | case DHT11: |
Wimpie | 0:9b5b3200688f | 215 | v = DHT_data[0]; |
Wimpie | 0:9b5b3200688f | 216 | return float(v); |
Wimpie | 0:9b5b3200688f | 217 | case DHT22: |
Wimpie | 0:9b5b3200688f | 218 | v = DHT_data[0]; |
Wimpie | 0:9b5b3200688f | 219 | v *= 256; |
Wimpie | 0:9b5b3200688f | 220 | v += DHT_data[1]; |
Wimpie | 0:9b5b3200688f | 221 | v /= 10; |
Wimpie | 0:9b5b3200688f | 222 | return float(v); |
Wimpie | 0:9b5b3200688f | 223 | } |
Wimpie | 0:9b5b3200688f | 224 | return 0; |
Wimpie | 0:9b5b3200688f | 225 | } |
Wimpie | 0:9b5b3200688f | 226 | |
Wimpie | 0:9b5b3200688f | 227 |