concordia

Dependencies:   mbed

Committer:
urietony
Date:
Tue Mar 07 00:38:17 2017 +0000
Revision:
0:1d0724ab888b
lalala

Who changed what in which revision?

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