8 static const char *
const TAG =
"scd4x";
10 static const uint16_t SCD4X_CMD_GET_SERIAL_NUMBER = 0x3682;
11 static const uint16_t SCD4X_CMD_TEMPERATURE_OFFSET = 0x241d;
12 static const uint16_t SCD4X_CMD_ALTITUDE_COMPENSATION = 0x2427;
13 static const uint16_t SCD4X_CMD_AMBIENT_PRESSURE_COMPENSATION = 0xe000;
14 static const uint16_t SCD4X_CMD_AUTOMATIC_SELF_CALIBRATION = 0x2416;
15 static const uint16_t SCD4X_CMD_START_CONTINUOUS_MEASUREMENTS = 0x21b1;
16 static const uint16_t SCD4X_CMD_START_LOW_POWER_CONTINUOUS_MEASUREMENTS = 0x21ac;
17 static const uint16_t SCD4X_CMD_START_LOW_POWER_SINGLE_SHOT = 0x219d;
18 static const uint16_t SCD4X_CMD_START_LOW_POWER_SINGLE_SHOT_RHT_ONLY = 0x2196;
19 static const uint16_t SCD4X_CMD_GET_DATA_READY_STATUS = 0xe4b8;
20 static const uint16_t SCD4X_CMD_READ_MEASUREMENT = 0xec05;
21 static const uint16_t SCD4X_CMD_PERFORM_FORCED_CALIBRATION = 0x362f;
22 static const uint16_t SCD4X_CMD_STOP_MEASUREMENTS = 0x3f86;
23 static const uint16_t SCD4X_CMD_FACTORY_RESET = 0x3632;
24 static const uint16_t SCD4X_CMD_GET_FEATURESET = 0x202f;
25 static const float SCD4X_TEMPERATURE_OFFSET_MULTIPLIER = (1 << 16) / 175.0f;
26 static const uint16_t SCD41_ID = 0x1408;
27 static const uint16_t SCD40_ID = 0x440;
30 ESP_LOGCONFIG(TAG,
"Setting up scd4x...");
35 ESP_LOGE(TAG,
"Failed to stop measurements");
42 uint16_t raw_serial_number[3];
43 if (!this->
get_register(SCD4X_CMD_GET_SERIAL_NUMBER, raw_serial_number, 3, 1)) {
44 ESP_LOGE(TAG,
"Failed to read serial number");
49 ESP_LOGD(TAG,
"Serial number %02d.%02d.%02d", (uint16_t(raw_serial_number[0]) >> 8),
50 uint16_t(raw_serial_number[0] & 0xFF), (uint16_t(raw_serial_number[1]) >> 8));
54 ESP_LOGE(TAG,
"Error setting temperature offset.");
64 ESP_LOGE(TAG,
"Error setting ambient pressure compensation.");
71 ESP_LOGE(TAG,
"Error setting altitude compensation.");
79 ESP_LOGE(TAG,
"Error setting automatic self calibration.");
88 ESP_LOGD(TAG,
"Sensor initialized");
94 ESP_LOGCONFIG(TAG,
"scd4x:");
99 ESP_LOGW(TAG,
"Communication failed! Is the sensor connected?");
102 ESP_LOGW(TAG,
"Measurement Initialization failed!");
105 ESP_LOGW(TAG,
"Unable to read sensor firmware version");
108 ESP_LOGW(TAG,
"Unknown setup error!");
112 ESP_LOGCONFIG(TAG,
" Automatic self calibration: %s", ONOFF(this->
enable_asc_));
114 ESP_LOGCONFIG(TAG,
" Dynamic ambient pressure compensation using sensor '%s'",
118 ESP_LOGCONFIG(TAG,
" Altitude compensation disabled");
119 ESP_LOGCONFIG(TAG,
" Ambient pressure compensation: %dmBar", this->
ambient_pressure_);
121 ESP_LOGCONFIG(TAG,
" Ambient pressure compensation disabled");
127 ESP_LOGCONFIG(TAG,
" Measurement mode: periodic (5s)");
130 ESP_LOGCONFIG(TAG,
" Measurement mode: low power periodic (30s)");
133 ESP_LOGCONFIG(TAG,
" Measurement mode: single shot");
136 ESP_LOGCONFIG(TAG,
" Measurement mode: single shot rht only");
140 LOG_UPDATE_INTERVAL(
this);
153 if (!std::isnan(pressure)) {
158 uint32_t wait_time = 0;
171 uint16_t raw_read_status;
173 if (!this->
read_data(raw_read_status) || raw_read_status == 0x00) {
175 ESP_LOGW(TAG,
"Data not ready yet!");
180 ESP_LOGW(TAG,
"Error reading measurement!");
185 uint16_t raw_data[3];
194 const float temperature = -45.0f + (175.0f * (raw_data[1])) / (1 << 16);
198 const float humidity = (100.0f * raw_data[2]) / (1 << 16);
212 ESP_LOGE(TAG,
"Failed to stop measurements");
215 this->
set_timeout(500, [
this, current_co2_concentration]() {
216 if (this->
write_command(SCD4X_CMD_PERFORM_FORCED_CALIBRATION, current_co2_concentration)) {
217 ESP_LOGD(TAG,
"setting forced calibration Co2 level %d ppm", current_co2_concentration);
225 ESP_LOGD(TAG,
"forced calibration complete");
229 ESP_LOGE(TAG,
"force calibration failed");
240 ESP_LOGE(TAG,
"Failed to stop measurements");
247 ESP_LOGE(TAG,
"Failed to send factory reset command");
251 ESP_LOGD(TAG,
"Factory reset complete");
259 uint16_t new_ambient_pressure = (uint16_t) pressure_in_hpa;
269 ESP_LOGD(TAG,
"ambient pressure compensation skipped - no change required");
274 if (this->
write_command(SCD4X_CMD_AMBIENT_PRESSURE_COMPENSATION, pressure_in_hpa)) {
275 ESP_LOGD(TAG,
"setting ambient pressure compensation to %d hPa", pressure_in_hpa);
278 ESP_LOGE(TAG,
"Error setting ambient pressure compensation.");
284 uint16_t measurement_command = SCD4X_CMD_START_CONTINUOUS_MEASUREMENTS;
287 measurement_command = SCD4X_CMD_START_CONTINUOUS_MEASUREMENTS;
290 measurement_command = SCD4X_CMD_START_LOW_POWER_CONTINUOUS_MEASUREMENTS;
293 measurement_command = SCD4X_CMD_START_LOW_POWER_SINGLE_SHOT;
296 measurement_command = SCD4X_CMD_START_LOW_POWER_SINGLE_SHOT_RHT_ONLY;
300 static uint8_t remaining_retries = 3;
301 while (remaining_retries) {
303 ESP_LOGE(TAG,
"Error starting measurements.");
306 if (--remaining_retries == 0)
sensor::Sensor * co2_sensor_
void set_ambient_pressure_compensation(float pressure_in_hpa)
sensor::Sensor * temperature_sensor_
void dump_config() override
void status_set_warning(const char *message="unspecified")
bool write_command(T i2c_register)
Write a command to the i2c device.
bool perform_forced_calibration(uint16_t current_co2_concentration)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
sensor::Sensor * ambient_pressure_source_
MeasurementMode measurement_mode_
bool read_data(uint16_t *data, uint8_t len)
Read data words from i2c device.
uint16_t ambient_pressure_
float temperature_offset_
bool ambient_pressure_compensation_
float state
This member variable stores the last state that has passed through all filters.
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
bool update_ambient_pressure_compensation_(uint16_t pressure_in_hpa)
constexpr const char * c_str() const
void status_clear_error()
bool start_measurement_()
bool get_register(uint16_t command, uint16_t *data, uint8_t len, uint8_t delay=0)
get data words from i2c register.
virtual void mark_failed()
Mark this component as failed.
sensor::Sensor * humidity_sensor_
Implementation of SPI Controller mode.
uint16_t altitude_compensation_
const StringRef & get_name() const
void IRAM_ATTR HOT delay(uint32_t ms)