ESPHome 2026.11.0b6
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::ee895 {
6
7ESPHOME_LOG_TAG(TAG, "ee895");
8
9// Serial number is 16 bytes
10static constexpr size_t EE895_SERIAL_NUMBER_SIZE = 16;
11
12static const uint16_t CRC16_ONEWIRE_START = 0xFFFF;
13static const uint8_t FUNCTION_CODE_READ = 0x03;
14static const uint16_t SERIAL_NUMBER = 0x0000;
15static const uint16_t TEMPERATURE_ADDRESS = 0x03EA;
16static const uint16_t CO2_ADDRESS = 0x0424;
17static const uint16_t PRESSURE_ADDRESS = 0x04B0;
18
20 uint16_t crc16_check = 0;
21 write_command_(SERIAL_NUMBER, 8);
22 uint8_t serial_number[20];
23 this->read(serial_number, 20);
24
25 crc16_check = (serial_number[19] << 8) + serial_number[18];
26 if (crc16_check != calc_crc16_(serial_number, 18)) {
27 this->mark_failed(LOG_STR("CRC check failed"));
28 return;
29 }
30#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
31 char serial_hex[format_hex_size(EE895_SERIAL_NUMBER_SIZE)];
32#endif
33 ESP_LOGV(TAG, " Serial Number: 0x%s", format_hex_to(serial_hex, serial_number + 2, EE895_SERIAL_NUMBER_SIZE));
34}
35
36void EE895Component::dump_config() {
37 ESP_LOGCONFIG(TAG, "EE895:");
38 LOG_I2C_DEVICE(this);
39 LOG_UPDATE_INTERVAL(this);
40 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
41 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
42 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
43}
44
45void EE895Component::update() {
46 write_command_(TEMPERATURE_ADDRESS, 2);
47 this->set_timeout(50, [this]() {
48 float temperature = read_float_();
49
50 write_command_(CO2_ADDRESS, 2);
51 float co2 = read_float_();
52
53 write_command_(PRESSURE_ADDRESS, 2);
54 float pressure = read_float_();
55 ESP_LOGD(TAG, "Got temperature=%.1f°C co2=%.0fppm pressure=%.1f%mbar", temperature, co2, pressure);
56 PUBLISH_STATE(this->temperature_sensor_, temperature);
57 PUBLISH_STATE(this->co2_sensor_, co2);
58 PUBLISH_STATE(this->pressure_sensor_, pressure);
60 });
61}
62
63void EE895Component::write_command_(uint16_t addr, uint16_t reg_cnt) {
64 uint8_t address[7];
65 uint16_t crc16 = 0;
66 address[0] = FUNCTION_CODE_READ;
67 address[1] = (addr >> 8) & 0xFF;
68 address[2] = addr & 0xFF;
69 address[3] = (reg_cnt >> 8) & 0xFF;
70 address[4] = reg_cnt & 0xFF;
72 address[5] = crc16 & 0xFF;
73 address[6] = (crc16 >> 8) & 0xFF;
74 this->write(address, 7);
75}
76
78 uint16_t crc16_check = 0;
79 uint8_t i2c_response[8];
80 this->read(i2c_response, 8);
81 crc16_check = (i2c_response[7] << 8) + i2c_response[6];
82 if (crc16_check != calc_crc16_(i2c_response, 6)) {
83 this->status_set_warning(LOG_STR("CRC check failed"));
84 return 0;
85 }
86 uint32_t x = encode_uint32(i2c_response[4], i2c_response[5], i2c_response[2], i2c_response[3]);
87 float value;
88 memcpy(&value, &x, sizeof(value)); // convert uin32_t IEEE-754 format to float
89 return value;
90}
91
92uint16_t EE895Component::calc_crc16_(const uint8_t buf[], uint8_t len) {
93 uint8_t addr = this->address_;
94 uint16_t crc = crc16(&addr, 1);
95 return crc16(buf, len, crc);
96}
97} // namespace esphome::ee895
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:309
void write_command_(uint16_t addr, uint16_t reg_cnt)
Definition ee895.cpp:63
uint16_t calc_crc16_(const uint8_t buf[], uint8_t len)
Definition ee895.cpp:92
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
uint8_t address_
store the address of the device on the bus
Definition i2c.h:327
ESPHOME_LOG_TAG(TAG, "ee895")
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:95
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
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:1661
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:1051
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:428
static void uint32_t
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