-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDHT11.cpp
140 lines (114 loc) · 3.37 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
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
#include <DHT11.h>
#include <Arduino.h>
#include <math.h>
#include <string.h>
DHT11::DHT11(int dht11Pin)
{
this -> dht11Pin = dht11Pin; // Make the pin number can be accessed wherever in the class
memset(this->buffer, 0, sizeof(this->buffer)); // Init the buffer
/* Give the bus a original state */
pinMode(dht11Pin, OUTPUT);
digitalWrite(dht11Pin, HIGH);
}
int DHT11::getData(void)
{
/* Prepare */
int dht11Pin = this -> dht11Pin;
unsigned long waitTime = 0; // NOTE: time in MICROSECONDS
unsigned long signalLen = 0;
int bitCount = 0;
int checksum = 0;
long int receivedChecksum = 0;
long int caculatedChecksum = 0;
/* Wake up DHT11 */
pinMode(dht11Pin, OUTPUT);
digitalWrite(dht11Pin, LOW);
delay(20);
digitalWrite(dht11Pin, HIGH);
delayMicroseconds(40);
digitalWrite(dht11Pin, LOW);
/* Begin to listen from DHT11 */
pinMode(dht11Pin, INPUT);
/* Wait for DHT11's reply */
waitTime = micros(); // Reset the timer
while (digitalRead(dht11Pin) != HIGH)
if (micros() - waitTime >= 1000000)
{
delay(100);
return -1;
} // Connection time out, give up the connection
/* Wait until finishing receiving the 80us reply signal */
waitTime = micros(); // Reset the timer
while (digitalRead(dht11Pin) != LOW )
{
if (micros() - waitTime >= 1000000)
{
delay(100);
return -2; // Connection time out, give up the connection
}
}
/* Receive data, 1 bit each loop */
for (int bitCount = 0; bitCount < 40; bitCount++)
{
/* Wait through the 50us bit signal */
while (digitalRead(dht11Pin) == LOW)
;
/* Receive and storage the data */
/* Get the length of the signal */
waitTime = micros();
while (digitalRead(dht11Pin) == HIGH)
;
signalLen = micros() - waitTime;
/* Tell the signal means 1 or 0 */
if (signalLen > 50)
this -> buffer[bitCount] = 1; // 1
if (signalLen < 50)
this -> buffer[bitCount] = 0; // 0
}
for (int i = 0; i < 40; i++)
Serial.print(this->buffer[i]);
/* Use the checksum to verify if the data received is correct */
/* Convert checksum that received from DHT11 */
receivedChecksum = 0;
for (int i = 0; i < 8; i ++)
receivedChecksum += this->buffer[39-i] * pow(2, i);
/* Calculate checksum according to the data received */
int tempSum = 0; // Variable for temporary usage
for (int i = 0; i < 32;)
for (int j = 7; j >= 0; j--)
tempSum += this->buffer[i++] * pow(2, j);
caculatedChecksum = tempSum % 256; // Cut off the number higher than 8 bit
/* Verify */
Serial.print(caculatedChecksum);Serial.println(receivedChecksum); //debuging message
if (caculatedChecksum != receivedChecksum)
return -3; // Data incorrect
return 0;
}
int DHT11::getTemp(void)
{
/* Declare variables */
int temp = 0;
int returnValue = 0;
/* Call the getData() function */
returnValue = getData();
if (returnValue != 0)
return returnValue; // Return the error message
/* Get the raw temperature out of the buffer and return it */
for (int i = 7; i >= 0; i--)
temp += this -> buffer[23-i] * pow(2, i);
return temp;
}
int DHT11::getHumi(void)
{
/* Declare variables */
int humi = 0;
int returnValue = 0;
/* Call the getData() function */
returnValue = getData();
if (returnValue != 0)
return returnValue;
/* Get data from the buffer */
for (int i = 7; i >= 0; i--)
humi += this -> buffer[23-i] * pow(2, i);
return humi;
}