ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
ens160_base.cpp
Go to the documentation of this file.
1// ENS160 sensor with I2C interface from ScioSense
2//
3// Datasheet: https://www.sciosense.com/wp-content/uploads/documents/SC-001224-DS-7-ENS160-Datasheet.pdf
4//
5// Implementation based on:
6// https://github.com/sciosense/ENS160_driver
7
8#include "ens160_base.h"
9#include "esphome/core/log.h"
10#include "esphome/core/hal.h"
11
12namespace esphome {
13namespace ens160_base {
14
15static const char *const TAG = "ens160";
16
17static const uint8_t ENS160_BOOTING = 10;
18
19static const uint16_t ENS160_PART_ID = 0x0160;
20
21static const uint8_t ENS160_REG_PART_ID = 0x00;
22static const uint8_t ENS160_REG_OPMODE = 0x10;
23static const uint8_t ENS160_REG_CONFIG = 0x11;
24static const uint8_t ENS160_REG_COMMAND = 0x12;
25static const uint8_t ENS160_REG_TEMP_IN = 0x13;
26static const uint8_t ENS160_REG_DATA_STATUS = 0x20;
27static const uint8_t ENS160_REG_DATA_AQI = 0x21;
28static const uint8_t ENS160_REG_DATA_TVOC = 0x22;
29static const uint8_t ENS160_REG_DATA_ECO2 = 0x24;
30
31static const uint8_t ENS160_REG_GPR_READ_0 = 0x48;
32static const uint8_t ENS160_REG_GPR_READ_4 = ENS160_REG_GPR_READ_0 + 4;
33
34static const uint8_t ENS160_COMMAND_NOP = 0x00;
35static const uint8_t ENS160_COMMAND_CLRGPR = 0xCC;
36static const uint8_t ENS160_COMMAND_GET_APPVER = 0x0E;
37
38static const uint8_t ENS160_OPMODE_RESET = 0xF0;
39static const uint8_t ENS160_OPMODE_IDLE = 0x01;
40static const uint8_t ENS160_OPMODE_STD = 0x02;
41
42static const uint8_t ENS160_DATA_STATUS_STATAS = 0x80;
43static const uint8_t ENS160_DATA_STATUS_STATER = 0x40;
44static const uint8_t ENS160_DATA_STATUS_VALIDITY = 0x0C;
45static const uint8_t ENS160_DATA_STATUS_NEWDAT = 0x02;
46static const uint8_t ENS160_DATA_STATUS_NEWGPR = 0x01;
47
48// helps remove reserved bits in aqi data register
49static const uint8_t ENS160_DATA_AQI = 0x07;
50
52 // check part_id
53 uint16_t part_id;
54 if (!this->read_bytes(ENS160_REG_PART_ID, reinterpret_cast<uint8_t *>(&part_id), 2)) {
55 this->error_code_ = COMMUNICATION_FAILED;
56 this->mark_failed();
57 return;
58 }
59 if (part_id != ENS160_PART_ID) {
60 this->error_code_ = INVALID_ID;
61 this->mark_failed();
62 return;
63 }
64
65 // set mode to reset
66 if (!this->write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_RESET)) {
67 this->error_code_ = WRITE_FAILED;
68 this->mark_failed();
69 return;
70 }
71 delay(ENS160_BOOTING);
72
73 // check status
74 uint8_t status_value;
75 if (!this->read_byte(ENS160_REG_DATA_STATUS, &status_value)) {
76 this->error_code_ = READ_FAILED;
77 this->mark_failed();
78 return;
79 }
80 this->validity_flag_ = static_cast<ValidityFlag>((ENS160_DATA_STATUS_VALIDITY & status_value) >> 2);
81
82 if (this->validity_flag_ == INVALID_OUTPUT) {
83 this->error_code_ = VALIDITY_INVALID;
84 this->mark_failed();
85 return;
86 }
87
88 // set mode to idle
89 if (!this->write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_IDLE)) {
90 this->error_code_ = WRITE_FAILED;
91 this->mark_failed();
92 return;
93 }
94 // clear command
95 if (!this->write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_NOP)) {
96 this->error_code_ = WRITE_FAILED;
97 this->mark_failed();
98 return;
99 }
100 if (!this->write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_CLRGPR)) {
101 this->error_code_ = WRITE_FAILED;
102 this->mark_failed();
103 return;
104 }
105
106 // read firmware version
107 if (!this->write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_GET_APPVER)) {
108 this->error_code_ = WRITE_FAILED;
109 this->mark_failed();
110 return;
111 }
112 uint8_t version_data[3];
113 if (!this->read_bytes(ENS160_REG_GPR_READ_4, version_data, 3)) {
114 this->error_code_ = READ_FAILED;
115 this->mark_failed();
116 return;
117 }
118 this->firmware_ver_major_ = version_data[0];
119 this->firmware_ver_minor_ = version_data[1];
120 this->firmware_ver_build_ = version_data[2];
121
122 // set mode to standard
123 if (!this->write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_STD)) {
124 this->error_code_ = WRITE_FAILED;
125 this->mark_failed();
126 return;
127 }
128
129 // read opmode and check standard mode is achieved before finishing Setup
130 uint8_t op_mode;
131 if (!this->read_byte(ENS160_REG_OPMODE, &op_mode)) {
132 this->error_code_ = READ_FAILED;
133 this->mark_failed();
134 return;
135 }
136
137 if (op_mode != ENS160_OPMODE_STD) {
138 this->error_code_ = STD_OPMODE_FAILED;
139 this->mark_failed();
140 return;
141 }
142}
143
145 uint8_t status_value, data_ready;
146
147 if (!this->read_byte(ENS160_REG_DATA_STATUS, &status_value)) {
148 ESP_LOGW(TAG, "Error reading status register");
149 this->status_set_warning();
150 return;
151 }
152
153 // verbose status logging
154 ESP_LOGV(TAG,
155 "ENS160 Status Register: 0x%02x\n"
156 " STATAS bit 0x%x\n"
157 " STATER bit 0x%x\n"
158 " VALIDITY FLAG 0x%02x\n"
159 " NEWDAT bit 0x%x\n"
160 " NEWGPR bit 0x%x",
161 status_value, (ENS160_DATA_STATUS_STATAS & (status_value)) == ENS160_DATA_STATUS_STATAS,
162 (ENS160_DATA_STATUS_STATER & (status_value)) == ENS160_DATA_STATUS_STATER,
163 (ENS160_DATA_STATUS_VALIDITY & status_value) >> 2,
164 (ENS160_DATA_STATUS_NEWDAT & (status_value)) == ENS160_DATA_STATUS_NEWDAT,
165 (ENS160_DATA_STATUS_NEWGPR & (status_value)) == ENS160_DATA_STATUS_NEWGPR);
166
167 data_ready = ENS160_DATA_STATUS_NEWDAT & status_value;
168 this->validity_flag_ = static_cast<ValidityFlag>((ENS160_DATA_STATUS_VALIDITY & status_value) >> 2);
169
170 switch (validity_flag_) {
171 case NORMAL_OPERATION:
172 if (data_ready != ENS160_DATA_STATUS_NEWDAT) {
173 ESP_LOGD(TAG, "ENS160 readings unavailable - Normal Operation but readings not ready");
174 return;
175 }
176 break;
177 case INITIAL_STARTUP:
178 if (!this->initial_startup_) {
179 this->initial_startup_ = true;
180 ESP_LOGI(TAG, "ENS160 readings unavailable - 1 hour startup required after first power on");
181 }
182 return;
183 case WARMING_UP:
184 if (!this->warming_up_) {
185 this->warming_up_ = true;
186 ESP_LOGI(TAG, "ENS160 readings not available yet - Warming up requires 3 minutes");
187 this->send_env_data_();
188 }
189 return;
190 case INVALID_OUTPUT:
191 ESP_LOGE(TAG, "ENS160 Invalid Status - No valid output");
192 this->status_set_warning();
193 return;
194 }
195
196 // read new data
197 uint16_t data_eco2;
198 if (!this->read_bytes(ENS160_REG_DATA_ECO2, reinterpret_cast<uint8_t *>(&data_eco2), 2)) {
199 ESP_LOGW(TAG, "Error reading eCO2 data register");
200 this->status_set_warning();
201 return;
202 }
203 if (this->co2_ != nullptr) {
204 this->co2_->publish_state(data_eco2);
205 }
206
207 uint16_t data_tvoc;
208 if (!this->read_bytes(ENS160_REG_DATA_TVOC, reinterpret_cast<uint8_t *>(&data_tvoc), 2)) {
209 ESP_LOGW(TAG, "Error reading TVOC data register");
210 this->status_set_warning();
211 return;
212 }
213 if (this->tvoc_ != nullptr) {
214 this->tvoc_->publish_state(data_tvoc);
215 }
216
217 uint8_t data_aqi;
218 if (!this->read_byte(ENS160_REG_DATA_AQI, &data_aqi)) {
219 ESP_LOGW(TAG, "Error reading AQI data register");
220 this->status_set_warning();
221 return;
222 }
223 if (this->aqi_ != nullptr) {
224 // remove reserved bits, just in case they are used in future
225 data_aqi = ENS160_DATA_AQI & data_aqi;
226
227 this->aqi_->publish_state(data_aqi);
228 }
229
230 this->status_clear_warning();
231
232 // set temperature and humidity compensation data
233 this->send_env_data_();
234}
235
237 if (this->temperature_ == nullptr && this->humidity_ == nullptr)
238 return;
239
240 float temperature = NAN;
241 if (this->temperature_ != nullptr)
242 temperature = this->temperature_->state;
243
244 if (std::isnan(temperature) || temperature < -40.0f || temperature > 85.0f) {
245 ESP_LOGW(TAG, "Invalid external temperature - compensation values not updated");
246 return;
247 } else {
248 ESP_LOGV(TAG, "External temperature compensation: %.1f°C", temperature);
249 }
250
251 float humidity = NAN;
252 if (this->humidity_ != nullptr)
253 humidity = this->humidity_->state;
254
255 if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
256 ESP_LOGW(TAG, "Invalid external humidity - compensation values not updated");
257 return;
258 } else {
259 ESP_LOGV(TAG, "External humidity compensation: %.1f%%", humidity);
260 }
261
262 uint16_t t = (uint16_t) ((temperature + 273.15f) * 64.0f);
263 uint16_t h = (uint16_t) (humidity * 512.0f);
264
265 uint8_t data[4];
266 data[0] = t & 0xff;
267 data[1] = (t >> 8) & 0xff;
268 data[2] = h & 0xff;
269 data[3] = (h >> 8) & 0xff;
270
271 if (!this->write_bytes(ENS160_REG_TEMP_IN, data, 4)) {
272 ESP_LOGE(TAG, "Error writing compensation values");
273 this->status_set_warning();
274 return;
275 }
276}
277
279 ESP_LOGCONFIG(TAG, "ENS160:");
280
281 switch (this->error_code_) {
283 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
284 break;
285 case READ_FAILED:
286 ESP_LOGE(TAG, "Error reading from register");
287 break;
288 case WRITE_FAILED:
289 ESP_LOGE(TAG, "Error writing to register");
290 break;
291 case INVALID_ID:
292 ESP_LOGE(TAG, "Sensor reported an invalid ID. Is this a ENS160?");
293 break;
294 case VALIDITY_INVALID:
295 ESP_LOGE(TAG, "Invalid Device Status - No valid output");
296 break;
298 ESP_LOGE(TAG, "Device failed to achieve Standard Operating Mode");
299 break;
300 case NONE:
301 ESP_LOGD(TAG, "Setup successful");
302 break;
303 }
304 ESP_LOGI(TAG, "Firmware Version: %d.%d.%d", this->firmware_ver_major_, this->firmware_ver_minor_,
305 this->firmware_ver_build_);
306
307 LOG_UPDATE_INTERVAL(this);
308 LOG_SENSOR(" ", "CO2 Sensor:", this->co2_);
309 LOG_SENSOR(" ", "TVOC Sensor:", this->tvoc_);
310 LOG_SENSOR(" ", "AQI Sensor:", this->aqi_);
311
312 if (this->temperature_ != nullptr && this->humidity_ != nullptr) {
313 LOG_SENSOR(" ", " Temperature Compensation:", this->temperature_);
314 LOG_SENSOR(" ", " Humidity Compensation:", this->humidity_);
315 } else {
316 ESP_LOGCONFIG(TAG, " Compensation: Not configured");
317 }
318}
319
320} // namespace ens160_base
321} // namespace esphome
uint8_t h
Definition bl0906.h:2
void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
virtual bool write_byte(uint8_t a_register, uint8_t data)=0
virtual bool read_byte(uint8_t a_register, uint8_t *data)=0
enum esphome::ens160_base::ENS160Component::ErrorCode NONE
virtual bool write_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
enum esphome::ens160_base::ENS160Component::ValidityFlag validity_flag_
virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len)=0
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:125
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void HOT delay(uint32_t ms)
Definition core.cpp:27
uint16_t temperature
Definition sun_gtil2.cpp:12