ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
sen5x.cpp
Go to the documentation of this file.
1#include "sen5x.h"
3#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6#include <cinttypes>
7
8namespace esphome::sen5x {
9
10static const char *const TAG = "sen5x";
11
12static const uint16_t SEN5X_CMD_AUTO_CLEANING_INTERVAL = 0x8004;
13static const uint16_t SEN5X_CMD_GET_DATA_READY_STATUS = 0x0202;
14static const uint16_t SEN5X_CMD_GET_FIRMWARE_VERSION = 0xD100;
15static const uint16_t SEN5X_CMD_GET_PRODUCT_NAME = 0xD014;
16static const uint16_t SEN5X_CMD_GET_SERIAL_NUMBER = 0xD033;
17static const uint16_t SEN5X_CMD_NOX_ALGORITHM_TUNING = 0x60E1;
18static const uint16_t SEN5X_CMD_READ_MEASUREMENT = 0x03C4;
19static const uint16_t SEN5X_CMD_RHT_ACCELERATION_MODE = 0x60F7;
20static const uint16_t SEN5X_CMD_START_CLEANING_FAN = 0x5607;
21static const uint16_t SEN5X_CMD_START_MEASUREMENTS = 0x0021;
22static const uint16_t SEN5X_CMD_START_MEASUREMENTS_RHT_ONLY = 0x0037;
23static const uint16_t SEN5X_CMD_STOP_MEASUREMENTS = 0x3f86;
24static const uint16_t SEN5X_CMD_TEMPERATURE_COMPENSATION = 0x60B2;
25static const uint16_t SEN5X_CMD_VOC_ALGORITHM_STATE = 0x6181;
26static const uint16_t SEN5X_CMD_VOC_ALGORITHM_TUNING = 0x60D0;
27
28static const int8_t SEN5X_INDEX_SCALE_FACTOR = 10; // used for VOC and NOx index values
29static const int8_t SEN5X_MIN_INDEX_VALUE = 1 * SEN5X_INDEX_SCALE_FACTOR; // must be adjusted by the scale factor
30static const int16_t SEN5X_MAX_INDEX_VALUE = 500 * SEN5X_INDEX_SCALE_FACTOR; // must be adjusted by the scale factor
31
32static const LogString *type_to_string(Sen5xType type) {
33 switch (type) {
35 return LOG_STR("SEN50");
37 return LOG_STR("SEN54");
39 return LOG_STR("SEN55");
40 default:
41 return LOG_STR("UNKNOWN");
42 }
43}
44
45static const LogString *rht_accel_mode_to_string(RhtAccelerationMode mode) {
46 switch (mode) {
48 return LOG_STR("LOW");
50 return LOG_STR("MEDIUM");
52 return LOG_STR("HIGH");
53 default:
54 return LOG_STR("UNKNOWN");
55 }
56}
57
59 // the sensor needs 1000 ms to enter the idle state
60 this->set_timeout(1000, [this]() {
61 // Check if measurement is ready before reading the value
62 if (!this->write_command(SEN5X_CMD_GET_DATA_READY_STATUS)) {
63 ESP_LOGE(TAG, "Failed to write data ready status command");
64 this->mark_failed();
65 return;
66 }
67 delay(20); // per datasheet
68
69 uint16_t raw_read_status;
70 if (!this->read_data(raw_read_status)) {
71 ESP_LOGE(TAG, "Failed to read data ready status");
72 this->mark_failed();
73 return;
74 }
75
76 uint32_t stop_measurement_delay = 0;
77 // In order to query the device periodic measurement must be ceased
78 if (raw_read_status) {
79 ESP_LOGD(TAG, "Data is available; stopping periodic measurement");
80 if (!this->write_command(SEN5X_CMD_STOP_MEASUREMENTS)) {
81 ESP_LOGE(TAG, "Failed to stop measurements");
82 this->mark_failed();
83 return;
84 }
85 // According to the SEN5x datasheet the sensor will only respond to other commands after waiting 200 ms after
86 // issuing the stop_periodic_measurement command
87 stop_measurement_delay = 200;
88 }
89 this->set_timeout(stop_measurement_delay, [this]() {
90 // note: serial number register is actually 32-bytes long but we grab only the first 16-bytes,
91 // this appears to be all that Sensirion uses for serial numbers, this could change
92 uint16_t raw_serial_number[8];
93 if (!this->get_register(SEN5X_CMD_GET_SERIAL_NUMBER, raw_serial_number, 8, 20)) {
94 ESP_LOGE(TAG, "Failed to read serial number");
96 this->mark_failed();
97 return;
98 }
99 const char *serial_number = sensirion_convert_to_string_in_place(raw_serial_number, 8);
100 snprintf(this->serial_number_, sizeof(this->serial_number_), "%s", serial_number);
101 ESP_LOGV(TAG, "Serial number %s", this->serial_number_);
102
103 uint16_t raw_product_name[16];
104 if (!this->get_register(SEN5X_CMD_GET_PRODUCT_NAME, raw_product_name, 16, 20)) {
105 ESP_LOGE(TAG, "Failed to read product name");
107 this->mark_failed();
108 return;
109 }
110 const char *product_name = sensirion_convert_to_string_in_place(raw_product_name, 16);
111 if (strncmp(product_name, "SEN50", 5) == 0) {
112 this->type_ = Sen5xType::SEN50;
113 } else if (strncmp(product_name, "SEN54", 5) == 0) {
114 this->type_ = Sen5xType::SEN54;
115 } else if (strncmp(product_name, "SEN55", 5) == 0) {
116 this->type_ = Sen5xType::SEN55;
117 } else {
119 ESP_LOGE(TAG, "Unknown product name: %.32s", product_name);
121 this->mark_failed();
122 return;
123 }
124
125 ESP_LOGD(TAG, "Type: %s", LOG_STR_ARG(type_to_string(this->type_)));
126 if (this->humidity_sensor_ && this->type_ == Sen5xType::SEN50) {
127 ESP_LOGE(TAG, "Relative humidity requires a SEN54 or SEN55");
128 this->humidity_sensor_ = nullptr; // mark as not used
129 }
130 if (this->temperature_sensor_ && this->type_ == Sen5xType::SEN50) {
131 ESP_LOGE(TAG, "Temperature requires a SEN54 or SEN55");
132 this->temperature_sensor_ = nullptr; // mark as not used
133 }
134 if (this->voc_sensor_ && this->type_ == Sen5xType::SEN50) {
135 ESP_LOGE(TAG, "VOC requires a SEN54 or SEN55");
136 this->voc_sensor_ = nullptr; // mark as not used
137 }
138 if (this->nox_sensor_ && this->type_ != Sen5xType::SEN55) {
139 ESP_LOGE(TAG, "NOx requires a SEN55");
140 this->nox_sensor_ = nullptr; // mark as not used
141 }
142
143 if (!this->get_register(SEN5X_CMD_GET_FIRMWARE_VERSION, this->firmware_version_, 20)) {
144 ESP_LOGE(TAG, "Failed to read firmware version");
146 this->mark_failed();
147 return;
148 }
149 this->firmware_version_ >>= 8;
150 ESP_LOGV(TAG, "Firmware version %d", this->firmware_version_);
151
152 if (this->voc_sensor_ && this->store_baseline_) {
153 // Hash with serial number, serial numbers are unique, so multiple sensors can be used without conflict
154 uint32_t hash = fnv1a_hash(this->serial_number_);
155 this->pref_ = global_preferences->make_preference<uint16_t[4]>(hash, true);
157 if (this->pref_.load(&this->voc_baseline_state_)) {
158 if (!this->write_command(SEN5X_CMD_VOC_ALGORITHM_STATE, this->voc_baseline_state_, 4)) {
159 ESP_LOGE(TAG, "VOC Baseline State write to sensor failed");
160 } else {
161 ESP_LOGV(TAG, "VOC Baseline State loaded");
162 delay(20);
163 }
164 }
165 }
166 bool result;
167 if (this->auto_cleaning_interval_.has_value()) {
168 // override default value
169 result = this->write_command(SEN5X_CMD_AUTO_CLEANING_INTERVAL, this->auto_cleaning_interval_.value());
170 } else {
171 result = this->write_command(SEN5X_CMD_AUTO_CLEANING_INTERVAL);
172 }
173 if (result) {
174 delay(20);
175 uint16_t secs[2];
176 if (this->read_data(secs, 2)) {
177 this->auto_cleaning_interval_ = secs[0] << 16 | secs[1];
178 }
179 }
180 if (this->acceleration_mode_.has_value()) {
181 result = this->write_command(SEN5X_CMD_RHT_ACCELERATION_MODE, this->acceleration_mode_.value());
182 } else {
183 result = this->write_command(SEN5X_CMD_RHT_ACCELERATION_MODE);
184 }
185 if (!result) {
186 ESP_LOGE(TAG, "Failed to set rh/t acceleration mode");
188 this->mark_failed();
189 return;
190 }
191 delay(20);
192 if (!this->acceleration_mode_.has_value()) {
193 uint16_t mode;
194 if (this->read_data(mode)) {
196 } else {
197 ESP_LOGE(TAG, "Failed to read RHT Acceleration mode");
198 }
199 }
200 if (this->voc_tuning_params_.has_value()) {
201 this->write_tuning_parameters_(SEN5X_CMD_VOC_ALGORITHM_TUNING, this->voc_tuning_params_.value());
202 delay(20);
203 }
204 if (this->nox_tuning_params_.has_value()) {
205 this->write_tuning_parameters_(SEN5X_CMD_NOX_ALGORITHM_TUNING, this->nox_tuning_params_.value());
206 delay(20);
207 }
208
209 if (this->temperature_compensation_.has_value()) {
211 delay(20);
212 }
213
214 // Finally start sensor measurements
215 auto cmd = SEN5X_CMD_START_MEASUREMENTS_RHT_ONLY;
216 if (this->pm_1_0_sensor_ || this->pm_2_5_sensor_ || this->pm_4_0_sensor_ || this->pm_10_0_sensor_) {
217 // if any of the gas sensors are active we need a full measurement
218 cmd = SEN5X_CMD_START_MEASUREMENTS;
219 }
220
221 if (!this->write_command(cmd)) {
222 ESP_LOGE(TAG, "Error starting continuous measurements");
224 this->mark_failed();
225 return;
226 }
227 this->initialized_ = true;
228 });
229 });
230}
231
233 ESP_LOGCONFIG(TAG, "SEN5X:");
234 LOG_I2C_DEVICE(this);
235 if (this->is_failed()) {
236 switch (this->error_code_) {
238 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
239 break;
241 ESP_LOGW(TAG, "Measurement initialization failed");
242 break;
244 ESP_LOGW(TAG, "Unable to read serial ID");
245 break;
247 ESP_LOGW(TAG, "Unable to read product name");
248 break;
249 case FIRMWARE_FAILED:
250 ESP_LOGW(TAG, "Unable to read firmware version");
251 break;
252 default:
253 ESP_LOGW(TAG, "Unknown setup error");
254 break;
255 }
256 }
257 ESP_LOGCONFIG(TAG,
258 " Type: %s\n"
259 " Firmware version: %d\n"
260 " Serial number: %s",
261 LOG_STR_ARG(type_to_string(this->type_)), this->firmware_version_, this->serial_number_);
262 if (this->auto_cleaning_interval_.has_value()) {
263 ESP_LOGCONFIG(TAG, " Auto cleaning interval: %" PRId32 "s", this->auto_cleaning_interval_.value());
264 }
265 if (this->acceleration_mode_.has_value()) {
266 ESP_LOGCONFIG(TAG, " RH/T acceleration mode: %s",
267 LOG_STR_ARG(rht_accel_mode_to_string(this->acceleration_mode_.value())));
268 }
269 if (this->voc_sensor_) {
270 char hex_buf[5 * 4];
271 format_hex_pretty_to(hex_buf, this->voc_baseline_state_, 4, 0);
272 ESP_LOGCONFIG(TAG,
273 " Store Baseline: %s\n"
274 " State: %s\n",
275 TRUEFALSE(this->store_baseline_), hex_buf);
276 }
277 LOG_UPDATE_INTERVAL(this);
278 LOG_SENSOR(" ", "PM 1.0", this->pm_1_0_sensor_);
279 LOG_SENSOR(" ", "PM 2.5", this->pm_2_5_sensor_);
280 LOG_SENSOR(" ", "PM 4.0", this->pm_4_0_sensor_);
281 LOG_SENSOR(" ", "PM 10.0", this->pm_10_0_sensor_);
282 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
283 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
284 LOG_SENSOR(" ", "VOC", this->voc_sensor_); // SEN54 and SEN55 only
285 LOG_SENSOR(" ", "NOx", this->nox_sensor_); // SEN55 only
286}
287
289 if (!this->initialized_) {
290 return;
291 }
292
293 if (!this->write_command(SEN5X_CMD_READ_MEASUREMENT)) {
294 this->status_set_warning();
295 ESP_LOGD(TAG, "Write error: read measurement (%d)", this->last_error_);
296 return;
297 }
298 this->set_timeout(20, [this]() {
299 uint16_t measurements[8];
300
301 if (!this->read_data(measurements, 8)) {
302 this->status_set_warning();
303 ESP_LOGD(TAG, "Read data error (%d)", this->last_error_);
304 return;
305 }
306
307 ESP_LOGVV(TAG, "pm_1_0 = 0x%.4x", measurements[0]);
308 float pm_1_0 = measurements[0] == UINT16_MAX ? NAN : measurements[0] / 10.0f;
309
310 ESP_LOGVV(TAG, "pm_2_5 = 0x%.4x", measurements[1]);
311 float pm_2_5 = measurements[1] == UINT16_MAX ? NAN : measurements[1] / 10.0f;
312
313 ESP_LOGVV(TAG, "pm_4_0 = 0x%.4x", measurements[2]);
314 float pm_4_0 = measurements[2] == UINT16_MAX ? NAN : measurements[2] / 10.0f;
315
316 ESP_LOGVV(TAG, "pm_10_0 = 0x%.4x", measurements[3]);
317 float pm_10_0 = measurements[3] == UINT16_MAX ? NAN : measurements[3] / 10.0f;
318
319 ESP_LOGVV(TAG, "humidity = 0x%.4x", measurements[4]);
320 float humidity = measurements[4] == INT16_MAX ? NAN : static_cast<int16_t>(measurements[4]) / 100.0f;
321
322 ESP_LOGVV(TAG, "temperature = 0x%.4x", measurements[5]);
323 float temperature = measurements[5] == INT16_MAX ? NAN : static_cast<int16_t>(measurements[5]) / 200.0f;
324
325 ESP_LOGVV(TAG, "voc = 0x%.4x", measurements[6]);
326 int16_t voc_idx = static_cast<int16_t>(measurements[6]);
327 float voc = (voc_idx < SEN5X_MIN_INDEX_VALUE || voc_idx > SEN5X_MAX_INDEX_VALUE)
328 ? NAN
329 : static_cast<float>(voc_idx) / 10.0f;
330
331 ESP_LOGVV(TAG, "nox = 0x%.4x", measurements[7]);
332 int16_t nox_idx = static_cast<int16_t>(measurements[7]);
333 float nox = (nox_idx < SEN5X_MIN_INDEX_VALUE || nox_idx > SEN5X_MAX_INDEX_VALUE)
334 ? NAN
335 : static_cast<float>(nox_idx) / 10.0f;
336
337 if (this->pm_1_0_sensor_ != nullptr) {
338 this->pm_1_0_sensor_->publish_state(pm_1_0);
339 }
340 if (this->pm_2_5_sensor_ != nullptr) {
341 this->pm_2_5_sensor_->publish_state(pm_2_5);
342 }
343 if (this->pm_4_0_sensor_ != nullptr) {
344 this->pm_4_0_sensor_->publish_state(pm_4_0);
345 }
346 if (this->pm_10_0_sensor_ != nullptr) {
347 this->pm_10_0_sensor_->publish_state(pm_10_0);
348 }
349 if (this->temperature_sensor_ != nullptr) {
350 this->temperature_sensor_->publish_state(temperature);
351 }
352 if (this->humidity_sensor_ != nullptr) {
353 this->humidity_sensor_->publish_state(humidity);
354 }
355 if (this->voc_sensor_ != nullptr) {
356 this->voc_sensor_->publish_state(voc);
357 }
358 if (this->nox_sensor_ != nullptr) {
359 this->nox_sensor_->publish_state(nox);
360 }
361
362 if (!this->voc_sensor_ || !this->store_baseline_ ||
363 (App.get_loop_component_start_time() - this->voc_baseline_time_) < SHORTEST_BASELINE_STORE_INTERVAL) {
364 this->status_clear_warning();
365 } else {
367 if (!this->write_command(SEN5X_CMD_VOC_ALGORITHM_STATE)) {
368 this->status_set_warning();
369 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
370 } else {
371 this->set_timeout(20, [this]() {
372 if (!this->read_data(this->voc_baseline_state_, 4)) {
373 this->status_set_warning();
374 ESP_LOGW(TAG, ESP_LOG_MSG_COMM_FAIL);
375 } else {
376 if (this->pref_.save(&this->voc_baseline_state_)) {
377 ESP_LOGD(TAG, "VOC Baseline State saved");
378 }
379 this->status_clear_warning();
380 }
381 });
382 }
383 }
384 });
385}
386
387bool SEN5XComponent::write_tuning_parameters_(uint16_t i2c_command, const GasTuning &tuning) {
388 uint16_t params[6];
389 params[0] = tuning.index_offset;
390 params[1] = tuning.learning_time_offset_hours;
391 params[2] = tuning.learning_time_gain_hours;
392 params[3] = tuning.gating_max_duration_minutes;
393 params[4] = tuning.std_initial;
394 params[5] = tuning.gain_factor;
395 auto result = write_command(i2c_command, params, 6);
396 if (!result) {
397 ESP_LOGE(TAG, "Set tuning parameters failed (command=%0xX, err=%d)", i2c_command, this->last_error_);
398 }
399 return result;
400}
401
403 uint16_t params[3];
404 params[0] = compensation.offset;
405 params[1] = compensation.normalized_offset_slope;
406 params[2] = compensation.time_constant;
407 if (!write_command(SEN5X_CMD_TEMPERATURE_COMPENSATION, params, 3)) {
408 ESP_LOGE(TAG, "Set temperature_compensation failed (%d)", this->last_error_);
409 return false;
410 }
411 return true;
412}
413
415 if (!write_command(SEN5X_CMD_START_CLEANING_FAN)) {
416 this->status_set_warning();
417 ESP_LOGE(TAG, "Start fan cleaning failed (%d)", this->last_error_);
418 return false;
419 } else {
420 ESP_LOGD(TAG, "Fan auto clean started");
421 }
422 return true;
423}
424
425} // namespace esphome::sen5x
BedjetMode mode
BedJet operating mode.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
void status_clear_warning()
Definition component.h:306
optional< RhtAccelerationMode > acceleration_mode_
Definition sen5x.h:124
sensor::Sensor * pm_4_0_sensor_
Definition sen5x.h:115
void dump_config() override
Definition sen5x.cpp:232
ESPPreferenceObject pref_
Definition sen5x.h:129
bool write_tuning_parameters_(uint16_t i2c_command, const GasTuning &tuning)
Definition sen5x.cpp:387
sensor::Sensor * pm_2_5_sensor_
Definition sen5x.h:114
sensor::Sensor * temperature_sensor_
Definition sen5x.h:118
optional< uint32_t > auto_cleaning_interval_
Definition sen5x.h:125
sensor::Sensor * pm_1_0_sensor_
Definition sen5x.h:113
sensor::Sensor * voc_sensor_
Definition sen5x.h:120
optional< TemperatureCompensation > temperature_compensation_
Definition sen5x.h:128
sensor::Sensor * nox_sensor_
Definition sen5x.h:122
optional< GasTuning > voc_tuning_params_
Definition sen5x.h:126
sensor::Sensor * pm_10_0_sensor_
Definition sen5x.h:116
sensor::Sensor * humidity_sensor_
Definition sen5x.h:119
bool write_temperature_compensation_(const TemperatureCompensation &compensation)
Definition sen5x.cpp:402
optional< GasTuning > nox_tuning_params_
Definition sen5x.h:127
uint16_t voc_baseline_state_[4]
Definition sen5x.h:105
i2c::ErrorCode last_error_
last error code from I2C operation
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.
static const char * sensirion_convert_to_string_in_place(uint16_t *array, size_t length)
This function performs an in-place conversion of the provided buffer from uint16_t values to big endi...
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint16_t type
@ PRODUCT_NAME_FAILED
Definition sen5x.h:15
@ MEASUREMENT_INIT_FAILED
Definition sen5x.h:14
@ FIRMWARE_FAILED
Definition sen5x.h:16
@ SERIAL_NUMBER_IDENTIFICATION_FAILED
Definition sen5x.h:13
@ COMMUNICATION_FAILED
Definition sen5x.h:12
RhtAccelerationMode
Definition sen5x.h:20
@ LOW_ACCELERATION
Definition sen5x.h:21
@ HIGH_ACCELERATION
Definition sen5x.h:23
@ MEDIUM_ACCELERATION
Definition sen5x.h:22
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:341
ESPPreferences * global_preferences
void HOT delay(uint32_t ms)
Definition hal.cpp:82
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint32_t fnv1a_hash(const char *str)
Calculate a FNV-1a hash of str.
Definition helpers.h:834
static void uint32_t
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
Definition preferences.h:24
uint16_t learning_time_gain_hours
Definition sen5x.h:31
uint16_t gating_max_duration_minutes
Definition sen5x.h:32
uint16_t learning_time_offset_hours
Definition sen5x.h:30
uint16_t temperature
Definition sun_gtil2.cpp:12
char serial_number[10]
Definition sun_gtil2.cpp:15