9static const char *
const TAG =
"sgp4x";
12 ESP_LOGCONFIG(TAG,
"Running setup");
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,
"Get serial number failed");
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]));
28 if (!this->
get_register(SGP4X_CMD_GET_FEATURESET, featureset, 1)) {
29 ESP_LOGD(TAG,
"Get feature set failed");
34 if (featureset == SGP40_FEATURESET) {
39 ESP_LOGE(TAG,
"SGP41 required for NOx");
48 }
else if (featureset == SGP41_FEATURESET) {
53 ESP_LOGD(TAG,
"Unknown feature set 0x%0X", featureset);
58 ESP_LOGD(TAG,
"Version 0x%0X", featureset);
67 if (this->
pref_.
load(&this->voc_baselines_storage_)) {
70 ESP_LOGV(TAG,
"Loaded VOC baseline state0: 0x%04" PRIX32
", state1: 0x%04" PRIX32,
78 ESP_LOGV(TAG,
"Setting VOC baseline from save state0: 0x%04" PRIX32
", state1: 0x%04" PRIX32,
110 ESP_LOGV(TAG,
"Component requires sampling of 1Hz, setting up background sampler");
115 ESP_LOGD(TAG,
"Starting self-test");
117 this->error_code_ = COMMUNICATION_FAILED;
118 ESP_LOGD(TAG, ESP_LOG_MSG_COMM_FAIL);
124 if (!this->
read_data(reply) || (reply != 0xD400)) {
125 this->error_code_ = SELF_TEST_FAILED;
126 ESP_LOGW(TAG,
"Self-test failed (0x%X)", reply);
132 ESP_LOGD(TAG,
"Self-test complete");
151 if (this->
pref_.
save(&this->voc_baselines_storage_)) {
152 ESP_LOGV(TAG,
"Stored VOC baseline state0: 0x%04" PRIX32
", state1: 0x%04" PRIX32,
155 ESP_LOGW(TAG,
"Storing VOC baselines failed");
162 ESP_LOGD(TAG,
"Stabilizing (%d/%d); VOC index: %" PRIu32, this->
samples_read_, this->samples_to_stabilize_,
168 float humidity = NAN;
169 static uint32_t nox_conditioning_start =
millis();
172 ESP_LOGW(TAG,
"Self-test incomplete");
178 if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
192 size_t response_words;
195 command = SGP40_CMD_MEASURE_RAW;
199 if (
millis() - nox_conditioning_start < 10000) {
200 command = SGP41_CMD_NOX_CONDITIONING;
203 command = SGP41_CMD_MEASURE_RAW;
207 uint16_t rhticks = llround((uint16_t) ((humidity * 65535) / 100));
208 uint16_t tempticks = (uint16_t) (((
temperature + 45) * 65535) / 175);
215 ESP_LOGD(TAG,
"write error (%d)", this->
last_error_);
221 uint16_t raw_data[2];
223 if (!this->
read_data(raw_data, response_words)) {
224 ESP_LOGD(TAG,
"read error (%d)", this->
last_error_);
258 ESP_LOGCONFIG(TAG,
"SGP4x:");
259 LOG_I2C_DEVICE(
this);
260 ESP_LOGCONFIG(TAG,
" Store baseline: %s", YESNO(this->
store_baseline_));
263 switch (this->error_code_) {
264 case COMMUNICATION_FAILED:
265 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
267 case SERIAL_NUMBER_IDENTIFICATION_FAILED:
268 ESP_LOGW(TAG,
"Get serial number failed");
270 case SELF_TEST_FAILED:
271 ESP_LOGW(TAG,
"Self-test failed");
274 ESP_LOGW(TAG,
"Unknown error");
280 " Serial number: %" PRIu64
"\n"
281 " Minimum Samples: %f",
284 LOG_UPDATE_INTERVAL(
this);
286 ESP_LOGCONFIG(TAG,
" Compensation:");
291 ESP_LOGCONFIG(TAG,
" No source configured");
std::string get_compilation_time() const
virtual void mark_failed()
Mark this component as failed.
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
void set_disabled_by_default(bool disabled_by_default)
void set_internal(bool internal)
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_
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 uint32_t SHORTEST_BASELINE_STORE_INTERVAL
const float MAXIMUM_STORAGE_DIFF
Providing packet encoding functions for exchanging data with a remote host.
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
ESPPreferences * global_preferences
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.