12static const char *
const TAG =
"sgp30";
14static const uint16_t SGP30_CMD_GET_SERIAL_ID = 0x3682;
15static const uint16_t SGP30_CMD_GET_FEATURESET = 0x202f;
16static const uint16_t SGP30_CMD_IAQ_INIT = 0x2003;
17static const uint16_t SGP30_CMD_MEASURE_IAQ = 0x2008;
18static const uint16_t SGP30_CMD_SET_ABSOLUTE_HUMIDITY = 0x2061;
19static const uint16_t SGP30_CMD_GET_IAQ_BASELINE = 0x2015;
20static const uint16_t SGP30_CMD_SET_IAQ_BASELINE = 0x201E;
39 uint16_t raw_serial_number[3];
40 if (!this->
get_register(SGP30_CMD_GET_SERIAL_ID, raw_serial_number, 3)) {
44 this->
serial_number_ = (
static_cast<uint64_t
>(raw_serial_number[0]) << 32) |
45 (
static_cast<uint64_t
>(raw_serial_number[1]) << 16) |
46 static_cast<uint64_t
>(raw_serial_number[2]);
50 uint16_t raw_featureset;
51 if (!this->
get_register(SGP30_CMD_GET_FEATURESET, raw_featureset)) {
67 ESP_LOGV(TAG,
"Product version: 0x%0X", uint16_t(this->
featureset_ & 0x1FF));
71 ESP_LOGE(TAG,
"sgp30_iaq_init failed");
115 ESP_LOGD(TAG,
"Error getting baseline");
120 uint16_t raw_data[2];
126 uint16_t eco2baseline = (raw_data[0]);
127 uint16_t tvocbaseline = (raw_data[1]);
129 ESP_LOGI(TAG,
"Baselines: eCO2: 0x%04X, TVOC: 0x%04X", eco2baseline, tvocbaseline);
147 if (this->
pref_.
save(&this->baselines_storage_)) {
149 this->baselines_storage_.tvoc);
151 ESP_LOGW(TAG,
"Could not store eCO2 and TVOC baselines");
158 ESP_LOGD(TAG,
"Baseline reading not available for: %.0fs",
166 float humidity = NAN;
169 if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
170 ESP_LOGW(TAG,
"Compensation not possible yet: bad humidity data");
173 ESP_LOGD(TAG,
"External compensation data received: Humidity %0.2f%%", humidity);
180 ESP_LOGW(TAG,
"Compensation not possible yet: bad temperature value");
183 ESP_LOGD(TAG,
"External compensation data received: Temperature %0.2f°C",
temperature);
186 float absolute_humidity;
199 SGP30_CMD_SET_ABSOLUTE_HUMIDITY & 0xFF,
200 uint8_t(std::floor(absolute_humidity)),
201 uint8_t(std::floor((absolute_humidity - std::floor(absolute_humidity)) * 256)),
204 data[3] =
crc8(&data[1], 2, 0xFF, sensirion_common::CRC_POLYNOMIAL,
true);
205 ESP_LOGD(TAG,
"Calculated absolute humidity: %0.3f g/m³ (0x%04X)", absolute_humidity,
207 if (!this->
write_bytes(SGP30_CMD_SET_ABSOLUTE_HUMIDITY >> 8, data, 4)) {
208 ESP_LOGE(TAG,
"Error sending compensation data");
214 data[0] = SGP30_CMD_SET_IAQ_BASELINE & 0xFF;
215 data[1] = tvoc_baseline >> 8;
216 data[2] = tvoc_baseline & 0xFF;
217 data[3] =
crc8(&data[1], 2, 0xFF, sensirion_common::CRC_POLYNOMIAL,
true);
218 data[4] = eco2_baseline >> 8;
219 data[5] = eco2_baseline & 0xFF;
220 data[6] =
crc8(&data[4], 2, 0xFF, sensirion_common::CRC_POLYNOMIAL,
true);
221 if (!this->
write_bytes(SGP30_CMD_SET_IAQ_BASELINE >> 8, data, 7)) {
222 ESP_LOGE(TAG,
"Error applying baselines: eCO2: 0x%04X, TVOC: 0x%04X", eco2_baseline, tvoc_baseline);
224 ESP_LOGI(TAG,
"Initial baselines applied: eCO2: 0x%04X, TVOC: 0x%04X", eco2_baseline, tvoc_baseline);
229 ESP_LOGCONFIG(TAG,
"SGP30:");
230 LOG_I2C_DEVICE(
this);
232 switch (this->error_code_) {
234 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
237 ESP_LOGW(TAG,
"Measurement Initialization failed");
240 ESP_LOGW(TAG,
"Invalid ID");
243 ESP_LOGW(TAG,
"Unsupported ID");
246 ESP_LOGW(TAG,
"Unknown setup error");
250 ESP_LOGCONFIG(TAG,
" Serial number: %" PRIu64, this->
serial_number_);
258 ESP_LOGCONFIG(TAG,
" Baselines not configured");
262 LOG_UPDATE_INTERVAL(
this);
267 ESP_LOGCONFIG(TAG,
"Store baseline: %s", YESNO(this->
store_baseline_));
269 ESP_LOGCONFIG(TAG,
" Compensation:");
273 ESP_LOGCONFIG(TAG,
" Compensation: No source configured");
284 uint16_t raw_data[2];
290 float eco2 = (raw_data[0]);
291 float tvoc = (raw_data[1]);
293 ESP_LOGV(TAG,
"eCO2=%.1fppm TVOC=%.1fppb", eco2, tvoc);
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()
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void status_clear_warning()
uint32_t update_interval_
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
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.
sensor::Sensor * eco2_sensor_baseline_
bool is_sensor_baseline_reliable_()
sensor::Sensor * temperature_sensor_
SGP30Baselines baselines_storage_
sensor::Sensor * tvoc_sensor_
sensor::Sensor * tvoc_sensor_baseline_
sensor::Sensor * humidity_sensor_
Input sensor for humidity and temperature compensation.
uint32_t seconds_since_last_store_
ESPPreferenceObject pref_
void write_iaq_baseline_(uint16_t eco2_baseline, uint16_t tvoc_baseline)
void dump_config() override
void read_iaq_baseline_()
sensor::Sensor * eco2_sensor_
@ MEASUREMENT_INIT_FAILED
uint32_t required_warm_up_time_
const uint32_t IAQ_BASELINE_WARM_UP_SECONDS_WITHOUT_BASELINE
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
const uint32_t MAXIMUM_STORAGE_DIFF
const uint32_t IAQ_BASELINE_WARM_UP_SECONDS_WITH_BASELINE_PROVIDED
Providing packet encoding functions for exchanging data with a remote host.
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
ESPPreferences * global_preferences
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
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)