B16

Dependencies:   mbed

Committer:
57340500084
Date:
Wed Dec 09 02:50:22 2015 +0000
Revision:
0:b0ed3674f5b2
16B

Who changed what in which revision?

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