forked from adafruit/DHT-sensor-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDHT.h
120 lines (101 loc) · 4.04 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
119
120
#ifndef DHT_H
#define DHT_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "limits.h"
#include "DHT_TempHumidUtils.h"
/***************************************************************************
* DHT sensor library
* https://github.com/zacronos/DHT-sensor-library
* (re)written by Joe Ibershoff
* distributed under MIT license
*
* based on the library originally written by Adafruit Industries
* https://github.com/adafruit/DHT-sensor-library
*
* fix for readings shortly after a failure based on Glenn Ramsey's fork
* https://github.com/glennra/DHT-sensor-library
***************************************************************************/
// different versions of the sensor; pass one of these in as the "type"
// parameter when constructing a DHT object
#define DHT_SENSOR_TYPE_DHT11 11
#define DHT_SENSOR_TYPE_DHT21 21
#define DHT_SENSOR_TYPE_AM2301 21
#define DHT_SENSOR_TYPE_DHT22 22
#define DHT_SENSOR_TYPE_AM2303 22
// milliseconds to wait on HIGH before sending the start signal the first time
#define DHT_FIRST_START_DELAY 250
// milliseconds to wait on HIGH before sending the start signal the other times
#define DHT_LATER_START_DELAYS 20
// how much data we want to read
#define DHT_NUM_BYTES 5
class DHT {
public:
// constructor and initializer
DHT(uint8_t pin, uint8_t type);
void begin();
// readSensorData() does the actual magic of reading data from the
// sensor and storing them in the buffer, returning a flag indicating
// whether the data in the buffer is valid. If readSensorData() is
// called more than once in less time than is allowed for the sensor,
// readSensorData() will not attempt to read from the sensor again,
// and instead will just return the flag value last returned.
//
// If you want to have full control over what happens when, you will
// want to use this function and the get*() functions; otherwise, you
// can just use the convenience read*() functions.
boolean readSensorData();
// the get*() functions read the data from the buffer, and in the case
// of getTemperatureFahrenheit(), converts the value from Celsius. If
// any of these functions returns NAN, then you will have to try again
// after the minimum sampling delay has passed
//
// depending on the sensor type, temperature is correct to within:
// DHT11: +/- 2.0 degrees C
// DHT21: +/- 1.0 degree C
// DHT22: +/- 0.2 degrees C
//
// depending on the sensor type, temperature is correct to within:
// DHT11: +/- 3.60 degrees F
// DHT21: +/- 1.80 degrees F
// DHT22: +/- 0.36 degrees F
//
// depending on the sensor type, percent humidity is correct to within:
// DHT11: +/- 5%
// DHT21: +/- 3% to 5%
// DHT22: +/- 2% to 5%
//
float getTemperatureCelsius();
float getTemperatureFahrenheit();
float getPercentHumidity();
// the read*() functions are convenience functions that encapsulate a
// readSensorData() call followed by any get*() and conversions needed
float readTemperatureCelsius();
float readTemperatureFahrenheit();
float readPercentHumidity();
// note that there are no corresponding getHeatIndex*() functions;
// this is because the heat index is a derived value, and is not
// actually present in the data buffers.
//
// See DHT_TempHumidUtils.h for notes on the accuracy of the heat
// index calculation, but also keep in mind that the error in the
// temperature and percentHumidity values will be compounded when
// passed through the heat index calculation, so the overall error
// bounds is larger than any of the individual error bounds
float readHeatIndexCelsius();
float readHeatIndexFahrenheit();
private:
uint8_t pin_, type_;
uint16_t minSampleDelayMillis_;
uint8_t data_[6];
unsigned long lastReadTime_;
boolean firstReading_;
boolean validData_;
boolean prepareRead();
int16_t timeSignalLength(uint8_t signalState);
int8_t readBit();
};
#endif