2018-11-22 23:37:53 +01:00
|
|
|
#ifdef HAS_BME
|
|
|
|
|
|
|
|
#include "bme680mems.h"
|
|
|
|
|
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = "main";
|
|
|
|
|
2018-11-25 11:48:03 +01:00
|
|
|
bmeStatus_t bme_status;
|
2018-11-25 16:05:30 +01:00
|
|
|
TaskHandle_t BmeTask;
|
2018-11-26 16:12:41 +01:00
|
|
|
|
2018-12-26 23:01:54 +01:00
|
|
|
Bsec iaqSensor;
|
2018-11-25 20:56:14 +01:00
|
|
|
|
2018-12-27 17:09:40 +01:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2018-11-25 11:48:03 +01:00
|
|
|
// initialize BME680 sensor
|
2018-11-25 12:49:49 +01:00
|
|
|
int bme_init(void) {
|
2018-11-22 23:37:53 +01:00
|
|
|
|
2018-12-27 17:09:40 +01:00
|
|
|
// block i2c bus access
|
|
|
|
if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) ==
|
|
|
|
pdTRUE) {
|
2018-11-22 23:37:53 +01:00
|
|
|
|
2018-12-27 17:09:40 +01:00
|
|
|
Wire.begin(HAS_BME);
|
|
|
|
iaqSensor.begin(BME_ADDR, Wire);
|
2018-11-25 16:05:30 +01:00
|
|
|
|
2018-12-27 17:09:40 +01:00
|
|
|
ESP_LOGI(TAG, "BSEC v%d.%d.%d.%d", iaqSensor.version.major,
|
|
|
|
iaqSensor.version.minor, iaqSensor.version.major_bugfix,
|
|
|
|
iaqSensor.version.minor_bugfix);
|
2018-12-21 00:35:21 +01:00
|
|
|
|
2018-12-27 17:09:40 +01:00
|
|
|
iaqSensor.setConfig(bsec_config_iaq);
|
2018-11-22 23:37:53 +01:00
|
|
|
|
2018-12-27 17:09:40 +01:00
|
|
|
if (checkIaqSensorStatus())
|
|
|
|
ESP_LOGI(TAG, "BME680 sensor found and initialized");
|
|
|
|
else {
|
|
|
|
ESP_LOGE(TAG, "BME680 sensor not found");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-11-25 11:48:03 +01:00
|
|
|
|
2018-12-27 17:09:40 +01:00
|
|
|
iaqSensor.setTemperatureOffset((float)BME_TEMP_OFFSET);
|
|
|
|
iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
|
2018-11-22 23:37:53 +01:00
|
|
|
|
2018-12-27 17:09:40 +01:00
|
|
|
if (checkIaqSensorStatus())
|
|
|
|
ESP_LOGI(TAG, "BSEC subscription succesful");
|
|
|
|
else {
|
|
|
|
ESP_LOGE(TAG, "BSEC subscription error");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xSemaphoreGive(I2Caccess); // release i2c bus access
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "I2c bus busy - BME680 initialization error");
|
2018-12-26 23:01:54 +01:00
|
|
|
return 1;
|
2018-11-22 23:37:53 +01:00
|
|
|
}
|
2018-12-27 17:09:40 +01:00
|
|
|
|
|
|
|
} // bme_init()
|
2018-11-22 23:37:53 +01:00
|
|
|
|
2018-12-26 23:01:54 +01:00
|
|
|
// Helper function definitions
|
|
|
|
int checkIaqSensorStatus(void) {
|
|
|
|
int rslt = 1; // true = 1 = no error, false = 0 = error
|
|
|
|
|
|
|
|
if (iaqSensor.status != BSEC_OK) {
|
|
|
|
rslt = 0;
|
|
|
|
if (iaqSensor.status < BSEC_OK)
|
|
|
|
ESP_LOGE(TAG, "BSEC error %d", iaqSensor.status);
|
|
|
|
else
|
|
|
|
ESP_LOGW(TAG, "BSEC warning %d", iaqSensor.status);
|
2018-11-22 23:37:53 +01:00
|
|
|
}
|
|
|
|
|
2018-12-26 23:01:54 +01:00
|
|
|
if (iaqSensor.bme680Status != BME680_OK) {
|
|
|
|
rslt = 0;
|
|
|
|
if (iaqSensor.bme680Status < BME680_OK)
|
|
|
|
ESP_LOGE(TAG, "BME680 error %d", iaqSensor.bme680Status);
|
|
|
|
else
|
|
|
|
ESP_LOGW(TAG, "BME680 warning %d", iaqSensor.bme680Status);
|
|
|
|
}
|
2018-11-25 11:48:03 +01:00
|
|
|
|
2018-12-26 23:01:54 +01:00
|
|
|
return rslt;
|
2018-12-27 17:09:40 +01:00
|
|
|
} // checkIaqSensorStatus()
|
2018-11-25 11:48:03 +01:00
|
|
|
|
2018-12-26 23:01:54 +01:00
|
|
|
// loop function which reads and processes data based on sensor settings
|
|
|
|
void bme_loop(void *pvParameters) {
|
2018-11-25 20:56:14 +01:00
|
|
|
|
2018-12-26 23:01:54 +01:00
|
|
|
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
|
2018-11-25 11:48:03 +01:00
|
|
|
|
2018-12-26 23:01:54 +01:00
|
|
|
#ifdef HAS_BME
|
|
|
|
while (checkIaqSensorStatus()) {
|
2018-12-27 17:09:40 +01:00
|
|
|
|
|
|
|
// block i2c bus access
|
|
|
|
if (xSemaphoreTake(I2Caccess, (DISPLAYREFRESH_MS / portTICK_PERIOD_MS)) ==
|
|
|
|
pdTRUE) {
|
|
|
|
|
|
|
|
if (iaqSensor.run()) { // If new data is available
|
|
|
|
bme_status.raw_temperature = iaqSensor.rawTemperature;
|
|
|
|
bme_status.raw_humidity = iaqSensor.rawHumidity;
|
|
|
|
bme_status.temperature = iaqSensor.temperature;
|
|
|
|
bme_status.humidity = iaqSensor.humidity;
|
|
|
|
bme_status.pressure =
|
|
|
|
(iaqSensor.pressure / 100.0); // conversion Pa -> hPa
|
|
|
|
bme_status.iaq = iaqSensor.iaqEstimate;
|
|
|
|
bme_status.iaq_accuracy = iaqSensor.iaqAccuracy;
|
|
|
|
bme_status.gas = iaqSensor.gasResistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
xSemaphoreGive(I2Caccess); // release i2c bus access
|
2018-12-26 23:01:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ESP_LOGE(TAG, "BME task ended");
|
|
|
|
vTaskDelete(BmeTask); // should never be reached
|
2018-11-22 23:37:53 +01:00
|
|
|
|
2018-12-26 23:01:54 +01:00
|
|
|
} // bme_loop()
|
2018-11-25 11:48:03 +01:00
|
|
|
|
2018-11-22 23:37:53 +01:00
|
|
|
#endif // HAS_BME
|