Temp and Humidity sensor device driver. had to fork under a new name, don't know why yet.

Dependents:   Wio-example-SORACOM-Harvest

Committer:
lamell
Date:
Fri May 08 11:40:25 2020 -0400
Revision:
9:64164cbcbab8
Parent:
8:4a66ebac2a2c
Really nothing was modified.

Who changed what in which revision?

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