-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensor.h
104 lines (85 loc) · 2.63 KB
/
Sensor.h
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
// All the code for reading the sensor is stored in this file
#ifndef THERMOSTAT_SENSOR_H_
#define THERMOSTAT_SENSOR_H_
//-----------------------------------------------------------
// Local includes
//-----------------------------------------------------------
#include "Config.h"
#include "Util.h"
//-----------------------------------------------------------
// Arduino/System includes
//-----------------------------------------------------------
#include <Bridge.h>
//-----------------------------------------------------------
// Library includes
//-----------------------------------------------------------
#include <DHT.h>
//-----------------------------------------------------------
// Global memory
//-----------------------------------------------------------
DHT dht(DHT_DATA_PIN, DHT_TYPE);
class Sensor
{
public:
inline Sensor() :
dht(DHT_DATA_PIN, DHT_TYPE),
temperatureLPF(0.5),
humidityLPF(0.2),
temperature(72.0),
humidity(20.0),
lastUpdateMillis(millis())
{
// Use the pins to power the DHT. It's easier than using jumpers
#ifdef DHT_VCC_PIN
pinMode(DHT_VCC_PIN,OUTPUT);
digitalWrite(DHT_VCC_PIN,HIGH);
#endif
#ifdef DHT_GND_PIN
pinMode(DHT_GND_PIN,OUTPUT);
digitalWrite(DHT_GND_PIN,LOW);
#endif
}
// Call this function frequently, it will not hurt to call it too
// frequently.
inline void Update();
// Returns the temperature in Farenheit.
inline float GetTemperature() const
{
return temperature;
}
// Returns the humidty in Percent.
inline float GetHumidity() const
{
return humidity;
}
private:
DHT dht;
float temperatureLPF = 0.5;
float humidityLPF = 0.5;
float temperature;
float humidity;
float lastUpdateMillis;
};
inline void Sensor::Update()
{
if ((millis() - lastUpdateMillis) < 2000)
{
// The DHT library won't check more frequently than every two seconds,
// so just don't update anything
return;
}
float newTemp = dht.readTemperature(true);
float newHumidity = dht.readHumidity();
if (isnan(newTemp) or isnan(humidity))
{
// There was a problem with the sensor library, don't accept this measurement.
return;
}
temperature = smooth(newTemp, temperatureLPF, temperature);
humidity = smooth(newHumidity, humidityLPF, humidity);
// Store the values in the Bridge.
BridgePutFloat("temperature", temperature);
BridgePutFloat("humidity", humidity);
lastUpdateMillis = millis();
}
#endif // INCLUDE GUARD