ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
ee895.cpp
Go to the documentation of this file.
1#include "ee895.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace ee895 {
7
8static const char *const TAG = "ee895";
9
10// Serial number is 16 bytes
11static constexpr size_t EE895_SERIAL_NUMBER_SIZE = 16;
12
13static const uint16_t CRC16_ONEWIRE_START = 0xFFFF;
14static const uint8_t FUNCTION_CODE_READ = 0x03;
15static const uint16_t SERIAL_NUMBER = 0x0000;
16static const uint16_t TEMPERATURE_ADDRESS = 0x03EA;
17static const uint16_t CO2_ADDRESS = 0x0424;
18static const uint16_t PRESSURE_ADDRESS = 0x04B0;
19
21 uint16_t crc16_check = 0;
22 write_command_(SERIAL_NUMBER, 8);
23 uint8_t serial_number[20];
24 this->read(serial_number, 20);
25
26 crc16_check = (serial_number[19] << 8) + serial_number[18];
27 if (crc16_check != calc_crc16_(serial_number, 19)) {
28 this->error_code_ = CRC_CHECK_FAILED;
29 this->mark_failed();
30 return;
31 }
32#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
33 char serial_hex[format_hex_size(EE895_SERIAL_NUMBER_SIZE)];
34#endif
35 ESP_LOGV(TAG, " Serial Number: 0x%s", format_hex_to(serial_hex, serial_number + 2, EE895_SERIAL_NUMBER_SIZE));
36}
37
39 ESP_LOGCONFIG(TAG, "EE895:");
40 LOG_I2C_DEVICE(this);
41 switch (this->error_code_) {
43 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
44 break;
46 ESP_LOGE(TAG, "The crc check failed");
47 break;
48 case NONE:
49 default:
50 break;
51 }
52 LOG_UPDATE_INTERVAL(this);
53 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
54 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
55 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
56}
57
59
61 write_command_(TEMPERATURE_ADDRESS, 2);
62 this->set_timeout(50, [this]() {
63 float temperature = read_float_();
64
65 write_command_(CO2_ADDRESS, 2);
66 float co2 = read_float_();
67
68 write_command_(PRESSURE_ADDRESS, 2);
69 float pressure = read_float_();
70 ESP_LOGD(TAG, "Got temperature=%.1f°C co2=%.0fppm pressure=%.1f%mbar", temperature, co2, pressure);
71 if (this->temperature_sensor_ != nullptr)
72 this->temperature_sensor_->publish_state(temperature);
73 if (this->co2_sensor_ != nullptr)
74 this->co2_sensor_->publish_state(co2);
75 if (this->pressure_sensor_ != nullptr)
76 this->pressure_sensor_->publish_state(pressure);
78 });
79}
80
81void EE895Component::write_command_(uint16_t addr, uint16_t reg_cnt) {
82 uint8_t address[7];
83 uint16_t crc16 = 0;
84 address[0] = FUNCTION_CODE_READ;
85 address[1] = (addr >> 8) & 0xFF;
86 address[2] = addr & 0xFF;
87 address[3] = (reg_cnt >> 8) & 0xFF;
88 address[4] = reg_cnt & 0xFF;
90 address[5] = crc16 & 0xFF;
91 address[6] = (crc16 >> 8) & 0xFF;
92 this->write(address, 7);
93}
94
96 uint16_t crc16_check = 0;
97 uint8_t i2c_response[8];
98 this->read(i2c_response, 8);
99 crc16_check = (i2c_response[7] << 8) + i2c_response[6];
100 if (crc16_check != calc_crc16_(i2c_response, 7)) {
101 this->error_code_ = CRC_CHECK_FAILED;
102 this->status_set_warning();
103 return 0;
104 }
105 uint32_t x = encode_uint32(i2c_response[4], i2c_response[5], i2c_response[2], i2c_response[3]);
106 float value;
107 memcpy(&value, &x, sizeof(value)); // convert uin32_t IEEE-754 format to float
108 return value;
109}
110
111uint16_t EE895Component::calc_crc16_(const uint8_t buf[], uint8_t len) {
112 uint8_t crc_check_buf[22];
113 for (int i = 0; i < len; i++) {
114 crc_check_buf[i + 1] = buf[i];
115 }
116 crc_check_buf[0] = this->address_;
117 return crc16(crc_check_buf, len);
118}
119} // namespace ee895
120} // namespace esphome
uint8_t address
Definition bl0906.h:4
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void dump_config() override
Definition ee895.cpp:38
sensor::Sensor * pressure_sensor_
Definition ee895.h:28
void write_command_(uint16_t addr, uint16_t reg_cnt)
Definition ee895.cpp:81
sensor::Sensor * co2_sensor_
Definition ee895.h:26
enum esphome::ee895::EE895Component::ErrorCode NONE
sensor::Sensor * temperature_sensor_
Definition ee895.h:27
uint16_t calc_crc16_(const uint8_t buf[], uint8_t len)
Definition ee895.cpp:111
float get_setup_priority() const override
Definition ee895.cpp:58
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:184
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
uint8_t address_
store the address of the device on the bus
Definition i2c.h:302
uint8_t size_t len
Definition i2c.h:273
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:77
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:81
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:72
std::string size_t len
Definition helpers.h:533
constexpr size_t format_hex_size(size_t byte_count)
Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1.
Definition helpers.h:732
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:428
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:319
uint16_t temperature
Definition sun_gtil2.cpp:12
char serial_number[10]
Definition sun_gtil2.cpp:15
uint16_t x
Definition tt21100.cpp:5
uint8_t pressure
Definition tt21100.cpp:7