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