10 static const char *
const TAG =
"sgp30";
12 static const uint16_t SGP30_CMD_GET_SERIAL_ID = 0x3682;
13 static const uint16_t SGP30_CMD_GET_FEATURESET = 0x202f;
14 static const uint16_t SGP30_CMD_IAQ_INIT = 0x2003;
15 static const uint16_t SGP30_CMD_MEASURE_IAQ = 0x2008;
16 static const uint16_t SGP30_CMD_SET_ABSOLUTE_HUMIDITY = 0x2061;
17 static const uint16_t SGP30_CMD_GET_IAQ_BASELINE = 0x2015;
18 static const uint16_t SGP30_CMD_SET_IAQ_BASELINE = 0x201E;
36 ESP_LOGCONFIG(TAG,
"Setting up SGP30...");
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_ = (uint64_t(raw_serial_number[0]) << 24) | (uint64_t(raw_serial_number[1]) << 16) |
45 (uint64_t(raw_serial_number[2]));
49 uint16_t raw_featureset;
50 if (!this->
get_register(SGP30_CMD_GET_FEATURESET, raw_featureset)) {
66 ESP_LOGD(TAG,
"Product version: 0x%0X", uint16_t(this->
featureset_ & 0x1FF));
70 ESP_LOGE(TAG,
"Sensor sgp30_iaq_init failed.");
82 if (this->pref_.load(&this->baselines_storage_)) {
114 ESP_LOGD(TAG,
"Error getting baseline");
119 uint16_t raw_data[2];
125 uint16_t eco2baseline = (raw_data[0]);
126 uint16_t tvocbaseline = (raw_data[1]);
128 ESP_LOGI(TAG,
"Current eCO2 baseline: 0x%04X, TVOC baseline: 0x%04X", eco2baseline, tvocbaseline);
146 if (this->
pref_.
save(&this->baselines_storage_)) {
148 this->baselines_storage_.tvoc);
150 ESP_LOGW(TAG,
"Could not store eCO2 and TVOC baselines");
157 ESP_LOGD(TAG,
"Baseline reading not available for: %.0fs",
165 float humidity = NAN;
168 if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
169 ESP_LOGW(TAG,
"Compensation not possible yet: bad humidity data.");
172 ESP_LOGD(TAG,
"External compensation data received: Humidity %0.2f%%", humidity);
178 if (std::isnan(temperature) || temperature < -40.0f || temperature > 85.0f) {
179 ESP_LOGW(TAG,
"Compensation not possible yet: bad temperature value data.");
182 ESP_LOGD(TAG,
"External compensation data received: Temperature %0.2f°C", temperature);
185 float absolute_humidity;
186 if (temperature < 0) {
189 ((humidity * 0.061121f * std::exp((23.036f - temperature / 333.7f) * (temperature / (279.82f + temperature)))) /
190 (273.15f + temperature));
194 ((humidity * 0.061121f * std::exp((18.678f - temperature / 234.5f) * (temperature / (257.14f + temperature)))) /
195 (273.15f + temperature));
197 uint8_t humidity_full = uint8_t(std::floor(absolute_humidity));
198 uint8_t humidity_dec = uint8_t(std::floor((absolute_humidity - std::floor(absolute_humidity)) * 256));
199 ESP_LOGD(TAG,
"Calculated Absolute humidity: %0.3f g/m³ (0x%04X)", absolute_humidity,
200 uint16_t(uint16_t(humidity_full) << 8 | uint16_t(humidity_dec)));
201 uint8_t
crc =
sht_crc_(humidity_full, humidity_dec);
203 data[0] = SGP30_CMD_SET_ABSOLUTE_HUMIDITY & 0xFF;
204 data[1] = humidity_full;
205 data[2] = humidity_dec;
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] =
sht_crc_(data[1], data[2]);
218 data[4] = eco2_baseline >> 8;
219 data[5] = eco2_baseline & 0xFF;
220 data[6] =
sht_crc_(data[4], data[5]);
221 if (!this->
write_bytes(SGP30_CMD_SET_IAQ_BASELINE >> 8, data, 7)) {
222 ESP_LOGE(TAG,
"Error applying eCO2 baseline: 0x%04X, TVOC baseline: 0x%04X", eco2_baseline, tvoc_baseline);
224 ESP_LOGI(TAG,
"Initial baselines applied successfully! eCO2 baseline: 0x%04X, TVOC baseline: 0x%04X", eco2_baseline,
230 ESP_LOGCONFIG(TAG,
"SGP30:");
231 LOG_I2C_DEVICE(
this);
233 switch (this->error_code_) {
235 ESP_LOGW(TAG,
"Communication failed! Is the sensor connected?");
238 ESP_LOGW(TAG,
"Measurement Initialization failed!");
241 ESP_LOGW(TAG,
"Sensor reported an invalid ID. Is this an SGP30?");
244 ESP_LOGW(TAG,
"Sensor reported an unsupported ID (SGPC3).");
247 ESP_LOGW(TAG,
"Unknown setup error!");
251 ESP_LOGCONFIG(TAG,
" Serial number: %" PRIu64, this->
serial_number_);
253 ESP_LOGCONFIG(TAG,
" Baseline:");
254 ESP_LOGCONFIG(TAG,
" eCO2 Baseline: 0x%04X", this->
eco2_baseline_);
255 ESP_LOGCONFIG(TAG,
" TVOC Baseline: 0x%04X", this->
tvoc_baseline_);
257 ESP_LOGCONFIG(TAG,
" Baseline: No baseline configured");
261 LOG_UPDATE_INTERVAL(
this);
266 ESP_LOGCONFIG(TAG,
"Store baseline: %s", YESNO(this->
store_baseline_));
268 ESP_LOGCONFIG(TAG,
" Compensation:");
272 ESP_LOGCONFIG(TAG,
" Compensation: No source configured");
283 uint16_t raw_data[2];
289 float eco2 = (raw_data[0]);
290 float tvoc = (raw_data[1]);
292 ESP_LOGD(TAG,
"Got eCO2=%.1fppm TVOC=%.1fppb", eco2, tvoc);
299 ESP_LOGW(TAG,
"Update interval for SGP30 sensor must be set to 1s for optimized readout");
uint32_t update_interval_
uint32_t seconds_since_last_store_
const char * to_string(SHTCXType type)
void status_set_warning(const char *message="unspecified")
bool write_command(T i2c_register)
Write a command to the i2c device.
sensor::Sensor * tvoc_sensor_
void write_iaq_baseline_(uint16_t eco2_baseline, uint16_t tvoc_baseline)
sensor::Sensor * eco2_sensor_baseline_
void read_iaq_baseline_()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
sensor::Sensor * eco2_sensor_
bool read_data(uint16_t *data, uint8_t len)
Read data words from i2c device.
uint8_t sht_crc_(uint16_t data)
8-bit CRC checksum that is transmitted after each data word for read and write operation ...
sensor::Sensor * tvoc_sensor_baseline_
bool is_sensor_baseline_reliable_()
uint32_t IRAM_ATTR HOT millis()
sensor::Sensor * humidity_sensor_
Input sensor for humidity and temperature compensation.
SGP30Baselines baselines_storage_
sensor::Sensor * temperature_sensor_
ESPPreferenceObject pref_
float state
This member variable stores the last state that has passed through all filters.
const uint32_t SHORTEST_BASELINE_STORE_INTERVAL
ESPPreferences * global_preferences
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
Application App
Global storage of Application pointer - only one Application can exist.
uint32_t required_warm_up_time_
const uint32_t IAQ_BASELINE_WARM_UP_SECONDS_WITHOUT_BASELINE
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
const uint32_t IAQ_BASELINE_WARM_UP_SECONDS_WITH_BASELINE_PROVIDED
const uint32_t MAXIMUM_STORAGE_DIFF
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.
Implementation of SPI Controller mode.
void dump_config() override
std::string get_compilation_time() const
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)