Use Bsec library for BME680

This commit is contained in:
Admar Schoonen 2022-05-22 23:41:02 +02:00
parent 1d65b69a1a
commit b05800e3f0
1 changed files with 85 additions and 71 deletions

View File

@ -10,11 +10,7 @@
#include <HTTPClient.h>
#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
//#include "bsec.h"
#include "bsec.h"
#define NUM_LEDS_PROTO_PAPER 93
#define DATA_PIN_PROTO_PAPER 5
@ -46,10 +42,8 @@ Adafruit_NeoPixel leds_rgbw_proto_v1(NUM_LEDS_PROTO_V1, DATA_PIN_PROTO_V1, NEO_R
#define SEALEVELPRESSURE_HPA (1013.25)
#define BME_CS 5
Adafruit_BME680 bme(BME_CS); // hardware SPI
//SPIClass SPI1(HSPI);
//Bsec iaqSensor;
String output;
Bsec iaqSensor;
static HTTPClient http;
static const String baseUrlAQI = "http://target.luon.net:2356//forecast?&metrics=PAQI&metrics=AQI&metrics=pollen&metrics=UVI";
@ -83,8 +77,6 @@ const char* root_ca = \
const float location[] = {51.445466493287434, 5.515445691496135}; // Telefoonstraat, Eindhoven
const String address = "Telefoonstraat 18, Eindhoven";
// const String address = "Bandeliersberg 157, Roosendaal";
//const float location[] = {51.51831326813842, 4.451744264773111}; // Bandeliersberg, Roosendaal
//const float location[] = {51.44083, 5.47778}; // Eindhoven
//const float location[] = {52.09083, 5.12222}; // Utrecht
@ -236,6 +228,33 @@ void leds_clear(void) {
}
}
int checkIaqSensorStatus(void)
{
if (iaqSensor.status != BSEC_OK) {
if (iaqSensor.status < BSEC_OK) {
output = "BSEC error code : " + String(iaqSensor.status);
Serial.println(output);
//for (;;);
} else {
output = "BSEC warning code : " + String(iaqSensor.status);
Serial.println(output);
}
}
if (iaqSensor.bme680Status != BME680_OK) {
if (iaqSensor.bme680Status < BME680_OK) {
output = "BME680 error code : " + String(iaqSensor.bme680Status);
Serial.println(output);
//for (;;);
} else {
output = "BME680 warning code : " + String(iaqSensor.bme680Status);
Serial.println(output);
}
}
return iaqSensor.bme680Status;
}
void readSensors(void) {
static bool readingInProgress = false;
static unsigned long endTime = 0;
@ -244,56 +263,24 @@ void readSensors(void) {
return;
}
if (readingInProgress == false) {
Serial.println("Starting new measurement");
unsigned long endTime = bme.beginReading();
if (endTime == 0) {
Serial.println(F("Failed to begin reading :("));
return;
}
readingInProgress = true;
Serial.print(F("Reading started at "));
Serial.print(millis());
Serial.print(F(" and will finish at "));
Serial.println(endTime);
unsigned long time_trigger = millis();
if (iaqSensor.run()) { // If new data is available
output = String(time_trigger);
output += " ms, Traw: " + String(iaqSensor.rawTemperature);
output += " *C , P: " + String(iaqSensor.pressure);
output += " hPa, RHraw: " + String(iaqSensor.rawHumidity);
output += " %, Rgas: " + String(iaqSensor.gasResistance);
output += " Ohm, IAQ: " + String(iaqSensor.iaq);
output += ", accuracy: " + String(iaqSensor.iaqAccuracy);
output += ", Tcorr: " + String(iaqSensor.temperature);
output += " *C, RHcorr" + String(iaqSensor.humidity);
output += " %, IAQ static" + String(iaqSensor.staticIaq);
output += ", eCO2: " + String(iaqSensor.co2Equivalent);
output += " ppm, eVOC: " + String(iaqSensor.breathVocEquivalent);
output += " ppm";
Serial.println(output);
} else {
if (millis() - endTime > 0) {
// Obtain measurement results from BME680. Note that this operation isn't
// instantaneous even if milli() >= endTime due to I2C/SPI latency.
//
// This call takes about 162 ms
if (!bme.endReading()) {
Serial.println(F("Failed to complete reading :("));
return;
}
readingInProgress = false;
Serial.print(F("Reading completed at "));
Serial.println(millis());
Serial.print(F("Temperature = "));
Serial.print(bme.temperature);
Serial.println(F(" *C"));
Serial.print(F("Pressure = "));
Serial.print(bme.pressure / 100.0);
Serial.println(F(" hPa"));
Serial.print(F("Humidity = "));
Serial.print(bme.humidity);
Serial.println(F(" %"));
Serial.print(F("Gas = "));
Serial.print(bme.gas_resistance / 1000.0);
Serial.println(F(" KOhms"));
Serial.print(F("Approx. Altitude = "));
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(F(" m"));
Serial.println();
}
checkIaqSensorStatus();
}
}
@ -323,30 +310,45 @@ void setup() {
Serial.begin(115200);
Serial.println("buienradarklok starting");
if (!bme.begin()) {
SPI.begin();
iaqSensor.begin(BME_CS, SPI);
String output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
Serial.println(output);
x = checkIaqSensorStatus();
if (x != BME680_OK) {
Serial.println(F("Could not find a valid BME680 sensor; assuming hw variant \"paper prototype\"!"));
hw_variant = HW_PROTO_PAPER;
setup_pins_proto_paper();
leds_rgb_proto_paper.begin(); // INITIALIZE NeoPixel strip object
} else {
Serial.println(F("BME680 found; assuming hw variant \"proto v1\""));
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
hw_variant = HW_PROTO_V1;
setup_pins_proto_v1();
leds_rgbw_proto_v1.begin(); // INITIALIZE NeoPixel strip object
}
//SPI1.begin();
//iaqSensor.begin(BME_CS, SPI1);
bsec_virtual_sensor_t sensorList[10] = {
BSEC_OUTPUT_RAW_TEMPERATURE,
BSEC_OUTPUT_RAW_PRESSURE,
BSEC_OUTPUT_RAW_HUMIDITY,
BSEC_OUTPUT_RAW_GAS,
BSEC_OUTPUT_IAQ,
BSEC_OUTPUT_STATIC_IAQ,
BSEC_OUTPUT_CO2_EQUIVALENT,
BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
};
//String output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
//Serial.println(output);
iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
checkIaqSensorStatus();
// Print the header
output = "Timestamp [ms], raw temperature [°C], pressure [hPa], raw relative humidity [%], gas [Ohm], IAQ, IAQ accuracy, temperature [°C], relative humidity [%], Static IAQ, CO2 equivalent, breath VOC equivalent";
Serial.println(output);
leds_clear();
#ifndef DISABLE_WIFI
@ -810,6 +812,18 @@ static void updateLeds(void) {
}
ledsSetPixelColor(IAQI_LED, r, g, b);
if (hw_variant == HW_PROTO_V1) {
float x = iaqSensor.iaq / 40;
x = x < 1.0f ? 1.0f : x;
color = colormap(x);
r = (color & 0xFF0000) >> 16;
g = (color & 0x00FF00) >> 8;
b = (color & 0x0000FF);
ledsSetPixelColor(IAQI_LED, r, g, b);
}
}
void readConnectButton(void) {