-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_P119_BME680.ino
471 lines (391 loc) · 16.6 KB
/
_P119_BME680.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
//#######################################################################################################
//#################### Plugin 120 BME680 I2C Temp/Hum/Barometric/Pressure/Gas Resistence Sensor ########
//#######################################################################################################
/*******************************************************************************
* Copyright 2017
* Written by Rossen Tchobanski (rosko@rosko.net)
* BSD license, all text above must be included in any redistribution
*
* Release notes:
Adafruit_BME680 Library v1.0.5 required (https://github.com/adafruit/Adafruit_BME680/tree/1.0.5)
*****************************************************************************/
//#ifdef PLUGIN_BUILD_DEV
//#ifdef PLUGIN_BUILD_TESTING
#include "_Plugin_Helper.h"
//#ifdef USES_P119
// needed for DeviceStruct ?!
#include "ESPEasy_common.h"
#include <js_BME680.h>
#ifndef PCONFIG
# define PCONFIG(n) (Settings.TaskDevicePluginConfig[event->TaskIndex][(n)])
#endif // ifndef PCONFIG
#define PLUGIN_119
#define PLUGIN_ID_119 119
#define PLUGIN_NAME_119 "Environment - BME680 -TVOC-119-V2"
#define PLUGIN_VALUENAME1_119 "Temperature"
#define PLUGIN_VALUENAME2_119 "Humidity"
#define PLUGIN_VALUENAME3_119 "Pressure"
#define PLUGIN_VALUENAME4_119 "TVOC"
#define SEALEVELPRESSURE_HPA (1013.25)
//JS_BME680 is declared in lib !
unsigned long cycleCounter = 0;
unsigned long prevTime = 0;
boolean Plugin_119_init = false;
boolean _debugEnabled = false;
// UDP - Multicast declarations
WiFiUDP udp;
IPAddress ipMulti(239, 255, 255, 250); // site-local
unsigned int portMulti = 2085; // port
char packetIn[255]; // UDP in-buffer
char packetOut[512]; // UPD out-buffer
void sendUdpMessage(char* msg)
{
if (WiFi.status() == WL_CONNECTED && strlen(msg) != 0) {
snprintf(packetOut, sizeof(packetOut), "T:IAQC;FW:1.0;ID:%06X;IP:%s;R:%d;%s", ESP.getChipId(), WiFi.localIP().toString().c_str(), WiFi.RSSI(), msg);
DEBUG_PRINT(packetOut);
udp.beginPacketMulticast( ipMulti, portMulti, WiFi.localIP() );
udp.println(packetOut);
udp.endPacket();
strcpy(msg, "");
};
};
boolean Plugin_119(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_119;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_QUAD;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 4;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_119);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_119));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_119));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_119));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], PSTR(PLUGIN_VALUENAME4_119));
break;
}
//-----------------------------------------------
// --- Init is performed during first read call !
// --- of Lib js_Bme680
//-----------------------------------------------
// case PLUGIN_INIT:
// {
// //--- start measuring
// JS_BME680.do_begin();
// Plugin_119_init = true;
// addLog(LOG_LEVEL_INFO, F("PLUGIN_INIT: JS_BME680 : ready + initialized!"));
// serial.println(F("PLUGIN_INIT: JS_BME680 : ready + initialized!"));
// success = true;
// break;
// // } else {
// //clearPluginTaskData(event->TaskIndex);
// }
// break;
// }
// --- on load configuration dialog
case PLUGIN_WEBFORM_LOAD:
{
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
int optionValues[2] = { 0x77, 0x76 };
addFormSelectorI2C(F("plugin_119_BME680_i2c"), 2, optionValues, choice);
addFormNote(F("SDO Low=0x76, High=0x77"));
addFormNumericBox(F("Altitude"), F("plugin_119_BME680_elev"), PCONFIG(1));
addUnit(F("m"));
//------------
addFormSubHeader(F("Measurement options"));
addFormCheckBox(F("Filter tVOC"), F("plugin_119_BME680_filtered_tvoc_enable"), PCONFIG(2) );
addFormNumericBox(F("Temp-Offset"), F("plugin_119_BME680_toffs"), PCONFIG(3));
addUnit(F("°C"));
addFormNumericBox(F("Hum-Offset"), F("plugin_119_BME680_hoffs"), PCONFIG(4));
addUnit(F("%"));
//------------
addFormSubHeader(F("Debug options"));
addFormCheckBox(F("Plot output enable"), F("plugin_119_BME680_enable_plot_output"), PCONFIG(5) );
addFormNote(F("Use with Arduino-Plotter on right COM-Port!"));
addFormCheckBox(F("Debug output enable"), F("plugin_119_BME680_enable_debug_output"), PCONFIG(6) );
addFormNote(F("Use Arduino-Serial-Monitor on right COM-Port!"));
//------------
addFormSubHeader(F("Communication options"));
addFormCheckBox(F("UDP slink transmission enable"), F("plugin_119_BME680_enable_slink"), PCONFIG(7) );
addLog(LOG_LEVEL_INFO, F("plugin_119_BME680_WEBFORM_LOADED"));
success = true;
break;
}
// --- on submit on configuration dialog
case PLUGIN_WEBFORM_SAVE:
{
//--- get int config values
PCONFIG(0) = getFormItemInt(F("plugin_119_BME680_i2c"));
PCONFIG(1) = getFormItemInt(F("plugin_119_BME680_elev"));
PCONFIG(3) = getFormItemInt(F("plugin_119_BME680_toffs"));
PCONFIG(4) = getFormItemInt(F("plugin_119_BME680_hoffs"));
//--- save checkboxes
if (isFormItemChecked( F("plugin_119_BME680_filtered_tvoc_enable")) )
{
PCONFIG(2) = 1;
}
else
{
PCONFIG(2) = 0;
}
//--- plot setting
if (isFormItemChecked( F("plugin_119_BME680_enable_plot_output")) )
{
PCONFIG(5) = 1;
}
else
{
PCONFIG(5) = 0;
}
//--- debug
if (isFormItemChecked( F("plugin_119_BME680_enable_debug_output")) )
{
PCONFIG(6) = 1;
}
else
{
PCONFIG(6) = 0;
}
if (isFormItemChecked( F("plugin_119_BME680_enable_slink")) )
{
PCONFIG(7) = 1;
}
else
{
PCONFIG(7) = 0;
}
addLog(LOG_LEVEL_INFO, F("plugin_119_BME680_WEBFORM_SAVE"));
success = true;
break;
}
//--- on read interal set in configuration dialog
case PLUGIN_READ:
{
//--- see if (Plugin_119_init) below
//--- here first start with init! Plugin_119_init = false
if (!Plugin_119_init)
{
addLog(LOG_LEVEL_INFO, F("PLUGIN_READ - BME680 : init"));
Plugin_119_init = true;
addLog(LOG_LEVEL_INFO, F("BME680 : PLUGIN_READ initialized."));
String log = F("BME680 : PLUGIN_READ-Settings: I2C: ");
log += PCONFIG(0) ;
log += F(" Elevation: ");
log += PCONFIG(1);
log += F(" Filter: ");
log += PCONFIG(2);
log += F(" Toffs: ");
log += PCONFIG(3);
log += F(" Hoffs: ");
log += PCONFIG(4);
log += F(" Plot: ");
log += PCONFIG(5);
log += F(" Debug: ");
log += PCONFIG(6);
addLog(LOG_LEVEL_INFO, log);
//-- checks
uint8_t use_this_i2c_adr = 0x76;
if (PCONFIG(0) != 0)
{
//--- other than default: 0x77
use_this_i2c_adr = (uint8_t) PCONFIG(0);
}
bool use_Plotting = false;
//--- plot-swithed on?
if (PCONFIG(5) != 0)
{
//--- switch debugging on
use_Plotting = (uint8_t) PCONFIG(5);
}
//--- debug-swithed on?
bool use_Debugging = false;
if (PCONFIG(6) != 0)
{
//--- switch debugging on
use_Debugging = (uint8_t) PCONFIG(6);
}
// --- Temp-offset
float t_offs = 0;
if (PCONFIG(3) != 0)
{
//--- switch debugging on
t_offs = (float) PCONFIG(3);
}
// --- HUM-offset
float h_offs = 0;
if (PCONFIG(4) != 0)
{
//--- switch debugging on
t_offs = (float) PCONFIG(4);
}
// --- TVOCFiltered
bool use_filtered_tvoc = false;
if (PCONFIG(2) != 0)
{
//--- switch filtered output on
use_filtered_tvoc = true;
}
// --- setup code, run once
JS_BME680.set_bme680_device_address(use_this_i2c_adr); // may be ommitted in case of default-address 0x76 (SDO = GND), declare in case of address 0x77 (SDO = High)
JS_BME680.useArduinoDebugOutput = use_Debugging;
JS_BME680.useArduinoPlotOutput = use_Plotting;
JS_BME680.useFilteredTvocOutput = use_filtered_tvoc;
JS_BME680.set_bme680_offset_hum ( h_offs);
JS_BME680.set_bme680_offset_temp (t_offs);
//--- start measuring
JS_BME680.do_begin();
addLog(LOG_LEVEL_INFO, F("JS_BME680-Read : ready + not initialized code done! do_begin called"));
_debugEnabled = (uint8_t) PCONFIG(6);
if (_debugEnabled)
Serial.println(F("PLUGIN_INIT: JS_BME680 : ready + initialized!"));
success = true;
break;
}
//-------------------------------------------------------------------------------------------------------
//--- this code is done by having initialized BME680 on first read and not on instanciating class object!
//--- is established after 2nd Reading, after initialize with do_begin()-method.
//--- using above set parameters as bme680 startup!
//-------------------------------------------------------------------------------------------------------
if (Plugin_119_init)
{
_debugEnabled = (uint8_t) PCONFIG(6);
if (_debugEnabled)
{
Serial.println(F("plugin_119_call_BME680_do_bme680_measurement"));
addLog(LOG_LEVEL_DEBUG, F("plugin_119_call_BME680_do_bme680_measurement"));
}
JS_BME680.do_bme680_measurement();
if (_debugEnabled)
{
addLog(LOG_LEVEL_INFO, F("BME680-Read: performed measurement!"));
addLog(LOG_LEVEL_DEBUG, F("plugin_119_call_BME680_do_bme680_measurement_done"));
Serial.println(F("plugin_119_BME680-Read: performed measurement!"));
}
_debugEnabled = (uint8_t) PCONFIG(6);
if (_debugEnabled)
Serial.println(F("PLUGIN_Read: JS_BME680 : init done, measure!"));
// //--- put your main code here, to run repeatedly:
// static unsigned long baseIntervall = JS_BME680.get_bme680Interval();
// unsigned long currentMillis = millis();
//---bme680 data screen added
// if (currentMillis - prevTime > baseIntervall)
// {
// --- ca. alle 10 Sekunden eine Messung aller Messgroessen
//JS_BME680.do_bme680_measurement();
// prevTime = currentMillis;
// }
//--- pass measurements to ESPEasy
UserVar[event->BaseVarIndex + 0] = JS_BME680.getTemp();
UserVar[event->BaseVarIndex + 1] = JS_BME680.getHum();
UserVar[event->BaseVarIndex + 2] = JS_BME680.getPress();
//--- debug-swithed on?
bool use_TVocFiltered = false;
if (PCONFIG(2) != 0)
{
//--- switch debugging on
use_TVocFiltered = (uint8_t) PCONFIG(2);
}
if (use_TVocFiltered)
{
addLog(LOG_LEVEL_INFO, F("BME680-Read: get filtered value."));
UserVar[event->BaseVarIndex + 3] = JS_BME680.getTVocFiltered(); //JS_BME680.getTVoc();
}
else
{
addLog(LOG_LEVEL_INFO, F("BME680-Read: get raw value."));
UserVar[event->BaseVarIndex + 3] = JS_BME680.getTVoc();
}
bool use_slink = false;
if (PCONFIG(7) != 0)
{
//--- switch debugging on
use_slink = (uint8_t) PCONFIG(7);
}
//--- send UDP-Message to fhem
char bme680Msg[128];
char str_temp[6];
char str_hum[6];
char str_absHum[6];
char str_dewPoint[6];
char str_pressure[16];
//char str_altitude[8];
char str_tVoc[8];
char str_gas[8];
char str_r[6];
char str_filtered[6];
char str_ratio[6];
char str_base[6];
//--- using udp transmission (once every minute)
// @fhem use these additional perl modules: https://github.com/herrmannj/AirQuality/tree/master/FHEM
if (use_slink)
{
dtostrf(JS_BME680.getTemp(), 4, 2, str_temp);
dtostrf(JS_BME680.getHum(), 4, 2, str_hum);
dtostrf(0, 4, 2, str_absHum);
dtostrf(0, 4, 2, str_dewPoint);
dtostrf(JS_BME680.getPress(), 3, 1, str_pressure);
dtostrf(JS_BME680.getGasRes(), 3, 1, str_gas);
dtostrf(JS_BME680.getTVoc(), 1, 0, str_tVoc);
// dtostrf(JS_BME680.getTVocFiltered(), 1, 0, str_tVoc);
dtostrf(0.0, 1, 0, str_r); //--- unused
dtostrf(0.0, 1, 0, str_filtered); //--- unused
dtostrf(1.0, 1, 0, str_ratio); //--- unused
dtostrf(1.0, 1, 0, str_base); //--- unused
//--- prepare to send UDP-message to fhem
snprintf(bme680Msg
, sizeof(bme680Msg)
, "F:THPV;T:%s;H:%s;AH:%s;D:%s;P:%s;V:%s;R:%lu;DB:%lu;DF:%s;DR:%s;"
, str_temp
, str_hum
, str_absHum
, str_dewPoint
, str_pressure
, str_tVoc
, str_r
, str_base
, str_filtered
, str_ratio);
sendUdpMessage(bme680Msg);
addLog(LOG_LEVEL_INFO, bme680Msg);
}
//-- done
success = true;
break;
}
success = true;
break;
} //--- case pluginread
case PLUGIN_ONCE_A_SECOND:
{
//code to be executed once a second. Tasks which do not require fast response can be added here
// https://ae-bst.resource.bosch.com/media/_tech/media/application_notes/BST-BME680-AN014.pdf , page 10
cycleCounter ++;
if (cycleCounter >= 12)
{
JS_BME680.do_bme680_measurement();
cycleCounter = 0;
}
success = true;
}
}
return success;
}
//#endif