forked from UCTRONICS/Arducam_ESP32_TVOC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht.cpp
129 lines (103 loc) · 3.27 KB
/
dht.cpp
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
#include "dht.h"
void DHT::attach(int pin)
{
_pin = pin;
pinMode(_pin, OUTPUT);
digitalWrite(_pin, HIGH);
}
DHTError DHT::update()
{
_lastError = DHT_ERROR_OK;
byte res;
// prepare sensor for reading
digitalWrite(_pin, LOW);
delay(18 + 5);
digitalWrite(_pin, HIGH);
// DHT datasheet says host should keep line high 20-40us,
// then watch for sensor taking line low.
// That low should last 80us. Acknowledges "start read
// and report" command.
delayMicroseconds(40);
// Next: Change Arduino pin to an input, to
// watch for the 80us low explained a moment ago.
pinMode(_pin, INPUT);
delayMicroseconds(40);
res = digitalRead(_pin);
if (res) {
_lastError = DHT_ERROR_START_FAILED_1;
return _lastError;
}
delayMicroseconds(80);
res = digitalRead(_pin);
if (!res) {
_lastError = DHT_ERROR_START_FAILED_2;
return _lastError;
}
// After 80us low, the line should be taken high for 80us by the
// sensor. The low following that high is the start of the first
// bit of the forty to come. The method readByte()
// expects to be called with the system already into this low.
delayMicroseconds(80);
// now ready for data reception... pick up the 5 bytes coming from
// the sensor
for (byte i = 0; i < 5; i++)
if (!readByte(_data + i))
_lastError = DHT_ERROR_READ_TIMEOUT;
// Restore pin to output duties
pinMode(_pin, OUTPUT); // was: DDRC |= _BV(dht_PIN);
digitalWrite(_pin, HIGH); // was: PORTC |= _BV(dht_PIN);
if (!_lastError) {
// See if data received consistent with checksum received
byte checkSum = _data[0] + _data[1] + _data[2] + _data[3];
if (_data[4] != checkSum)
_lastError = DHT_ERROR_CHECKSUM_FAILURE;
}
return _lastError;
}
bool DHT::readByte(byte* out) const
{
// Restrict waiting to prevent hanging
unsigned long numloops = 0;
unsigned long maxloops = microsecondsToClockCycles(100) / 16;
// Collect 8 bits from datastream, return them interpreted
// as a byte. I.e. if 0000.0101 is sent, return decimal 5.
byte result = 0;
for (byte i = 8; i--; ) {
// We enter this during the first start bit (low for 50uS) of the byte
// Wait until pin goes high
numloops = 0;
while (digitalRead(_pin) == LOW)
if (++numloops == maxloops)
return false;
// Dataline will now stay high for 27 or 70 uS, depending on
// whether a 0 or a 1 is being sent, respectively. Take to
// a middle of that period to read the value
delayMicroseconds(45);
if (digitalRead(_pin) == HIGH)
result |= 1 << i; // set subsequent bit
// Wait until pin goes low again, which signals the START
// of the NEXT bit's transmission.
numloops = 0;
while (digitalRead(_pin) == HIGH)
if (++numloops == maxloops)
return false;
}
*out = result;
return true;
}
byte DHT::getTemperatureInt() const
{
return _data[2];
}
byte DHT::getTemperatureFrac() const
{
return _data[3];
}
byte DHT::getHumidityInt() const
{
return _data[0];
}
byte DHT::getHumidityFrac() const
{
return _data[1];
}