13namespace ens160_base {
15static const char *
const TAG =
"ens160";
17static const uint8_t ENS160_BOOTING = 10;
19static const uint16_t ENS160_PART_ID = 0x0160;
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;
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;
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;
38static const uint8_t ENS160_OPMODE_RESET = 0xF0;
39static const uint8_t ENS160_OPMODE_IDLE = 0x01;
40static const uint8_t ENS160_OPMODE_STD = 0x02;
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;
49static const uint8_t ENS160_DATA_AQI = 0x07;
54 if (!this->
read_bytes(ENS160_REG_PART_ID,
reinterpret_cast<uint8_t *
>(&part_id), 2)) {
59 if (part_id != ENS160_PART_ID) {
66 if (!this->
write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_RESET)) {
71 delay(ENS160_BOOTING);
75 if (!this->
read_byte(ENS160_REG_DATA_STATUS, &status_value)) {
89 if (!this->
write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_IDLE)) {
95 if (!this->
write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_NOP)) {
100 if (!this->
write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_CLRGPR)) {
107 if (!this->
write_byte(ENS160_REG_COMMAND, ENS160_COMMAND_GET_APPVER)) {
112 uint8_t version_data[3];
113 if (!this->
read_bytes(ENS160_REG_GPR_READ_4, version_data, 3)) {
123 if (!this->
write_byte(ENS160_REG_OPMODE, ENS160_OPMODE_STD)) {
131 if (!this->
read_byte(ENS160_REG_OPMODE, &op_mode)) {
137 if (op_mode != ENS160_OPMODE_STD) {
145 uint8_t status_value, data_ready;
147 if (!this->
read_byte(ENS160_REG_DATA_STATUS, &status_value)) {
148 ESP_LOGW(TAG,
"Error reading status register");
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);
164 data_ready = ENS160_DATA_STATUS_NEWDAT & status_value;
169 if (data_ready != ENS160_DATA_STATUS_NEWDAT) {
170 ESP_LOGD(TAG,
"ENS160 readings unavailable - Normal Operation but readings not ready");
177 ESP_LOGI(TAG,
"ENS160 readings unavailable - 1 hour startup required after first power on");
183 ESP_LOGI(TAG,
"ENS160 readings not available yet - Warming up requires 3 minutes");
188 ESP_LOGE(TAG,
"ENS160 Invalid Status - No valid output");
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");
200 if (this->
co2_ !=
nullptr) {
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");
210 if (this->
tvoc_ !=
nullptr) {
215 if (!this->
read_byte(ENS160_REG_DATA_AQI, &data_aqi)) {
216 ESP_LOGW(TAG,
"Error reading AQI data register");
220 if (this->
aqi_ !=
nullptr) {
222 data_aqi = ENS160_DATA_AQI & data_aqi;
242 ESP_LOGW(TAG,
"Invalid external temperature - compensation values not updated");
245 ESP_LOGV(TAG,
"External temperature compensation: %.1f°C",
temperature);
248 float humidity = NAN;
252 if (std::isnan(humidity) || humidity < 0.0f || humidity > 100.0f) {
253 ESP_LOGW(TAG,
"Invalid external humidity - compensation values not updated");
256 ESP_LOGV(TAG,
"External humidity compensation: %.1f%%", humidity);
259 uint16_t t = (uint16_t) ((
temperature + 273.15f) * 64.0f);
260 uint16_t
h = (uint16_t) (humidity * 512.0f);
264 data[1] = (t >> 8) & 0xff;
266 data[3] = (
h >> 8) & 0xff;
268 if (!this->
write_bytes(ENS160_REG_TEMP_IN, data, 4)) {
269 ESP_LOGE(TAG,
"Error writing compensation values");
276 ESP_LOGCONFIG(TAG,
"ENS160:");
278 switch (this->error_code_) {
280 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
283 ESP_LOGE(TAG,
"Error reading from register");
286 ESP_LOGE(TAG,
"Error writing to register");
289 ESP_LOGE(TAG,
"Sensor reported an invalid ID. Is this a ENS160?");
292 ESP_LOGE(TAG,
"Invalid Device Status - No valid output");
295 ESP_LOGE(TAG,
"Device failed to achieve Standard Operating Mode");
298 ESP_LOGD(TAG,
"Setup successful");
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_);
310 LOG_SENSOR(
" ",
" Temperature Compensation:", this->
temperature_);
311 LOG_SENSOR(
" ",
" Humidity Compensation:", this->
humidity_);
313 ESP_LOGCONFIG(TAG,
" Compensation: Not configured");