12static constexpr uint16_t STCC4_CMD_START_CONTINUOUS_MEASUREMENT = 0x218b;
13static constexpr uint16_t STCC4_CMD_STOP_CONTINUOUS_MEASUREMENT = 0x3f86;
14static constexpr uint16_t STCC4_CMD_MEASURE_SINGLE_SHOT = 0x219d;
15static constexpr uint16_t STCC4_CMD_READ_MEASUREMENT = 0xec05;
16static constexpr uint16_t STCC4_CMD_GET_PRODUCT_ID = 0x365b;
17static constexpr uint16_t STCC4_CMD_SET_RHT_COMPENSATION = 0xe000;
18static constexpr uint16_t STCC4_CMD_SET_PRESSURE_COMPENSATION = 0xe016;
21static constexpr uint8_t STCC4_CMD_EXIT_SLEEP_MODE = 0x00;
23static constexpr uint32_t STCC4_PRODUCT_ID = 0x0901018a;
28static constexpr uint32_t READY_TIMEOUT_MS = 1200;
31static constexpr uint32_t READY_POLL_INTERVAL_MS = 100;
35 return uint16_t((std::clamp(temperature_in_c, -45.f, 130.f) + 45.f) * 65535.f / 175.f);
39 return temperature_in_ticks * 175.f / 65535.f - 45.f;
43 return uint16_t((std::clamp(humidity_in_percent, 0.f, 100.f) + 6.f) * 65535.f / 125.f);
47 return humidity_in_ticks * 125.f / 65535.f - 6.f;
51 return uint16_t(std::clamp(pressure_in_hpa, 400.f, 1100.f) * 50.f);
70 if (this->
write_command(STCC4_CMD_STOP_CONTINUOUS_MEASUREMENT)) {
73 uint16_t raw_product_id[6];
74 if (this->
get_register(STCC4_CMD_GET_PRODUCT_ID, raw_product_id, 6, 1)) {
75 uint32_t product_id = (
uint32_t(raw_product_id[0]) << 16) | raw_product_id[1];
76 uint64_t
serial_number = (uint64_t(raw_product_id[2]) << 48) | (uint64_t(raw_product_id[3]) << 32) |
77 (uint64_t(raw_product_id[4]) << 16) | raw_product_id[5];
78 ESP_LOGD(TAG,
"Product ID: 0x%08" PRIX32
", Serial: 0x%016" PRIX64, product_id,
serial_number);
79 if (!this->
setup_check(product_id == STCC4_PRODUCT_ID, LOG_STR(
"Unsupported product ID")))
89 if (this->temperature_source_sensor_ !=
nullptr && this->humidity_source_sensor_ !=
nullptr) {
92 if (this->ambient_pressure_source_sensor_ !=
nullptr) {
104 LOG_STR(
"Failed to start continuous measurement")))
112 if (
millis() - start_time < READY_TIMEOUT_MS) {
113 ESP_LOGVV(TAG,
"Retry sync");
119 this->
mark_failed(LOG_STR(
"Failed to stop continuous measurements and read product ID"));
125 if (this->temperature_source_sensor_ !=
nullptr && this->humidity_source_sensor_ !=
nullptr) {
126 this->temperature_source_sensor_->add_on_state_callback(
128 this->humidity_source_sensor_->add_on_state_callback(
131 if (this->ambient_pressure_source_sensor_ !=
nullptr) {
132 this->ambient_pressure_source_sensor_->add_on_state_callback(
137void STCC4Component::dump_config() {
138 ESP_LOGCONFIG(TAG,
"STCC4:");
139 LOG_I2C_DEVICE(
this);
140 ESP_LOGCONFIG(TAG,
" Measurement mode: %s",
142 : LOG_STR_LITERAL(
"Single shot"));
143 if (this->ambient_pressure_source_sensor_ !=
nullptr) {
144 ESP_LOGCONFIG(TAG,
" Dynamic ambient pressure compensation using '%s'",
145 LOG_STR_ARG(this->ambient_pressure_source_sensor_->get_log_name()));
147 ESP_LOGCONFIG(TAG,
" Ambient pressure compensation: %f hPa",
150 if (this->temperature_source_sensor_ !=
nullptr) {
151 ESP_LOGCONFIG(TAG,
" Temperature compensation using '%s'",
152 LOG_STR_ARG(this->temperature_source_sensor_->get_log_name()));
154 if (this->humidity_source_sensor_ !=
nullptr) {
155 ESP_LOGCONFIG(TAG,
" Humidity compensation using '%s'",
156 LOG_STR_ARG(this->humidity_source_sensor_->get_log_name()));
158 LOG_UPDATE_INTERVAL(
this);
159 LOG_SENSOR(
" ",
"CO2", this->co2_sensor_);
160 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
161 LOG_SENSOR(
" ",
"Humidity", this->humidity_sensor_);
164void STCC4Component::update() {
170 ESP_LOGW(TAG,
"Failed to start single shot measurement");
178 ESP_LOGW(TAG,
"Failed to read measurement data");
188 this->
set_timeout(retry_for_clock_drift ? 150 : 1000, [
this, retry_for_clock_drift]() {
189 if (this->
read_measurement_(retry_for_clock_drift ? 0 : sensirion_common::SENSIRION_OPTION_READ_MAY_NACK)) {
192 }
else if (!retry_for_clock_drift) {
203 uint16_t raw_data[4];
209 const int16_t co2_raw = int16_t(raw_data[0]);
210 if (this->co2_sensor_ !=
nullptr && co2_raw >= 0) {
211 this->co2_sensor_->publish_state(co2_raw);
221 const float temperature_in_c = this->temperature_source_sensor_->state;
222 const float humidity_in_percent = this->humidity_source_sensor_->state;
223 if (std::isnan(temperature_in_c) || std::isnan(humidity_in_percent))
235void STCC4Component::set_ambient_pressure_compensation(
float pressure_in_hpa) {
240 const float pressure_in_hpa = this->ambient_pressure_source_sensor_->state;
241 if (std::isnan(pressure_in_hpa))
244 if (pressure_in_hpa < 100.f || pressure_in_hpa > 10000.f) {
249 ESP_LOGW(TAG,
"Ambient pressure compensation sensor might have incompatible units: got %f hPa", pressure_in_hpa);
266 const uint16_t data[2] = {temperature_in_ticks, humidity_in_ticks};
267 if (!this->
write_command(STCC4_CMD_SET_RHT_COMPENSATION, data, 2)) {
268 ESP_LOGE(TAG,
"Failed to set RHT compensation");
276 if (!this->
write_command(STCC4_CMD_SET_PRESSURE_COMPENSATION, pressure_in_pa_2)) {
277 ESP_LOGE(TAG,
"Failed to set ambient pressure compensation");
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
void status_set_warning()
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
void status_clear_warning()
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay, uint8_t sensirion_options=0)
get data words from I2C register.
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 write_rht_compensation_(uint16_t temperature_in_ticks, uint16_t humidity_in_ticks)
uint16_t ambient_pressure_in_pa_2_
void schedule_continuous_update_(bool retry_for_clock_drift)
bool write_ambient_pressure_compensation_(uint16_t pressure_in_pa_2)
void update_rht_compensation_from_source_()
void poll_until_ready_for_setup_or_timeout_(uint32_t start_time)
bool read_measurement_(uint8_t sensirion_options)
MeasurementMode measurement_mode_
bool ambient_pressure_unit_warning_logged_
uint16_t humidity_in_ticks_
uint16_t temperature_in_ticks_
void update_ambient_pressure_compensation_from_source_()
constexpr float temperature_in_ticks_to_c(uint16_t temperature_in_ticks)
constexpr uint16_t humidity_in_percent_to_ticks(float humidity_in_percent)
constexpr uint16_t pressure_in_hpa_to_pa_2(float pressure_in_hpa)
constexpr float humidity_in_ticks_to_percent(uint16_t humidity_in_ticks)
ESPHOME_LOG_TAG(TAG, "stcc4")
constexpr float pressure_in_pa_2_to_hpa(uint16_t pressure_in_pa_2)
constexpr uint16_t temperature_in_c_to_ticks(float temperature_in_c)
uint32_t IRAM_ATTR HOT millis()