ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
scd30.cpp
Go to the documentation of this file.
1#include "scd30.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5#ifdef USE_ESP8266
6#include <Wire.h>
7#endif
8
9namespace esphome {
10namespace scd30 {
11
12static const char *const TAG = "scd30";
13
14static const uint16_t SCD30_CMD_GET_FIRMWARE_VERSION = 0xd100;
15static const uint16_t SCD30_CMD_START_CONTINUOUS_MEASUREMENTS = 0x0010;
16static const uint16_t SCD30_CMD_ALTITUDE_COMPENSATION = 0x5102;
17static const uint16_t SCD30_CMD_AUTOMATIC_SELF_CALIBRATION = 0x5306;
18static const uint16_t SCD30_CMD_GET_DATA_READY_STATUS = 0x0202;
19static const uint16_t SCD30_CMD_READ_MEASUREMENT = 0x0300;
20
22static const uint16_t SCD30_CMD_STOP_MEASUREMENTS = 0x0104;
23static const uint16_t SCD30_CMD_MEASUREMENT_INTERVAL = 0x4600;
24static const uint16_t SCD30_CMD_FORCED_CALIBRATION = 0x5204;
25static const uint16_t SCD30_CMD_TEMPERATURE_OFFSET = 0x5403;
26static const uint16_t SCD30_CMD_SOFT_RESET = 0xD304;
27
29#ifdef USE_ESP8266
30 Wire.setClockStretchLimit(150000);
31#endif
32
34 uint16_t raw_firmware_version[3];
35 if (!this->get_register(SCD30_CMD_GET_FIRMWARE_VERSION, raw_firmware_version, 3)) {
36 this->error_code_ = FIRMWARE_IDENTIFICATION_FAILED;
37 this->mark_failed();
38 return;
39 }
40 ESP_LOGD(TAG, "SCD30 Firmware v%0d.%02d", (uint16_t(raw_firmware_version[0]) >> 8),
41 uint16_t(raw_firmware_version[0] & 0xFF));
42
43 uint16_t temp_offset;
44 if (this->temperature_offset_ > 0) {
45 temp_offset = (this->temperature_offset_ * 100);
46 } else {
47 temp_offset = 0;
48 }
49
50 if (!this->write_command(SCD30_CMD_TEMPERATURE_OFFSET, temp_offset)) {
51 ESP_LOGE(TAG, "Sensor SCD30 error setting temperature offset.");
52 this->error_code_ = MEASUREMENT_INIT_FAILED;
53 this->mark_failed();
54 return;
55 }
56#ifdef USE_ESP32
57 // According ESP32 clock stretching is typically 30ms and up to 150ms "due to
58 // internal calibration processes". The I2C peripheral only supports 13ms (at
59 // least when running at 80MHz).
60 // In practice it seems that clock stretching occurs during this calibration
61 // calls. It also seems that delays in between calls makes them
62 // disappear/shorter. Hence work around with delays for ESP32.
63 //
64 // By experimentation a delay of 20ms as already sufficient. Let's go
65 // safe and use 30ms delays.
66 delay(30);
67#endif
68
69 if (!this->write_command(SCD30_CMD_MEASUREMENT_INTERVAL, update_interval_)) {
70 ESP_LOGE(TAG, "Sensor SCD30 error setting update interval.");
71 this->error_code_ = MEASUREMENT_INIT_FAILED;
72 this->mark_failed();
73 return;
74 }
75#ifdef USE_ESP32
76 delay(30);
77#endif
78
79 // The start measurement command disables the altitude compensation, if any, so we only set it if it's turned on
80 if (this->altitude_compensation_ != 0xFFFF) {
81 if (!this->write_command(SCD30_CMD_ALTITUDE_COMPENSATION, altitude_compensation_)) {
82 ESP_LOGE(TAG, "Sensor SCD30 error setting altitude compensation.");
83 this->error_code_ = MEASUREMENT_INIT_FAILED;
84 this->mark_failed();
85 return;
86 }
87 }
88#ifdef USE_ESP32
89 delay(30);
90#endif
91
92 if (!this->write_command(SCD30_CMD_AUTOMATIC_SELF_CALIBRATION, enable_asc_ ? 1 : 0)) {
93 ESP_LOGE(TAG, "Sensor SCD30 error setting automatic self calibration.");
94 this->error_code_ = MEASUREMENT_INIT_FAILED;
95 this->mark_failed();
96 return;
97 }
98#ifdef USE_ESP32
99 delay(30);
100#endif
101
103 if (!this->write_command(SCD30_CMD_START_CONTINUOUS_MEASUREMENTS, this->ambient_pressure_compensation_)) {
104 ESP_LOGE(TAG, "Sensor SCD30 error starting continuous measurements.");
105 this->error_code_ = MEASUREMENT_INIT_FAILED;
106 this->mark_failed();
107 return;
108 }
109
110 // check each 500ms if data is ready, and read it in that case
111 this->set_interval("status-check", 500, [this]() {
112 if (this->is_data_ready_())
113 this->update();
114 });
115}
116
118 ESP_LOGCONFIG(TAG, "scd30:");
119 LOG_I2C_DEVICE(this);
120 if (this->is_failed()) {
121 switch (this->error_code_) {
123 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
124 break;
126 ESP_LOGW(TAG, "Measurement Initialization failed");
127 break;
129 ESP_LOGW(TAG, "Unable to read sensor firmware version");
130 break;
131 default:
132 ESP_LOGW(TAG, "Unknown setup error");
133 break;
134 }
135 }
136 if (this->altitude_compensation_ == 0xFFFF) {
137 ESP_LOGCONFIG(TAG, " Altitude compensation: OFF");
138 } else {
139 ESP_LOGCONFIG(TAG, " Altitude compensation: %dm", this->altitude_compensation_);
140 }
141 ESP_LOGCONFIG(TAG,
142 " Automatic self calibration: %s\n"
143 " Ambient pressure compensation: %dmBar\n"
144 " Temperature offset: %.2f °C\n"
145 " Update interval: %ds",
147 this->update_interval_);
148 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
149 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
150 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
151}
152
154 uint16_t raw_read_status;
155 if (!this->read_data(raw_read_status) || raw_read_status == 0x00) {
156 this->status_set_warning();
157 ESP_LOGW(TAG, "Data not ready yet!");
158 return;
159 }
160
161 if (!this->write_command(SCD30_CMD_READ_MEASUREMENT)) {
162 ESP_LOGW(TAG, "Error reading measurement!");
163 this->status_set_warning();
164 return;
165 }
166
167 this->set_timeout(50, [this]() {
168 uint16_t raw_data[6];
169 if (!this->read_data(raw_data, 6)) {
170 this->status_set_warning();
171 return;
172 }
173
174 union uint32_float_t {
175 uint32_t uint32;
176 float value;
177 };
178 uint32_t temp_c_o2_u32 = (((uint32_t(raw_data[0])) << 16) | (uint32_t(raw_data[1])));
179 uint32_float_t co2{.uint32 = temp_c_o2_u32};
180
181 uint32_t temp_temp_u32 = (((uint32_t(raw_data[2])) << 16) | (uint32_t(raw_data[3])));
182 uint32_float_t temperature{.uint32 = temp_temp_u32};
183
184 uint32_t temp_hum_u32 = (((uint32_t(raw_data[4])) << 16) | (uint32_t(raw_data[5])));
185 uint32_float_t humidity{.uint32 = temp_hum_u32};
186
187 ESP_LOGD(TAG, "Got CO2=%.2fppm temperature=%.2f°C humidity=%.2f%%", co2.value, temperature.value, humidity.value);
188 if (this->co2_sensor_ != nullptr)
189 this->co2_sensor_->publish_state(co2.value);
190 if (this->temperature_sensor_ != nullptr)
192 if (this->humidity_sensor_ != nullptr)
193 this->humidity_sensor_->publish_state(humidity.value);
194
195 this->status_clear_warning();
196 });
197}
198
200 if (!this->write_command(SCD30_CMD_GET_DATA_READY_STATUS)) {
201 return false;
202 }
203 delay(4);
204 uint16_t is_data_ready;
205 if (!this->read_data(&is_data_ready, 1)) {
206 return false;
207 }
208 return is_data_ready == 1;
209}
210
212 ESP_LOGD(TAG, "Performing CO2 force recalibration with reference %dppm.", co2_reference);
213 if (this->write_command(SCD30_CMD_FORCED_CALIBRATION, co2_reference)) {
214 ESP_LOGD(TAG, "Force recalibration complete.");
215 return true;
216 } else {
217 ESP_LOGE(TAG, "Failed to force recalibration with reference.");
218 this->error_code_ = FORCE_RECALIBRATION_FAILED;
219 this->status_set_warning();
220 return false;
221 }
222}
223
225 uint16_t forced_calibration_reference;
226 // Get current CO2 calibration
227 if (!this->get_register(SCD30_CMD_FORCED_CALIBRATION, forced_calibration_reference)) {
228 ESP_LOGE(TAG, "Unable to read forced calibration reference.");
229 }
230 return forced_calibration_reference;
231}
232
233} // namespace scd30
234} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition component.cpp:89
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
uint16_t ambient_pressure_compensation_
Definition scd30.h:42
sensor::Sensor * humidity_sensor_
Definition scd30.h:47
sensor::Sensor * temperature_sensor_
Definition scd30.h:48
uint16_t get_forced_calibration_reference()
Definition scd30.cpp:224
bool force_recalibration_with_reference(uint16_t co2_reference)
Definition scd30.cpp:211
sensor::Sensor * co2_sensor_
Definition scd30.h:46
void dump_config() override
Definition scd30.cpp:117
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.
Definition sensor.cpp:45
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t temperature
Definition sun_gtil2.cpp:12