dht library

Dependents:   prog_envoyer_B_v3

Fork of DHT by Wim De Roeve

Committer:
guillouhouede
Date:
Sat May 13 16:27:52 2017 +0000
Revision:
1:a2bc4effa732
Parent:
0:9b5b3200688f
dht;

Who changed what in which revision?

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