DHT sensor LIB

Dependents:   DHT22tester LASS_LoRa_mbed TestingDHT22sensor_fork1 Test_sensor_DHT22

Fork of DHT by Wim De Roeve

Committer:
gert_lauritsen
Date:
Fri Feb 05 10:03:57 2016 +0000
Revision:
1:6ddabfde1db6
Parent:
0:9b5b3200688f
Child:
2:cd93a537b2e7
There was an error in converting to float, trunkating the value

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