-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht_manager.cpp
53 lines (41 loc) · 1.2 KB
/
dht_manager.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
#include "dht_manager.h"
#include <DHT.h>
#include <Arduino.h>
DHT dht(0, 0);
float cachedTemperature = -999.0;
float cachedHumidity = -999.0;
unsigned long lastDHTUpdateTime = 0;
const unsigned long DHT_UPDATE_INTERVAL = 30000;
void setupDHT(uint8_t pin, uint8_t type) {
dht = DHT(pin, type);
dht.begin();
}
void updateDHTData() {
unsigned long currentMillis = millis();
if (lastDHTUpdateTime == 0 || (currentMillis - lastDHTUpdateTime >= DHT_UPDATE_INTERVAL)) {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
// Testing only
// float hum = random(400, 800) / 10.0;
// float temp = random(150, 300) / 10.0;
// End: Testing Only
if (!isnan(temp) && !isnan(hum)) {
cachedTemperature = temp;
cachedHumidity = hum;
lastDHTUpdateTime = currentMillis;
} else {
Serial.println("Failed to read DHT sensor data!");
}
}
}
float readDHTTemperature(bool inCelsius) {
updateDHTData();
if (cachedTemperature == -999.0) {
return -999.0;
}
return dht.computeHeatIndex(cachedTemperature, cachedHumidity, !inCelsius);
}
float readDHTHumidity() {
updateDHTData();
return (cachedHumidity == -999.0) ? -999.0 : cachedHumidity;
}