Programme d'une sonde de température DHT 11 associée à de la sauvegarde de données sur clé USB et à l'affichage de ces données sur afficheur LCD
Dependencies: FatFileSystemCpp mbed
DHT.cpp@0:ed0b4e66d2ad, 2015-06-02 (annotated)
- Committer:
- Fanta025
- Date:
- Tue Jun 02 14:19:54 2015 +0000
- Revision:
- 0:ed0b4e66d2ad
Programme d'une sonde de temp?rature DHT 11 associ?e ? de la sauvegarde de donn?es sur USB et un affichage sur ?cran LCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Fanta025 | 0:ed0b4e66d2ad | 1 | /* |
Fanta025 | 0:ed0b4e66d2ad | 2 | * DHT Library for Digital-output Humidity and Temperature sensors |
Fanta025 | 0:ed0b4e66d2ad | 3 | * |
Fanta025 | 0:ed0b4e66d2ad | 4 | * Works with DHT11, DHT22 |
Fanta025 | 0:ed0b4e66d2ad | 5 | * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio) |
Fanta025 | 0:ed0b4e66d2ad | 6 | * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio) |
Fanta025 | 0:ed0b4e66d2ad | 7 | * AM2302 , temperature-humidity sensor |
Fanta025 | 0:ed0b4e66d2ad | 8 | * HM2303 , Digital-output humidity and temperature sensor |
Fanta025 | 0:ed0b4e66d2ad | 9 | * |
Fanta025 | 0:ed0b4e66d2ad | 10 | * Copyright (C) Wim De Roeve |
Fanta025 | 0:ed0b4e66d2ad | 11 | * based on DHT22 sensor library by HO WING KIT |
Fanta025 | 0:ed0b4e66d2ad | 12 | * Arduino DHT11 library |
Fanta025 | 0:ed0b4e66d2ad | 13 | * |
Fanta025 | 0:ed0b4e66d2ad | 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Fanta025 | 0:ed0b4e66d2ad | 15 | * of this software and associated documnetation files (the "Software"), to deal |
Fanta025 | 0:ed0b4e66d2ad | 16 | * in the Software without restriction, including without limitation the rights |
Fanta025 | 0:ed0b4e66d2ad | 17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Fanta025 | 0:ed0b4e66d2ad | 18 | * copies of the Software, and to permit persons to whom the Software is |
Fanta025 | 0:ed0b4e66d2ad | 19 | * furished to do so, subject to the following conditions: |
Fanta025 | 0:ed0b4e66d2ad | 20 | * |
Fanta025 | 0:ed0b4e66d2ad | 21 | * The above copyright notice and this permission notice shall be included in |
Fanta025 | 0:ed0b4e66d2ad | 22 | * all copies or substantial portions of the Software. |
Fanta025 | 0:ed0b4e66d2ad | 23 | * |
Fanta025 | 0:ed0b4e66d2ad | 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Fanta025 | 0:ed0b4e66d2ad | 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Fanta025 | 0:ed0b4e66d2ad | 26 | * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Fanta025 | 0:ed0b4e66d2ad | 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Fanta025 | 0:ed0b4e66d2ad | 28 | * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Fanta025 | 0:ed0b4e66d2ad | 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Fanta025 | 0:ed0b4e66d2ad | 30 | * THE SOFTWARE. |
Fanta025 | 0:ed0b4e66d2ad | 31 | */ |
Fanta025 | 0:ed0b4e66d2ad | 32 | |
Fanta025 | 0:ed0b4e66d2ad | 33 | #include "DHT.h" |
Fanta025 | 0:ed0b4e66d2ad | 34 | |
Fanta025 | 0:ed0b4e66d2ad | 35 | #define DHT_DATA_BIT_COUNT 40 |
Fanta025 | 0:ed0b4e66d2ad | 36 | |
Fanta025 | 0:ed0b4e66d2ad | 37 | DHT::DHT(PinName pin, eType DHTtype) |
Fanta025 | 0:ed0b4e66d2ad | 38 | { |
Fanta025 | 0:ed0b4e66d2ad | 39 | _pin = pin; |
Fanta025 | 0:ed0b4e66d2ad | 40 | _DHTtype = DHTtype; |
Fanta025 | 0:ed0b4e66d2ad | 41 | _firsttime = true; |
Fanta025 | 0:ed0b4e66d2ad | 42 | } |
Fanta025 | 0:ed0b4e66d2ad | 43 | |
Fanta025 | 0:ed0b4e66d2ad | 44 | DHT::~DHT() |
Fanta025 | 0:ed0b4e66d2ad | 45 | { |
Fanta025 | 0:ed0b4e66d2ad | 46 | |
Fanta025 | 0:ed0b4e66d2ad | 47 | } |
Fanta025 | 0:ed0b4e66d2ad | 48 | |
Fanta025 | 0:ed0b4e66d2ad | 49 | eError DHT::stall(DigitalInOut &io, int const level, int const max_time) |
Fanta025 | 0:ed0b4e66d2ad | 50 | { |
Fanta025 | 0:ed0b4e66d2ad | 51 | int cnt = 0; |
Fanta025 | 0:ed0b4e66d2ad | 52 | while (level == io) { |
Fanta025 | 0:ed0b4e66d2ad | 53 | if (cnt > max_time) { |
Fanta025 | 0:ed0b4e66d2ad | 54 | return ERROR_NO_PATIENCE; |
Fanta025 | 0:ed0b4e66d2ad | 55 | } |
Fanta025 | 0:ed0b4e66d2ad | 56 | cnt++; |
Fanta025 | 0:ed0b4e66d2ad | 57 | wait_us(1); |
Fanta025 | 0:ed0b4e66d2ad | 58 | } |
Fanta025 | 0:ed0b4e66d2ad | 59 | return ERROR_NONE; |
Fanta025 | 0:ed0b4e66d2ad | 60 | } |
Fanta025 | 0:ed0b4e66d2ad | 61 | |
Fanta025 | 0:ed0b4e66d2ad | 62 | eError DHT::readData() |
Fanta025 | 0:ed0b4e66d2ad | 63 | { |
Fanta025 | 0:ed0b4e66d2ad | 64 | uint8_t i = 0, j = 0, b = 0, data_valid = 0; |
Fanta025 | 0:ed0b4e66d2ad | 65 | uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0}; |
Fanta025 | 0:ed0b4e66d2ad | 66 | |
Fanta025 | 0:ed0b4e66d2ad | 67 | eError err = ERROR_NONE; |
Fanta025 | 0:ed0b4e66d2ad | 68 | time_t currentTime = time(NULL); |
Fanta025 | 0:ed0b4e66d2ad | 69 | |
Fanta025 | 0:ed0b4e66d2ad | 70 | DigitalInOut DHT_io(_pin); |
Fanta025 | 0:ed0b4e66d2ad | 71 | |
Fanta025 | 0:ed0b4e66d2ad | 72 | // IO must be in hi state to start |
Fanta025 | 0:ed0b4e66d2ad | 73 | if (ERROR_NONE != stall(DHT_io, 0, 250)) { |
Fanta025 | 0:ed0b4e66d2ad | 74 | return BUS_BUSY; |
Fanta025 | 0:ed0b4e66d2ad | 75 | } |
Fanta025 | 0:ed0b4e66d2ad | 76 | |
Fanta025 | 0:ed0b4e66d2ad | 77 | // start the transfer |
Fanta025 | 0:ed0b4e66d2ad | 78 | DHT_io.output(); |
Fanta025 | 0:ed0b4e66d2ad | 79 | DHT_io = 0; |
Fanta025 | 0:ed0b4e66d2ad | 80 | // only 500uS for DHT22 but 18ms for DHT11 |
Fanta025 | 0:ed0b4e66d2ad | 81 | // *** RF : Le signal de démarrage consiste à un niveau de tension bas sur la ligne de données pour un minimum |
Fanta025 | 0:ed0b4e66d2ad | 82 | //de 18 ms pour s'assurer que la DHT11 détecte, suivie par une tension de pull-up pour environ 40US. |
Fanta025 | 0:ed0b4e66d2ad | 83 | (_DHTtype == 11) ? wait_ms(18) : wait(1); |
Fanta025 | 0:ed0b4e66d2ad | 84 | DHT_io = 1; |
Fanta025 | 0:ed0b4e66d2ad | 85 | wait_us(30); |
Fanta025 | 0:ed0b4e66d2ad | 86 | DHT_io.input(); |
Fanta025 | 0:ed0b4e66d2ad | 87 | // wait till the sensor grabs the bus |
Fanta025 | 0:ed0b4e66d2ad | 88 | if (ERROR_NONE != stall(DHT_io, 1, 40)) { |
Fanta025 | 0:ed0b4e66d2ad | 89 | return ERROR_NOT_PRESENT; |
Fanta025 | 0:ed0b4e66d2ad | 90 | } |
Fanta025 | 0:ed0b4e66d2ad | 91 | // sensor should signal low 80us and then hi 80us |
Fanta025 | 0:ed0b4e66d2ad | 92 | // *** RF : Une fois que la DHT11 détecte le signal de démarrage, il enverra un signal basse tension en réponse à ce niveau, ce qui |
Fanta025 | 0:ed0b4e66d2ad | 93 | //dure 80US. Ensuite, il fait passer le niveau de tension de faible à élevé et le maintient pour 80US. |
Fanta025 | 0:ed0b4e66d2ad | 94 | if (ERROR_NONE != stall(DHT_io, 0, 100)) { |
Fanta025 | 0:ed0b4e66d2ad | 95 | return ERROR_SYNC_TIMEOUT; |
Fanta025 | 0:ed0b4e66d2ad | 96 | } |
Fanta025 | 0:ed0b4e66d2ad | 97 | if (ERROR_NONE != stall(DHT_io, 1, 100)) { |
Fanta025 | 0:ed0b4e66d2ad | 98 | return ERROR_NO_PATIENCE; |
Fanta025 | 0:ed0b4e66d2ad | 99 | } |
Fanta025 | 0:ed0b4e66d2ad | 100 | // capture the data |
Fanta025 | 0:ed0b4e66d2ad | 101 | for (i = 0; i < 5; i++) { |
Fanta025 | 0:ed0b4e66d2ad | 102 | for (j = 0; j < 8; j++) { |
Fanta025 | 0:ed0b4e66d2ad | 103 | if (ERROR_NONE != stall(DHT_io, 0, 75)) { |
Fanta025 | 0:ed0b4e66d2ad | 104 | return ERROR_DATA_TIMEOUT; |
Fanta025 | 0:ed0b4e66d2ad | 105 | } |
Fanta025 | 0:ed0b4e66d2ad | 106 | // logic 0 is 28us max, 1 is 70us |
Fanta025 | 0:ed0b4e66d2ad | 107 | // *** RF : un bit 1 a une durée de 27uS en haute tension, un bit 0 a une durée de 70uS avec un niveau haut de tension |
Fanta025 | 0:ed0b4e66d2ad | 108 | //Lorsque les données du dernier bit est transmis, DHT11 tire le niveau de tension vers le bas et de garde pour 50us, |
Fanta025 | 0:ed0b4e66d2ad | 109 | wait_us(40); |
Fanta025 | 0:ed0b4e66d2ad | 110 | bit_value[i*8+j] = DHT_io; |
Fanta025 | 0:ed0b4e66d2ad | 111 | if (ERROR_NONE != stall(DHT_io, 1, 50)) { |
Fanta025 | 0:ed0b4e66d2ad | 112 | return ERROR_DATA_TIMEOUT; |
Fanta025 | 0:ed0b4e66d2ad | 113 | } |
Fanta025 | 0:ed0b4e66d2ad | 114 | } |
Fanta025 | 0:ed0b4e66d2ad | 115 | } |
Fanta025 | 0:ed0b4e66d2ad | 116 | // store the data |
Fanta025 | 0:ed0b4e66d2ad | 117 | for (i = 0; i < 5; i++) { |
Fanta025 | 0:ed0b4e66d2ad | 118 | b=0; |
Fanta025 | 0:ed0b4e66d2ad | 119 | for (j=0; j<8; j++) { |
Fanta025 | 0:ed0b4e66d2ad | 120 | if (bit_value[i*8+j] == 1) { |
Fanta025 | 0:ed0b4e66d2ad | 121 | b |= (1 << (7-j)); |
Fanta025 | 0:ed0b4e66d2ad | 122 | } |
Fanta025 | 0:ed0b4e66d2ad | 123 | } |
Fanta025 | 0:ed0b4e66d2ad | 124 | DHT_data[i]=b; |
Fanta025 | 0:ed0b4e66d2ad | 125 | } |
Fanta025 | 0:ed0b4e66d2ad | 126 | |
Fanta025 | 0:ed0b4e66d2ad | 127 | // uncomment to see the checksum error if it exists |
Fanta025 | 0:ed0b4e66d2ad | 128 | //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]); |
Fanta025 | 0:ed0b4e66d2ad | 129 | data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]; |
Fanta025 | 0:ed0b4e66d2ad | 130 | if (DHT_data[4] == data_valid) { |
Fanta025 | 0:ed0b4e66d2ad | 131 | _lastReadTime = currentTime; |
Fanta025 | 0:ed0b4e66d2ad | 132 | _lastTemperature = CalcTemperature(); |
Fanta025 | 0:ed0b4e66d2ad | 133 | _lastHumidity = CalcHumidity(); |
Fanta025 | 0:ed0b4e66d2ad | 134 | |
Fanta025 | 0:ed0b4e66d2ad | 135 | } else { |
Fanta025 | 0:ed0b4e66d2ad | 136 | err = ERROR_CHECKSUM; |
Fanta025 | 0:ed0b4e66d2ad | 137 | } |
Fanta025 | 0:ed0b4e66d2ad | 138 | |
Fanta025 | 0:ed0b4e66d2ad | 139 | return err; |
Fanta025 | 0:ed0b4e66d2ad | 140 | |
Fanta025 | 0:ed0b4e66d2ad | 141 | } |
Fanta025 | 0:ed0b4e66d2ad | 142 | |
Fanta025 | 0:ed0b4e66d2ad | 143 | float DHT::CalcTemperature() |
Fanta025 | 0:ed0b4e66d2ad | 144 | { |
Fanta025 | 0:ed0b4e66d2ad | 145 | int v; |
Fanta025 | 0:ed0b4e66d2ad | 146 | |
Fanta025 | 0:ed0b4e66d2ad | 147 | switch (_DHTtype) { |
Fanta025 | 0:ed0b4e66d2ad | 148 | case DHT11: |
Fanta025 | 0:ed0b4e66d2ad | 149 | v = DHT_data[2]; |
Fanta025 | 0:ed0b4e66d2ad | 150 | return float(v); |
Fanta025 | 0:ed0b4e66d2ad | 151 | case DHT22: |
Fanta025 | 0:ed0b4e66d2ad | 152 | v = DHT_data[2] & 0x7F; |
Fanta025 | 0:ed0b4e66d2ad | 153 | v *= 256; |
Fanta025 | 0:ed0b4e66d2ad | 154 | v += DHT_data[3]; |
Fanta025 | 0:ed0b4e66d2ad | 155 | v /= 10; |
Fanta025 | 0:ed0b4e66d2ad | 156 | if (DHT_data[2] & 0x80) |
Fanta025 | 0:ed0b4e66d2ad | 157 | v *= -1; |
Fanta025 | 0:ed0b4e66d2ad | 158 | return float(v); |
Fanta025 | 0:ed0b4e66d2ad | 159 | } |
Fanta025 | 0:ed0b4e66d2ad | 160 | return 0; |
Fanta025 | 0:ed0b4e66d2ad | 161 | } |
Fanta025 | 0:ed0b4e66d2ad | 162 | |
Fanta025 | 0:ed0b4e66d2ad | 163 | float DHT::ReadHumidity() |
Fanta025 | 0:ed0b4e66d2ad | 164 | { |
Fanta025 | 0:ed0b4e66d2ad | 165 | return _lastHumidity; |
Fanta025 | 0:ed0b4e66d2ad | 166 | } |
Fanta025 | 0:ed0b4e66d2ad | 167 | |
Fanta025 | 0:ed0b4e66d2ad | 168 | float DHT::ConvertCelciustoFarenheit(float const celsius) |
Fanta025 | 0:ed0b4e66d2ad | 169 | { |
Fanta025 | 0:ed0b4e66d2ad | 170 | return celsius * 9 / 5 + 32; |
Fanta025 | 0:ed0b4e66d2ad | 171 | } |
Fanta025 | 0:ed0b4e66d2ad | 172 | |
Fanta025 | 0:ed0b4e66d2ad | 173 | float DHT::ConvertCelciustoKelvin(float const celsius) |
Fanta025 | 0:ed0b4e66d2ad | 174 | { |
Fanta025 | 0:ed0b4e66d2ad | 175 | return celsius + 273.15; |
Fanta025 | 0:ed0b4e66d2ad | 176 | } |
Fanta025 | 0:ed0b4e66d2ad | 177 | |
Fanta025 | 0:ed0b4e66d2ad | 178 | // dewPoint function NOAA |
Fanta025 | 0:ed0b4e66d2ad | 179 | // reference: http://wahiduddin.net/calc/density_algorithms.htm |
Fanta025 | 0:ed0b4e66d2ad | 180 | float DHT::CalcdewPoint(float const celsius, float const humidity) |
Fanta025 | 0:ed0b4e66d2ad | 181 | { |
Fanta025 | 0:ed0b4e66d2ad | 182 | float A0= 373.15/(273.15 + celsius); |
Fanta025 | 0:ed0b4e66d2ad | 183 | float SUM = -7.90298 * (A0-1); |
Fanta025 | 0:ed0b4e66d2ad | 184 | SUM += 5.02808 * log10(A0); |
Fanta025 | 0:ed0b4e66d2ad | 185 | SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; |
Fanta025 | 0:ed0b4e66d2ad | 186 | SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; |
Fanta025 | 0:ed0b4e66d2ad | 187 | SUM += log10(1013.246); |
Fanta025 | 0:ed0b4e66d2ad | 188 | float VP = pow(10, SUM-3) * humidity; |
Fanta025 | 0:ed0b4e66d2ad | 189 | float T = log(VP/0.61078); // temp var |
Fanta025 | 0:ed0b4e66d2ad | 190 | return (241.88 * T) / (17.558-T); |
Fanta025 | 0:ed0b4e66d2ad | 191 | } |
Fanta025 | 0:ed0b4e66d2ad | 192 | |
Fanta025 | 0:ed0b4e66d2ad | 193 | // delta max = 0.6544 wrt dewPoint() |
Fanta025 | 0:ed0b4e66d2ad | 194 | // 5x faster than dewPoint() |
Fanta025 | 0:ed0b4e66d2ad | 195 | // reference: http://en.wikipedia.org/wiki/Dew_point |
Fanta025 | 0:ed0b4e66d2ad | 196 | float DHT::CalcdewPointFast(float const celsius, float const humidity) |
Fanta025 | 0:ed0b4e66d2ad | 197 | { |
Fanta025 | 0:ed0b4e66d2ad | 198 | float a = 17.271; |
Fanta025 | 0:ed0b4e66d2ad | 199 | float b = 237.7; |
Fanta025 | 0:ed0b4e66d2ad | 200 | float temp = (a * celsius) / (b + celsius) + log(humidity/100); |
Fanta025 | 0:ed0b4e66d2ad | 201 | float Td = (b * temp) / (a - temp); |
Fanta025 | 0:ed0b4e66d2ad | 202 | return Td; |
Fanta025 | 0:ed0b4e66d2ad | 203 | } |
Fanta025 | 0:ed0b4e66d2ad | 204 | |
Fanta025 | 0:ed0b4e66d2ad | 205 | float DHT::ReadTemperature(eScale Scale) |
Fanta025 | 0:ed0b4e66d2ad | 206 | { |
Fanta025 | 0:ed0b4e66d2ad | 207 | if (Scale == FARENHEIT) |
Fanta025 | 0:ed0b4e66d2ad | 208 | return ConvertCelciustoFarenheit(_lastTemperature); |
Fanta025 | 0:ed0b4e66d2ad | 209 | else if (Scale == KELVIN) |
Fanta025 | 0:ed0b4e66d2ad | 210 | return ConvertCelciustoKelvin(_lastTemperature); |
Fanta025 | 0:ed0b4e66d2ad | 211 | else |
Fanta025 | 0:ed0b4e66d2ad | 212 | return _lastTemperature; |
Fanta025 | 0:ed0b4e66d2ad | 213 | } |
Fanta025 | 0:ed0b4e66d2ad | 214 | |
Fanta025 | 0:ed0b4e66d2ad | 215 | float DHT::CalcHumidity() |
Fanta025 | 0:ed0b4e66d2ad | 216 | { |
Fanta025 | 0:ed0b4e66d2ad | 217 | int v; |
Fanta025 | 0:ed0b4e66d2ad | 218 | |
Fanta025 | 0:ed0b4e66d2ad | 219 | switch (_DHTtype) { |
Fanta025 | 0:ed0b4e66d2ad | 220 | case DHT11: |
Fanta025 | 0:ed0b4e66d2ad | 221 | v = DHT_data[0]; |
Fanta025 | 0:ed0b4e66d2ad | 222 | return float(v); |
Fanta025 | 0:ed0b4e66d2ad | 223 | case DHT22: |
Fanta025 | 0:ed0b4e66d2ad | 224 | v = DHT_data[0]; |
Fanta025 | 0:ed0b4e66d2ad | 225 | v *= 256; |
Fanta025 | 0:ed0b4e66d2ad | 226 | v += DHT_data[1]; |
Fanta025 | 0:ed0b4e66d2ad | 227 | v /= 10; |
Fanta025 | 0:ed0b4e66d2ad | 228 | return float(v); |
Fanta025 | 0:ed0b4e66d2ad | 229 | } |
Fanta025 | 0:ed0b4e66d2ad | 230 | return 0; |
Fanta025 | 0:ed0b4e66d2ad | 231 | } |
Fanta025 | 0:ed0b4e66d2ad | 232 | |
Fanta025 | 0:ed0b4e66d2ad | 233 |