FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ST_Anything/Arduino/libraries/DHT/dht.cpp at master · DanielOgorchock/ST_Anything · GitHub
DanielOgorchock
/
ST_Anything
Public
Notifications
You must be signed in to change notification settings
Fork
435
Star
438
Code
Issues
2
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
ST_Anything
/
Arduino
/
libraries
/
DHT
/
dht.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
169 lines (149 loc) · 4.09 KB
Breadcrumbs
ST_Anything
/
Arduino
/
libraries
/
DHT
/
dht.cpp
Copy path
File metadata and controls
169 lines (149 loc) · 4.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
//
FILE: dht.cpp
//
AUTHOR: Rob Tillaart
//
VERSION: 0.2.0
//
PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//
URL: https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTstable
//
//
HISTORY:
//
0.2.1 2017-09-20 fix https://github.com/RobTillaart/Arduino/issues/80
//
0.2.0 2017-07-24 fix https://github.com/RobTillaart/Arduino/issues/31 + 33
//
0.1.13 fix negative temperature
//
0.1.12 support DHT33 and DHT44 initial version
//
0.1.11 renamed DHTLIB_TIMEOUT
//
0.1.10 optimized faster WAKEUP + TIMEOUT
//
0.1.09 optimize size: timeout check + use of mask
//
0.1.08 added formula for timeout based upon clockspeed
//
0.1.07 added support for DHT21
//
0.1.06 minimize footprint (2012-12-27)
//
0.1.05 fixed negative temperature bug (thanks to Roseman)
//
0.1.04 improved readability of code using DHTLIB_OK in code
//
0.1.03 added error values for temp and humidity when read failed
//
0.1.02 added error codes
//
0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)
//
0.1.0 by Rob Tillaart (01/04/2011)
//
//
inspired by DHT11 library
//
//
Released to the public domain
//
#
include
"
dht.h
"
//
///////////////////////////////////////////////////
//
//
PUBLIC
//
//
return values:
//
DHTLIB_OK
//
DHTLIB_ERROR_CHECKSUM
//
DHTLIB_ERROR_TIMEOUT
int
dht::read11
(
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
uint8_t
sum = bits[
0
] + bits[
1
] + bits[
2
] + bits[
3
];
if
(bits[
4
] != sum)
{
return
DHTLIB_ERROR_CHECKSUM
;
}
return
DHTLIB_OK
;
}
//
return values:
//
DHTLIB_OK
//
DHTLIB_ERROR_CHECKSUM
//
DHTLIB_ERROR_TIMEOUT
int
dht::read
(
uint8_t
pin)
{
//
READ VALUES
int
rv =
_readSensor
(pin,
DHTLIB_DHT_WAKEUP
);
if
(rv !=
DHTLIB_OK
)
{
humidity =
DHTLIB_INVALID_VALUE
;
//
invalid value, or is NaN prefered?
temperature =
DHTLIB_INVALID_VALUE
;
//
invalid value
return
rv;
//
propagate error value
}
//
CONVERT AND STORE
humidity =
word
(bits[
0
], bits[
1
]) *
0.1
;
temperature =
word
(bits[
2
] &
0x7F
, bits[
3
]) *
0.1
;
if
(bits[
2
] &
0x80
)
//
negative temperature
{
temperature = -temperature;
}
//
TEST CHECKSUM
uint8_t
sum = bits[
0
] + bits[
1
] + bits[
2
] + bits[
3
];
if
(bits[
4
] != sum)
{
return
DHTLIB_ERROR_CHECKSUM
;
}
return
DHTLIB_OK
;
}
//
///////////////////////////////////////////////////
//
//
PRIVATE
//
//
return values:
//
DHTLIB_OK
//
DHTLIB_ERROR_TIMEOUT
int
dht::_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);
pinMode
(pin,
INPUT
);
delayMicroseconds
(
40
);
//
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++;
}
}
return
DHTLIB_OK
;
}
//
//
END OF FILE
//
Back
|
FazBrowse Home
|
New Git URL