ESPHome  2024.10.2
ee895.cpp
Go to the documentation of this file.
1 #include "ee895.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 namespace ee895 {
7 
8 static const char *const TAG = "ee895";
9 
10 static const uint16_t CRC16_ONEWIRE_START = 0xFFFF;
11 static const uint8_t FUNCTION_CODE_READ = 0x03;
12 static const uint16_t SERIAL_NUMBER = 0x0000;
13 static const uint16_t TEMPERATURE_ADDRESS = 0x03EA;
14 static const uint16_t CO2_ADDRESS = 0x0424;
15 static const uint16_t PRESSURE_ADDRESS = 0x04B0;
16 
18  uint16_t crc16_check = 0;
19  ESP_LOGCONFIG(TAG, "Setting up EE895...");
20  write_command_(SERIAL_NUMBER, 8);
21  uint8_t serial_number[20];
22  this->read(serial_number, 20);
23 
24  crc16_check = (serial_number[19] << 8) + serial_number[18];
25  if (crc16_check != calc_crc16_(serial_number, 19)) {
26  this->error_code_ = CRC_CHECK_FAILED;
27  this->mark_failed();
28  return;
29  }
30  ESP_LOGV(TAG, " Serial Number: 0x%s", format_hex(serial_number + 2, 16).c_str());
31 }
32 
34  ESP_LOGCONFIG(TAG, "EE895:");
35  LOG_I2C_DEVICE(this);
36  switch (this->error_code_) {
38  ESP_LOGE(TAG, "Communication with EE895 failed!");
39  break;
40  case CRC_CHECK_FAILED:
41  ESP_LOGE(TAG, "The crc check failed");
42  break;
43  case NONE:
44  default:
45  break;
46  }
47  LOG_UPDATE_INTERVAL(this);
48  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
49  LOG_SENSOR(" ", "CO2", this->co2_sensor_);
50  LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
51 }
52 
54 
56  write_command_(TEMPERATURE_ADDRESS, 2);
57  this->set_timeout(50, [this]() {
58  float temperature = read_float_();
59 
60  write_command_(CO2_ADDRESS, 2);
61  float co2 = read_float_();
62 
63  write_command_(PRESSURE_ADDRESS, 2);
64  float pressure = read_float_();
65  ESP_LOGD(TAG, "Got temperature=%.1f°C co2=%.0fppm pressure=%.1f%mbar", temperature, co2, pressure);
66  if (this->temperature_sensor_ != nullptr)
67  this->temperature_sensor_->publish_state(temperature);
68  if (this->co2_sensor_ != nullptr)
69  this->co2_sensor_->publish_state(co2);
70  if (this->pressure_sensor_ != nullptr)
71  this->pressure_sensor_->publish_state(pressure);
72  this->status_clear_warning();
73  });
74 }
75 
76 void EE895Component::write_command_(uint16_t addr, uint16_t reg_cnt) {
77  uint8_t address[7];
78  uint16_t crc16 = 0;
79  address[0] = FUNCTION_CODE_READ;
80  address[1] = (addr >> 8) & 0xFF;
81  address[2] = addr & 0xFF;
82  address[3] = (reg_cnt >> 8) & 0xFF;
83  address[4] = reg_cnt & 0xFF;
84  crc16 = calc_crc16_(address, 6);
85  address[5] = crc16 & 0xFF;
86  address[6] = (crc16 >> 8) & 0xFF;
87  this->write(address, 7, true);
88 }
89 
91  uint16_t crc16_check = 0;
92  uint8_t i2c_response[8];
93  this->read(i2c_response, 8);
94  crc16_check = (i2c_response[7] << 8) + i2c_response[6];
95  if (crc16_check != calc_crc16_(i2c_response, 7)) {
96  this->error_code_ = CRC_CHECK_FAILED;
97  this->status_set_warning();
98  return 0;
99  }
100  uint32_t x = encode_uint32(i2c_response[4], i2c_response[5], i2c_response[2], i2c_response[3]);
101  float value;
102  memcpy(&value, &x, sizeof(value)); // convert uin32_t IEEE-754 format to float
103  return value;
104 }
105 
106 uint16_t EE895Component::calc_crc16_(const uint8_t buf[], uint8_t len) {
107  uint8_t crc_check_buf[22];
108  for (int i = 0; i < len; i++) {
109  crc_check_buf[i + 1] = buf[i];
110  }
111  crc_check_buf[0] = this->address_;
112  return crc16(crc_check_buf, len);
113 }
114 } // namespace ee895
115 } // namespace esphome
sensor::Sensor * temperature_sensor_
Definition: ee895.h:27
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
Definition: helpers.cpp:347
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
uint8_t pressure
Definition: tt21100.cpp:19
uint16_t x
Definition: tt21100.cpp:17
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
float get_setup_priority() const override
Definition: ee895.cpp:53
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition: helpers.h:186
sensor::Sensor * co2_sensor_
Definition: ee895.h:26
void dump_config() override
Definition: ee895.cpp:33
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
Definition: helpers.cpp:110
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
enum esphome::ee895::EE895Component::ErrorCode NONE
void update() override
Definition: ee895.cpp:55
char serial_number[10]
Definition: sun_gtil2.cpp:29
void setup() override
Definition: ee895.cpp:17
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t calc_crc16_(const uint8_t buf[], uint8_t len)
Definition: ee895.cpp:106
uint16_t temperature
Definition: sun_gtil2.cpp:26
sensor::Sensor * pressure_sensor_
Definition: ee895.h:28
uint8_t address_
store the address of the device on the bus
Definition: i2c.h:269
std::string size_t len
Definition: helpers.h:292
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
void write_command_(uint16_t addr, uint16_t reg_cnt)
Definition: ee895.cpp:76