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:
Wed May 08 13:47:41 2019 -0400
Revision:
6:49350e4b126d
Parent:
5:28eb2ec7c6d6
Child:
8:4a66ebac2a2c
Modified the library so older sensors give less errors in the readings.
Works with all DHt22, DHt11, AM2302, AM2305 sensor family.

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 *
Wimpie 0:9b5b3200688f 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
Wimpie 0:9b5b3200688f 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
Wimpie 0:9b5b3200688f 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 #ifndef MBED_DHT_H
Wimpie 0:9b5b3200688f 35 #define MBED_DHT_H
Wimpie 0:9b5b3200688f 36
Wimpie 0:9b5b3200688f 37 #include "mbed.h"
Wimpie 0:9b5b3200688f 38
lamell 6:49350e4b126d 39 enum eType{
lamell 6:49350e4b126d 40 DHT11 = 11,
lamell 6:49350e4b126d 41 SEN11301P = 11,
lamell 6:49350e4b126d 42 RHT01 = 11,
lamell 6:49350e4b126d 43 DHT22 = 22,
lamell 6:49350e4b126d 44 AM2302 = 22,
lamell 6:49350e4b126d 45 SEN51035P = 22,
lamell 6:49350e4b126d 46 RHT02 = 22,
lamell 6:49350e4b126d 47 RHT03 = 22
lamell 6:49350e4b126d 48 } ;
Wimpie 0:9b5b3200688f 49
lamell 6:49350e4b126d 50 enum eError {
Wimpie 0:9b5b3200688f 51 ERROR_NONE = 0,
lamell 6:49350e4b126d 52 BUS_BUSY =1,
lamell 6:49350e4b126d 53 ERROR_NOT_PRESENT =2 ,
lamell 6:49350e4b126d 54 ERROR_ACK_TOO_LONG =3 ,
lamell 6:49350e4b126d 55 ERROR_SYNC_TIMEOUT = 4,
lamell 6:49350e4b126d 56 ERROR_DATA_TIMEOUT =5 ,
lamell 6:49350e4b126d 57 ERROR_CHECKSUM = 6,
lamell 6:49350e4b126d 58 ERROR_NO_PATIENCE =7
lamell 6:49350e4b126d 59 } ;
Wimpie 0:9b5b3200688f 60
lamell 6:49350e4b126d 61 typedef enum {
lamell 6:49350e4b126d 62 CELCIUS =0 ,
lamell 6:49350e4b126d 63 FARENHEIT =1,
lamell 6:49350e4b126d 64 KELVIN=2
lamell 5:28eb2ec7c6d6 65 } eScale;
Wimpie 0:9b5b3200688f 66
Wimpie 0:9b5b3200688f 67
lamell 6:49350e4b126d 68 class DHT {
Wimpie 0:9b5b3200688f 69
Wimpie 0:9b5b3200688f 70 public:
Wimpie 0:9b5b3200688f 71
lamell 6:49350e4b126d 72 DHT(PinName pin,int DHTtype);
Wimpie 0:9b5b3200688f 73 ~DHT();
lamell 6:49350e4b126d 74 int readData(void);
Wimpie 0:9b5b3200688f 75 float ReadHumidity(void);
lamell 6:49350e4b126d 76 float ReadTemperature(eScale Scale);
lamell 6:49350e4b126d 77 float CalcdewPoint(float celsius, float humidity);
lamell 6:49350e4b126d 78 float CalcdewPointFast(float celsius, float humidity);
Wimpie 0:9b5b3200688f 79
Wimpie 0:9b5b3200688f 80 private:
Wimpie 0:9b5b3200688f 81 time_t _lastReadTime;
Wimpie 0:9b5b3200688f 82 float _lastTemperature;
Wimpie 0:9b5b3200688f 83 float _lastHumidity;
Wimpie 0:9b5b3200688f 84 PinName _pin;
Wimpie 0:9b5b3200688f 85 bool _firsttime;
lamell 6:49350e4b126d 86 int _DHTtype;
lamell 6:49350e4b126d 87 int DHT_data[6];
Wimpie 0:9b5b3200688f 88 float CalcTemperature();
Wimpie 0:9b5b3200688f 89 float CalcHumidity();
lamell 6:49350e4b126d 90 float ConvertCelciustoFarenheit(float);
lamell 6:49350e4b126d 91 float ConvertCelciustoKelvin(float);
Wimpie 0:9b5b3200688f 92
Wimpie 0:9b5b3200688f 93 };
Wimpie 0:9b5b3200688f 94
Wimpie 0:9b5b3200688f 95 #endif