Skip to content

Commit

Permalink
sns: Add BME680 (#2295)
Browse files Browse the repository at this point in the history
Add support for BME680 using libalgobsec proprietary algorithms for precise Indoor Air Quality (IAQ) measurement. Unlike traditional CO2 sensors - and good ones are expensive - it measures nearly all VOCs compounds in the air (plus other gases) and compensates those measurements with its built-in temperature and humidity sensors to determine indoor air quality.

Co-authored-by: Max Prokhorov <prokhorov.max@outlook.com>
  • Loading branch information
ruimarinho and mcspr authored Aug 2, 2020
1 parent 6919d11 commit 6266930
Show file tree
Hide file tree
Showing 17 changed files with 524 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Since November 2018, Max Prokhorov (**@mcspr**) is also actively working as a co
* Environment
* **DHT11 / DHT12 / DHT22 / DHT21 / AM2301 / Itead's SI7021**
* **BMP180**, **BMP280** and **BME280** pressure, humidity (BME280) and temperature (BMP280 & BME280) sensor by Bosch
* **BME680** pressure, humidity, temperature and gas sensor by Bosch with support for Bosch's proprietary library BSEC for accurate Indoor Air Quality (IAQ) monitoring (⚠️ [learn more](https://github.com/xoseperez/espurna/wiki/Sensors#bme680-environmental-sensor))
* **TMP35** and **TMP36** analog temperature sensors
* **MAX6675** temperature sensor
* **NTC** temperature sensors
Expand Down
3 changes: 3 additions & 0 deletions code/espurna/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ PROGMEM const char espurna_sensors[] =
#if BMX280_SUPPORT
"BMX280 "
#endif
#if BME680_SUPPORT
"BME680 "
#endif
#if CSE7766_SUPPORT
"CSE7766 "
#endif
Expand Down
1 change: 1 addition & 0 deletions code/espurna/config/arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
//#define BH1750_SUPPORT 1
//#define BMP180_SUPPORT 1
//#define BMX280_SUPPORT 1
//#define BME680_SUPPORT 1
//#define CSE7766_SUPPORT 1
//#define DALLAS_SUPPORT 1
//#define DHT_SUPPORT 1
Expand Down
9 changes: 9 additions & 0 deletions code/espurna/config/dependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,12 @@
#warning "MQTT_MAX_PACKET_SIZE should be set in `build_flags = ...` of the environment! Default value is used instead."
#endif
#endif

//------------------------------------------------------------------------------
// Disable BME680 support if using Core version 2.3.0 due to memory constraints.

#if BME680_SUPPORT && defined(ARDUINO_ESP8266_RELEASE_2_3_0)
#warning "BME680_SUPPORT is not available when using Arduino Core 2.3.0 due to memory constraints. Please use Arduino Core 2.6.3+ instead (or set `platform = ${common.platform_latest}` for the latest version)."
#undef BME680_SUPPORT
#define BME680_SUPPORT 0
#endif
4 changes: 2 additions & 2 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@
#endif

// ref: https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/kconfig.html#config-lwip-esp-gratuitous-arp
// ref: https://github.com/xoseperez/espurna/pull/1877#issuecomment-525612546
// ref: https://github.com/xoseperez/espurna/pull/1877#issuecomment-525612546
//
// Broadcast gratuitous ARP periodically to update ARP tables on the AP and all devices on the same network.
// Helps to solve compatibility issues when ESP fails to timely reply to ARP requests, causing the device's ARP table entry to expire.
Expand Down Expand Up @@ -1028,7 +1028,7 @@
#endif

#ifndef MQTT_SECURE_CLIENT_MFLN
#define MQTT_SECURE_CLIENT_MFLN SECURE_CLIENT_MFLN // Use global MFLN setting by default
#define MQTT_SECURE_CLIENT_MFLN SECURE_CLIENT_MFLN // Use global MFLN setting by default
#endif

#ifndef MQTT_SECURE_CLIENT_INCLUDE_CA
Expand Down
43 changes: 34 additions & 9 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,39 +782,39 @@
// LDR sensor
// Enable support by passing LDR_SUPPORT=1 build flag
//------------------------------------------------------------------------------

#ifndef LDR_SUPPORT
#define LDR_SUPPORT 0
#endif

#ifndef LDR_SAMPLES
#define LDR_SAMPLES 10 // Number of samples
#endif

#ifndef LDR_DELAY
#define LDR_DELAY 0 // Delay between samples in micros
#endif

#ifndef LDR_TYPE
#define LDR_TYPE LDR_GL5528
#endif

#ifndef LDR_ON_GROUND
#define LDR_ON_GROUND true
#endif

#ifndef LDR_RESISTOR
#define LDR_RESISTOR 10000 // Resistance
#endif

#ifndef LDR_MULTIPLICATION
#define LDR_MULTIPLICATION 32017200
#endif

#ifndef LDR_POWER
#define LDR_POWER 1.5832
#endif

//------------------------------------------------------------------------------
// MHZ19 CO2 sensor
// Enable support by passing MHZ19_SUPPORT=1 build flag
Expand Down Expand Up @@ -1303,6 +1303,29 @@
#define SI1145_ADDRESS 0x60
#endif

//------------------------------------------------------------------------------
// BME680
// Enable support by passing BME680_SUPPORT=1 build flag
//------------------------------------------------------------------------------

#ifndef BME680_SUPPORT
#define BME680_SUPPORT 0
#endif

#ifndef BME680_I2C_ADDRESS
#define BME680_I2C_ADDRESS 0x00 // 0x00 means auto
#endif

#ifndef BME680_BSEC_CONFIG
#define BME680_BSEC_CONFIG BME680_BSEC_CONFIG_GENERIC_33V_3S_4D // BSEC config config value. By default, 3.3V as supply voltage,
#endif // 3 seconds as maximum time between bsec_sensor_control` calls
// and 4 days as the time considered for background calibration.

#ifndef BME680_STATE_SAVE_INTERVAL
#define BME680_STATE_SAVE_INTERVAL 0 // How frequently (in milliseconds) should state be stored in
#endif // non-volatile memory. A common value would be every 6h or
// 360 * 60 * 1000 milliseconds. By default, this is disabled.

// -----------------------------------------------------------------------------
// ADC
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -1356,6 +1379,7 @@
BH1750_SUPPORT || \
BMP180_SUPPORT || \
BMX280_SUPPORT || \
BME680_SUPPORT || \
EMON_ADC121_SUPPORT || \
EMON_ADS1X15_SUPPORT || \
SHT3X_I2C_SUPPORT || \
Expand Down Expand Up @@ -1391,6 +1415,7 @@
ANALOG_SUPPORT || \
BH1750_SUPPORT || \
BMP180_SUPPORT || \
BME680_SUPPORT || \
BMX280_SUPPORT || \
CSE7766_SUPPORT || \
DALLAS_SUPPORT || \
Expand Down
7 changes: 6 additions & 1 deletion code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@
#define SENSOR_SI1145_ID 39
#define SENSOR_HDC1080_ID 40
#define SENSOR_PZEM004TV30_ID 41
#define SENSOR_BME680_ID 42

//--------------------------------------------------------------------------------
// Magnitudes
Expand Down Expand Up @@ -372,8 +373,12 @@
#define MAGNITUDE_RESISTANCE 30
#define MAGNITUDE_PH 31
#define MAGNITUDE_FREQUENCY 32
#define MAGNITUDE_IAQ 33
#define MAGNITUDE_IAQ_ACCURACY 34
#define MAGNITUDE_IAQ_STATIC 35
#define MAGNITUDE_VOC 36

#define MAGNITUDE_MAX 33
#define MAGNITUDE_MAX 38

#define SENSOR_ERROR_OK 0 // No error
#define SENSOR_ERROR_OUT_OF_RANGE 1 // Result out of sensor range
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ DebugLogMode convert(const String& value) {

void debugConfigureBoot() {
static_assert(
std::is_same<int, std::underlying_type<DebugLogMode>::type>::value,
std::is_same<int, std::underlying_type<DebugLogMode>::type>::value,
"should be able to match DebugLogMode with int"
);

Expand Down
44 changes: 43 additions & 1 deletion code/espurna/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include "sensors/BMX280Sensor.h"
#endif

#if BME680_SUPPORT
#include "sensors/BME680Sensor.h"
#endif

#if CSE7766_SUPPORT
#include "sensors/CSE7766Sensor.h"
#endif
Expand Down Expand Up @@ -515,6 +519,8 @@ sensor_magnitude_t::sensor_magnitude_t(unsigned char slot, unsigned char index_l
++_counts[type];

switch (type) {
case MAGNITUDE_IAQ:
case MAGNITUDE_IAQ_STATIC:
case MAGNITUDE_ENERGY:
filter = new LastFilter();
break;
Expand Down Expand Up @@ -641,6 +647,18 @@ String magnitudeTopic(unsigned char type) {
case MAGNITUDE_CO2:
result = F("co2");
break;
case MAGNITUDE_VOC:
result = F("voc");
break;
case MAGNITUDE_IAQ:
result = F("iaq");
break;
case MAGNITUDE_IAQ_ACCURACY:
result = F("iaq_accuracy");
break;
case MAGNITUDE_IAQ_STATIC:
result = F("iaq_static");
break;
case MAGNITUDE_LUX:
result = F("lux");
break;
Expand Down Expand Up @@ -927,6 +945,10 @@ const char * const _magnitudeSettingsPrefix(unsigned char type) {
case MAGNITUDE_PM2dot5: return "pm1dot5";
case MAGNITUDE_PM10: return "pm10";
case MAGNITUDE_CO2: return "co2";
case MAGNITUDE_VOC: return "voc";
case MAGNITUDE_IAQ: return "iaq";
case MAGNITUDE_IAQ_ACCURACY: return "iaqAccuracy";
case MAGNITUDE_IAQ_STATIC: return "iaqStatic";
case MAGNITUDE_LUX: return "lux";
case MAGNITUDE_UVA: return "uva";
case MAGNITUDE_UVB: return "uvb";
Expand Down Expand Up @@ -1155,6 +1177,18 @@ String magnitudeName(unsigned char type) {
case MAGNITUDE_CO2:
result = F("CO2");
break;
case MAGNITUDE_VOC:
result = F("VOC");
break;
case MAGNITUDE_IAQ_STATIC:
result = F("IAQ (Static)");
break;
case MAGNITUDE_IAQ:
result = F("IAQ");
break;
case MAGNITUDE_IAQ_ACCURACY:
result = F("IAQ Accuracy");
break;
case MAGNITUDE_LUX:
result = F("Lux");
break;
Expand Down Expand Up @@ -1570,6 +1604,14 @@ void _sensorLoad() {
}
#endif

#if BME680_SUPPORT
{
BME680Sensor * sensor = new BME680Sensor();
sensor->setAddress(BME680_I2C_ADDRESS);
_sensors.push_back(sensor);
}
#endif

#if CSE7766_SUPPORT
{
CSE7766Sensor * sensor = new CSE7766Sensor();
Expand Down Expand Up @@ -1986,7 +2028,7 @@ void _sensorLoad() {
_sensors.push_back(sensor);
}
#endif

#if T6613_SUPPORT
{
T6613Sensor * sensor = new T6613Sensor();
Expand Down
Loading

0 comments on commit 6266930

Please sign in to comment.