10static constexpr uint32_t READ_TIMEOUT_ID = 0;
14std::vector<BME680BSECComponent *>
15 BME680BSECComponent::instances;
19 uint8_t new_idx = BME680BSECComponent::instances.size();
20 BME680BSECComponent::instances.push_back(
this);
30 this->
bme680_.intf = BME680_I2C_INTF;
55 const uint8_t config[] = {
56#include "config/generic_33v_300s_28d/bsec_iaq.txt"
61 const uint8_t config[] = {
62#include "config/generic_18v_300s_28d/bsec_iaq.txt"
69 const uint8_t config[] = {
70#include "config/generic_33v_3s_28d/bsec_iaq.txt"
75 const uint8_t config[] = {
76#include "config/generic_18v_3s_28d/bsec_iaq.txt"
88 return sample_rate ==
SAMPLE_RATE_ULP ? BSEC_SAMPLE_RATE_ULP : BSEC_SAMPLE_RATE_LP;
92 bsec_sensor_configuration_t virtual_sensors[BSEC_NUMBER_OUTPUTS];
93 int num_virtual_sensors = 0;
95 if (this->iaq_sensor_) {
96 virtual_sensors[num_virtual_sensors].sensor_id =
99 num_virtual_sensors++;
102 if (this->co2_equivalent_sensor_) {
103 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_CO2_EQUIVALENT;
105 num_virtual_sensors++;
108 if (this->breath_voc_equivalent_sensor_) {
109 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_BREATH_VOC_EQUIVALENT;
111 num_virtual_sensors++;
114 if (this->pressure_sensor_) {
115 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_RAW_PRESSURE;
117 num_virtual_sensors++;
120 if (this->gas_resistance_sensor_) {
121 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_RAW_GAS;
123 num_virtual_sensors++;
126 if (this->temperature_sensor_) {
127 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE;
129 num_virtual_sensors++;
132 if (this->humidity_sensor_) {
133 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY;
135 num_virtual_sensors++;
138 bsec_sensor_configuration_t sensor_settings[BSEC_MAX_PHYSICAL_SENSOR];
139 uint8_t num_sensor_settings = BSEC_MAX_PHYSICAL_SENSOR;
141 bsec_update_subscription(virtual_sensors, num_virtual_sensors, sensor_settings, &num_sensor_settings);
142 ESP_LOGV(TAG,
"%s: updating subscription for %d virtual sensors (out=%d sensors)", this->
device_id_,
143 num_virtual_sensors, num_sensor_settings);
147 ESP_LOGCONFIG(TAG,
"%s via BSEC:", this->
device_id_);
149 bsec_version_t version;
150 bsec_get_version(&version);
151 ESP_LOGCONFIG(TAG,
" BSEC Version: %d.%d.%d.%d", version.major, version.minor, version.major_bugfix,
152 version.minor_bugfix);
154 LOG_I2C_DEVICE(
this);
157 ESP_LOGE(TAG,
"Communication failed (BSEC Status: %d, BME680 Status: %d)", this->
bsec_status_,
162 " Temperature Offset: %.2f\n"
164 " Supply Voltage: %sV\n"
166 " State Save Interval: %" PRIu32
"ms",
172 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
174 LOG_SENSOR(
" ",
"Pressure", this->pressure_sensor_);
176 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
178 LOG_SENSOR(
" ",
"Gas Resistance", this->gas_resistance_sensor_);
179 LOG_SENSOR(
" ",
"IAQ", this->iaq_sensor_);
180 LOG_SENSOR(
" ",
"Numeric IAQ Accuracy", this->iaq_accuracy_sensor_);
181 LOG_TEXT_SENSOR(
" ",
"IAQ Accuracy", this->iaq_accuracy_text_sensor_);
182 LOG_SENSOR(
" ",
"CO2 Equivalent", this->co2_equivalent_sensor_);
183 LOG_SENSOR(
" ",
"Breath VOC Equivalent", this->breath_voc_equivalent_sensor_);
198 if (this->
queue_.size()) {
199 auto action = std::move(this->
queue_.front());
211 ESP_LOGV(TAG,
"%s: Performing sensor run", this->
device_id_);
216 if (BME680BSECComponent::instances.
size() > 1) {
224 ESP_LOGW(TAG,
"Failed to fetch sensor control settings (BSEC Error Code %d)", this->
bsec_status_);
236 this->
bme680_.power_mode = BME680_FORCED_MODE;
237 uint16_t desired_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL;
240 ESP_LOGW(TAG,
"Failed to set sensor settings (BME680 Error Code %d)", this->
bme680_status_);
246 ESP_LOGW(TAG,
"Failed to set sensor mode (BME680 Error Code %d)", this->
bme680_status_);
250 uint16_t meas_dur = 0;
251 bme680_get_profile_dur(&meas_dur, &this->
bme680_);
256 if (BME680BSECComponent::instances.
size() > 1)
259 ESP_LOGV(TAG,
"Queueing read in %ums", meas_dur);
262 ESP_LOGV(TAG,
"Measurement not required");
268 ESP_LOGV(TAG,
"%s: Reading data", this->
device_id_);
273 while (this->
bme680_.power_mode != BME680_SLEEP_MODE) {
274 if (
millis() - start > 50) {
275 ESP_LOGE(TAG,
"Timeout waiting for BME680 to enter sleep mode");
280 ESP_LOGE(TAG,
"Failed to get sensor mode (BME680 Error Code %d)", this->
bme680_status_);
287 ESP_LOGV(TAG,
"Data processing not required");
291 struct bme680_field_data data;
294 if (this->bme680_status_ != BME680_OK) {
295 ESP_LOGW(TAG,
"Failed to get sensor data (BME680 Error Code %d)", this->bme680_status_);
298 if (!(data.status & BME680_NEW_DATA_MSK)) {
299 ESP_LOGD(TAG,
"BME680 did not report new data");
303 bsec_input_t inputs[BSEC_MAX_PHYSICAL_SENSOR];
304 uint8_t num_inputs = 0;
307 inputs[num_inputs].sensor_id = BSEC_INPUT_TEMPERATURE;
308 inputs[num_inputs].signal = data.temperature / 100.0f;
309 inputs[num_inputs].time_stamp = curr_time_ns;
313 inputs[num_inputs].sensor_id = BSEC_INPUT_HEATSOURCE;
315 inputs[num_inputs].time_stamp = curr_time_ns;
319 inputs[num_inputs].sensor_id = BSEC_INPUT_HUMIDITY;
320 inputs[num_inputs].signal = data.humidity / 1000.0f;
321 inputs[num_inputs].time_stamp = curr_time_ns;
325 inputs[num_inputs].sensor_id = BSEC_INPUT_PRESSURE;
326 inputs[num_inputs].signal = data.pressure;
327 inputs[num_inputs].time_stamp = curr_time_ns;
331 if (data.status & BME680_GASM_VALID_MSK) {
332 inputs[num_inputs].sensor_id = BSEC_INPUT_GASRESISTOR;
333 inputs[num_inputs].signal = data.gas_resistance;
334 inputs[num_inputs].time_stamp = curr_time_ns;
337 ESP_LOGD(TAG,
"BME680 did not report gas data");
340 if (num_inputs < 1) {
341 ESP_LOGD(TAG,
"No signal inputs available for BSEC");
348 if (BME680BSECComponent::instances.
size() > 1) {
356 ESP_LOGW(TAG,
"Failed to fetch sensor control settings (BSEC Error Code %d)", this->
bsec_status_);
361 bsec_output_t outputs[BSEC_NUMBER_OUTPUTS];
362 uint8_t num_outputs = BSEC_NUMBER_OUTPUTS;
363 this->
bsec_status_ = bsec_do_steps(inputs, num_inputs, outputs, &num_outputs);
365 ESP_LOGW(TAG,
"BSEC failed to process signals (BSEC Error Code %d)", this->
bsec_status_);
368 ESP_LOGV(TAG,
"%s: after bsec_do_steps: num_inputs=%d num_outputs=%d", this->
device_id_, num_inputs, num_outputs);
371 if (BME680BSECComponent::instances.
size() > 1)
374 if (num_outputs < 1) {
375 ESP_LOGD(TAG,
"No signal outputs provided by BSEC");
379 this->
publish_(outputs, num_outputs);
383 ESP_LOGV(TAG,
"%s: Queuing sensor state publish actions", this->
device_id_);
384 for (uint8_t i = 0; i < num_outputs; i++) {
385 float signal = outputs[i].signal;
386 switch (outputs[i].sensor_id) {
387 case BSEC_OUTPUT_IAQ:
388 case BSEC_OUTPUT_STATIC_IAQ: {
389 uint8_t accuracy = std::min<uint8_t>(outputs[i].accuracy, IaqAccuracyStrings::LAST_INDEX);
393 IaqAccuracyStrings::get_progmem_str(accuracy, IaqAccuracyStrings::LAST_INDEX));
400 case BSEC_OUTPUT_CO2_EQUIVALENT:
403 case BSEC_OUTPUT_BREATH_VOC_EQUIVALENT:
406 case BSEC_OUTPUT_RAW_PRESSURE:
409 case BSEC_OUTPUT_RAW_GAS:
412 case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE:
415 case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY:
423 int64_t time_ms =
millis();
441 (sensor->
has_state() && ESPHOME_strcmp_P(sensor->
state.c_str(),
reinterpret_cast<ESPHOME_PGM_P
>(
value)) == 0)) {
464 ESP_LOGV(TAG,
"Delaying for %" PRIu32
"ms", period);
471 uint32_t num_serialized_state = BSEC_MAX_STATE_BLOB_SIZE;
475 ESP_LOGW(TAG,
"%s: Failed to fetch BSEC library state for snapshot (BSEC Error Code %d)", this->
device_id_,
486 ESP_LOGV(TAG,
"%s: BSEC state data NOT valid, aborting restore_state_()", this->
device_id_);
493 ESP_LOGW(TAG,
"Failed to restore BSEC library state (BSEC Error Code %d)", this->
bsec_status_);
531 ESP_LOGV(TAG,
"%s: Loading BSEC library state", this->
device_id_);
535 ESP_LOGW(TAG,
"%s: Failed to load BSEC library state (BSEC Error Code %d)", this->
device_id_, this->
bsec_status_);
540 ESP_LOGI(TAG,
"%s: Loaded BSEC library state", this->
device_id_);
547 if (BME680BSECComponent::instances.
size() <= 1) {
555 ESP_LOGV(TAG,
"%s: Saving state", this->
device_id_);
558 ESP_LOGW(TAG,
"Failed to save state");
563 ESP_LOGI(TAG,
"Saved state");
void mark_failed()
Mark this component as failed.
void status_clear_error()
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
ESPPreferenceObject bsec_state_
bsec_library_return_t bsec_status_
uint32_t state_save_interval_ms_
SampleRate pressure_sample_rate_
uint32_t millis_overflow_counter_
float temperature_offset_
std::queue< std::function< void()> > queue_
SampleRate temperature_sample_rate_
static SUB_SENSOR(temperature) SUB_SENSOR(pressure) SUB_SENSOR(humidity) SUB_SENSOR(gas_resistance) SUB_SENSOR(iaq) SUB_TEXT_SENSOR(iaq_accuracy) SUB_SENSOR(iaq_accuracy) SUB_SENSOR(co2_equivalent) SUB_SENSOR(breath_voc_equivalent) static std int8_t read_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len)
bool bsec_state_data_valid_
SupplyVoltage supply_voltage_
void save_state_(uint8_t accuracy)
static uint8_t work_buffer_[BSEC_MAX_WORKBUFFER_SIZE]
void queue_push_(std::function< void()> &&f)
float calc_sensor_sample_rate_(SampleRate sample_rate)
uint32_t last_state_save_ms_
void dump_config() override
static int8_t write_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len)
uint8_t bsec_state_data_[BSEC_MAX_STATE_BLOB_SIZE]
void publish_(const bsec_output_t *outputs, uint8_t num_outputs)
void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only=false)
SampleRate humidity_sample_rate_
bsec_bme_settings_t bme680_settings_
void update_subscription_()
struct bme680_dev bme680_
static void delay_ms(uint32_t period)
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Base-class for all sensors.
void publish_state(float state)
Publish a new state to the front-end.
float state
This member variable stores the last state that has passed through all filters.
void publish_state(const std::string &state)
ESPHOME_LOG_TAG(TAG, "bme680_bsec.sensor")
PROGMEM_STRING_TABLE(IaqAccuracyStrings, "Stabilizing", "Uncertain", "Calibrating", "Calibrated")
constexpr uint32_t fnv1_hash_extend(uint32_t hash, T value)
Extend a FNV-1 hash with an integer (hashes each byte).
ESPPreferences * global_preferences
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
const __FlashStringHelper * ProgmemStr
ESPPreferenceObject make_preference(size_t, uint32_t, bool)