ESPHome 2026.5.0b1
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
7static const char *const 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->error_code_ = CRC_CHECK_FAILED;
28 this->mark_failed();
29 return;
30 }
31#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
32 char serial_hex[format_hex_size(EE895_SERIAL_NUMBER_SIZE)];
33#endif
34 ESP_LOGV(TAG, " Serial Number: 0x%s", format_hex_to(serial_hex, serial_number + 2, EE895_SERIAL_NUMBER_SIZE));
35}
36
38 ESP_LOGCONFIG(TAG, "EE895:");
39 LOG_I2C_DEVICE(this);
40 switch (this->error_code_) {
42 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
43 break;
45 ESP_LOGE(TAG, "The crc check failed");
46 break;
47 case NONE:
48 default:
49 break;
50 }
51 LOG_UPDATE_INTERVAL(this);
52 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
53 LOG_SENSOR(" ", "CO2", this->co2_sensor_);
54 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
55}
56
58 write_command_(TEMPERATURE_ADDRESS, 2);
59 this->set_timeout(50, [this]() {
60 float temperature = read_float_();
61
62 write_command_(CO2_ADDRESS, 2);
63 float co2 = read_float_();
64
65 write_command_(PRESSURE_ADDRESS, 2);
66 float pressure = read_float_();
67 ESP_LOGD(TAG, "Got temperature=%.1f°C co2=%.0fppm pressure=%.1f%mbar", temperature, co2, pressure);
68 if (this->temperature_sensor_ != nullptr)
69 this->temperature_sensor_->publish_state(temperature);
70 if (this->co2_sensor_ != nullptr)
71 this->co2_sensor_->publish_state(co2);
72 if (this->pressure_sensor_ != nullptr)
73 this->pressure_sensor_->publish_state(pressure);
75 });
76}
77
78void EE895Component::write_command_(uint16_t addr, uint16_t reg_cnt) {
79 uint8_t address[7];
80 uint16_t crc16 = 0;
81 address[0] = FUNCTION_CODE_READ;
82 address[1] = (addr >> 8) & 0xFF;
83 address[2] = addr & 0xFF;
84 address[3] = (reg_cnt >> 8) & 0xFF;
85 address[4] = reg_cnt & 0xFF;
87 address[5] = crc16 & 0xFF;
88 address[6] = (crc16 >> 8) & 0xFF;
89 this->write(address, 7);
90}
91
93 uint16_t crc16_check = 0;
94 uint8_t i2c_response[8];
95 this->read(i2c_response, 8);
96 crc16_check = (i2c_response[7] << 8) + i2c_response[6];
97 if (crc16_check != calc_crc16_(i2c_response, 6)) {
98 this->error_code_ = CRC_CHECK_FAILED;
99 this->status_set_warning();
100 return 0;
101 }
102 uint32_t x = encode_uint32(i2c_response[4], i2c_response[5], i2c_response[2], i2c_response[3]);
103 float value;
104 memcpy(&value, &x, sizeof(value)); // convert uin32_t IEEE-754 format to float
105 return value;
106}
107
108uint16_t EE895Component::calc_crc16_(const uint8_t buf[], uint8_t len) {
109 uint8_t addr = this->address_;
110 uint16_t crc = crc16(&addr, 1);
111 return crc16(buf, len, crc);
112}
113} // namespace esphome::ee895
uint8_t address
Definition bl0906.h:4
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
void status_clear_warning()
Definition component.h:306
void dump_config() override
Definition ee895.cpp:37
sensor::Sensor * pressure_sensor_
Definition ee895.h:26
void write_command_(uint16_t addr, uint16_t reg_cnt)
Definition ee895.cpp:78
sensor::Sensor * co2_sensor_
Definition ee895.h:24
enum esphome::ee895::EE895Component::ErrorCode NONE
sensor::Sensor * temperature_sensor_
Definition ee895.h:25
uint16_t calc_crc16_(const uint8_t buf[], uint8_t len)
Definition ee895.cpp:108
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
uint8_t address_
store the address of the device on the bus
Definition i2c.h:270
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
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:86
std::string size_t len
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:1360
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:867
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:335
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