-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathDHT.cpp
454 lines (388 loc) · 10.3 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* DHT library
MIT license
written by Adafruit Industries
*/
#include <math.h>
#include "DHT.h"
//#define NAN 0
#ifdef DEBUG
#define DEBUG_PRINT(...) Serial.println(__VA_ARGS__)
#else
#define DEBUG_PRINT(...)
#endif
DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
_pin = pin;
_type = type;
_count = count;
firstreading = true;
}
DHT::DHT(uint8_t type) {
_pin = 0;
_type = type;
_count = COUNT;
firstreading = true;
if (_type != DHT10){
DEBUG_PRINT("Error : No pins are defined\n");
}
}
void DHT::begin(void) {
if (_type == DHT10) {
if (DHT10Init()) {
SERIALPRINT.println("Error : Failed to init DHT 11\n");
while (1);
}
} else {
// set up the pins!
pinMode(_pin, INPUT);
digitalWrite(_pin, HIGH);
_lastreadtime = 0;
}
}
/** Common interface to get temp&humi value.support all DHT device.
@return 0 for calibrated failed,1 for succeed.
**/
int DHT::readTempAndHumidity(float* data) {
uint32_t target_val[2] = {0};
uint32_t cnt = 0;
if (_type == DHT10) {
while (DHT10ReadStatus() == 0) {
DHT10Init();
delay(30);
cnt++;
if (cnt > 3) {
return -1;
}
}
//wait for data ready。
while (readTargetData(target_val)) {
cnt++;
delay(50);
if (cnt > 5) {
return -1;
}
}
data[0] = target_val[0] * 100.0 / 1024 / 1024;
data[1] = target_val[1] * 200.0 / 1024 / 1024 - 50;
} else {
data[0] = readHumidity();
data[1] = readTemperature();
if (isnan(data[0]) || isnan(data[1])) {
return -1;
}
}
return 0;
}
//boolean S == Scale. True == Farenheit; False == Celcius
float DHT::readTemperature(bool S) {
if(_type == DHT10){
float temp[2];
readTempAndHumidity(temp);
if (S) {
temp[1] = convertCtoF(temp[1]);
}
return temp[1];
}
else{
float f;
if (read()) {
switch (_type) {
case DHT11:
f = data[2];
if(data[3]%128<10){
f += data[3]%128/10.0f;
}else if(data[3]%128<100){
f += data[3]%128/100.0f;
}else{
f += data[3]%128/1000.0f;
}
if(data[3]>=128){ // The left-most digit indicate the negative sign.
f = -f;
}
if (S) {
f = convertCtoF(f);
}
return f;
case DHT22:
case DHT21:
f = data[2] & 0x7F;
f *= 256;
f += data[3];
f /= 10;
if (data[2] & 0x80) {
f *= -1;
}
if (S) {
f = convertCtoF(f);
}
return f;
}
}
}
DEBUG_PRINT("Read fail");
return NAN;
}
float DHT::convertCtoF(float c) {
return c * 9 / 5 + 32;
}
float DHT::readHumidity(void) {
if(_type == DHT10){
float temp[2];
readTempAndHumidity(temp);
return temp[0];
}
else{
float f;
if (read()) {
switch (_type) {
case DHT11:
f = data[0];
return f;
case DHT22:
case DHT21:
f = data[0];
f *= 256;
f += data[1];
f /= 10;
return f;
}
}
}
DEBUG_PRINT("Read fail");
return NAN;
}
boolean DHT::read(void) {
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
unsigned long currenttime;
// pull the pin high and wait 250 milliseconds
digitalWrite(_pin, HIGH);
delay(250);
currenttime = millis();
if (currenttime < _lastreadtime) {
// ie there was a rollover
_lastreadtime = 0;
}
if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
return true; // return last correct measurement
//delay(2000 - (currenttime - _lastreadtime));
}
firstreading = false;
/*
DEBUG_PRINT("Currtime: "); DEBUG_PRINT(currenttime);
DEBUG_PRINT(" Lasttime: "); DEBUG_PRINT(_lastreadtime);
*/
_lastreadtime = millis();
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
// now pull it low for ~20 milliseconds
pinMode(_pin, OUTPUT);
digitalWrite(_pin, LOW);
delay(20);
//cli();
digitalWrite(_pin, HIGH);
delayMicroseconds(20);
pinMode(_pin, INPUT);
// read in timings
for (i = 0; i < MAXTIMINGS; i++) {
counter = 0;
while (digitalRead(_pin) == laststate) {
counter++;
delayMicroseconds(1);
if (counter == 255) {
break;
}
}
laststate = digitalRead(_pin);
if (counter == 255) {
break;
}
// ignore first 3 transitions
if ((i >= 4) && (i % 2 == 0)) {
// shove each bit into the storage bytes
data[j / 8] <<= 1;
if (counter > _count) {
data[j / 8] |= 1;
}
j++;
}
}
//sei();
/*
DEBUG_PRINTln(j, DEC);
DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(", ");
DEBUG_PRINT(data[1], HEX); DEBUG_PRINT(", ");
DEBUG_PRINT(data[2], HEX); DEBUG_PRINT(", ");
DEBUG_PRINT(data[3], HEX); DEBUG_PRINT(", ");
DEBUG_PRINT(data[4], HEX); DEBUG_PRINT(" =? ");
DEBUG_PRINTln(data[0] + data[1] + data[2] + data[3], HEX);
*/
// check we read 40 bits and that the checksum matches
if ((j >= 40) &&
(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
return true;
}
return false;
}
/*****************************************************************************/
/*****************************************************************************/
/** Reset sensor.
@return 0 for calibrated failed,1 for succeed.
**/
int DHT::DHT10Reset(void) {
if (_type == DHT10) {
return i2cWriteByte(RESET_REG_ADDR);
} else {
return 0;
SERIALPRINT.println("This function only support for DHT10");
}
}
/** Read status register.check the calibration flag - bit[3]: 1- calibrated ok ,0 - Not calibrated.
@return 0 for calibrated failed,1 for succeed.
**/
int DHT::DHT10ReadStatus(void) {
int ret = 0;
uint8_t statu = 0;
if (_type == DHT10) {
ret = i2cReadByte(statu);
if (ret) {
SERIALPRINT.println("Failed to read byte\n");
}
if ((statu & 0x8) == 0x8) {
return 1;
} else {
return 0;
}
} else {
SERIALPRINT.println("This function only support for DHT10");
return 0;
}
}
/** Init sensor,send 0x08,0x00 to register 0xe1.
@ return : 0 if success, non-zero if failed.
**/
int DHT::setSystemCfg(void) {
uint8_t cfg_param[] = {0xe1, 0x08, 0x00};
if (_type == DHT10) {
return i2cWriteBytes(cfg_param, sizeof(cfg_param));
} else {
SERIALPRINT.println("This function only support for DHT10");
return 0;
}
}
/** Read temp & humi result buf from sensor.
total 6 bytes,the first byte for status register,other 5 bytes for temp & humidity data.
@ return : 0 if success, non-zero if failed.
**/
int DHT::readTargetData(uint32_t* data) {
uint8_t statu = 0;
uint8_t bytes[6] = {0};
uint8_t cfg_params[] = {0xac, 0x33, 0x00};
//int ret = 0;
if (_type == DHT10) {
if (i2cWriteBytes(cfg_params, sizeof(cfg_params))) {
return -1;
}
delay(75);
// check device busy flag, bit[7]:1 for busy, 0 for idle.
while ((statu & 0x80) == 0x80) {
SERIALPRINT.println("Device busy!");
delay(200);
if (i2cReadByte(statu)) {
return -1;
}
}
if (i2cReadBytes(bytes, sizeof(bytes))) {
return -1;
}
data[HUMIDITY_INDEX] = (data[HUMIDITY_INDEX] | bytes[1]) << 8;
data[HUMIDITY_INDEX] = (data[HUMIDITY_INDEX] | bytes[2]) << 8;
data[HUMIDITY_INDEX] = (data[HUMIDITY_INDEX] | bytes[3]);
data[HUMIDITY_INDEX] = data[HUMIDITY_INDEX] >> 4;
data[TEMPRATURE_INDEX] = (data[TEMPRATURE_INDEX] | bytes[3]) << 8;
data[TEMPRATURE_INDEX] = (data[TEMPRATURE_INDEX] | bytes[4]) << 8;
data[TEMPRATURE_INDEX] = (data[TEMPRATURE_INDEX] | bytes[5]);
data[TEMPRATURE_INDEX] &= 0xfffff;
return 0;
} else {
SERIALPRINT.println("This function only support for DHT10");
return 0;
}
}
/** DHT10 Init function.
Reset sensor and wait for calibration complete.
@ return : 0 if success, non-zero if failed.
**/
int DHT::DHT10Init(void) {
int ret = 0;
int cnt = 0;
if (_type == DHT10) {
delay(500);
DHT10Reset();
delay(300);
ret = setSystemCfg();
if (ret) {
SERIALPRINT.println("Failed to set system conf reg \n");
}
//SERIALPRINT.println("Set system cfg OK!");
delay(500);
while (DHT10ReadStatus() == 0) {
SERIALPRINT.println("get status error!");
DHT10Reset();
delay(500);
if (setSystemCfg()) {
SERIALPRINT.println("Failed to set system conf reg \n");
}
delay(500);
cnt++;
if (cnt > 5) {
return -1;
}
}
return 0;
} else {
SERIALPRINT.println("This function only support for DHT10");
return 0;
}
}
/*****************************************************************************/
/*****************************************************************************/
int DHT::i2cReadByte(uint8_t& byte) {
int cnt = 0;
Wire.requestFrom(DEFAULT_IIC_ADDR, 1);
while (1 != Wire.available()) {
cnt++;
if (cnt >= 10) {
return -1;
}
delay(1);
}
byte = Wire.read();
return 0;
}
int DHT::i2cReadBytes(uint8_t* bytes, uint32_t len) {
int cnt = 0;
Wire.requestFrom(DEFAULT_IIC_ADDR, len);
while (len != (uint32_t) Wire.available()) {
cnt++;
if (cnt >= 10) {
return -1;
}
delay(1);
}
for (uint32_t i = 0; i < len; i++) {
bytes[i] = Wire.read();
}
return 0;
}
int DHT::i2cWriteBytes(uint8_t* bytes, uint32_t len) {
Wire.beginTransmission(DEFAULT_IIC_ADDR);
for (uint32_t i = 0; i < len; i++) {
Wire.write(bytes[i]);
}
return Wire.endTransmission();
}
int DHT::i2cWriteByte(uint8_t byte) {
Wire.beginTransmission(DEFAULT_IIC_ADDR);
Wire.write(byte);
return Wire.endTransmission();
}