-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht11.cpp
85 lines (71 loc) · 1.63 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
80
81
82
83
84
#include <stdint.h>
#include "Arduino.h"
#include "dht11.h"
const int MAX_BIT_US = 100;
DHT11::DHT11(int pin)
{
dht11_pin = pin;
}
void DHT11::init(void)
{
// default pin state is high (pulled up)
pinMode(dht11_pin, INPUT);
// wait for initialisation
unsigned long m = millis();
if (m < 1000) {
delay(1000 - m);
}
}
static int dht_bit(int pin)
{
// wait for rising edge
unsigned long t0 = micros();
while (digitalRead(pin) == LOW) {
if ((micros() - t0) > MAX_BIT_US) {
return -1;
}
}
// wait for falling edge
unsigned long t1 = micros();
while (digitalRead(pin) == HIGH) {
if ((micros() - t1) > MAX_BIT_US) {
return -2;
}
}
// return length of high part
return (int)(micros() - t1);
}
bool DHT11::read(int *humidity, int *temperature)
{
uint8_t buf[5];
// send wake up pulse
pinMode(dht11_pin, OUTPUT);
digitalWrite(dht11_pin, LOW);
delay(18);
pinMode(dht11_pin, INPUT);
delayMicroseconds(50);
// check start bit
if (dht_bit(dht11_pin) < 0) {
return false;
}
// read 40 data bits
for (int i = 0; i < 5; i++) {
uint8_t b = 0;
for (int j = 0; j < 8; j++) {
int us = dht_bit(dht11_pin);
b = b << 1;
if (us >= 48) {
b |= 1;
}
}
buf[i] = b;
}
// verify checksum
uint8_t sum = buf[0] + buf[1] + buf[2] + buf[3];
if (sum != buf[4]) {
return false;
}
*humidity = buf[0];
*temperature = buf[2];
return true;
}