15static const char *
const TAG =
"ens210";
18static const uint8_t ENS210_BOOTING_MS = 2;
19static const uint8_t ENS210_SINGLE_MEASURMENT_CONVERSION_TIME_MS =
21static const uint16_t ENS210_PART_ID = 0x0210;
24static const uint8_t ENS210_REGISTER_PART_ID = 0x00;
25static const uint8_t ENS210_REGISTER_UID = 0x04;
26static const uint8_t ENS210_REGISTER_SYS_CTRL = 0x10;
27static const uint8_t ENS210_REGISTER_SYS_STAT = 0x11;
28static const uint8_t ENS210_REGISTER_SENS_RUN = 0x21;
29static const uint8_t ENS210_REGISTER_SENS_START = 0x22;
30static const uint8_t ENS210_REGISTER_SENS_STOP = 0x23;
31static const uint8_t ENS210_REGISTER_SENS_STAT = 0x24;
32static const uint8_t ENS210_REGISTER_T_VAL = 0x30;
33static const uint8_t ENS210_REGISTER_H_VAL = 0x33;
36static const uint8_t CRC7_WIDTH = 7;
37static const uint8_t CRC7_POLY = 0x89;
38static const uint8_t CRC7_IVEC = 0x7F;
41static const uint8_t DATA7_WIDTH = 17;
42static const uint32_t DATA7_MASK = ((1UL << DATA7_WIDTH) - 1);
43static const uint32_t DATA7_MSB = (1UL << (DATA7_WIDTH - 1));
46static const LogString *ens210_status_to_human(
int status) {
49 return LOG_STR(
"I2C error - communication with ENS210 failed!");
51 return LOG_STR(
"CRC error");
53 return LOG_STR(
"Invalid data");
55 return LOG_STR(
"Status OK");
57 return LOG_STR(
"ENS210 has wrong chip ID! Is it a ENS210?");
59 return LOG_STR(
"Unknown");
69 polynomial = polynomial << (DATA7_WIDTH - CRC7_WIDTH - 1);
73 value = value << CRC7_WIDTH;
74 bit = bit << CRC7_WIDTH;
75 polynomial = polynomial << CRC7_WIDTH;
79 while (bit & (DATA7_MASK << CRC7_WIDTH)) {
92 if (!this->
write_byte(ENS210_REGISTER_SYS_CTRL, 0x80)) {
93 this->
write_byte(ENS210_REGISTER_SYS_CTRL, 0x80);
99 delay(ENS210_BOOTING_MS);
109 if (!this->
read_bytes(ENS210_REGISTER_PART_ID, data, 2)) {
117 part_id = data[1] * 256U + data[0] * 1U;
119 if (part_id != ENS210_PART_ID) {
128 ESP_LOGCONFIG(TAG,
"ENS210:");
129 LOG_I2C_DEVICE(
this);
131 ESP_LOGE(TAG,
"%s", LOG_STR_ARG(ens210_status_to_human(this->error_code_)));
133 LOG_UPDATE_INTERVAL(
this);
140 if (!this->
write_byte(ENS210_REGISTER_SENS_RUN, 0x00)) {
141 ESP_LOGE(TAG,
"Starting single measurement failed!");
146 if (!this->
write_byte(ENS210_REGISTER_SENS_START, 0x03)) {
147 ESP_LOGE(TAG,
"Trigger of measurement failed!");
152 this->
set_timeout(
"data",
uint32_t(ENS210_SINGLE_MEASURMENT_CONVERSION_TIME_MS), [
this]() {
153 int temperature_data, temperature_status, humidity_data, humidity_status;
161 if (!this->
read_bytes(ENS210_REGISTER_T_VAL, data, 6)) {
162 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
173 float humidity = (humidity_data & 0xFFFF) / 512.0;
177 ESP_LOGW(TAG,
"Humidity status failure: %s", LOG_STR_ARG(ens210_status_to_human(humidity_status)));
189 float temperature = (temperature_data & 0xFFFF) / 64.0 - 27315L / 100.0;
193 ESP_LOGW(TAG,
"Temperature status failure: %s", LOG_STR_ARG(ens210_status_to_human(temperature_status)));
200 *data = (
val >> 0) & 0xffff;
205 uint8_t crc_ok = crc7(payload) == crc;
218 uint8_t low_power_cmd = enable ? 0x01 : 0x00;
219 ESP_LOGD(TAG,
"Enable low power: %s", enable ?
"true" :
"false");
220 bool result = this->
write_byte(ENS210_REGISTER_SYS_CTRL, low_power_cmd);
221 delay(ENS210_BOOTING_MS);
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.