9 static const char *
const TAG =
"sgp4x";
12 ESP_LOGCONFIG(TAG,
"Setting up SGP4x...");
15 uint16_t raw_serial_number[3];
16 if (!this->
get_register(SGP4X_CMD_GET_SERIAL_ID, raw_serial_number, 3, 1)) {
17 ESP_LOGE(TAG,
"Failed to read serial number");
18 this->error_code_ = SERIAL_NUMBER_IDENTIFICATION_FAILED;
22 this->
serial_number_ = (uint64_t(raw_serial_number[0]) << 24) | (uint64_t(raw_serial_number[1]) << 16) |
23 (uint64_t(raw_serial_number[2]));
27 uint16_t raw_featureset;
28 if (!this->
get_register(SGP4X_CMD_GET_FEATURESET, raw_featureset, 1)) {
29 ESP_LOGD(TAG,
"raw_featureset write_command_ failed");
34 if ((this->
featureset_ & 0x1FF) == SGP40_FEATURESET) {
39 ESP_LOGE(TAG,
"Measuring NOx requires a SGP41 sensor but a SGP40 sensor is detected");
49 if ((this->
featureset_ & 0x1FF) == SGP41_FEATURESET) {
54 ESP_LOGD(TAG,
"Product feature set failed 0x%0X , expecting 0x%0X", uint16_t(this->
featureset_ & 0x1FF),
61 ESP_LOGD(TAG,
"Product version: 0x%0X", uint16_t(this->
featureset_ & 0x1FF));
69 if (this->pref_.load(&this->voc_baselines_storage_)) {
80 ESP_LOGI(TAG,
"Setting VOC baseline from save state0: 0x%04X, state1: 0x%04X",
112 ESP_LOGD(TAG,
"Component requires sampling of 1Hz, setting up background sampler");
117 ESP_LOGD(TAG,
"Self-test started");
119 this->error_code_ = COMMUNICATION_FAILED;
120 ESP_LOGD(TAG,
"Self-test communication failed");
127 this->error_code_ = SELF_TEST_FAILED;
128 ESP_LOGD(TAG,
"Self-test read_data_ failed");
133 if (reply == 0xD400) {
135 ESP_LOGD(TAG,
"Self-test completed");
138 this->error_code_ = SELF_TEST_FAILED;
139 ESP_LOGD(TAG,
"Self-test failed 0x%X", reply);
143 ESP_LOGD(TAG,
"Self-test failed 0x%X", reply);
168 ESP_LOGV(TAG,
"VOC = %d, NOx = %d", voc, nox);
179 if (this->
pref_.
save(&this->voc_baselines_storage_)) {
183 ESP_LOGW(TAG,
"Could not store VOC baselines");
198 float humidity = NAN;
199 static uint32_t nox_conditioning_start =
millis();
202 ESP_LOGD(TAG,
"Self-test not yet complete");
208 if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
216 if (std::isnan(temperature) || temperature < -40.0f || temperature > 85.0f) {
222 size_t response_words;
225 command = SGP40_CMD_MEASURE_RAW;
229 if (
millis() - nox_conditioning_start < 10000) {
230 command = SGP41_CMD_NOX_CONDITIONING;
233 command = SGP41_CMD_MEASURE_RAW;
237 uint16_t rhticks = llround((uint16_t)((humidity * 65535) / 100));
238 uint16_t tempticks = (uint16_t)(((temperature + 45) * 65535) / 175);
246 ESP_LOGD(TAG,
"write error (%d)", this->
last_error_);
250 uint16_t raw_data[2];
252 if (!this->
read_data(raw_data, response_words)) {
254 ESP_LOGD(TAG,
"read error (%d)", this->
last_error_);
257 voc_raw = raw_data[0];
258 nox_raw = raw_data[1];
270 ESP_LOGE(TAG,
"measure gas indices failed");
275 ESP_LOGD(TAG,
"Sensor has not collected enough samples yet. (%d/%d) VOC index is: %u", this->
samples_read_,
276 this->samples_to_stabilize_, this->
voc_index_);
304 ESP_LOGCONFIG(TAG,
"SGP4x:");
305 LOG_I2C_DEVICE(
this);
309 switch (this->error_code_) {
310 case COMMUNICATION_FAILED:
311 ESP_LOGW(TAG,
"Communication failed! Is the sensor connected?");
313 case SERIAL_NUMBER_IDENTIFICATION_FAILED:
314 ESP_LOGW(TAG,
"Get Serial number failed.");
316 case SELF_TEST_FAILED:
317 ESP_LOGW(TAG,
"Self test failed.");
321 ESP_LOGW(TAG,
"Unknown setup error!");
325 ESP_LOGCONFIG(TAG,
" Type: %s",
sgp_type_ ==
SGP41 ?
"SGP41" :
"SPG40");
326 ESP_LOGCONFIG(TAG,
" Serial number: %" PRIu64, this->
serial_number_);
327 ESP_LOGCONFIG(TAG,
" Minimum Samples: %f", GasIndexAlgorithm_INITIAL_BLACKOUT);
329 LOG_UPDATE_INTERVAL(
this);
332 ESP_LOGCONFIG(TAG,
" Compensation:");
336 ESP_LOGCONFIG(TAG,
" Compensation: No source configured");
void set_disabled_by_default(bool disabled_by_default)
sensor::Sensor * voc_sensor_
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
void set_internal(bool internal)
const float MAXIMUM_STORAGE_DIFF
bool write_command(T i2c_register)
Write a command to the i2c device.
void dump_config() override
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
VOCGasIndexAlgorithm voc_algorithm_
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
optional< GasTuning > nox_tuning_params_
ESPPreferenceObject pref_
bool read_data(uint16_t *data, uint8_t len)
Read data words from i2c device.
sensor::Sensor * nox_sensor_
sensor::Sensor * humidity_sensor_
Input sensor for humidity and temperature compensation.
uint32_t IRAM_ATTR HOT millis()
optional< GasTuning > voc_tuning_params_
float state
This member variable stores the last state that has passed through all filters.
bool measure_gas_indices_(int32_t &voc, int32_t &nox)
Combined the measured gasses, temperature, and humidity to calculate the VOC Index.
void update_gas_indices()
ESPPreferences * global_preferences
void status_clear_warning()
uint32_t seconds_since_last_store_
void publish_state(float state)
Publish a new state to the front-end.
SGP4xBaselines voc_baselines_storage_
Application App
Global storage of Application pointer - only one Application can exist.
bool measure_raw_(uint16_t &voc_raw, uint16_t &nox_raw)
Return the raw gas measurement.
uint8_t samples_to_stabilize_
NOxGasIndexAlgorithm nox_algorithm_
void status_set_warning()
const std::string & get_compilation_time() const
bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from i2c register.
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
virtual void mark_failed()
Mark this component as failed.
i2c::ErrorCode last_error_
last error code from i2c operation
sensor::Sensor * temperature_sensor_
void IRAM_ATTR HOT delay(uint32_t ms)