-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndoorAirSensor.ino
462 lines (333 loc) · 13.5 KB
/
IndoorAirSensor.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
Indoor air sensor for iobroker IoT Framework
Version: F5_2.0 (release)
Date: 2020-12-10
This sketch has several prerquisites discribed in the documentation of the repository:
https://github.com/AndreasExner/ioBroker-IoT-IndoorAirSensor
This sketch is based on my ioBroker IoT Framework V5.3.0 (or higher)
https://github.com/AndreasExner/ioBroker-IoT-Framework
MIT License
Copyright (c) 2020 Andreas Exner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//+++++++++++++++++++++++++++++++++++++++++ enable sensor specific functions +++++++++++++++++++++++++++++++++++++++++++++++++
#define AEX_iobroker_IoT_Framework //generic functions DO NOT CHANGE
// uncomment required sections
#define BME280_active
#define BME680_active
#define SCD30_active
//#define SPS30_active
//#define WindSensor_active
#define ePaper_active
//+++++++++++++++++++++++++++++++++++++++++ generic device section +++++++++++++++++++++++++++++++++++++++++++++++++
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// device settings - change settings to match your requirements
const char* ssid = "<ssid>"; // Wifi SSID
const char* password = "<password>"; //Wifi password
String SensorID = "04"; //predefinded sensor ID, DEV by default to prevent overwriting productive data
int interval = 10; // waiting time for the first masurement and fallback on error reading interval from iobroker
/*
* The BSEC library sets the pace for the loop duration -> BSEC_LP ~ 3000 ms per request
* For a transmission interval of 5 minutes configure an interval of 100 (*3 = 300 seconds = 5 minutes)
*/
bool DevMode = true; //enable DEV mode on boot (do not change)
bool debug = true; //debug to serial monitor
bool led = true; //enable external status LED on boot
bool sensor_active = false; // dectivate sensor(s) on boot (do not change)
/*
* build base URL's
* Change IP/FQND and path to match your environment
*/
String baseURL_DEVICE_GET = "http://192.168.1.240:8087/getPlainValue/0_userdata.0.IoT-Devices." + SensorID + ".";
String baseURL_DEVICE_SET = "http://192.168.1.240:8087/set/0_userdata.0.IoT-Devices." + SensorID + ".";
// end of device settings - don not change anything below the line until required
// define generic device URL's
String URL_IP = baseURL_DEVICE_SET + "SensorIP?value=";
String URL_RST = baseURL_DEVICE_SET + "Reset?value=";
String URL_LED = baseURL_DEVICE_GET + "LED";
String URL_MAC = baseURL_DEVICE_SET + "MAC?value=";
String URL_RSSI = baseURL_DEVICE_SET + "RSSI?value=";
String URL_DevMode = baseURL_DEVICE_GET + "DevMode";
String URL_ErrorLog = baseURL_DEVICE_SET + "ErrorLog?value=";
String URL_sensor_active = baseURL_DEVICE_GET + "SensorActive";
String baseURL_DATA_GET, baseURL_DATA_SET; // URL's for data
String URL_SID, URL_INT; // URL's for sensor ID and interval
// other definitions
#define LED D4 // gpio pin for external status LED
void(* HWReset) (void) = 0; // define reset function DO NOT CHANGE
int counter = interval; // countdown for next interval
//+++++++++++++++++++++++++++++++++++++++++ ePaper Display section +++++++++++++++++++++++++++++++++++++++++++++++++
#include <GxEPD.h>
#include <GxGDEH0154D67/GxGDEH0154D67.h> // 1.54" b/w
#include <GxIO/GxIO_SPI/GxIO_SPI.h>
#include <GxIO/GxIO.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeMonoBold18pt7b.h>
#include <Fonts/FreeSans9pt7b.h>
GxIO_Class io(SPI, /*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D6*/ 12);
GxEPD_Class display(io, /*RST=D6*/ 12, /*BUSY=D0*/ 16);
String URL_LastUpdate;
String LastUpdate;
//+++++++++++++++++++++++++++++++++++++++++ BME680 section +++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <EEPROM.h>
#include "bsec.h"
bool BME680_activated = false;
/* Configure the BSEC library with information about the sensor
18v/33v = Voltage at Vdd. 1.8V or 3.3V
3s/300s = BSEC operating mode, BSEC_SAMPLE_RATE_LP or BSEC_SAMPLE_RATE_ULP
4d/28d = Operating age of the sensor in days
generic_18v_3s_4d
generic_18v_3s_28d
generic_18v_300s_4d
generic_18v_300s_28d
generic_33v_3s_4d
generic_33v_3s_28d
generic_33v_300s_4d
generic_33v_300s_28d
*/
const uint8_t bsec_config_iaq[] = {
#include "config/generic_33v_3s_4d/bsec_iaq.txt"
};
#define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // 360 minutes - 4 times a day
// Create an object of the class Bsec
Bsec iaqSensor;
uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE] = {0};
uint16_t stateUpdateCounter = 0;
int iaSAhistory = 0;
String iaqD, iaDA, iaqS, iaSA, VOCe, temp, humi;
String URL_iaqD, URL_iaDA, URL_iaqS, URL_iaSA, URL_VOCe;
String URL_BME680_updateState, URL_BME680_loadState, URL_BME680_eraseEEPROM, URL_BME680_reset_get, URL_BME680_reset_set;
//+++++++++++++++++++++++++++++++++++++++++ BME280 section +++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <Adafruit_BME280.h>
bool BME280_activated = false;
Adafruit_BME280 bme;
String bme280_temp, bme280_humi, bme280_airp, bme280_alti;
String URL_temp, URL_humi, URL_airp;
float bme280_pressure;
int pressure_sl;
String URL_PRESL = "http://192.168.1.240:8087/getPlainValue/daswetter.0.NextHours.Location_1.Day_1.pressure_value"; //reference for pressure at sea level for altitude
//+++++++++++++++++++++++++++++++++++++++++ SCD30 section +++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <Wire.h>
#include "SparkFun_SCD30_Arduino_Library.h"
bool SCD30_activated = false;
SCD30 airSensor;
int scd30_interval = 3; // intervall in sec.
int scd30_offset = 2; // temperature offset
String scd30_co2, scd30_humi, scd30_temp;
String URL_co2, URL_SCD30_autoCal;
//######################################### setup ##############################################################
void setup(void) {
Serial.begin(115200);
delay(1000);
pinMode(LED, OUTPUT);
connect_wifi();
get_wifi_state();
// initial communication with iobroker on boot
bool debug_state = debug; //debug output during setup
debug = true;
get_dynamic_config();
build_urls();
send_ip();
send_sid();
send_rst();
debug = debug_state;
// setup sensors
if (sensor_active) {BME680_setup();}
if (sensor_active) {BME280_setup();}
if (sensor_active) {SCD30_setup();}
// ePaper display setup
display.init();
display.eraseDisplay();
ePaper_showBlank();
}
//######################################### specific device functions #######################################################
void build_urls() {
URL_SID = baseURL_DATA_SET + "SensorID?value=" + SensorID;
URL_INT = baseURL_DATA_GET + "Interval";
URL_BME680_updateState = baseURL_DATA_SET + "BME680_updateState?value=";
URL_BME680_loadState = baseURL_DATA_SET + "BME680_loadState?value=";
URL_BME680_eraseEEPROM = baseURL_DATA_SET + "BME680_eraseEEPROM?value=";
URL_BME680_reset_get = baseURL_DATA_GET + "BME680_reset";
URL_BME680_reset_set = baseURL_DATA_SET + "BME680_reset?value=";
URL_iaqD = baseURL_DATA_SET + "iaqD?value=";
URL_iaDA = baseURL_DATA_SET + "iaDA?value=";
URL_iaqS = baseURL_DATA_SET + "iaqS?value=";
URL_iaSA = baseURL_DATA_SET + "iaSA?value=";
URL_VOCe = baseURL_DATA_SET + "VOCe?value=";
URL_temp = baseURL_DATA_SET + "temp?value=";
URL_humi = baseURL_DATA_SET + "humi?value=";
URL_airp = baseURL_DATA_SET + "airp?value=";
URL_LastUpdate = baseURL_DATA_GET + "LastUpdate";
URL_co2 = baseURL_DATA_SET + "co2?value=";
URL_SCD30_autoCal = baseURL_DATA_GET + "SCD30_autoCal";
}
void send_data() {
Serial.println("send data to iobroker");
HTTPClient http;
String sendURL;
sendURL = URL_iaDA + iaDA;
http.begin(sendURL);
http.GET();
sendURL = URL_iaSA + iaSA;
http.begin(sendURL);
http.GET();
sendURL = URL_co2 + scd30_co2;
http.begin(sendURL);
http.GET();
sendURL = URL_humi + bme280_humi;
http.begin(sendURL);
http.GET();
sendURL = URL_temp + bme280_temp;
http.begin(sendURL);
http.GET();
sendURL = URL_airp + bme280_airp;
http.begin(sendURL);
http.GET();
sendURL = URL_iaqD + iaqD;
http.begin(sendURL);
http.GET();
sendURL = URL_iaqS + iaqS;
http.begin(sendURL);
http.GET();
sendURL = URL_VOCe + VOCe;
http.begin(sendURL);
http.GET();
http.end();
}
//######################################### ePaper display functions ########################################################
void ePaper_showData()
{
const GFXfont* font_b = &FreeMonoBold18pt7b;
const GFXfont* font_a = &FreeMonoBold9pt7b;
const GFXfont* font_c = &FreeSans9pt7b;
display.fillScreen(GxEPD_WHITE);
display.setTextColor(GxEPD_BLACK);
display.setCursor(0, 10);
display.setFont(font_a);
display.println("Temperatur:");
display.setCursor(10, 45);
display.setFont(font_b);
display.println(temp + " °C");
display.setCursor(0, 70);
display.setFont(font_a);
display.println("Rel. Luftfeuchte:");
display.setCursor(10, 102);
display.setFont(font_b);
display.println(bme280_humi + " %");
display.setCursor(0, 132);
display.setFont(font_a);
display.println("CO2 Gehalt:");
display.setCursor(10, 163);
display.setFont(font_b);
display.println(scd30_co2 + " ppm");
display.setCursor(0, 197);
display.setFont(font_c);
display.println("Stand: " + LastUpdate);
display.update();
}
void ePaper_showBlank()
{
const GFXfont* font_b = &FreeMonoBold18pt7b;
const GFXfont* font_a = &FreeMonoBold9pt7b;
const GFXfont* font_c = &FreeSans9pt7b;
display.fillScreen(GxEPD_WHITE);
display.setTextColor(GxEPD_BLACK);
display.setCursor(0, 10);
display.setFont(font_a);
display.println("Temperatur:");
display.setCursor(10, 45);
display.setFont(font_b);
display.println("--- °C");
display.setCursor(0, 70);
display.setFont(font_a);
display.println("Rel. Luftfeuchte:");
display.setCursor(10, 102);
display.setFont(font_b);
display.println("--- %");
display.setCursor(0, 132);
display.setFont(font_a);
display.println("CO2 Gehalt:");
display.setCursor(10, 163);
display.setFont(font_b);
display.println("--- ppm");
display.setCursor(0, 197);
display.setFont(font_c);
display.println("-------------");
display.update();
}
//####################################################################
// Loop
void loop(void) {
/* run BME680 sample and wait for new data
*
* The BSEC library sets the pace for the loop interval -> LP = 3000 ms pre run
* results in round about 0,3Hz. For a transmission interval of 5 minutes configure
* an interval of 100 (x3 = 300 seconds)
* EVERY command has to be included in the if-clause!!
*/
if (iaqSensor.run()) {
// read data and serial output
if (sensor_active && BME680_activated) {BME680_get_data();}
if (sensor_active && BME280_activated) {BME280_get_data();}
if (sensor_active && SCD30_activated) {SCD30_get_data();}
if (sensor_active && BME680_activated) {BME680_serial_output();}
if (sensor_active && BME280_activated) {BME280_serial_output();}
if (sensor_active && SCD30_activated) {SCD30_serial_output();}
// send data to iobroker ever n-th BME680 run
if (counter == 0) {
ePaper_showData();
get_wifi_state();
send_ip();
get_dynamic_config();
build_urls();
if (sensor_active && BME280_activated && BME680_activated && SCD30_activated) {
send_data();
ePaper_get_LastUpdate();
ePaper_showData();
}
else
{
ePaper_showBlank();
}
if (sensor_active && BME680_activated) {BME680_reset();}
if (sensor_active && BME680_activated == false) {BME680_setup();}
if (sensor_active && BME280_activated == false) {BME280_setup();}
if (sensor_active && SCD30_activated == false) {SCD30_setup();}
if (sensor_active && BME280_activated) {BME280_get_sealevel_pressure();}
if (sensor_active && SCD30_activated) {SCD30_AutoCal();}
get_interval();
counter = interval;
}
else {
counter--;
Serial.println(counter);
}
// Blink LED
if (led && (counter % 2)) {
digitalWrite(LED, LOW);
}
else {
digitalWrite(LED, HIGH);
}
// check sensor if BME680 run fails
} else {
if (sensor_active && BME680_activated) {
BME680_checkIaqSensorStatus();
}
}
}