-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht.cpp
73 lines (65 loc) · 1.67 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
/*
Simple_DHT.cpp - Library for comunication and data interpretation with DHT sensors.
Created by Tilen Stermecki, Apr 2023.
Released into the public domain.
*/
#ifndef DHT_CPP
#define DHT_CPP
#include "dht.h"
DHT::DHT(byte DHT_PIN) {
pin = DHT_PIN;
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);
fail = false;
}
void DHT::readSensor() {
byte data[5];
digitalWrite(pin, 0);
delayMicroseconds(1000);
digitalWrite(pin, 1);
delayMicroseconds(30);
pinMode(pin, INPUT);
delayMicroseconds(10);
// while (digitalRead(pin));
uint8_t timeout = 1;
while (!digitalRead(pin) && timeout != 0) {
++timeout;
}
if (timeout == 0) { // most of the time comes to ~19 (Arduino UNO R3, this is very bad method, I know)
fail = 2;
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);
return;
}
for (byte dat = 0; dat < 5; dat++) {
data[dat] = 0;
for (byte i = 0; i < 8; i++) {
unsigned long t = pulseIn(pin, HIGH, 10000UL);
if (t == 0) {
fail = 2;
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);
return;
}
data[dat] |= (t > 50) << (7 - i);
}
}
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);
uint8_t tmp = 0;
for (byte i = 0; i < 4; i++)
tmp += data[i];
if (tmp == data[4]) {
fail = 0;
hum = (float)(((uint16_t)(data[0] & 0b01111111) << 8) | data[1]) / 10.0;
if (data[0] & 0b10000000) hum *= -1;
temp = (float)(((uint16_t)(data[2] & 0b01111111) << 8) | data[3]) / 10.0;
if (data[2] & 0b10000000) temp *= -1;
} else {
fail = 1;
}
}
String DHT::errorVerbose() {
return errorLookup[fail];
}
#endif