-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSensor.hpp
275 lines (243 loc) · 9.46 KB
/
Sensor.hpp
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
#ifndef _SENSOR_NODE_
#define _SENSOR_NODE_
#include <Homie.h>
#include <limits>
#include "BME280_Impl.hpp"
#include "DHTxx_Impl.hpp"
#include "SHT3x_Impl.hpp"
class Sensor {
public:
Sensor();
virtual ~Sensor();
void setup();
bool publish();
bool force();
private:
// taken from adafruit DHT library
float computeHeatIndex(float temperature, float relativeHumidity) const;
// Sättigungsdampfdruck in hPa
double SDD(float temperature) const;
// Dampfdruck in hPa
double DD(float temperature, float relativeHumidity) const;
// Taupunkt in °C
float TD(float temperature, float relativeHumidity) const;
// absolute Feuchte in g Wasserdampf pro m3 Luft
float AF(float temperature, float relativeHumidity) const;
// check and update the health state. publish health state if it has changed.
void checkHealth(bool publish);
SensorInterface *sensor;
SensorInterface::SensorState ss;
unsigned long errors;
static const constexpr char* dht11 = "dht11";
static const constexpr char* dht21 = "dht21";
static const constexpr char* dht22 = "dht22";
static const constexpr char* bme280 = "bme280";
static const constexpr char* sht30 = "sht30";
static const constexpr char* sht31 = "sht31";
static const constexpr char* temperature = "temperature";
static const constexpr char* pressure = "pressure";
static const constexpr char* humidity = "humidity";
static const constexpr char* healthState = "healthState";
HomieSetting<const char*> typeSetting;
HomieSetting<const char*> forceKeySetting;
HomieSetting<double> forceValSetting;
float lastTemperature;
float lastHumidity;
float lastPressure;
HomieNode *sensorStateNode;
HomieNode *temperatureNode;
HomieNode *humidityNode;
HomieNode *pressureNode;
};
inline Sensor::Sensor()
: sensor(NULL)
, ss(SensorInterface::unknown)
, errors(0)
, typeSetting("type", "Type of the sensor. Use either \"dht11\", \"dht21\", \"dht22\", \"bme280\", \"sht30\" or \"sht31\"")
, forceKeySetting("force key", "The key of the force value. Use either \"temperature\", \"pressure\" or \"humidity\"")
, forceValSetting("force value", "The value for the force key. E.g. Key / Value of 0.5 and \"temperature\" means publishing on 0.5 degress temperature difference.")
, lastTemperature(std::numeric_limits<float>::min())
, lastHumidity(std::numeric_limits<float>::min())
, lastPressure(std::numeric_limits<float>::min())
, sensorStateNode(NULL)
, temperatureNode(NULL)
, humidityNode(NULL)
, pressureNode(NULL) {
typeSetting.setDefaultValue(sht31).setValidator([] (const char* candidate) {
return (String(candidate) == dht11) ||
(String(candidate) == dht21) ||
(String(candidate) == dht22) ||
(String(candidate) == bme280) ||
(String(candidate) == sht30) ||
(String(candidate) == sht31);
});
forceKeySetting.setDefaultValue("temperature").setValidator([] (const char* candidate) {
return (String(candidate) == temperature) ||
(String(candidate) == humidity) ||
(String(candidate) == pressure);
});
forceValSetting.setDefaultValue(0.2);
sensorStateNode = new HomieNode(healthState, healthState);
temperatureNode = new HomieNode(temperature, temperature);
humidityNode = new HomieNode(humidity, humidity);
pressureNode = new HomieNode(pressure, pressure);
sensorStateNode->advertise("health");
sensorStateNode->advertise("errors");
temperatureNode->advertise("absolut");
temperatureNode->advertise("heatindex");
temperatureNode->advertise("dewPoint");
humidityNode->advertise("relative");
humidityNode->advertise("absolute");
pressureNode->advertise("pressure");
}
inline Sensor::~Sensor() {
delete pressureNode;
delete humidityNode;
delete temperatureNode;
delete sensorStateNode;
delete sensor;
}
inline float Sensor::computeHeatIndex(float temperature, float percentHumidity) const {
// Using both Rothfusz and Steadman's equations
// http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml
// to fahrenheit
temperature = temperature * 1.8 + 32;
float hi = 0.5 * (temperature + 61.0 + ((temperature - 68.0) * 1.2) + (percentHumidity * 0.094));
if (hi > 79) {
hi = -42.379 +
2.04901523 * temperature +
10.14333127 * percentHumidity +
-0.22475541 * temperature * percentHumidity +
-0.00683783 * pow(temperature, 2) +
-0.05481717 * pow(percentHumidity, 2) +
0.00122874 * pow(temperature, 2) * percentHumidity +
0.00085282 * temperature * pow(percentHumidity, 2) +
-0.00000199 * pow(temperature, 2) * pow(percentHumidity, 2);
if ((percentHumidity < 13) && (temperature >= 80.0) && (temperature <= 112.0))
hi -= ((13.0 - percentHumidity) * 0.25) * sqrt((17.0 - abs(temperature - 95.0)) * 0.05882);
else if ((percentHumidity > 85.0) && (temperature >= 80.0) && (temperature <= 87.0))
hi += ((percentHumidity - 85.0) * 0.1) * ((87.0 - temperature) * 0.2);
}
hi = (hi - 32) * 0.55555;
return hi;
}
inline double Sensor::SDD(float temperature) const {
float a = (temperature >= 0) ? 7.5 : 7.6;
float b = (temperature >= 0) ? 237.3 : 240.7;
float exponent = ((a * temperature) / (b + temperature));
float sdd = 6.1078 * pow(10, exponent);
return sdd;
}
inline double Sensor::DD(float temperature, float relativeHumidity) const {
double dd = relativeHumidity / 100 * SDD(temperature);
return dd;
}
inline float Sensor::TD(float temperature, float relativeHumidity) const {
float a = (temperature >= 0) ? 7.5 : 7.6;
float b = (temperature >= 0) ? 237.3 : 240.7;
float v = log10(DD(temperature, relativeHumidity) / 6.1078);
float td = b * v / (a - v);
return td;
}
inline float Sensor::AF(float temperature, float relativeHumidity) const {
double dd = DD(temperature, relativeHumidity);
float af = 216.686912909 * dd / (temperature + 273.15);
return af;
}
inline void Sensor::checkHealth(bool publish) {
SensorInterface::SensorState state = SensorInterface::unknown;
if(sensor) {
state = sensor->state();
}
String health = [](SensorInterface::SensorState state) {
if (state == SensorInterface::ok)
return "ok";
if (state == SensorInterface::read_error)
return "read_error";
if (state == SensorInterface::connect_error)
return "connect_error";
return "unknown";
}(state);
if (state != ss) {
ss = state;
}
if (ss != SensorInterface::ok) {
Homie.getLogger() << "Sensor health check failed: " << health << endl;
errors++;
}
if(publish) {
// todo: keep errors and health persistant
sensorStateNode->setProperty("errors").setRetained(false).send(String(errors));
sensorStateNode->setProperty("health").setRetained(false).send(health);
}
}
inline void Sensor::setup() {
String type(typeSetting.get());
if (type == dht11) {
sensor = new DHTxx_Impl(2, DHTxx_Impl::TYPE11);
} else if (type == dht21) {
sensor = new DHTxx_Impl(2, DHTxx_Impl::TYPE21);
} else if (type == dht22) {
sensor = new DHTxx_Impl(2, DHTxx_Impl::TYPE22);
} else if (type == bme280) {
sensor = new BME280_Impl(0x76);
} else if (type == sht30) {
sensor = new SHT3x_Impl(0x45);
} else if (type == sht31) {
sensor = new SHT3x_Impl(0x44);
} else {
sensor = new SensorInterface();
}
}
inline bool Sensor::force() {
bool force = false;
checkHealth(false);
if (ss != SensorInterface::ok) {
return false;
}
String forceKey(forceKeySetting.get());
if (forceKey == temperature) {
force = (abs(lastTemperature - sensor->temperature()) > forceValSetting.get());
} else if (forceKey == humidity) {
force = (abs(lastHumidity - sensor->humidity()) > forceValSetting.get());
} else if (forceKey == pressure) {
force = (abs(lastPressure - sensor->pressure()) > forceValSetting.get());
}
return force;
}
inline bool Sensor::publish() {
checkHealth(true);
if (ss != SensorInterface::ok) {
return false;
}
float temperature = sensor->temperature();
float humidity = sensor->humidity();
float pressure = sensor->pressure();
String t(temperature);
String h(humidity);
String p(pressure);
String hi(computeHeatIndex(temperature, humidity));
String sdd(SDD(temperature));
String dd(DD(temperature, humidity));
String td(TD(temperature, humidity));
String af(AF(temperature, humidity));
Homie.getLogger() << "Sensor reading: " << endl <<
" • errors : " << errors << endl <<
" • temperature : " << t << " °C" << endl <<
" • pressure : " << p << " hPa" << endl <<
" • relative humidity : " << h << " %" << endl <<
" • heat index: : " << hi << " °C" << endl <<
" • dew point temperature : " << td << " °C" << endl <<
" • absolute humidty : " << af << " g/m³" << endl;
temperatureNode->setProperty("absolute").setRetained(true).send(t);
temperatureNode->setProperty("heatindex").setRetained(true).send(hi);
temperatureNode->setProperty("dewPoint").setRetained(true).send(td);
humidityNode->setProperty("relative").setRetained(true).send(h);
humidityNode->setProperty("absolute").setRetained(true).send(af);
pressureNode->setProperty("pressure").setRetained(true).send(p);
lastTemperature = temperature;
lastHumidity = humidity;
lastPressure = pressure;
return true;
}
#endif