BME680 and BMP280 code merged

This commit is contained in:
Ollo 2021-11-27 12:29:27 +01:00
parent 7d188e9c8e
commit 9807aa9818
3 changed files with 67 additions and 18 deletions

View File

@ -12,7 +12,7 @@
#ifndef HOMIE_SETTINGS #ifndef HOMIE_SETTINGS
#define HOMIE_SETTINGS #define HOMIE_SETTINGS
#define HOMIE_FIRMWARE_NAME "Vindriktning" #define HOMIE_FIRMWARE_NAME "RoomSensor"
#define HOMIE_FIRMWARE_VERSION "1.2.1" #define HOMIE_FIRMWARE_VERSION "2.0.0"
#endif #endif

View File

@ -13,10 +13,16 @@ platform = espressif8266
board = d1_mini board = d1_mini
framework = arduino framework = arduino
build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
; build_flag needs define the Bosch sensor...
; -D BMP280
;or
; -D BME680
; the latest development branch (convention V3.0.x) ; the latest development branch (convention V3.0.x)
lib_deps = https://github.com/homieiot/homie-esp8266.git#develop lib_deps = https://github.com/homieiot/homie-esp8266.git#develop
EspSoftwareSerial EspSoftwareSerial
NeoPixel NeoPixel
adafruit/Adafruit BMP280 Library @ ^2.4.2
adafruit/Adafruit BME680 Library @ ^2.0.1 adafruit/Adafruit BME680 Library @ ^2.0.1
upload_port = /dev/ttyUSB1 upload_port = /dev/ttyUSB1

View File

@ -18,7 +18,15 @@
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
#include <Wire.h> #include <Wire.h>
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#ifdef BME680
#include "Adafruit_BME680.h" #include "Adafruit_BME680.h"
#else
#ifdef BMP280
#include "Adafruit_BMP280.h"
#else
#error "Decition, which BMx??? is used missing"
#endif
#endif
/****************************************************************************** /******************************************************************************
* DEFINES * DEFINES
@ -93,18 +101,37 @@ HomieNode particle(NODE_PARTICLE, "particle", "number"); /**< Measuret in micro
HomieNode temperatureNode(NODE_TEMPERATUR, "Room Temperature", "number"); HomieNode temperatureNode(NODE_TEMPERATUR, "Room Temperature", "number");
HomieNode pressureNode(NODE_PRESSURE, "Pressure", "number"); HomieNode pressureNode(NODE_PRESSURE, "Pressure", "number");
HomieNode altitudeNode(NODE_ALTITUDE, "Altitude", "number"); HomieNode altitudeNode(NODE_ALTITUDE, "Altitude", "number");
#ifdef BME680
HomieNode gasNode(NODE_GAS, "Gas", "number"); HomieNode gasNode(NODE_GAS, "Gas", "number");
HomieNode humidityNode(NODE_HUMIDITY, "Humidity", "number"); HomieNode humidityNode(NODE_HUMIDITY, "Humidity", "number");
#endif
/****************************** Output control ***********************/ /****************************** Output control ***********************/
HomieNode ledStripNode /* to rule them all */("led", "RGB led", "color"); HomieNode ledStripNode /* to rule them all */("led", "RGB led", "color");
/************************** Settings ******************************/ /************************** Settings ******************************/
HomieSetting<bool> i2cEnable("i2c", "BME280 sensor present"); HomieSetting<bool> i2cEnable("i2c",
#ifdef BME680
"BME680 sensor present"
#else
#ifdef BMP280
"BMP280 sensor present"
#else
"No I2C sensor specified in the project"
#endif
#endif
);
HomieSetting<bool> rgbTemp("rgbTemp", "Show temperatur via red (>20 °C) and blue (< 20°C)"); HomieSetting<bool> rgbTemp("rgbTemp", "Show temperatur via red (>20 °C) and blue (< 20°C)");
static SoftwareSerial pmSerial(SENSOR_PM1006_RX, SENSOR_PM1006_TX); static SoftwareSerial pmSerial(SENSOR_PM1006_RX, SENSOR_PM1006_TX);
Adafruit_BME680 bme(&Wire); // connected via I2C #ifdef BME680
Adafruit_BME680 bmx(&Wire); // connected via I2C
#else
#ifdef BMP280
Adafruit_BMP280 bmx; // connected via I2C
#endif
#endif
Adafruit_NeoPixel strip(PIXEL_COUNT, GPIO_WS2812, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel strip(PIXEL_COUNT, GPIO_WS2812, NEO_GRB + NEO_KHZ800);
@ -199,21 +226,25 @@ void onHomieEvent(const HomieEvent &event)
} }
void bmpPublishValues() { void bmpPublishValues() {
#ifdef BME680
// Tell BME680 to begin measurement. // Tell BME680 to begin measurement.
unsigned long endTime = bme.beginReading(); unsigned long endTime = bmx.beginReading();
if (endTime == 0) { if (endTime == 0) {
log(MQTT_LEVEL_ERROR, F("BME680 not accessable"), MQTT_LOG_I2READ); log(MQTT_LEVEL_ERROR, F("BME680 not accessable"), MQTT_LOG_I2READ);
return; return;
} }
temperatureNode.setProperty(NODE_TEMPERATUR).send(String(bme.readTemperature())); #endif
pressureNode.setProperty(NODE_PRESSURE).send(String(bme.readPressure() / 100.0F)); // Publish the values
altitudeNode.setProperty(NODE_ALTITUDE).send(String(bme.readAltitude(SEALEVELPRESSURE_HPA))); temperatureNode.setProperty(NODE_TEMPERATUR).send(String(bmx.readTemperature()));
gasNode.setProperty(NODE_GAS).send(String((bme.gas_resistance / 1000.0))); pressureNode.setProperty(NODE_PRESSURE).send(String(bmx.readPressure() / 100.0F));
altitudeNode.setProperty(NODE_ALTITUDE).send(String(bmx.readAltitude(SEALEVELPRESSURE_HPA)));
humidityNode.setProperty(NODE_HUMIDITY).send(String(bme.humidity)); #ifdef BME680
gasNode.setProperty(NODE_GAS).send(String((bmx.gas_resistance / 1000.0)));
humidityNode.setProperty(NODE_HUMIDITY).send(String(bmx.humidity));
#endif
if ( (rgbTemp.get()) && (!mSomethingReceived) ) { if ( (rgbTemp.get()) && (!mSomethingReceived) ) {
if (bme.readTemperature() < TEMPBORDER) { if (bmx.readTemperature() < TEMPBORDER) {
strip.setPixelColor(0, strip.Color(0,0,255)); strip.setPixelColor(0, strip.Color(0,0,255));
} else { } else {
strip.setPixelColor(0, strip.Color(255,0,0)); strip.setPixelColor(0, strip.Color(255,0,0));
@ -327,12 +358,14 @@ void setup()
altitudeNode.advertise(NODE_ALTITUDE).setName("Altitude") altitudeNode.advertise(NODE_ALTITUDE).setName("Altitude")
.setDatatype("float") .setDatatype("float")
.setUnit("m"); .setUnit("m");
#ifdef BME680
gasNode.advertise(NODE_GAS).setName("Gas") gasNode.advertise(NODE_GAS).setName("Gas")
.setDatatype("float") .setDatatype("float")
.setUnit(" KOhms"); .setUnit(" KOhms");
humidityNode.advertise(NODE_HUMIDITY).setName("Humidity") humidityNode.advertise(NODE_HUMIDITY).setName("Humidity")
.setDatatype("float") .setDatatype("float")
.setUnit("%"); .setUnit("%");
#endif
ledStripNode.advertise(NODE_AMBIENT).setName("All Leds") ledStripNode.advertise(NODE_AMBIENT).setName("All Leds")
.setDatatype("color").setFormat("rgb") .setDatatype("color").setFormat("rgb")
.settable(ledHandler); .settable(ledHandler);
@ -350,13 +383,23 @@ void setup()
strip.fill(strip.Color(0,128,0)); strip.fill(strip.Color(0,128,0));
strip.show(); strip.show();
/* Extracted from library's example */ /* Extracted from library's example */
mFailedI2Cinitialization = !bme.begin(); mFailedI2Cinitialization = !bmx.begin();
if (!mFailedI2Cinitialization) { if (!mFailedI2Cinitialization) {
bme.setTemperatureOversampling(BME680_OS_8X); #ifdef BME680
bme.setHumidityOversampling(BME680_OS_2X); bmx.setTemperatureOversampling(BME680_OS_8X);
bme.setPressureOversampling(BME680_OS_4X); bmx.setHumidityOversampling(BME680_OS_2X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3); bmx.setPressureOversampling(BME680_OS_4X);
bme.setGasHeater(320, 150); // 320*C for 150 ms bmx.setIIRFilterSize(BME680_FILTER_SIZE_3);
bmx.setGasHeater(320, 150); // 320*C for 150 ms
#endif
#ifdef BMP280
/* Default settings from datasheet. */
bmx.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
#endif
} else { } else {
printf("Faild to initialize I2C bus\r\n"); printf("Faild to initialize I2C bus\r\n");
} }