ESPHome 2026.2.3
Loading...
Searching...
No Matches
bme680_bsec.h
Go to the documentation of this file.
1#pragma once
2
9#include <map>
10
11#ifdef USE_BSEC
12#include <bsec.h>
13#endif
14
15namespace esphome {
16namespace bme680_bsec {
17#ifdef USE_BSEC
18
23
28
34
35#define BME680_BSEC_SAMPLE_RATE_LOG(r) (r == SAMPLE_RATE_DEFAULT ? "Default" : (r == SAMPLE_RATE_ULP ? "ULP" : "LP"))
36
38 public:
39 void set_device_id(const std::string &devid) { this->device_id_.assign(devid); }
40 void set_temperature_offset(float offset) { this->temperature_offset_ = offset; }
41 void set_iaq_mode(IAQMode iaq_mode) { this->iaq_mode_ = iaq_mode; }
42 void set_state_save_interval(uint32_t interval) { this->state_save_interval_ms_ = interval; }
43 void set_supply_voltage(SupplyVoltage supply_voltage) { this->supply_voltage_ = supply_voltage; }
44
45 void set_sample_rate(SampleRate sample_rate) { this->sample_rate_ = sample_rate; }
46 void set_temperature_sample_rate(SampleRate sample_rate) { this->temperature_sample_rate_ = sample_rate; }
47 void set_pressure_sample_rate(SampleRate sample_rate) { this->pressure_sample_rate_ = sample_rate; }
48 void set_humidity_sample_rate(SampleRate sample_rate) { this->humidity_sample_rate_ = sample_rate; }
49
51 void set_pressure_sensor(sensor::Sensor *sensor) { this->pressure_sensor_ = sensor; }
52 void set_humidity_sensor(sensor::Sensor *sensor) { this->humidity_sensor_ = sensor; }
54 void set_iaq_sensor(sensor::Sensor *sensor) { this->iaq_sensor_ = sensor; }
59
60 static std::vector<BME680BSECComponent *> instances;
61 static int8_t read_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len);
62 static int8_t write_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len);
63 static void delay_ms(uint32_t period);
64
65 void setup() override;
66 void dump_config() override;
67 void loop() override;
68
69 protected:
70 void set_config_();
71 float calc_sensor_sample_rate_(SampleRate sample_rate);
73
74 void run_();
75 void read_();
76 void publish_(const bsec_output_t *outputs, uint8_t num_outputs);
77 int64_t get_time_ns_();
78
79 void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only = false);
80 void publish_sensor_(text_sensor::TextSensor *sensor, const std::string &value);
81
82 void snapshot_state_(); // Fetch the current BSEC library state and save it in the bsec_state_data_ member (volatile
83 // memory)
84 void restore_state_(); // Push the state contained in the bsec_state_data_ member (volatile memory) to the BSEC
85 // library
86 int reinit_bsec_lib_(); // Prepare the BSEC library to be used again after this object returns active
87 // (as the library may have been used by other objects)
88 void load_state_(); // Initialize the ESP preferences object; retrieve the BSEC library state from the ESP
89 // preferences (storage); then save it in the bsec_state_data_ member (volatile memory) and
90 // push it to the BSEC library
91 void save_state_(
92 uint8_t accuracy); // Save the bsec_state_data_ member (volatile memory) to the ESP preferences (storage)
93
94 void queue_push_(std::function<void()> &&f) { this->queue_.push(std::move(f)); }
95
96 static uint8_t work_buffer_[BSEC_MAX_WORKBUFFER_SIZE];
97 struct bme680_dev bme680_;
98 bsec_library_return_t bsec_status_{BSEC_OK};
99 int8_t bme680_status_{BME680_OK};
100
101 int64_t last_time_ms_{0};
103 int64_t next_call_ns_{0};
104
105 std::queue<std::function<void()>> queue_;
106
108 uint8_t bsec_state_data_[BSEC_MAX_STATE_BLOB_SIZE]; // This is the current snapshot of the BSEC library state
110 uint32_t state_save_interval_ms_{21600000}; // 6 hours - 4 times a day
112 bsec_bme_settings_t bme680_settings_;
113
114 std::string device_id_;
118
119 SampleRate sample_rate_{SAMPLE_RATE_LP}; // Core/gas sample rate
123
133};
134#endif
135} // namespace bme680_bsec
136} // namespace esphome
ESPDEPRECATED("set_retry is deprecated and will be removed in 2026.8.0. Use set_timeout or set_interval instead.", "2026.2.0") void set_retry(const std uint32_t uint8_t std::function< RetryResult(uint8_t)> && f
Definition component.h:373
void set_device_id(const std::string &devid)
Definition bme680_bsec.h:39
void set_pressure_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:51
std::queue< std::function< void()> > queue_
void set_state_save_interval(uint32_t interval)
Definition bme680_bsec.h:42
void set_co2_equivalent_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:57
void set_iaq_accuracy_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:56
void set_breath_voc_equivalent_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:58
static std::vector< BME680BSECComponent * > instances
Definition bme680_bsec.h:60
void set_humidity_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:52
void set_pressure_sample_rate(SampleRate sample_rate)
Definition bme680_bsec.h:47
void set_humidity_sample_rate(SampleRate sample_rate)
Definition bme680_bsec.h:48
static int8_t read_bytes_wrapper(uint8_t devid, uint8_t a_register, uint8_t *data, uint16_t len)
void set_supply_voltage(SupplyVoltage supply_voltage)
Definition bme680_bsec.h:43
static uint8_t work_buffer_[BSEC_MAX_WORKBUFFER_SIZE]
Definition bme680_bsec.h:96
void queue_push_(std::function< void()> &&f)
Definition bme680_bsec.h:94
float calc_sensor_sample_rate_(SampleRate sample_rate)
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 set_iaq_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:54
void publish_(const bsec_output_t *outputs, uint8_t num_outputs)
void publish_sensor_(sensor::Sensor *sensor, float value, bool change_only=false)
text_sensor::TextSensor * iaq_accuracy_text_sensor_
void set_sample_rate(SampleRate sample_rate)
Definition bme680_bsec.h:45
void set_temperature_sample_rate(SampleRate sample_rate)
Definition bme680_bsec.h:46
void set_gas_resistance_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:53
void set_iaq_accuracy_text_sensor(text_sensor::TextSensor *sensor)
Definition bme680_bsec.h:55
void set_temperature_sensor(sensor::Sensor *sensor)
Definition bme680_bsec.h:50
static void delay_ms(uint32_t period)
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:133
uint8_t size_t len
Definition i2c.h:273
Base-class for all sensors.
Definition sensor.h:43
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7