-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht11.cpp
79 lines (65 loc) · 1.79 KB
/
dht11.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
#include "dht11.h"
int dht11::read(uint8_t pin) {
// READ VALUES
int rv = _readSensor(pin, DHTLIB_DHT11_WAKEUP);
if (rv != DHTLIB_OK) {
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
temperature = DHTLIB_INVALID_VALUE; // invalid value
return rv;
}
// CONVERT AND STORE
humidity = bits[0]; // bits[1] == 0;
temperature = bits[2]; // bits[3] == 0;
// TEST CHECKSUM
// bits[1] && bits[3] both 0
uint8_t sum = bits[0] + bits[2];
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK;
}
int dht11::_readSensor(uint8_t pin, uint8_t wakeupDelay) {
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t mask = 128;
uint8_t idx = 0;
// EMPTY BUFFER
for (uint8_t i = 0; i < 5; i++) bits[i] = 0;
// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(wakeupDelay);
digitalWrite(pin, HIGH);
delayMicroseconds(40);
pinMode(pin, INPUT);
// GET ACKNOWLEDGE or TIMEOUT
uint16_t loopCnt = DHTLIB_TIMEOUT;
while (digitalRead(pin) == LOW) {
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
loopCnt = DHTLIB_TIMEOUT;
while (digitalRead(pin) == HIGH) {
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
// READ THE OUTPUT - 40 BITS => 5 BYTES
for (uint8_t i = 40; i != 0; i--) {
loopCnt = DHTLIB_TIMEOUT;
while (digitalRead(pin) == LOW) {
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
uint32_t t = micros();
loopCnt = DHTLIB_TIMEOUT;
while (digitalRead(pin) == HIGH) {
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
if ((micros() - t) > 40) {
bits[idx] |= mask;
}
mask >>= 1;
if (mask == 0) // next byte?
{
mask = 128;
idx++;
}
}
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
return DHTLIB_OK;
}