-
Notifications
You must be signed in to change notification settings - Fork 0
/
_10_air.ino
345 lines (294 loc) · 10.6 KB
/
_10_air.ino
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
#if FWTYPE == 10 // esp8266-air
void sensorSetup() {
bme280Status = bme.begin(0x76);
logString = "BME280 ";
if (bme280Status) {
// The following are the recommended values from shapter 3.5 of the BME280 datasheet with
// the expeception that the sampling rate is governed by the mqtt_interval value
// forced mode, 1x temperature / 1x humidity / 1x pressure oversampling, filter off
bme.setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF );
} else {
logString = logString + "NOT ";
}
logString = logString + "found";
mqttLog(app_name_sensors, logString);
sht31Status = sht31.begin(0x44); // Set to 0x45 for alternate i2c addr
logString = "SHT-31 initialised";
mqttLog(app_name_sensors, logString);
// The library always returns true for begin() so it's not much point checking the returned value
logString = "SHT-31 ";
uint16_t sht31State = sht31.readStatus();
if (sht31State == 65535) { // returns 65535 if no sensor (no I2C device at all?) found
logString = logString + "NOT ";
sht31Error = true;
sht31Status = false;
}
logString = logString + "found";
mqttLog(app_name_sensors, logString);
/*
// The library always returns 0x8010 so this is pretty useless too
logString = "SHT-31 status: 0x" + String(sht31State, HEX);
mqttLog(app_name_sensors, logString);
*/
}
void printSensorConfig(const String &cfgStr) {
mqttSend(String(cfgStr + "elevation"), String(elevation), true);
}
void sensorImportJSON(JsonObject& json) {
if (json["elevation"].is<float>()) {
elevation = json["elevation"];
}
}
void sensorExportJSON(JsonObject& json) {
json["elevation"] = elevation;
}
bool sensorUpdateConfig(const String &key, const String &value) {
if ( key == "elevation" ) {
elevation = value.toFloat();
} else {
return false;
}
return true;
}
bool sensorRunAction(const String &key, const String &value) {
return false;
}
void calcData() {}
void sendData() {
temperature = 1000;
humidity = 1000;
dewpoint = 1000;
pressure = 0;
sealevel = 0;
// Start SHT-31
if (! sht31Status) {
logString = "Retrying SHT-31";
mqttLog(app_name_sensors, logString);
sht31Status = sht31.begin();
uint16_t sht31State = sht31.readStatus();
if (sht31State != 65535) { // returns 65535 if no sensor (no I2C device at all?) found
logString = "SHT-31 found";
sht31Error = false;
sht31Status = true;
mqttLog(app_name_sensors, logString);
} else {
sht31Status = false;
}
} else {
temperature1 = sht31.readTemperature();
humidity1 = sht31.readHumidity();
// Start temperature
// The lowest recorded temperature is -88C and the highest is 58C so if it's outside this then it's a false reading
if ( temperature1 > -100 && temperature1 < 65) {
temperature = temperature1;
if (sht31ErrorT) {
if (sht31ErrorT >= error_count_log) {
logString = "SHT-31 temperature recovered: " + String(temperature1) + "C";
mqttLog(app_name_sensors, logString);
}
sht31ErrorT = 0;
}
} else { // Bad temperature reading
sht31ErrorT++;
if (sht31ErrorT == error_count_log) {
logString = "SHT-31 temperature error: " + String(temperature1) + "C";
mqttLog(app_name_sensors, logString);
}
}
// End temperature
// Start humidity
// 100% humidity seems to be acceptable on a SHT-31
if ( humidity1 > 0 && humidity1 <= 100) {
humidity = humidity1;
if (sht31ErrorH) {
if (sht31ErrorH >= error_count_log) {
logString = "SHT-31 humidity recovered: " + String(humidity1) + "%";
mqttLog(app_name_sensors, logString);
}
sht31ErrorH = 0;
}
} else { // Bad humidity reading
sht31ErrorH++;
if (sht31ErrorH == error_count_log) {
logString = "SHT-31 humidity error: " + String(humidity1) + "%";
mqttLog(app_name_sensors, logString);
}
}
// End humidity
}
// End SHT-31
// Start BME280
if (! bme280Status) {
logString = "Retrying BME280";
mqttLog(app_name_sensors, logString);
bme280Status = bme.begin();
if (bme280Status) {
// The following are the recommended values from shapter 3.5 of the BME280 datasheet with
// the expeception that the sampling rate is governed by the mqtt_interval value
// forced mode, 1x temperature / 1x humidity / 1x pressure oversampling, filter off
bme.setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF );
logString = "BME280 found";
mqttLog(app_name_sensors, logString);
}
} else {
// Only needed in forced mode! In normal mode, you can remove the next line. Normal mode can result in higher temperatures due to the BME280 getting heating up from continuous readings.
bme.takeForcedMeasurement(); // has no effect in normal mode
temperature2 = bme.readTemperature();
// humidity2 = bme.readHumidity();
humidity2 = 255; // Permanently treat BME280 humidity as bad
pressure2 = bme.readPressure() / 100.0;
// The following equation is from https://www.sandhurstweather.org.uk/barometric.pdf
sealevel2 = pressure2 / exp(-elevation/((temperature2 + 273.15)*29.263));
// Start temperature
// The lowest recorded temperature is -88C and the highest is 58C so if it's outside this then it's a false reading
if ( temperature2 > -100 && temperature2 < 65) {
if (sht31ErrorT) { // If the SHT-31 has a problem report this temperature otherwise let it report it's temperature
temperature = temperature2;
}
if (bme280ErrorT) {
if (bme280ErrorT >= error_count_log) {
logString = "BME280 temperature recovered: " + String(temperature2) + "C";
mqttLog(app_name_sensors, logString);
}
bme280ErrorT = 0;
}
} else { // Bad temperature reading
bme280ErrorT++;
if (bme280ErrorT == error_count_log) {
logString = "BME280 temperature error: " + String(temperature2) + "C";
mqttLog(app_name_sensors, logString);
}
}
// End temperature
// Start humidity
// 100% humidity means the humidity sensor has gotten wet and is no longer useful
if ( humidity2 > 0 && humidity2 < 100) {
if (sht31ErrorH) { // If the SHT-31 has a problem report this humidity otherwise let the SHT-31 report it's humidity
humidity = humidity2;
}
if (bme280ErrorH) {
if (bme280ErrorH >= error_count_log) {
logString = "BME280 humidity recovered: " + String(humidity2) + "%";
mqttLog(app_name_sensors, logString);
}
bme280ErrorH = 0;
}
} else {
bme280ErrorH++;
if (bme280ErrorH == error_count_log) {
logString = "BME280 humidity error: " + String(humidity2) + "%";
mqttLog(app_name_sensors, logString);
}
}
// End humidity
// Start pressure
// The lowest recorded pressure is 870hpa and the highest is 1085 so if it's outside this then it's a false reading
if ( (sealevel2 > 850) && (sealevel2 < 1100) ) {
pressure = pressure2;
sealevel = sealevel2;
if (bme280ErrorP) {
if (bme280ErrorP >= error_count_log) {
logString = "BME280 pressure recovered: " + String(sealevel2) + "hpa";
mqttLog(app_name_sensors, logString);
}
bme280ErrorP = 0;
}
} else { // Bad pressure reading
bme280ErrorP++;
if (bme280ErrorP == error_count_log) {
logString = "BME280 pressure error: " + String(sealevel2) + "hpa";
mqttLog(app_name_sensors, logString);
}
pressure = 0; // This is a bad pressure so set it to 0 so it's ignored later
}
// End pressure
}
// End BME280
if (temperature != 1000) {
mqttSend(String("temperature/celsius"), String(temperature), false);
} else {
mqttSend(String("temperature/celsius"), str_NaN, false);
}
if (humidity != 1000) {
mqttSend(String("humidity/percentage"), String(humidity), false);
} else {
mqttSend(String("humidity/percentage"), str_NaN, false);
}
if ( (temperature != 1000) && (humidity != 1000) ) {
dewpoint = 5351/(5351/(temperature + 273.15) - log(humidity/100.0)) - 273.15;
mqttSend(String("dewpoint/celsius"), String(dewpoint), false);
} else {
mqttSend(String("dewpoint/celsius"), str_NaN, false);
}
if (pressure != 0) {
mqttSend(String("pressure/hpa"), String(pressure), false);
mqttSend(String("pressure-sealevel/hpa"), String(sealevel), false);
} else {
mqttSend(String("pressure/hpa"), str_NaN, false);
mqttSend(String("pressure-sealevel/hpa"), str_NaN, false);
}
}
String httpSensorData() {
String httpData = tableStart;
httpData += trStart + "Temperature:" + tdBreak;
if (temperature != 1000) {
httpData += String(temperature) + " C";
} else {
httpData += "-";
}
httpData += trEnd;
httpData += trStart + "Humidity:" + tdBreak;
if (humidity != 1000) {
httpData += String(humidity) + " %";
} else {
httpData += "-";
}
httpData += trEnd;
httpData += trStart + "Dew point:" + tdBreak;
if (dewpoint != 1000) {
httpData += String(dewpoint) + " C";
} else {
httpData += "-";
}
httpData += trEnd;
httpData += trStart + "Pressure:" + tdBreak;
if (pressure != 0) {
httpData += String(pressure) + " hPa";
} else {
httpData += "-";
}
httpData += trEnd;
httpData += trStart + "Pressure (sea level):" + tdBreak;
if (sealevel != 0) {
httpData += String(sealevel) + " hPa";
} else {
httpData += "-";
}
httpData += trEnd;
httpData += tableEnd;
return httpData;
}
String httpSensorSetup() {
String httpData;
httpData += trStart + "Elevation (m):" + tdBreak + htmlInput(str_text, "elevation", String(elevation)) + trEnd;
return httpData;
}
String httpSensorConfig() {
if (httpServer.hasArg("elevation")) {
tmpString = String(elevation);
if (httpServer.arg("elevation") != tmpString) {
elevation = httpServer.arg("elevation").toFloat();
}
}
}
void sensorMqttCallback(char* topic, byte* payload, unsigned int length) {}
void sensorMqttSubs() {}
#endif