-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHT.h
119 lines (100 loc) · 2.43 KB
/
DHT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/******************************************************************
* DHT Temperature & Humidity Sensor library for Arduino.
*
* Features:
* - Support for DHT11 and DHT22/AM2302/RHT03
* - Auto detect sensor model
* - Very low memory footprint
* - Very small code
*
* http://www.github.com/markruys/arduino-DHT
*
* Written by Mark Ruys, mark@paracas.nl.
*
* BSD license, check license.txt for more information.
* All text above must be included in any redistribution.
*
* Datasheets:
* - http://www.micro4you.com/files/sensor/DHT11.pdf
* - http://www.adafruit.com/datasheets/DHT22.pdf
* - http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Weather/RHT03.pdf
* - http://meteobox.tk/files/AM2302.pdf
*
* Changelog:
* 2013-06-10: Initial version
* 2013-06-12: Refactored code
* 2013-07-01: Add a resetTimer method
******************************************************************/
#ifndef dht_h
#define dht_h
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
class DHT
{
public:
typedef enum {
AUTO_DETECT,
DHT11,
DHT22,
AM2302, // Packaged DHT22
RHT03 // Equivalent to DHT22
}
DHT_MODEL_t;
typedef enum {
ERROR_NONE = 0,
ERROR_TIMEOUT,
ERROR_CHECKSUM
}
DHT_ERROR_t;
void setup(uint8_t pin, DHT_MODEL_t model=AUTO_DETECT);
void resetTimer();
float getTemperature();
float getHumidity();
DHT_ERROR_t getStatus() {
return error;
};
const char* getStatusString();
DHT_MODEL_t getModel() {
return model;
}
int getMinimumSamplingPeriod() {
return model == DHT11 ? 1000 : 2000;
}
int8_t getNumberOfDecimalsTemperature() {
return model == DHT11 ? 0 : 1;
};
int8_t getLowerBoundTemperature() {
return model == DHT11 ? 0 : -40;
};
int8_t getUpperBoundTemperature() {
return model == DHT11 ? 50 : 125;
};
int8_t getNumberOfDecimalsHumidity() {
return 0;
};
int8_t getLowerBoundHumidity() {
return model == DHT11 ? 20 : 0;
};
int8_t getUpperBoundHumidity() {
return model == DHT11 ? 90 : 100;
};
static float toFahrenheit(float fromCelcius) {
return 1.8 * fromCelcius + 32.0;
};
static float toCelsius(float fromFahrenheit) {
return (fromFahrenheit - 32.0) / 1.8;
};
protected:
void readSensor();
float temperature;
float humidity;
uint8_t pin;
private:
DHT_MODEL_t model;
DHT_ERROR_t error;
unsigned long lastReadTime;
};
#endif /*dht_h*/