11 namespace bme68x_bsec2 {
13 #define BME68X_BSEC2_ALGORITHM_OUTPUT_LOG(a) (a == ALGORITHM_OUTPUT_CLASSIFICATION ? "Classification" : "Regression") 14 #define BME68X_BSEC2_OPERATING_AGE_LOG(o) (o == OPERATING_AGE_4D ? "4 days" : "28 days") 15 #define BME68X_BSEC2_SAMPLE_RATE_LOG(r) (r == SAMPLE_RATE_DEFAULT ? "Default" : (r == SAMPLE_RATE_ULP ? "ULP" : "LP")) 16 #define BME68X_BSEC2_VOLTAGE_LOG(v) (v == VOLTAGE_3_3V ? "3.3V" : "1.8V") 18 static const char *
const TAG =
"bme68x_bsec2.sensor";
20 static const std::string IAQ_ACCURACY_STATES[4] = {
"Stabilizing",
"Uncertain",
"Calibrating",
"Calibrated"};
23 ESP_LOGCONFIG(TAG,
"Setting up BME68X via BSEC2...");
28 ESP_LOGE(TAG,
"bsec_init_m failed: status %d", this->
bsec_status_);
37 ESP_LOGE(TAG,
"bme68x_init failed: status %d", this->
bme68x_status_);
44 ESP_LOGE(TAG,
"bsec_set_configuration_m failed: status %d", this->
bsec_status_);
52 ESP_LOGE(TAG,
"bsec_update_subscription_m failed: status %d", this->
bsec_status_);
60 ESP_LOGCONFIG(TAG,
"BME68X via BSEC2:");
62 ESP_LOGCONFIG(TAG,
" BSEC2 version: %d.%d.%d.%d", this->
version_.major, this->version_.minor,
63 this->version_.major_bugfix, this->version_.minor_bugfix);
65 ESP_LOGCONFIG(TAG,
" BSEC2 configuration blob:");
72 ESP_LOGE(TAG,
"Communication failed (BSEC2 status: %d, BME68X status: %d)", this->
bsec_status_,
77 ESP_LOGCONFIG(TAG,
" Algorithm output: %s", BME68X_BSEC2_ALGORITHM_OUTPUT_LOG(this->
algorithm_output_));
79 ESP_LOGCONFIG(TAG,
" Operating age: %s", BME68X_BSEC2_OPERATING_AGE_LOG(this->
operating_age_));
80 ESP_LOGCONFIG(TAG,
" Sample rate: %s", BME68X_BSEC2_SAMPLE_RATE_LOG(this->
sample_rate_));
81 ESP_LOGCONFIG(TAG,
" Voltage: %s", BME68X_BSEC2_VOLTAGE_LOG(this->
voltage_));
99 #ifdef USE_TEXT_SENSOR 109 if (this->bsec_status_ < BSEC_OK || this->
bme68x_status_ < BME68X_OK) {
121 if (this->
queue_.size()) {
122 auto action = std::move(this->
queue_.front());
129 if (len > BSEC_MAX_PROPERTY_BLOB_SIZE) {
130 ESP_LOGE(TAG,
"Configuration is larger than BSEC_MAX_PROPERTY_BLOB_SIZE");
134 uint8_t work_buffer[BSEC_MAX_PROPERTY_BLOB_SIZE];
145 return sample_rate ==
SAMPLE_RATE_ULP ? BSEC_SAMPLE_RATE_ULP : BSEC_SAMPLE_RATE_LP;
149 bsec_sensor_configuration_t virtual_sensors[BSEC_NUMBER_OUTPUTS];
150 uint8_t num_virtual_sensors = 0;
153 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_IAQ;
155 num_virtual_sensors++;
159 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_STATIC_IAQ;
161 num_virtual_sensors++;
165 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_CO2_EQUIVALENT;
167 num_virtual_sensors++;
171 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_BREATH_VOC_EQUIVALENT;
173 num_virtual_sensors++;
177 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_RAW_PRESSURE;
179 num_virtual_sensors++;
183 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_RAW_GAS;
185 num_virtual_sensors++;
189 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE;
191 num_virtual_sensors++;
195 virtual_sensors[num_virtual_sensors].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY;
197 num_virtual_sensors++;
200 bsec_sensor_configuration_t sensor_settings[BSEC_MAX_PHYSICAL_SENSOR];
201 uint8_t num_sensor_settings = BSEC_MAX_PHYSICAL_SENSOR;
203 sensor_settings, &num_sensor_settings);
214 ESP_LOGV(TAG,
"Performing sensor run");
216 struct bme68x_conf bme68x_conf;
218 if (this->bsec_status_ < BSEC_OK) {
219 ESP_LOGW(TAG,
"Failed to fetch sensor control settings (BSEC2 error code %d)", this->bsec_status_);
225 bme68x_get_conf(&bme68x_conf, &this->
bme68x_);
228 bme68x_conf.os_temp = this->
bsec_settings_.temperature_oversampling;
230 bme68x_set_conf(&bme68x_conf, &this->
bme68x_);
233 case BME68X_FORCED_MODE:
238 status = bme68x_set_op_mode(this->
bsec_settings_.op_mode, &this->bme68x_);
240 status = bme68x_set_op_mode(BME68X_FORCED_MODE, &this->
bme68x_);
241 this->
op_mode_ = BME68X_FORCED_MODE;
243 ESP_LOGV(TAG,
"Using forced mode");
246 case BME68X_PARALLEL_MODE:
253 BSEC_TOTAL_HEAT_DUR -
254 (bme68x_get_meas_dur(BME68X_PARALLEL_MODE, &bme68x_conf, &this->
bme68x_) / INT64_C(1000));
258 status = bme68x_set_op_mode(BME68X_PARALLEL_MODE, &this->
bme68x_);
259 this->
op_mode_ = BME68X_PARALLEL_MODE;
261 ESP_LOGV(TAG,
"Using parallel mode");
264 case BME68X_SLEEP_MODE:
266 bme68x_set_op_mode(BME68X_SLEEP_MODE, &this->
bme68x_);
268 ESP_LOGV(TAG,
"Using sleep mode");
273 uint32_t meas_dur = 0;
274 meas_dur = bme68x_get_meas_dur(this->
op_mode_, &bme68x_conf, &this->
bme68x_);
275 ESP_LOGV(TAG,
"Queueing read in %uus", meas_dur);
276 this->
set_timeout(
"read", meas_dur / 1000, [
this, curr_time_ns]() { this->
read_(curr_time_ns); });
278 ESP_LOGV(TAG,
"Measurement not required");
279 this->
read_(curr_time_ns);
284 ESP_LOGV(TAG,
"Reading data");
287 uint8_t current_op_mode;
290 if (current_op_mode == BME68X_SLEEP_MODE) {
291 ESP_LOGV(TAG,
"Still in sleep mode, doing nothing");
297 ESP_LOGV(TAG,
"Data processing not required");
301 struct bme68x_data data[3];
306 ESP_LOGW(TAG,
"Failed to get sensor data (BME68X error code %d)", this->
bme68x_status_);
310 ESP_LOGD(TAG,
"BME68X did not provide new data");
314 for (uint8_t i = 0; i < nFields; i++) {
315 bsec_input_t inputs[BSEC_MAX_PHYSICAL_SENSOR];
316 uint8_t num_inputs = 0;
318 if (BSEC_CHECK_INPUT(this->
bsec_settings_.process_data, BSEC_INPUT_TEMPERATURE)) {
319 inputs[num_inputs].sensor_id = BSEC_INPUT_TEMPERATURE;
320 inputs[num_inputs].signal = data[i].temperature;
321 inputs[num_inputs].time_stamp = trigger_time_ns;
324 if (BSEC_CHECK_INPUT(this->
bsec_settings_.process_data, BSEC_INPUT_HEATSOURCE)) {
325 inputs[num_inputs].sensor_id = BSEC_INPUT_HEATSOURCE;
327 inputs[num_inputs].time_stamp = trigger_time_ns;
330 if (BSEC_CHECK_INPUT(this->
bsec_settings_.process_data, BSEC_INPUT_HUMIDITY)) {
331 inputs[num_inputs].sensor_id = BSEC_INPUT_HUMIDITY;
332 inputs[num_inputs].signal = data[i].humidity;
333 inputs[num_inputs].time_stamp = trigger_time_ns;
336 if (BSEC_CHECK_INPUT(this->
bsec_settings_.process_data, BSEC_INPUT_PRESSURE)) {
337 inputs[num_inputs].sensor_id = BSEC_INPUT_PRESSURE;
338 inputs[num_inputs].signal = data[i].pressure;
339 inputs[num_inputs].time_stamp = trigger_time_ns;
342 if (BSEC_CHECK_INPUT(this->
bsec_settings_.process_data, BSEC_INPUT_GASRESISTOR)) {
343 if (data[i].
status & BME68X_GASM_VALID_MSK) {
344 inputs[num_inputs].sensor_id = BSEC_INPUT_GASRESISTOR;
345 inputs[num_inputs].signal = data[i].gas_resistance;
346 inputs[num_inputs].time_stamp = trigger_time_ns;
349 ESP_LOGD(TAG,
"BME68X did not report gas data");
352 if (BSEC_CHECK_INPUT(this->
bsec_settings_.process_data, BSEC_INPUT_PROFILE_PART) &&
353 (data[i].status & BME68X_GASM_VALID_MSK)) {
354 inputs[num_inputs].sensor_id = BSEC_INPUT_PROFILE_PART;
355 inputs[num_inputs].signal = (this->
op_mode_ == BME68X_FORCED_MODE) ? 0 : data[i].gas_index;
356 inputs[num_inputs].time_stamp = trigger_time_ns;
360 if (num_inputs < 1) {
361 ESP_LOGD(TAG,
"No signal inputs available for BSEC2");
365 bsec_output_t outputs[BSEC_NUMBER_OUTPUTS];
366 uint8_t num_outputs = BSEC_NUMBER_OUTPUTS;
369 ESP_LOGW(TAG,
"BSEC2 failed to process signals (BSEC2 error code %d)", this->
bsec_status_);
372 if (num_outputs < 1) {
373 ESP_LOGD(TAG,
"No signal outputs provided by BSEC2");
377 this->
publish_(outputs, num_outputs);
382 ESP_LOGV(TAG,
"Publishing sensor states");
383 bool update_accuracy =
false;
384 uint8_t max_accuracy = 0;
385 for (uint8_t i = 0; i < num_outputs; i++) {
386 float signal = outputs[i].signal;
387 switch (outputs[i].sensor_id) {
388 case BSEC_OUTPUT_IAQ:
389 max_accuracy = std::max(outputs[i].accuracy, max_accuracy);
390 update_accuracy =
true;
395 case BSEC_OUTPUT_STATIC_IAQ:
396 max_accuracy = std::max(outputs[i].accuracy, max_accuracy);
397 update_accuracy =
true;
402 case BSEC_OUTPUT_CO2_EQUIVALENT:
407 case BSEC_OUTPUT_BREATH_VOC_EQUIVALENT:
412 case BSEC_OUTPUT_RAW_PRESSURE:
417 case BSEC_OUTPUT_RAW_GAS:
422 case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE:
427 case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY:
434 if (update_accuracy) {
439 #ifdef USE_TEXT_SENSOR 450 int64_t time_ms =
millis();
461 if (!sensor || (change_only && sensor->
has_state() && sensor->
state == value)) {
468 #ifdef USE_TEXT_SENSOR 481 uint8_t
state[BSEC_MAX_STATE_BLOB_SIZE];
483 ESP_LOGV(TAG,
"Loading state");
484 uint8_t work_buffer[BSEC_MAX_WORKBUFFER_SIZE];
486 bsec_set_state_m(&this->
bsec_instance_, state, BSEC_MAX_STATE_BLOB_SIZE, work_buffer,
sizeof(work_buffer));
488 ESP_LOGW(TAG,
"Failed to load state (BSEC2 error code %d)", this->
bsec_status_);
490 ESP_LOGI(TAG,
"Loaded state");
499 ESP_LOGV(TAG,
"Saving state");
501 uint8_t
state[BSEC_MAX_STATE_BLOB_SIZE];
502 uint8_t work_buffer[BSEC_MAX_STATE_BLOB_SIZE];
503 uint32_t num_serialized_state = BSEC_MAX_STATE_BLOB_SIZE;
506 BSEC_MAX_STATE_BLOB_SIZE, &num_serialized_state);
508 ESP_LOGW(TAG,
"Failed fetch state for save (BSEC2 error code %d)", this->
bsec_status_);
513 ESP_LOGW(TAG,
"Failed to save state");
518 ESP_LOGI(TAG,
"Saved state");
void publish_(const bsec_output_t *outputs, uint8_t num_outputs)
SampleRate pressure_sample_rate_
const float DATA
For components that import data from directly connected sensors like DHT.
uint32_t state_save_interval_ms_
void set_config_(const uint8_t *config, u_int32_t len)
void status_set_warning(const char *message="unspecified")
sensor::Sensor * gas_resistance_sensor_
void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only=false)
ESPPreferenceObject bsec_state_
uint32_t millis_overflow_counter_
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
float temperature_offset_
bsec_library_return_t bsec_status_
uint8_t bsec_instance_[BSEC_INSTANCE_SIZE]
float get_setup_priority() const override
sensor::Sensor * iaq_accuracy_sensor_
sensor::Sensor * breath_voc_equivalent_sensor_
sensor::Sensor * humidity_sensor_
void publish_state(const std::string &state)
bool bsec2_blob_configured_
uint32_t IRAM_ATTR HOT millis()
sensor::Sensor * co2_equivalent_sensor_
float calc_sensor_sample_rate_(SampleRate sample_rate)
struct bme68x_dev bme68x_
float state
This member variable stores the last state that has passed through all filters.
void status_set_error(const char *message="unspecified")
void update_subscription_()
AlgorithmOutput algorithm_output_
struct bme68x_heatr_conf bme68x_heatr_conf_
ESPPreferences * global_preferences
sensor::Sensor * iaq_sensor_
void status_clear_warning()
void save_state_(uint8_t accuracy)
sensor::Sensor * pressure_sensor_
void publish_state(float state)
Publish a new state to the front-end.
uint8_t const * bsec2_configuration_
uint32_t bsec2_configuration_length_
text_sensor::TextSensor * iaq_accuracy_text_sensor_
bsec_bme_settings_t bsec_settings_
OperatingAge operating_age_
void status_clear_error()
SampleRate temperature_sample_rate_
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
sensor::Sensor * iaq_static_sensor_
void dump_config() override
virtual void mark_failed()
Mark this component as failed.
void queue_push_(std::function< void()> &&f)
Implementation of SPI Controller mode.
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
virtual uint32_t get_hash()=0
Base-class for all sensors.
SampleRate humidity_sample_rate_
void read_(int64_t trigger_time_ns)
std::queue< std::function< void()> > queue_
uint32_t last_state_save_ms_
esphome::sensor::Sensor * sensor
sensor::Sensor * temperature_sensor_