DHT11 used for Temperature & Humidity sensor.
Dependents: LoRaWAN_mbed_lmic_agriculture_app
Fork of DHT by
DHT.cpp@5:e7e3db429c9e, 2018-04-11 (annotated)
- Committer:
- GTsapparellas
- Date:
- Wed Apr 11 19:42:32 2018 +0000
- Revision:
- 5:e7e3db429c9e
- Parent:
- 4:0c34c98a87ec
version 1.0 Commenting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GTsapparellas | 4:0c34c98a87ec | 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. |
GTsapparellas | 4:0c34c98a87ec | 31 | * |
GTsapparellas | 4:0c34c98a87ec | 32 | * ///////////////////////////////////////////////////////////////////////////// |
GTsapparellas | 4:0c34c98a87ec | 33 | * |
GTsapparellas | 4:0c34c98a87ec | 34 | * Used by Giorgos Tsapparellas for Internet of Things (IoT) smart monitoring |
GTsapparellas | 4:0c34c98a87ec | 35 | * device for agriculture using LoRaWAN technology. |
GTsapparellas | 4:0c34c98a87ec | 36 | * |
GTsapparellas | 4:0c34c98a87ec | 37 | * Sensor Used: DHT11 - Digital-output Humidity and Temperature sensor. |
GTsapparellas | 4:0c34c98a87ec | 38 | * |
GTsapparellas | 4:0c34c98a87ec | 39 | * Date of issued copy: 10 January 2018 |
GTsapparellas | 4:0c34c98a87ec | 40 | * |
GTsapparellas | 4:0c34c98a87ec | 41 | * Modifications: |
GTsapparellas | 4:0c34c98a87ec | 42 | * - No external modifications of the existing "AS IT IS" software. |
GTsapparellas | 4:0c34c98a87ec | 43 | * - Added some external comments for meeting good principles of |
GTsapparellas | 4:0c34c98a87ec | 44 | * source code re-usability. |
GTsapparellas | 4:0c34c98a87ec | 45 | ******************************************************************************/ |
Wimpie | 0:9b5b3200688f | 46 | |
Wimpie | 0:9b5b3200688f | 47 | #include "DHT.h" |
Wimpie | 0:9b5b3200688f | 48 | |
GTsapparellas | 4:0c34c98a87ec | 49 | #define DHT_DATA_BIT_COUNT 40 // Data bit count set to 40 |
GTsapparellas | 4:0c34c98a87ec | 50 | |
GTsapparellas | 4:0c34c98a87ec | 51 | /* Uncomment this line for configuring pc variable for UART usage. */ |
GTsapparellas | 4:0c34c98a87ec | 52 | //Serial pc(USBTX, USBRX); // UART tranmitter(tx) and receiver(rx). |
Wimpie | 0:9b5b3200688f | 53 | |
GTsapparellas | 4:0c34c98a87ec | 54 | /* |
GTsapparellas | 4:0c34c98a87ec | 55 | * DHT constructor. |
GTsapparellas | 4:0c34c98a87ec | 56 | * Input parameters: PinName pin |
GTsapparellas | 4:0c34c98a87ec | 57 | * eType DHTtype |
GTsapparellas | 4:0c34c98a87ec | 58 | * |
GTsapparellas | 4:0c34c98a87ec | 59 | */ |
sam_grove | 2:df22ddf10d75 | 60 | DHT::DHT(PinName pin, eType DHTtype) |
sam_grove | 2:df22ddf10d75 | 61 | { |
GTsapparellas | 4:0c34c98a87ec | 62 | |
Wimpie | 0:9b5b3200688f | 63 | _pin = pin; |
Wimpie | 0:9b5b3200688f | 64 | _DHTtype = DHTtype; |
sam_grove | 2:df22ddf10d75 | 65 | _firsttime = true; |
GTsapparellas | 4:0c34c98a87ec | 66 | |
GTsapparellas | 4:0c34c98a87ec | 67 | } // end of DHT constructor. |
sam_grove | 2:df22ddf10d75 | 68 | |
GTsapparellas | 4:0c34c98a87ec | 69 | /* |
GTsapparellas | 4:0c34c98a87ec | 70 | * Null DHT destructor. |
GTsapparellas | 4:0c34c98a87ec | 71 | * |
GTsapparellas | 4:0c34c98a87ec | 72 | */ |
sam_grove | 2:df22ddf10d75 | 73 | DHT::~DHT() |
sam_grove | 2:df22ddf10d75 | 74 | { |
sam_grove | 2:df22ddf10d75 | 75 | |
GTsapparellas | 4:0c34c98a87ec | 76 | } // end of DHT destructor. |
Wimpie | 0:9b5b3200688f | 77 | |
GTsapparellas | 4:0c34c98a87ec | 78 | /* |
GTsapparellas | 4:0c34c98a87ec | 79 | * stall DHT function of type eError. |
GTsapparellas | 4:0c34c98a87ec | 80 | * |
GTsapparellas | 4:0c34c98a87ec | 81 | * Input parameters: DigitalInOut io |
GTsapparellas | 4:0c34c98a87ec | 82 | * int const level |
GTsapparellas | 4:0c34c98a87ec | 83 | * int const max_time |
GTsapparellas | 4:0c34c98a87ec | 84 | * |
GTsapparellas | 4:0c34c98a87ec | 85 | * Return: ERROR_NO_PATIENCE or ERROR_NONE |
GTsapparellas | 4:0c34c98a87ec | 86 | */ |
sam_grove | 2:df22ddf10d75 | 87 | eError DHT::stall(DigitalInOut &io, int const level, int const max_time) |
sam_grove | 2:df22ddf10d75 | 88 | { |
GTsapparellas | 4:0c34c98a87ec | 89 | |
sam_grove | 2:df22ddf10d75 | 90 | int cnt = 0; |
sam_grove | 2:df22ddf10d75 | 91 | while (level == io) { |
sam_grove | 2:df22ddf10d75 | 92 | if (cnt > max_time) { |
sam_grove | 2:df22ddf10d75 | 93 | return ERROR_NO_PATIENCE; |
sam_grove | 2:df22ddf10d75 | 94 | } |
sam_grove | 2:df22ddf10d75 | 95 | cnt++; |
sam_grove | 2:df22ddf10d75 | 96 | wait_us(1); |
sam_grove | 2:df22ddf10d75 | 97 | } |
sam_grove | 2:df22ddf10d75 | 98 | return ERROR_NONE; |
GTsapparellas | 4:0c34c98a87ec | 99 | |
GTsapparellas | 4:0c34c98a87ec | 100 | } // end of stall function. |
Wimpie | 0:9b5b3200688f | 101 | |
GTsapparellas | 4:0c34c98a87ec | 102 | /* |
GTsapparellas | 4:0c34c98a87ec | 103 | * readData DHT function of type eError. |
GTsapparellas | 4:0c34c98a87ec | 104 | * |
GTsapparellas | 4:0c34c98a87ec | 105 | * Input parameters: None |
GTsapparellas | 4:0c34c98a87ec | 106 | * |
GTsapparellas | 4:0c34c98a87ec | 107 | * Return: Either BUS_BUSY, ERROR_NOT_PRESENT, ERROR_SYNC_TIMEOUT, |
GTsapparellas | 4:0c34c98a87ec | 108 | * ERROR_NO_PATIENCE, ERROR_DATA_TIMEOUT or ERROR_CHECKSUM. |
GTsapparellas | 4:0c34c98a87ec | 109 | */ |
sam_grove | 2:df22ddf10d75 | 110 | eError DHT::readData() |
sam_grove | 2:df22ddf10d75 | 111 | { |
sam_grove | 2:df22ddf10d75 | 112 | uint8_t i = 0, j = 0, b = 0, data_valid = 0; |
sam_grove | 2:df22ddf10d75 | 113 | uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0}; |
Wimpie | 0:9b5b3200688f | 114 | |
Wimpie | 0:9b5b3200688f | 115 | eError err = ERROR_NONE; |
Wimpie | 0:9b5b3200688f | 116 | time_t currentTime = time(NULL); |
Wimpie | 0:9b5b3200688f | 117 | |
Wimpie | 0:9b5b3200688f | 118 | DigitalInOut DHT_io(_pin); |
Wimpie | 0:9b5b3200688f | 119 | |
GTsapparellas | 4:0c34c98a87ec | 120 | // I/O must be in high state to start |
sam_grove | 2:df22ddf10d75 | 121 | if (ERROR_NONE != stall(DHT_io, 0, 250)) { |
sam_grove | 2:df22ddf10d75 | 122 | return BUS_BUSY; |
Wimpie | 0:9b5b3200688f | 123 | } |
Wimpie | 0:9b5b3200688f | 124 | |
GTsapparellas | 4:0c34c98a87ec | 125 | // Start the transfer |
Wimpie | 0:9b5b3200688f | 126 | DHT_io.output(); |
Wimpie | 0:9b5b3200688f | 127 | DHT_io = 0; |
sam_grove | 3:6937e130feca | 128 | wait_ms(18); |
Wimpie | 0:9b5b3200688f | 129 | DHT_io = 1; |
sam_grove | 2:df22ddf10d75 | 130 | wait_us(30); |
Wimpie | 0:9b5b3200688f | 131 | DHT_io.input(); |
GTsapparellas | 4:0c34c98a87ec | 132 | |
GTsapparellas | 4:0c34c98a87ec | 133 | // Wait until the sensor grabs the bus |
sam_grove | 3:6937e130feca | 134 | if (ERROR_NONE != stall(DHT_io, 1, 100)) { |
sam_grove | 2:df22ddf10d75 | 135 | return ERROR_NOT_PRESENT; |
Wimpie | 0:9b5b3200688f | 136 | } |
GTsapparellas | 4:0c34c98a87ec | 137 | // Sensor should signal low 80us and then high 80us |
sam_grove | 2:df22ddf10d75 | 138 | if (ERROR_NONE != stall(DHT_io, 0, 100)) { |
sam_grove | 2:df22ddf10d75 | 139 | return ERROR_SYNC_TIMEOUT; |
sam_grove | 2:df22ddf10d75 | 140 | } |
sam_grove | 2:df22ddf10d75 | 141 | if (ERROR_NONE != stall(DHT_io, 1, 100)) { |
sam_grove | 2:df22ddf10d75 | 142 | return ERROR_NO_PATIENCE; |
sam_grove | 2:df22ddf10d75 | 143 | } |
GTsapparellas | 4:0c34c98a87ec | 144 | // Capture the data |
Wimpie | 0:9b5b3200688f | 145 | for (i = 0; i < 5; i++) { |
Wimpie | 0:9b5b3200688f | 146 | for (j = 0; j < 8; j++) { |
sam_grove | 2:df22ddf10d75 | 147 | if (ERROR_NONE != stall(DHT_io, 0, 75)) { |
sam_grove | 2:df22ddf10d75 | 148 | return ERROR_DATA_TIMEOUT; |
sam_grove | 2:df22ddf10d75 | 149 | } |
GTsapparellas | 4:0c34c98a87ec | 150 | // Logic 0 is 28us max, 1 is 70us |
Wimpie | 0:9b5b3200688f | 151 | wait_us(40); |
sam_grove | 2:df22ddf10d75 | 152 | bit_value[i*8+j] = DHT_io; |
sam_grove | 2:df22ddf10d75 | 153 | if (ERROR_NONE != stall(DHT_io, 1, 50)) { |
sam_grove | 2:df22ddf10d75 | 154 | return ERROR_DATA_TIMEOUT; |
Wimpie | 0:9b5b3200688f | 155 | } |
Wimpie | 0:9b5b3200688f | 156 | } |
Wimpie | 0:9b5b3200688f | 157 | } |
GTsapparellas | 4:0c34c98a87ec | 158 | // Store the data |
Wimpie | 0:9b5b3200688f | 159 | for (i = 0; i < 5; i++) { |
Wimpie | 0:9b5b3200688f | 160 | b=0; |
Wimpie | 0:9b5b3200688f | 161 | for (j=0; j<8; j++) { |
sam_grove | 2:df22ddf10d75 | 162 | if (bit_value[i*8+j] == 1) { |
sam_grove | 2:df22ddf10d75 | 163 | b |= (1 << (7-j)); |
Wimpie | 0:9b5b3200688f | 164 | } |
Wimpie | 0:9b5b3200688f | 165 | } |
Wimpie | 0:9b5b3200688f | 166 | DHT_data[i]=b; |
Wimpie | 0:9b5b3200688f | 167 | } |
Wimpie | 0:9b5b3200688f | 168 | |
GTsapparellas | 4:0c34c98a87ec | 169 | // Uncomment to see the checksum error if it exists |
GTsapparellas | 5:e7e3db429c9e | 170 | // pc.printf(" 0x%02x + 0x%02x + 0x%02x + 0x%02x = 0x%02x \n", DHT_data[0], |
GTsapparellas | 5:e7e3db429c9e | 171 | // DHT_data[1], DHT_data[2], DHT_data[3], DHT_data[4]); |
sam_grove | 2:df22ddf10d75 | 172 | data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]; |
sam_grove | 2:df22ddf10d75 | 173 | if (DHT_data[4] == data_valid) { |
Wimpie | 0:9b5b3200688f | 174 | _lastReadTime = currentTime; |
sam_grove | 2:df22ddf10d75 | 175 | _lastTemperature = CalcTemperature(); |
sam_grove | 2:df22ddf10d75 | 176 | _lastHumidity = CalcHumidity(); |
Wimpie | 0:9b5b3200688f | 177 | |
Wimpie | 0:9b5b3200688f | 178 | } else { |
Wimpie | 0:9b5b3200688f | 179 | err = ERROR_CHECKSUM; |
Wimpie | 0:9b5b3200688f | 180 | } |
Wimpie | 0:9b5b3200688f | 181 | |
Wimpie | 0:9b5b3200688f | 182 | return err; |
GTsapparellas | 4:0c34c98a87ec | 183 | |
GTsapparellas | 4:0c34c98a87ec | 184 | } // end of readData function. |
Wimpie | 0:9b5b3200688f | 185 | |
GTsapparellas | 4:0c34c98a87ec | 186 | /* |
GTsapparellas | 4:0c34c98a87ec | 187 | * CalcTemperature DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 188 | * |
GTsapparellas | 4:0c34c98a87ec | 189 | * Input parameters: None |
GTsapparellas | 4:0c34c98a87ec | 190 | * |
GTsapparellas | 4:0c34c98a87ec | 191 | * Return: float temperature value. |
GTsapparellas | 4:0c34c98a87ec | 192 | */ |
sam_grove | 2:df22ddf10d75 | 193 | float DHT::CalcTemperature() |
sam_grove | 2:df22ddf10d75 | 194 | { |
GTsapparellas | 4:0c34c98a87ec | 195 | |
Wimpie | 0:9b5b3200688f | 196 | int v; |
Wimpie | 0:9b5b3200688f | 197 | |
Wimpie | 0:9b5b3200688f | 198 | switch (_DHTtype) { |
Wimpie | 0:9b5b3200688f | 199 | case DHT11: |
Wimpie | 0:9b5b3200688f | 200 | v = DHT_data[2]; |
Wimpie | 0:9b5b3200688f | 201 | return float(v); |
Wimpie | 0:9b5b3200688f | 202 | case DHT22: |
Wimpie | 0:9b5b3200688f | 203 | v = DHT_data[2] & 0x7F; |
Wimpie | 0:9b5b3200688f | 204 | v *= 256; |
Wimpie | 0:9b5b3200688f | 205 | v += DHT_data[3]; |
Wimpie | 0:9b5b3200688f | 206 | v /= 10; |
Wimpie | 0:9b5b3200688f | 207 | if (DHT_data[2] & 0x80) |
Wimpie | 0:9b5b3200688f | 208 | v *= -1; |
Wimpie | 0:9b5b3200688f | 209 | return float(v); |
Wimpie | 0:9b5b3200688f | 210 | } |
Wimpie | 0:9b5b3200688f | 211 | return 0; |
GTsapparellas | 4:0c34c98a87ec | 212 | |
GTsapparellas | 4:0c34c98a87ec | 213 | } // end of CalcTemperature function. |
Wimpie | 0:9b5b3200688f | 214 | |
GTsapparellas | 4:0c34c98a87ec | 215 | /* |
GTsapparellas | 4:0c34c98a87ec | 216 | * ReadHumidity DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 217 | * |
GTsapparellas | 4:0c34c98a87ec | 218 | * Input parameters: None |
GTsapparellas | 4:0c34c98a87ec | 219 | * |
GTsapparellas | 4:0c34c98a87ec | 220 | * Return: float last humidity value. |
GTsapparellas | 4:0c34c98a87ec | 221 | */ |
sam_grove | 2:df22ddf10d75 | 222 | float DHT::ReadHumidity() |
sam_grove | 2:df22ddf10d75 | 223 | { |
Wimpie | 0:9b5b3200688f | 224 | return _lastHumidity; |
GTsapparellas | 4:0c34c98a87ec | 225 | } // end of ReadHumidity function. |
Wimpie | 0:9b5b3200688f | 226 | |
GTsapparellas | 4:0c34c98a87ec | 227 | /* |
GTsapparellas | 4:0c34c98a87ec | 228 | * ConvertCelsiustoFarenheit DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 229 | * |
GTsapparellas | 4:0c34c98a87ec | 230 | * Input parameters: float const celsius |
GTsapparellas | 4:0c34c98a87ec | 231 | * |
GTsapparellas | 4:0c34c98a87ec | 232 | * Return: float farenheit temperature value. |
GTsapparellas | 4:0c34c98a87ec | 233 | */ |
sam_grove | 2:df22ddf10d75 | 234 | float DHT::ConvertCelciustoFarenheit(float const celsius) |
sam_grove | 2:df22ddf10d75 | 235 | { |
Wimpie | 0:9b5b3200688f | 236 | return celsius * 9 / 5 + 32; |
GTsapparellas | 4:0c34c98a87ec | 237 | } // end of ConvertCelciustoFarenheit function. |
Wimpie | 0:9b5b3200688f | 238 | |
GTsapparellas | 4:0c34c98a87ec | 239 | /* |
GTsapparellas | 4:0c34c98a87ec | 240 | * ConvertCelsiustoKelvin DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 241 | * |
GTsapparellas | 4:0c34c98a87ec | 242 | * Input parameters: float const celsius |
GTsapparellas | 4:0c34c98a87ec | 243 | * |
GTsapparellas | 4:0c34c98a87ec | 244 | * Return: float kelvin temperature value. |
GTsapparellas | 4:0c34c98a87ec | 245 | */ |
sam_grove | 2:df22ddf10d75 | 246 | float DHT::ConvertCelciustoKelvin(float const celsius) |
sam_grove | 2:df22ddf10d75 | 247 | { |
sam_grove | 3:6937e130feca | 248 | return celsius + 273.15f; |
GTsapparellas | 4:0c34c98a87ec | 249 | } // end of ConvertCelciusKelvin function. |
Wimpie | 0:9b5b3200688f | 250 | |
GTsapparellas | 4:0c34c98a87ec | 251 | /* |
GTsapparellas | 4:0c34c98a87ec | 252 | * CalcdewPoint DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 253 | * |
GTsapparellas | 4:0c34c98a87ec | 254 | * Input parameters: float const celsius |
GTsapparellas | 4:0c34c98a87ec | 255 | * float const humidity |
GTsapparellas | 4:0c34c98a87ec | 256 | * |
GTsapparellas | 4:0c34c98a87ec | 257 | * Return: dewPoint. |
GTsapparellas | 4:0c34c98a87ec | 258 | * Reference: http://wahiduddin.net/calc/density_algorithms.htm |
GTsapparellas | 4:0c34c98a87ec | 259 | */ |
sam_grove | 2:df22ddf10d75 | 260 | float DHT::CalcdewPoint(float const celsius, float const humidity) |
sam_grove | 2:df22ddf10d75 | 261 | { |
sam_grove | 3:6937e130feca | 262 | float A0= 373.15f/(273.15f + celsius); |
Wimpie | 0:9b5b3200688f | 263 | float SUM = -7.90298 * (A0-1); |
sam_grove | 3:6937e130feca | 264 | SUM += 5.02808f * log10(A0); |
sam_grove | 3:6937e130feca | 265 | SUM += -1.3816e-7 * (pow(10, (11.344f*(1-1/A0)))-1) ; |
Wimpie | 0:9b5b3200688f | 266 | SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; |
Wimpie | 0:9b5b3200688f | 267 | SUM += log10(1013.246); |
Wimpie | 0:9b5b3200688f | 268 | float VP = pow(10, SUM-3) * humidity; |
sam_grove | 3:6937e130feca | 269 | float T = log(VP/0.61078f); // temp var |
sam_grove | 3:6937e130feca | 270 | return (241.88f * T) / (17.558f-T); |
GTsapparellas | 4:0c34c98a87ec | 271 | |
GTsapparellas | 4:0c34c98a87ec | 272 | } // end of CalcdewPoint function. |
Wimpie | 0:9b5b3200688f | 273 | |
GTsapparellas | 4:0c34c98a87ec | 274 | /* |
GTsapparellas | 4:0c34c98a87ec | 275 | * CalcdewPointFast DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 276 | * |
GTsapparellas | 4:0c34c98a87ec | 277 | * Input parameters: float const celsius |
GTsapparellas | 4:0c34c98a87ec | 278 | * float const humidity |
GTsapparellas | 4:0c34c98a87ec | 279 | * |
GTsapparellas | 4:0c34c98a87ec | 280 | * Return: 5x faster dewPoint. |
GTsapparellas | 4:0c34c98a87ec | 281 | * Reference: http://en.wikipedia.org/wiki/Dew_point |
GTsapparellas | 4:0c34c98a87ec | 282 | */ |
sam_grove | 2:df22ddf10d75 | 283 | float DHT::CalcdewPointFast(float const celsius, float const humidity) |
Wimpie | 0:9b5b3200688f | 284 | { |
sam_grove | 2:df22ddf10d75 | 285 | float a = 17.271; |
sam_grove | 2:df22ddf10d75 | 286 | float b = 237.7; |
sam_grove | 2:df22ddf10d75 | 287 | float temp = (a * celsius) / (b + celsius) + log(humidity/100); |
sam_grove | 2:df22ddf10d75 | 288 | float Td = (b * temp) / (a - temp); |
sam_grove | 2:df22ddf10d75 | 289 | return Td; |
GTsapparellas | 4:0c34c98a87ec | 290 | |
GTsapparellas | 4:0c34c98a87ec | 291 | } // end of CalcdewPointFast function |
Wimpie | 0:9b5b3200688f | 292 | |
GTsapparellas | 4:0c34c98a87ec | 293 | /* |
GTsapparellas | 4:0c34c98a87ec | 294 | * ReadTemperature DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 295 | * |
GTsapparellas | 4:0c34c98a87ec | 296 | * Input parameters: eScale Scale |
GTsapparellas | 4:0c34c98a87ec | 297 | * |
GTsapparellas | 4:0c34c98a87ec | 298 | * Return: float scaled temperature value. |
GTsapparellas | 4:0c34c98a87ec | 299 | * Reference: http://en.wikipedia.org/wiki/Dew_point |
GTsapparellas | 4:0c34c98a87ec | 300 | */ |
sam_grove | 2:df22ddf10d75 | 301 | float DHT::ReadTemperature(eScale Scale) |
sam_grove | 2:df22ddf10d75 | 302 | { |
Wimpie | 0:9b5b3200688f | 303 | if (Scale == FARENHEIT) |
Wimpie | 0:9b5b3200688f | 304 | return ConvertCelciustoFarenheit(_lastTemperature); |
Wimpie | 0:9b5b3200688f | 305 | else if (Scale == KELVIN) |
Wimpie | 0:9b5b3200688f | 306 | return ConvertCelciustoKelvin(_lastTemperature); |
Wimpie | 0:9b5b3200688f | 307 | else |
Wimpie | 0:9b5b3200688f | 308 | return _lastTemperature; |
GTsapparellas | 4:0c34c98a87ec | 309 | } // end of ReadTemperature function. |
Wimpie | 0:9b5b3200688f | 310 | |
GTsapparellas | 4:0c34c98a87ec | 311 | /* |
GTsapparellas | 4:0c34c98a87ec | 312 | * Calc DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 313 | * |
GTsapparellas | 4:0c34c98a87ec | 314 | * Input parameters: None |
GTsapparellas | 4:0c34c98a87ec | 315 | * |
GTsapparellas | 4:0c34c98a87ec | 316 | * Return: float humidity value. |
GTsapparellas | 4:0c34c98a87ec | 317 | */ |
sam_grove | 2:df22ddf10d75 | 318 | float DHT::CalcHumidity() |
sam_grove | 2:df22ddf10d75 | 319 | { |
Wimpie | 0:9b5b3200688f | 320 | int v; |
Wimpie | 0:9b5b3200688f | 321 | |
Wimpie | 0:9b5b3200688f | 322 | switch (_DHTtype) { |
Wimpie | 0:9b5b3200688f | 323 | case DHT11: |
Wimpie | 0:9b5b3200688f | 324 | v = DHT_data[0]; |
Wimpie | 0:9b5b3200688f | 325 | return float(v); |
Wimpie | 0:9b5b3200688f | 326 | case DHT22: |
Wimpie | 0:9b5b3200688f | 327 | v = DHT_data[0]; |
Wimpie | 0:9b5b3200688f | 328 | v *= 256; |
Wimpie | 0:9b5b3200688f | 329 | v += DHT_data[1]; |
Wimpie | 0:9b5b3200688f | 330 | v /= 10; |
Wimpie | 0:9b5b3200688f | 331 | return float(v); |
Wimpie | 0:9b5b3200688f | 332 | } |
Wimpie | 0:9b5b3200688f | 333 | return 0; |
GTsapparellas | 4:0c34c98a87ec | 334 | } // end of CalcHumidity function. |