-
Notifications
You must be signed in to change notification settings - Fork 1
/
DEV_6.1.1.ino
499 lines (344 loc) · 15.7 KB
/
DEV_6.1.1.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
DEV Template for iobroker IoT Framework
Version: F6_6.1.1
Date: 2022-08-31
Supported features / sensors:
- Wifi
- Deep Sleep (Tested with ESP8266 only! Requires shortcut between D0 and RST!)
- MQTT (NEW in 6.0)
- BME280
- BME680
- SCD30
- SPS30 WARNING the SPS30 section is not yet converted to MQTT! Please use the latest version of F5!
- WindSensor WARNING the WindSensor section is not yet converted to MQTT! Please use the latest version of F5!
- ePaper Displays (code for IndoorAirSensor)
- ADS1115 (code for DaylightSensor)
WARNING: Framework version 6 deprecates the formerly used HTTP/REST API for the
ioBrober communication. It was replaced by MQTT.
This sketch is based on my ioBroker IoT Framework 6
https://github.com/AndreasExner/ioBroker-IoT-Framework
MIT License
Copyright (c) 2022 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 //NOTE: the SCD30 sensor needs the ambient air pressure for calibration. BME280 must be used and activated for SCD30!
//#define SPS30_active //WARNING the SPS30 section is not yet converted to MQTT! Please use the latest version of F5!
//#define WindSensor_active //WARNING the WindSensor section is not yet converted to MQTT! Please use the latest version of F5!
//#define ePaper_active //WARNING useable for IndoorAirSensor sketch only! Code needs to be changed for other sketches
//#define ADS1115_active
//+++++++++++++++++++++++++++++++++++++++++ generic device section +++++++++++++++++++++++++++++++++++++++++++++++++
#include <ESP8266WiFi.h>
// ------------------------------------ device settings - change settings to match your requirements
String deviceID = "DEV"; //predefined sensor ID, DEV by default to prevent overwriting productive data
String deviceName = "DevTemplate"; //predefined sensor Name, DEV by default to prevent overwriting productive data
bool devMode = true; //enable DEV mode on boot (do not change until required)
bool debug = true; //debug to serial monitor
bool ledActive = true; //enable external status LED on boot
bool deviceActive = false; // dectivate device (all sensors) on boot (do not change until required)
int interval = 10; // (initial) interval between transmissions, multiplied with the intervalDelay
int intervalDelay = 1000; // in ms
bool deepSleepEnabled = false; //enable deep sleep on boot
/*
* The BSEC library for the BME680 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)
* For other cases please define the intervalDelay in the loop section
*/
// ------------------------------------ MQTT section
#include <ArduinoMqttClient.h>
String MQTT_deviceRootPath = "0_userdata/0/IoT-Devices/"; // The root path for all the sensor's (devices) state and configuration subscription in your MQTT/iobroker
String MQTT_prodDataRootPath = "0_userdata/0/IoT/"; // The root path for all the sensor's productive data in your MQTT/iobroker
String MQTT_devDataRootPath = "0_userdata/0/IoT-Dev/"; // The root path for all the sensor's development data in your MQTT/iobroker (usen in dev mode)
// do not change the next lines
// topic for sensor (device) state and configuration subscription
String MQTT_topicDevice = MQTT_deviceRootPath + deviceID + "/";
// topic for sensor data to send too. Choose a different path for DEV mode to avoid overwriting production data
String MQTT_topicDataDEV = MQTT_devDataRootPath + deviceName + "/";
String MQTT_topicDataPROD = MQTT_prodDataRootPath + deviceName + "/";
String MQTT_topicData;
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
// ------------------------------------ other definitions (do not change)
#define LED D4 // gpio pin for external status LED
void(* HWReset) (void) = 0; // define reset function DO NOT CHANGE
int counter;
//+++++++++++++++++++++++++++++++++++++++++ ePaper Display section +++++++++++++++++++++++++++++++++++++++++++++++++
#ifdef ePaper_active
#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 ePaperDisplayLastUpdate, ePaperTemp, ePaperHumi, ePaperCO2;
bool ePaperDisplayActive = true; // set to false if no display is attached to the device
bool ePaperDisplayActivated = false;
#endif
//+++++++++++++++++++++++++++++++++++++++++ BME680 section +++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifdef BME680_active
#include <EEPROM.h>
#include "bsec.h"
bool bme680Activated = 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 bsecStateUpdateCounter = 0;
int bme680iaSAhistory = 0;
bool bme680Reset = false;
#endif
//+++++++++++++++++++++++++++++++++++++++++ BME280 section +++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifdef BME280_active
#include <Adafruit_BME280.h>
bool bme280Activated = false;
Adafruit_BME280 bme;
int bme280PressureSeaLevel, bme280PressureSeaLevelHistory;
int bme280TempOffset = 3;
float bme280Pressure; // raw ambient pressure, required to calibrate SCD30 Sensor
String bme280AirPressureTopicMQTT = "daswetter/0/NextHours/Location_1/Day_1/pressure_value"; //source for air pressure at sealevel, required for altitude calculation
#endif
//+++++++++++++++++++++++++++++++++++++++++ SCD30 section +++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifdef SCD30_active
#include <Wire.h>
#include "SparkFun_SCD30_Arduino_Library.h"
bool scd30Activated = false;
SCD30 airSensor;
int scd30Interval = 3; // intervall in sec.
int scd30Offset = 3; // temperature offset
bool scd30AutoCal = true;
#endif
//+++++++++++++++++++++++++++++++++++++++++ ADS1115 section +++++++++++++++++++++++++++++++++++++++++++++++++++++++
#ifdef ADS1115_active
#include <Wire.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads1115(0x48);
bool ads1115Activated = false;
#endif
//######################################### specific device functions #######################################################
void mqtt_parseSpecificConfig(String mqttTopic, String mqttMessage) {
// parse sensor specific config here. This function is called automatically on every interval.
#ifdef BME680_active
if (mqttTopic == MQTT_topicData + "BME680_reset" && mqttMessage == "false") {bme680Reset = false;}
else if (mqttTopic == MQTT_topicData + "BME680_reset" && mqttMessage == "true") {bme680Reset = true;}
if (debug) {Serial.println(" bme680Reset: " + bool_to_string(bme680Reset));}
#endif
#ifdef BME280_active
if (mqttTopic == bme280AirPressureTopicMQTT) {bme280PressureSeaLevel = mqttMessage.toInt();}
if (debug) {Serial.println(" bme280PressureSeaLevel: " + String(bme280PressureSeaLevel));}
#endif
#ifdef SCD30_active
if (mqttTopic == MQTT_topicData + "SCD30_autoCal" && mqttMessage == "false") {scd30AutoCal = false;}
else if (mqttTopic == MQTT_topicData + "SCD30_autoCal" && mqttMessage == "true") {scd30AutoCal = true;}
if (debug) {Serial.println(" scd30AutoCal: " + bool_to_string(scd30AutoCal));}
#endif
#ifdef ePaper_active
if (mqttTopic == MQTT_topicDataPROD + "LastUpdate") {ePaperDisplayLastUpdate = mqttMessage;}
if (debug) {Serial.println(" ePaperDisplayLastUpdate: " + ePaperDisplayLastUpdate);}
#endif
}
void mqtt_subscribeSpecificSensorConfig() {
if (debug) {Serial.println("### MQTT_subscribeSpecificSensorConfig");}
String subscribeConfig;
#ifdef BME280_active
subscribeConfig = bme280AirPressureTopicMQTT;
mqttClient.subscribe(subscribeConfig);
if (debug) {Serial.println(" subscribe topic = " + subscribeConfig);}
#endif
#ifdef SCD30_active
subscribeConfig = MQTT_topicData + "SCD30_autoCal";
mqttClient.subscribe(subscribeConfig);
if (debug) {Serial.println(" subscribe topic = " + subscribeConfig);}
#endif
#ifdef ePaper_active
subscribeConfig = MQTT_topicData + "LastUpdate";
mqttClient.subscribe(subscribeConfig);
if (debug) {Serial.println(" subscribe topic = " + subscribeConfig);}
#endif
delay(2000); // give MQTT time to subscribe/receive. 2000ms are a good point to start.
}
//######################################### setup ##############################################################
void setup(void) {
Serial.begin(115200);
delay(1000);
pinMode(LED, OUTPUT);
wifi_connect();
wifi_getState();
mqtt_connect();
// initial communication with iobroker on boot
bool debug_state = debug; //debug output during setup
debug = true;
mqtt_getState();
delay(1000); // give MQTT time to finish the connection
mqtt_sendDeviceState(); // send initial device state
mqtt_send(MQTT_topicDevice + "ErrorLog", "Info: ESP8266 restart");
mqtt_send(MQTT_topicDevice + "Reset", "true"); //set the last reset timestamp
mqtt_subscribeDeviceConfig(); // subscribe device config
mqtt_subscribeSpecificSensorConfig(); // subscribe specific sensor config
mqtt_getDeviceConfig(); // parse device and sensor config
delay(1000);
counter = interval; // countdown for next interval
debug = debug_state;
// setup sensors
#ifdef BME680_active
if (deviceActive) {BME680_setup();}
#endif
#ifdef BME280_active
if (deviceActive) {BME280_setup();}
#endif
#ifdef SCD30_active
if (deviceActive) {SCD30_setup();}
#endif
#ifdef ePaper_active
if (deviceActive && ePaperDisplayActive) {ePaper_setup();}
#endif
#ifdef ADS1115_active
if (deviceActive) {ADS1115_setup();}
#endif
}
//####################################################################
// Loop
void loop(void) {
// +++++++++++++++++++++ Start loop
if(deepSleepEnabled){digitalWrite(LED, HIGH);} // set LED on during execution in deep sleep mode
if (debug) {Serial.println("--- Loop " + String(counter) + " of " + String(interval));}
// activate inactive sensors and run sensor specific actions on every loop (interval)
#ifdef BME680_active
if (deviceActive && !bme680Activated) {BME680_setup();}
if (deviceActive && bme680Activated && bme680Reset) {BME680_reset();}
#endif
#ifdef BME280_active
if (deviceActive && !bme280Activated) {BME280_setup();}
#endif
#ifdef SCD30_active
if (deviceActive && !scd30Activated) {SCD30_setup();}
if (deviceActive && scd30Activated && counter <=0) {SCD30_AutoCal();} //(interval)
#endif
#ifdef ePaper_active
if (deviceActive && ePaperDisplayActive && !ePaperDisplayActivated) {ePaper_setup();}
#endif
#ifdef ADS1115_active
if (deviceActive && !ads1115Activated) {ADS1115_setup();}
#endif
// +++++++++++++++++++++ Loop Delay
/* 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)
*
* Otherwise use intervalDelay to control the loop speed
*/
bool bme680DataReady = false;
#ifdef BME680_active
if (deviceActive && bme680Activated) {
int timeout = 10; //wait 10 seconds for data (LP should deliver date after 3 seconds)
if (debug) {Serial.print(" waiting for BME680 data: ");}
while (bme680DataReady == false && timeout > 0) {
bme680DataReady = iaqSensor.run();
if (debug) {Serial.print(String(timeout) + " ");}
--timeout;
delay(1000);
}
if (timeout <= 0) {
if (debug) {Serial.println(" timeout");}
BME680_checkIaqSensorStatus(); // check sensor if BME680 run fails
}
else {
if (debug) {Serial.println(" done");}
}
}
#endif
if (!bme680DataReady) {delay(intervalDelay);}
// +++++++++++++++++++++ Process data on every loop, but transmit on every transmission interval (counter <=0)
#ifdef BME680_active
if (deviceActive && bme680Activated && bme680DataReady) {BME680_getData();}
#endif
#ifdef BME280_active
if (deviceActive && bme280Activated) {BME280_getData();}
#endif
#ifdef SCD30_active
if (deviceActive && scd30Activated) {SCD30_getData();}
#endif
#ifdef ADS1115_active
if (deviceActive && ads1115Activated) {ADS1115_getData();}
#endif
// +++++++++++++++++++++ Deep Sleep
// enable deep sleep mode on ESP
// use ESP reset for wake up
if (deepSleepEnabled) {
if (debug) {Serial.println("### Start DeepSleep for " + String(interval) + " seconds");}
digitalWrite(LED, LOW);
mqtt_send(MQTT_topicDevice + "ErrorLog", "Info: ESP8266 deepSleep");
delay(2000); //give MQTT time to send data before entering deep sleep
ESP.deepSleep(interval * 1000000);
}
// +++++++++++++++++++++ look for config changes
mqtt_getState(); // check mqtt connection state
mqtt_getDeviceConfig(); // update sensor config
// +++++++++++++++++++++ Transmission Interval
if (counter <= 0) { //reset counter
counter = interval;
mqtt_sendDeviceState(); // send device state
delay(2000);
if (deviceActive) {
// write data to ePaper if active
#ifdef ePaper_active
if (ePaperDisplayActivated) {
mqtt_getDeviceConfig(); // refresh ePaperDisplayLastUpdate timestamp from iobroker
ePaper_showData_1_54_3fields("Temperatur:", "Rel. Luftfeuchte:", "CO2 Gehalt:", ePaperTemp + " °C", ePaperHumi + " %", ePaperCO2 + " ppm", "Stand: " + ePaperDisplayLastUpdate);
}
#endif
// reset ePaper if device is not active
}
else {
#ifdef ePaper_active
ePaper_showData_1_54_3fields("Temperatur:", "Rel. Luftfeuchte:", "CO2 Gehalt:", "--.-- °C", "--.-- %", "---- ppm", "Stand: ----------");
#endif
}
}
else {counter--;}
// Blink LED
if (ledActive && (counter % 2)) {
digitalWrite(LED, LOW);
}
else {
digitalWrite(LED, HIGH);
}
}