ESPHome 2025.9.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, "Status: ENS160 STATAS bit 0x%x",
155 (ENS160_DATA_STATUS_STATAS & (status_value)) == ENS160_DATA_STATUS_STATAS);
156 ESP_LOGV(TAG, "Status: ENS160 STATER bit 0x%x",
157 (ENS160_DATA_STATUS_STATER & (status_value)) == ENS160_DATA_STATUS_STATER);
158 ESP_LOGV(TAG, "Status: ENS160 VALIDITY FLAG 0x%02x", (ENS160_DATA_STATUS_VALIDITY & status_value) >> 2);
159 ESP_LOGV(TAG, "Status: ENS160 NEWDAT bit 0x%x",
160 (ENS160_DATA_STATUS_NEWDAT & (status_value)) == ENS160_DATA_STATUS_NEWDAT);
161 ESP_LOGV(TAG, "Status: ENS160 NEWGPR bit 0x%x",
162 (ENS160_DATA_STATUS_NEWGPR & (status_value)) == ENS160_DATA_STATUS_NEWGPR);
163
164 data_ready = ENS160_DATA_STATUS_NEWDAT & status_value;
165 this->validity_flag_ = static_cast<ValidityFlag>((ENS160_DATA_STATUS_VALIDITY & status_value) >> 2);
166
167 switch (validity_flag_) {
168 case NORMAL_OPERATION:
169 if (data_ready != ENS160_DATA_STATUS_NEWDAT) {
170 ESP_LOGD(TAG, "ENS160 readings unavailable - Normal Operation but readings not ready");
171 return;
172 }
173 break;
174 case INITIAL_STARTUP:
175 if (!this->initial_startup_) {
176 this->initial_startup_ = true;
177 ESP_LOGI(TAG, "ENS160 readings unavailable - 1 hour startup required after first power on");
178 }
179 return;
180 case WARMING_UP:
181 if (!this->warming_up_) {
182 this->warming_up_ = true;
183 ESP_LOGI(TAG, "ENS160 readings not available yet - Warming up requires 3 minutes");
184 this->send_env_data_();
185 }
186 return;
187 case INVALID_OUTPUT:
188 ESP_LOGE(TAG, "ENS160 Invalid Status - No valid output");
189 this->status_set_warning();
190 return;
191 }
192
193 // read new data
194 uint16_t data_eco2;
195 if (!this->read_bytes(ENS160_REG_DATA_ECO2, reinterpret_cast<uint8_t *>(&data_eco2), 2)) {
196 ESP_LOGW(TAG, "Error reading eCO2 data register");
197 this->status_set_warning();
198 return;
199 }
200 if (this->co2_ != nullptr) {
201 this->co2_->publish_state(data_eco2);
202 }
203
204 uint16_t data_tvoc;
205 if (!this->read_bytes(ENS160_REG_DATA_TVOC, reinterpret_cast<uint8_t *>(&data_tvoc), 2)) {
206 ESP_LOGW(TAG, "Error reading TVOC data register");
207 this->status_set_warning();
208 return;
209 }
210 if (this->tvoc_ != nullptr) {
211 this->tvoc_->publish_state(data_tvoc);
212 }
213
214 uint8_t data_aqi;
215 if (!this->read_byte(ENS160_REG_DATA_AQI, &data_aqi)) {
216 ESP_LOGW(TAG, "Error reading AQI data register");
217 this->status_set_warning();
218 return;
219 }
220 if (this->aqi_ != nullptr) {
221 // remove reserved bits, just in case they are used in future
222 data_aqi = ENS160_DATA_AQI & data_aqi;
223
224 this->aqi_->publish_state(data_aqi);
225 }
226
227 this->status_clear_warning();
228
229 // set temperature and humidity compensation data
230 this->send_env_data_();
231}
232
234 if (this->temperature_ == nullptr && this->humidity_ == nullptr)
235 return;
236
237 float temperature = NAN;
238 if (this->temperature_ != nullptr)
239 temperature = this->temperature_->state;
240
241 if (std::isnan(temperature) || temperature < -40.0f || temperature > 85.0f) {
242 ESP_LOGW(TAG, "Invalid external temperature - compensation values not updated");
243 return;
244 } else {
245 ESP_LOGV(TAG, "External temperature compensation: %.1f°C", temperature);
246 }
247
248 float humidity = NAN;
249 if (this->humidity_ != nullptr)
250 humidity = this->humidity_->state;
251
252 if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
253 ESP_LOGW(TAG, "Invalid external humidity - compensation values not updated");
254 return;
255 } else {
256 ESP_LOGV(TAG, "External humidity compensation: %.1f%%", humidity);
257 }
258
259 uint16_t t = (uint16_t) ((temperature + 273.15f) * 64.0f);
260 uint16_t h = (uint16_t) (humidity * 512.0f);
261
262 uint8_t data[4];
263 data[0] = t & 0xff;
264 data[1] = (t >> 8) & 0xff;
265 data[2] = h & 0xff;
266 data[3] = (h >> 8) & 0xff;
267
268 if (!this->write_bytes(ENS160_REG_TEMP_IN, data, 4)) {
269 ESP_LOGE(TAG, "Error writing compensation values");
270 this->status_set_warning();
271 return;
272 }
273}
274
276 ESP_LOGCONFIG(TAG, "ENS160:");
277
278 switch (this->error_code_) {
280 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
281 break;
282 case READ_FAILED:
283 ESP_LOGE(TAG, "Error reading from register");
284 break;
285 case WRITE_FAILED:
286 ESP_LOGE(TAG, "Error writing to register");
287 break;
288 case INVALID_ID:
289 ESP_LOGE(TAG, "Sensor reported an invalid ID. Is this a ENS160?");
290 break;
291 case VALIDITY_INVALID:
292 ESP_LOGE(TAG, "Invalid Device Status - No valid output");
293 break;
295 ESP_LOGE(TAG, "Device failed to achieve Standard Operating Mode");
296 break;
297 case NONE:
298 ESP_LOGD(TAG, "Setup successful");
299 break;
300 }
301 ESP_LOGI(TAG, "Firmware Version: %d.%d.%d", this->firmware_ver_major_, this->firmware_ver_minor_,
302 this->firmware_ver_build_);
303
304 LOG_UPDATE_INTERVAL(this);
305 LOG_SENSOR(" ", "CO2 Sensor:", this->co2_);
306 LOG_SENSOR(" ", "TVOC Sensor:", this->tvoc_);
307 LOG_SENSOR(" ", "AQI Sensor:", this->aqi_);
308
309 if (this->temperature_ != nullptr && this->humidity_ != nullptr) {
310 LOG_SENSOR(" ", " Temperature Compensation:", this->temperature_);
311 LOG_SENSOR(" ", " Humidity Compensation:", this->humidity_);
312 } else {
313 ESP_LOGCONFIG(TAG, " Compensation: Not configured");
314 }
315}
316
317} // namespace ens160_base
318} // namespace esphome
uint8_t h
Definition bl0906.h:2
virtual 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:45
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:133
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