DHT11 used for Temperature & Humidity sensor.
Dependents: LoRaWAN_mbed_lmic_agriculture_app
Fork of DHT by
DHT.cpp@4:0c34c98a87ec, 2018-04-02 (annotated)
- Committer:
- GTsapparellas
- Date:
- Mon Apr 02 12:06:29 2018 +0000
- Revision:
- 4:0c34c98a87ec
- Parent:
- 3:6937e130feca
- Child:
- 5:e7e3db429c9e
DHT11 library employed for temperature & humidity sensor.
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 | 4:0c34c98a87ec | 170 | //pc.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 | 171 | data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]; |
sam_grove | 2:df22ddf10d75 | 172 | if (DHT_data[4] == data_valid) { |
Wimpie | 0:9b5b3200688f | 173 | _lastReadTime = currentTime; |
sam_grove | 2:df22ddf10d75 | 174 | _lastTemperature = CalcTemperature(); |
sam_grove | 2:df22ddf10d75 | 175 | _lastHumidity = CalcHumidity(); |
Wimpie | 0:9b5b3200688f | 176 | |
Wimpie | 0:9b5b3200688f | 177 | } else { |
Wimpie | 0:9b5b3200688f | 178 | err = ERROR_CHECKSUM; |
Wimpie | 0:9b5b3200688f | 179 | } |
Wimpie | 0:9b5b3200688f | 180 | |
Wimpie | 0:9b5b3200688f | 181 | return err; |
GTsapparellas | 4:0c34c98a87ec | 182 | |
GTsapparellas | 4:0c34c98a87ec | 183 | } // end of readData function. |
Wimpie | 0:9b5b3200688f | 184 | |
GTsapparellas | 4:0c34c98a87ec | 185 | /* |
GTsapparellas | 4:0c34c98a87ec | 186 | * CalcTemperature DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 187 | * |
GTsapparellas | 4:0c34c98a87ec | 188 | * Input parameters: None |
GTsapparellas | 4:0c34c98a87ec | 189 | * |
GTsapparellas | 4:0c34c98a87ec | 190 | * Return: float temperature value. |
GTsapparellas | 4:0c34c98a87ec | 191 | */ |
sam_grove | 2:df22ddf10d75 | 192 | float DHT::CalcTemperature() |
sam_grove | 2:df22ddf10d75 | 193 | { |
GTsapparellas | 4:0c34c98a87ec | 194 | |
Wimpie | 0:9b5b3200688f | 195 | int v; |
Wimpie | 0:9b5b3200688f | 196 | |
Wimpie | 0:9b5b3200688f | 197 | switch (_DHTtype) { |
Wimpie | 0:9b5b3200688f | 198 | case DHT11: |
Wimpie | 0:9b5b3200688f | 199 | v = DHT_data[2]; |
Wimpie | 0:9b5b3200688f | 200 | return float(v); |
Wimpie | 0:9b5b3200688f | 201 | case DHT22: |
Wimpie | 0:9b5b3200688f | 202 | v = DHT_data[2] & 0x7F; |
Wimpie | 0:9b5b3200688f | 203 | v *= 256; |
Wimpie | 0:9b5b3200688f | 204 | v += DHT_data[3]; |
Wimpie | 0:9b5b3200688f | 205 | v /= 10; |
Wimpie | 0:9b5b3200688f | 206 | if (DHT_data[2] & 0x80) |
Wimpie | 0:9b5b3200688f | 207 | v *= -1; |
Wimpie | 0:9b5b3200688f | 208 | return float(v); |
Wimpie | 0:9b5b3200688f | 209 | } |
Wimpie | 0:9b5b3200688f | 210 | return 0; |
GTsapparellas | 4:0c34c98a87ec | 211 | |
GTsapparellas | 4:0c34c98a87ec | 212 | } // end of CalcTemperature function. |
Wimpie | 0:9b5b3200688f | 213 | |
GTsapparellas | 4:0c34c98a87ec | 214 | /* |
GTsapparellas | 4:0c34c98a87ec | 215 | * ReadHumidity DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 216 | * |
GTsapparellas | 4:0c34c98a87ec | 217 | * Input parameters: None |
GTsapparellas | 4:0c34c98a87ec | 218 | * |
GTsapparellas | 4:0c34c98a87ec | 219 | * Return: float last humidity value. |
GTsapparellas | 4:0c34c98a87ec | 220 | */ |
sam_grove | 2:df22ddf10d75 | 221 | float DHT::ReadHumidity() |
sam_grove | 2:df22ddf10d75 | 222 | { |
Wimpie | 0:9b5b3200688f | 223 | return _lastHumidity; |
GTsapparellas | 4:0c34c98a87ec | 224 | } // end of ReadHumidity function. |
Wimpie | 0:9b5b3200688f | 225 | |
GTsapparellas | 4:0c34c98a87ec | 226 | /* |
GTsapparellas | 4:0c34c98a87ec | 227 | * ConvertCelsiustoFarenheit DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 228 | * |
GTsapparellas | 4:0c34c98a87ec | 229 | * Input parameters: float const celsius |
GTsapparellas | 4:0c34c98a87ec | 230 | * |
GTsapparellas | 4:0c34c98a87ec | 231 | * Return: float farenheit temperature value. |
GTsapparellas | 4:0c34c98a87ec | 232 | */ |
sam_grove | 2:df22ddf10d75 | 233 | float DHT::ConvertCelciustoFarenheit(float const celsius) |
sam_grove | 2:df22ddf10d75 | 234 | { |
Wimpie | 0:9b5b3200688f | 235 | return celsius * 9 / 5 + 32; |
GTsapparellas | 4:0c34c98a87ec | 236 | } // end of ConvertCelciustoFarenheit function. |
Wimpie | 0:9b5b3200688f | 237 | |
GTsapparellas | 4:0c34c98a87ec | 238 | /* |
GTsapparellas | 4:0c34c98a87ec | 239 | * ConvertCelsiustoKelvin DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 240 | * |
GTsapparellas | 4:0c34c98a87ec | 241 | * Input parameters: float const celsius |
GTsapparellas | 4:0c34c98a87ec | 242 | * |
GTsapparellas | 4:0c34c98a87ec | 243 | * Return: float kelvin temperature value. |
GTsapparellas | 4:0c34c98a87ec | 244 | */ |
sam_grove | 2:df22ddf10d75 | 245 | float DHT::ConvertCelciustoKelvin(float const celsius) |
sam_grove | 2:df22ddf10d75 | 246 | { |
sam_grove | 3:6937e130feca | 247 | return celsius + 273.15f; |
GTsapparellas | 4:0c34c98a87ec | 248 | } // end of ConvertCelciusKelvin function. |
Wimpie | 0:9b5b3200688f | 249 | |
GTsapparellas | 4:0c34c98a87ec | 250 | /* |
GTsapparellas | 4:0c34c98a87ec | 251 | * CalcdewPoint DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 252 | * |
GTsapparellas | 4:0c34c98a87ec | 253 | * Input parameters: float const celsius |
GTsapparellas | 4:0c34c98a87ec | 254 | * float const humidity |
GTsapparellas | 4:0c34c98a87ec | 255 | * |
GTsapparellas | 4:0c34c98a87ec | 256 | * Return: dewPoint. |
GTsapparellas | 4:0c34c98a87ec | 257 | * Reference: http://wahiduddin.net/calc/density_algorithms.htm |
GTsapparellas | 4:0c34c98a87ec | 258 | */ |
sam_grove | 2:df22ddf10d75 | 259 | float DHT::CalcdewPoint(float const celsius, float const humidity) |
sam_grove | 2:df22ddf10d75 | 260 | { |
sam_grove | 3:6937e130feca | 261 | float A0= 373.15f/(273.15f + celsius); |
Wimpie | 0:9b5b3200688f | 262 | float SUM = -7.90298 * (A0-1); |
sam_grove | 3:6937e130feca | 263 | SUM += 5.02808f * log10(A0); |
sam_grove | 3:6937e130feca | 264 | SUM += -1.3816e-7 * (pow(10, (11.344f*(1-1/A0)))-1) ; |
Wimpie | 0:9b5b3200688f | 265 | SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; |
Wimpie | 0:9b5b3200688f | 266 | SUM += log10(1013.246); |
Wimpie | 0:9b5b3200688f | 267 | float VP = pow(10, SUM-3) * humidity; |
sam_grove | 3:6937e130feca | 268 | float T = log(VP/0.61078f); // temp var |
sam_grove | 3:6937e130feca | 269 | return (241.88f * T) / (17.558f-T); |
GTsapparellas | 4:0c34c98a87ec | 270 | |
GTsapparellas | 4:0c34c98a87ec | 271 | } // end of CalcdewPoint function. |
Wimpie | 0:9b5b3200688f | 272 | |
GTsapparellas | 4:0c34c98a87ec | 273 | /* |
GTsapparellas | 4:0c34c98a87ec | 274 | * CalcdewPointFast DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 275 | * |
GTsapparellas | 4:0c34c98a87ec | 276 | * Input parameters: float const celsius |
GTsapparellas | 4:0c34c98a87ec | 277 | * float const humidity |
GTsapparellas | 4:0c34c98a87ec | 278 | * |
GTsapparellas | 4:0c34c98a87ec | 279 | * Return: 5x faster dewPoint. |
GTsapparellas | 4:0c34c98a87ec | 280 | * Reference: http://en.wikipedia.org/wiki/Dew_point |
GTsapparellas | 4:0c34c98a87ec | 281 | */ |
sam_grove | 2:df22ddf10d75 | 282 | float DHT::CalcdewPointFast(float const celsius, float const humidity) |
Wimpie | 0:9b5b3200688f | 283 | { |
sam_grove | 2:df22ddf10d75 | 284 | float a = 17.271; |
sam_grove | 2:df22ddf10d75 | 285 | float b = 237.7; |
sam_grove | 2:df22ddf10d75 | 286 | float temp = (a * celsius) / (b + celsius) + log(humidity/100); |
sam_grove | 2:df22ddf10d75 | 287 | float Td = (b * temp) / (a - temp); |
sam_grove | 2:df22ddf10d75 | 288 | return Td; |
GTsapparellas | 4:0c34c98a87ec | 289 | |
GTsapparellas | 4:0c34c98a87ec | 290 | } // end of CalcdewPointFast function |
Wimpie | 0:9b5b3200688f | 291 | |
GTsapparellas | 4:0c34c98a87ec | 292 | /* |
GTsapparellas | 4:0c34c98a87ec | 293 | * ReadTemperature DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 294 | * |
GTsapparellas | 4:0c34c98a87ec | 295 | * Input parameters: eScale Scale |
GTsapparellas | 4:0c34c98a87ec | 296 | * |
GTsapparellas | 4:0c34c98a87ec | 297 | * Return: float scaled temperature value. |
GTsapparellas | 4:0c34c98a87ec | 298 | * Reference: http://en.wikipedia.org/wiki/Dew_point |
GTsapparellas | 4:0c34c98a87ec | 299 | */ |
sam_grove | 2:df22ddf10d75 | 300 | float DHT::ReadTemperature(eScale Scale) |
sam_grove | 2:df22ddf10d75 | 301 | { |
Wimpie | 0:9b5b3200688f | 302 | if (Scale == FARENHEIT) |
Wimpie | 0:9b5b3200688f | 303 | return ConvertCelciustoFarenheit(_lastTemperature); |
Wimpie | 0:9b5b3200688f | 304 | else if (Scale == KELVIN) |
Wimpie | 0:9b5b3200688f | 305 | return ConvertCelciustoKelvin(_lastTemperature); |
Wimpie | 0:9b5b3200688f | 306 | else |
Wimpie | 0:9b5b3200688f | 307 | return _lastTemperature; |
GTsapparellas | 4:0c34c98a87ec | 308 | } // end of ReadTemperature function. |
Wimpie | 0:9b5b3200688f | 309 | |
GTsapparellas | 4:0c34c98a87ec | 310 | /* |
GTsapparellas | 4:0c34c98a87ec | 311 | * Calc DHT function of type float. |
GTsapparellas | 4:0c34c98a87ec | 312 | * |
GTsapparellas | 4:0c34c98a87ec | 313 | * Input parameters: None |
GTsapparellas | 4:0c34c98a87ec | 314 | * |
GTsapparellas | 4:0c34c98a87ec | 315 | * Return: float humidity value. |
GTsapparellas | 4:0c34c98a87ec | 316 | */ |
sam_grove | 2:df22ddf10d75 | 317 | float DHT::CalcHumidity() |
sam_grove | 2:df22ddf10d75 | 318 | { |
Wimpie | 0:9b5b3200688f | 319 | int v; |
Wimpie | 0:9b5b3200688f | 320 | |
Wimpie | 0:9b5b3200688f | 321 | switch (_DHTtype) { |
Wimpie | 0:9b5b3200688f | 322 | case DHT11: |
Wimpie | 0:9b5b3200688f | 323 | v = DHT_data[0]; |
Wimpie | 0:9b5b3200688f | 324 | return float(v); |
Wimpie | 0:9b5b3200688f | 325 | case DHT22: |
Wimpie | 0:9b5b3200688f | 326 | v = DHT_data[0]; |
Wimpie | 0:9b5b3200688f | 327 | v *= 256; |
Wimpie | 0:9b5b3200688f | 328 | v += DHT_data[1]; |
Wimpie | 0:9b5b3200688f | 329 | v /= 10; |
Wimpie | 0:9b5b3200688f | 330 | return float(v); |
Wimpie | 0:9b5b3200688f | 331 | } |
Wimpie | 0:9b5b3200688f | 332 | return 0; |
GTsapparellas | 4:0c34c98a87ec | 333 | } // end of CalcHumidity function. |