DHT testprogram

Dependencies:   mbed

Fork of DHT_TestProgram by Wim De Roeve

Committer:
Wimpie
Date:
Tue Jul 10 13:11:23 2012 +0000
Revision:
0:9ef709bfeb08
Child:
1:731de3b11b74

        

Who changed what in which revision?

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