forked from beegee-tokyo/DHTesp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDHTesp.h
181 lines (150 loc) · 6.54 KB
/
DHTesp.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/******************************************************************
DHT Temperature & Humidity Sensor library for Arduino & ESP32.
Features:
- Support for DHT11 and DHT22/AM2302/RHT03
- Auto detect sensor model
- Very low memory footprint
- Very small code
https://github.com/beegee-tokyo/arduino-DHTesp
Written by Mark Ruys, [email protected].
Updated to work with ESP32 by Bernd Giesecke, [email protected]
GNU General Public License, check LICENSE 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
2017-12-12: Added task switch disable
Added computeHeatIndex function from Adafruit DNT library
2017-12-14: Added computeDewPoint function from idDHTLib Library
Added getComfortRatio function from libDHT Library
2017-12-15: Added computePerception function
2018-01-02: Added example for multiple sensors usage.
2018-01-03: Added function getTempAndHumidity which returns temperature and humidity in one call.
2018-01-03: Added retry in case the reading from the sensor fails with a timeout.
2018-01-08: Added ESP8266 (and probably AVR) compatibility.
2018-03-11: Updated DHT example
2018-06-19: Updated DHT example to distinguish between ESP8266 examples and ESP32 examples
2018-07-06: Fixed bug in ESP32 example
2018-07-17: Use correct field separator in keywords.txt + corrected wrong deprecation
2019-03-07: Added computeAbsoluteHumidity which returns the absolute humidity in g/m³
2019-03-22: Fixed auto detection problem
2019-07-26: Added getPin function
******************************************************************/
#ifndef dhtesp_h
#define dhtesp_h
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
// Reference: http://epb.apogee.net/res/refcomf.asp (References invalid)
enum ComfortState {
Comfort_OK = 0,
Comfort_TooHot = 1,
Comfort_TooCold = 2,
Comfort_TooDry = 4,
Comfort_TooHumid = 8,
Comfort_HotAndHumid = 9,
Comfort_HotAndDry = 5,
Comfort_ColdAndHumid = 10,
Comfort_ColdAndDry = 6
};
// References https://en.wikipedia.org/wiki/Dew_point ==> Relationship to human comfort
enum PerceptionState {
Perception_Dry = 0,
Perception_VeryComfy = 1,
Perception_Comfy = 2,
Perception_Ok = 3,
Perception_UnComfy = 4,
Perception_QuiteUnComfy = 5,
Perception_VeryUnComfy = 6,
Perception_SevereUncomfy = 7
};
struct TempAndHumidity {
float temperature;
float humidity;
};
struct ComfortProfile
{
//Represent the 4 line equations:
//dry, humid, hot, cold, using the y = mx + b formula
float m_tooHot_m, m_tooHot_b;
float m_tooCold_m, m_tooHCold_b;
float m_tooDry_m, m_tooDry_b;
float m_tooHumid_m, m_tooHumid_b;
inline bool isTooHot(float temp, float humidity) {return (temp > (humidity * m_tooHot_m + m_tooHot_b));}
inline bool isTooHumid(float temp, float humidity) {return (temp > (humidity * m_tooHumid_m + m_tooHumid_b));}
inline bool isTooCold(float temp, float humidity) {return (temp < (humidity * m_tooCold_m + m_tooHCold_b));}
inline bool isTooDry(float temp, float humidity) {return (temp < (humidity * m_tooDry_m + m_tooDry_b));}
inline float distanceTooHot(float temp, float humidity) {return temp - (humidity * m_tooHot_m + m_tooHot_b);}
inline float distanceTooHumid(float temp, float humidity) {return temp - (humidity * m_tooHumid_m + m_tooHumid_b);}
inline float distanceTooCold(float temp, float humidity) {return (humidity * m_tooCold_m + m_tooHCold_b) - temp;}
inline float distanceTooDry(float temp, float humidity) {return (humidity * m_tooDry_m + m_tooDry_b) - temp;}
};
class DHTesp
{
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;
TempAndHumidity values;
// setup(dhtPin) is deprecated, auto detection is not working well on ESP32. Use setup(dhtPin, DHTesp::DHT11) instead!
void setup(uint8_t dhtPin) __attribute__((deprecated));
void setup(uint8_t pin, DHT_MODEL_t model=AUTO_DETECT);
void resetTimer();
float getTemperature();
float getHumidity();
TempAndHumidity getTempAndHumidity();
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; };
float computeHeatIndex(float temperature, float percentHumidity, bool isFahrenheit=false);
float computeDewPoint(float temperature, float percentHumidity, bool isFahrenheit=false);
float getComfortRatio(ComfortState& destComfStatus, float temperature, float percentHumidity, bool isFahrenheit=false);
ComfortProfile getComfortProfile() {return m_comfort;}
void setComfortProfile(ComfortProfile& c) {m_comfort = c;}
inline bool isTooHot(float temp, float humidity) {return m_comfort.isTooHot(temp, humidity);}
inline bool isTooHumid(float temp, float humidity) {return m_comfort.isTooHumid(temp, humidity);}
inline bool isTooCold(float temp, float humidity) {return m_comfort.isTooCold(temp, humidity);}
inline bool isTooDry(float temp, float humidity) {return m_comfort.isTooDry(temp, humidity);}
byte computePerception(float temperature, float percentHumidity, bool isFahrenheit=false);
float computeAbsoluteHumidity(float temperature, float percentHumidity, bool isFahrenheit=false);
uint8_t getPin() { return pin; }
protected:
void readSensor();
float temperature;
float humidity;
uint8_t pin;
private:
DHT_MODEL_t model;
DHT_ERROR_t error;
unsigned long lastReadTime;
ComfortProfile m_comfort;
};
#endif /*dhtesp_h*/