12 static const char *
const TAG =
"scd30";
14 static const uint16_t SCD30_CMD_GET_FIRMWARE_VERSION = 0xd100;
15 static const uint16_t SCD30_CMD_START_CONTINUOUS_MEASUREMENTS = 0x0010;
16 static const uint16_t SCD30_CMD_ALTITUDE_COMPENSATION = 0x5102;
17 static const uint16_t SCD30_CMD_AUTOMATIC_SELF_CALIBRATION = 0x5306;
18 static const uint16_t SCD30_CMD_GET_DATA_READY_STATUS = 0x0202;
19 static const uint16_t SCD30_CMD_READ_MEASUREMENT = 0x0300;
22 static const uint16_t SCD30_CMD_STOP_MEASUREMENTS = 0x0104;
23 static const uint16_t SCD30_CMD_MEASUREMENT_INTERVAL = 0x4600;
24 static const uint16_t SCD30_CMD_FORCED_CALIBRATION = 0x5204;
25 static const uint16_t SCD30_CMD_TEMPERATURE_OFFSET = 0x5403;
26 static const uint16_t SCD30_CMD_SOFT_RESET = 0xD304;
29 ESP_LOGCONFIG(TAG,
"Setting up scd30...");
32 Wire.setClockStretchLimit(150000);
36 uint16_t raw_firmware_version[3];
37 if (!this->
get_register(SCD30_CMD_GET_FIRMWARE_VERSION, raw_firmware_version, 3)) {
42 ESP_LOGD(TAG,
"SCD30 Firmware v%0d.%02d", (uint16_t(raw_firmware_version[0]) >> 8),
43 uint16_t(raw_firmware_version[0] & 0xFF));
52 if (!this->
write_command(SCD30_CMD_TEMPERATURE_OFFSET, temp_offset)) {
53 ESP_LOGE(TAG,
"Sensor SCD30 error setting temperature offset.");
72 ESP_LOGE(TAG,
"Sensor SCD30 error setting update interval.");
84 ESP_LOGE(TAG,
"Sensor SCD30 error setting altitude compensation.");
95 ESP_LOGE(TAG,
"Sensor SCD30 error setting automatic self calibration.");
106 ESP_LOGE(TAG,
"Sensor SCD30 error starting continuous measurements.");
120 ESP_LOGCONFIG(TAG,
"scd30:");
121 LOG_I2C_DEVICE(
this);
123 switch (this->error_code_) {
125 ESP_LOGW(TAG,
"Communication failed! Is the sensor connected?");
128 ESP_LOGW(TAG,
"Measurement Initialization failed!");
131 ESP_LOGW(TAG,
"Unable to read sensor firmware version");
134 ESP_LOGW(TAG,
"Unknown setup error!");
139 ESP_LOGCONFIG(TAG,
" Altitude compensation: OFF");
143 ESP_LOGCONFIG(TAG,
" Automatic self calibration: %s", ONOFF(this->
enable_asc_));
153 uint16_t raw_read_status;
154 if (!this->
read_data(raw_read_status) || raw_read_status == 0x00) {
156 ESP_LOGW(TAG,
"Data not ready yet!");
161 ESP_LOGW(TAG,
"Error reading measurement!");
167 uint16_t raw_data[6];
173 union uint32_float_t {
177 uint32_t temp_c_o2_u32 = (((uint32_t(raw_data[0])) << 16) | (uint32_t(raw_data[1])));
178 uint32_float_t co2{.uint32 = temp_c_o2_u32};
180 uint32_t temp_temp_u32 = (((uint32_t(raw_data[2])) << 16) | (uint32_t(raw_data[3])));
181 uint32_float_t
temperature{.uint32 = temp_temp_u32};
183 uint32_t temp_hum_u32 = (((uint32_t(raw_data[4])) << 16) | (uint32_t(raw_data[5])));
184 uint32_float_t humidity{.uint32 = temp_hum_u32};
186 ESP_LOGD(TAG,
"Got CO2=%.2fppm temperature=%.2f°C humidity=%.2f%%", co2.value,
temperature.value, humidity.value);
203 uint16_t is_data_ready;
204 if (!this->
read_data(&is_data_ready, 1)) {
207 return is_data_ready == 1;
211 ESP_LOGD(TAG,
"Performing CO2 force recalibration with reference %dppm.", co2_reference);
212 if (this->
write_command(SCD30_CMD_FORCED_CALIBRATION, co2_reference)) {
213 ESP_LOGD(TAG,
"Force recalibration complete.");
216 ESP_LOGE(TAG,
"Failed to force recalibration with reference.");
224 uint16_t forced_calibration_reference;
226 if (!this->
get_register(SCD30_CMD_FORCED_CALIBRATION, forced_calibration_reference)) {
227 ESP_LOGE(TAG,
"Unable to read forced calibration reference.");
229 return forced_calibration_reference;
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")
bool write_command(T i2c_register)
Write a command to the i2c device.
float temperature_offset_
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool read_data(uint16_t *data, uint8_t len)
Read data words from i2c device.
bool force_recalibration_with_reference(uint16_t co2_reference)
sensor::Sensor * humidity_sensor_
uint16_t update_interval_
sensor::Sensor * co2_sensor_
sensor::Sensor * temperature_sensor_
void status_clear_warning()
uint16_t get_forced_calibration_reference()
void publish_state(float state)
Publish a new state to the front-end.
uint16_t altitude_compensation_
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.
Implementation of SPI Controller mode.
uint16_t ambient_pressure_compensation_
void dump_config() override
void IRAM_ATTR HOT delay(uint32_t ms)