10static const char *
const TAG =
"sgp4x";
14 uint16_t raw_serial_number[3];
15 if (!this->
get_register(SGP4X_CMD_GET_SERIAL_ID, raw_serial_number, 3, 1)) {
16 ESP_LOGE(TAG,
"Get serial number failed");
17 this->error_code_ = SERIAL_NUMBER_IDENTIFICATION_FAILED;
21 this->
serial_number_ = (uint64_t(raw_serial_number[0]) << 32) | (uint64_t(raw_serial_number[1]) << 16) |
22 (uint64_t(raw_serial_number[2]));
27 if (!this->
get_register(SGP4X_CMD_GET_FEATURESET, featureset, 1)) {
28 ESP_LOGD(TAG,
"Get feature set failed");
33 if (featureset == SGP40_FEATURESET) {
37 ESP_LOGE(TAG,
"SGP41 required for NOx, disabling NOx sensor");
42 }
else if (featureset == SGP41_FEATURESET) {
46 ESP_LOGD(TAG,
"Unknown feature set 0x%0X", featureset);
51 ESP_LOGD(TAG,
"Version 0x%0X", featureset);
77 if (this->
pref_.
load(&this->voc_baselines_storage_)) {
82 this->voc_baselines_storage_.state1);
86 this->voc_baselines_storage_.state1);
107 ESP_LOGV(TAG,
"Component requires sampling of 1Hz, setting up background sampler");
112 ESP_LOGD(TAG,
"Starting self-test");
114 this->error_code_ = COMMUNICATION_FAILED;
115 ESP_LOGD(TAG, ESP_LOG_MSG_COMM_FAIL);
124 bool passed = this->
read_data(reply) && (this->
sgp_type_ ==
SGP41 ? (reply & 0x0003) == 0 : (reply >> 8) == 0xD4);
126 this->error_code_ = SELF_TEST_FAILED;
127 ESP_LOGW(TAG,
"Self-test failed (0x%X)", reply);
134 ESP_LOGD(TAG,
"Self-test complete");
153 if (this->
pref_.
save(&this->voc_baselines_storage_)) {
155 this->voc_baselines_storage_.state1);
157 ESP_LOGW(TAG,
"Storing VOC baselines failed");
164 ESP_LOGD(TAG,
"Stabilizing (%d/%d); VOC index: %" PRIu32, this->
samples_read_, this->samples_to_stabilize_,
170 float humidity = NAN;
173 ESP_LOGW(TAG,
"Self-test incomplete");
179 if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
193 size_t response_words;
195 command = SGP40_CMD_MEASURE_RAW;
199 command = SGP41_CMD_NOX_CONDITIONING;
203 command = SGP41_CMD_MEASURE_RAW;
206 if (command == SGP41_CMD_NOX_CONDITIONING) {
212 data[0] = (uint16_t) std::llround((humidity * 65535) / 100);
214 data[1] = (uint16_t) (((
temperature + 45) * 65535) / 175);
218 ESP_LOGD(TAG,
"write error (%d)", this->
last_error_);
224 uint16_t raw_data[2];
226 if (!this->
read_data(raw_data, response_words)) {
227 ESP_LOGD(TAG,
"read error (%d)", this->
last_error_);
263 ESP_LOGCONFIG(TAG,
"SGP4x:");
264 LOG_I2C_DEVICE(
this);
265 ESP_LOGCONFIG(TAG,
" Store baseline: %s", YESNO(this->
store_baseline_));
268 switch (this->error_code_) {
269 case COMMUNICATION_FAILED:
270 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
272 case SERIAL_NUMBER_IDENTIFICATION_FAILED:
273 ESP_LOGW(TAG,
"Get serial number failed");
275 case SELF_TEST_FAILED:
276 ESP_LOGW(TAG,
"Self-test failed");
279 ESP_LOGW(TAG,
"Unknown error");
285 " Serial number: %" PRIu64
"\n"
286 " Minimum Samples: %f",
288 GasIndexAlgorithm_INITIAL_BLACKOUT);
290 LOG_UPDATE_INTERVAL(
this);
292 ESP_LOGCONFIG(TAG,
" Compensation:");
297 ESP_LOGCONFIG(TAG,
" No source configured");
uint32_t get_config_version_hash()
Get the config hash extended with ESPHome version.
void mark_failed()
Mark this component as failed.
void status_set_warning()
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
void status_clear_warning()
i2c::ErrorCode last_error_
last error code from I2C operation
bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from I2C register.
bool write_command(T i2c_register)
Write a command to the I2C device.
bool read_data(uint16_t *data, uint8_t len)
Read data words from I2C device.
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.
SGP4xBaselines voc_baselines_storage_
optional< uint32_t > nox_conditioning_start_
uint32_t seconds_since_last_store_
ESPPreferenceObject pref_
uint8_t samples_to_stabilize_
void dump_config() override
sensor::Sensor * humidity_sensor_
Input sensor for humidity and temperature compensation.
sensor::Sensor * voc_sensor_
VOCGasIndexAlgorithm voc_algorithm_
sensor::Sensor * temperature_sensor_
optional< GasTuning > voc_tuning_params_
NOxGasIndexAlgorithm nox_algorithm_
void update_gas_indices_()
optional< GasTuning > nox_tuning_params_
sensor::Sensor * nox_sensor_
const float MAXIMUM_STORAGE_DIFF_STATE0
const float MAXIMUM_STORAGE_DIFF_STATE1
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
ESPPreferences * global_preferences
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
ESPPreferenceObject make_preference(size_t, uint32_t, bool)