ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
daly_bms.cpp
Go to the documentation of this file.
1#include "daly_bms.h"
2#include <vector>
5#include "esphome/core/log.h"
6
8
9ESPHOME_LOG_TAG(TAG, "daly_bms");
10
11static const uint8_t DALY_FRAME_SIZE = 13;
12static const uint8_t DALY_TEMPERATURE_OFFSET = 40;
13static const uint16_t DALY_CURRENT_OFFSET = 30000;
14
15static const uint8_t DALY_REQUEST_BATTERY_LEVEL = 0x90;
16static const uint8_t DALY_REQUEST_MIN_MAX_VOLTAGE = 0x91;
17static const uint8_t DALY_REQUEST_MIN_MAX_TEMPERATURE = 0x92;
18static const uint8_t DALY_REQUEST_MOS = 0x93;
19static const uint8_t DALY_REQUEST_STATUS = 0x94;
20static const uint8_t DALY_REQUEST_CELL_VOLTAGE = 0x95;
21static const uint8_t DALY_REQUEST_TEMPERATURE = 0x96;
22
23void DalyBmsComponent::setup() { this->next_request_ = 1; }
24
25void DalyBmsComponent::dump_config() { ESP_LOGCONFIG(TAG, "Daly BMS:"); }
26
28 this->trigger_next_ = true;
29 this->next_request_ = 0;
30}
31
34 if (this->receiving_ && (now - this->last_transmission_ >= 200)) {
35 // last transmission too long ago. Reset RX index.
36 ESP_LOGW(TAG, "Last transmission too long ago. Reset RX index.");
37 this->data_.clear();
38 this->receiving_ = false;
39 }
40 if ((now - this->last_transmission_ >= 250) && !this->trigger_next_) {
41 // last transmittion longer than 0.25s ago -> trigger next request
42 this->last_transmission_ = now;
43 this->trigger_next_ = true;
44 }
45 const size_t avail = this->available();
46 if (avail > 0)
47 this->last_transmission_ = now;
48 this->read_available(
49 [this](uint8_t c) {
50 if (!this->receiving_) {
51 if (c != 0xa5)
52 return;
53 this->receiving_ = true;
54 }
55 this->data_.push_back(c);
56 if (this->data_.size() == 4)
57 this->data_count_ = c;
58 if ((this->data_.size() > 4) and (data_.size() == this->data_count_ + 5)) {
59 this->decode_data_(this->data_);
60 this->data_.clear();
61 this->receiving_ = false;
62 }
63 },
64 avail);
65
66 if (this->trigger_next_) {
67 this->trigger_next_ = false;
68 switch (this->next_request_) {
69 case 0:
70 this->request_data_(DALY_REQUEST_BATTERY_LEVEL);
71 this->next_request_ = 1;
72 break;
73 case 1:
74 this->request_data_(DALY_REQUEST_MIN_MAX_VOLTAGE);
75 this->next_request_ = 2;
76 break;
77 case 2:
78 this->request_data_(DALY_REQUEST_MIN_MAX_TEMPERATURE);
79 this->next_request_ = 3;
80 break;
81 case 3:
82 this->request_data_(DALY_REQUEST_MOS);
83 this->next_request_ = 4;
84 break;
85 case 4:
86 this->request_data_(DALY_REQUEST_STATUS);
87 this->next_request_ = 5;
88 break;
89 case 5:
90 this->request_data_(DALY_REQUEST_CELL_VOLTAGE);
91 this->next_request_ = 6;
92 break;
93 case 6:
94 this->request_data_(DALY_REQUEST_TEMPERATURE);
95 this->next_request_ = 7;
96 break;
97 case 7:
98 default:
99 break;
100 }
101 }
102}
103
104void DalyBmsComponent::request_data_(uint8_t data_id) {
105 uint8_t request_message[DALY_FRAME_SIZE];
106
107 request_message[0] = 0xA5; // Start Flag
108 request_message[1] = this->addr_; // Communication Module Address
109 request_message[2] = data_id; // Data ID
110 request_message[3] = 0x08; // Data Length (Fixed)
111 request_message[4] = 0x00; // Empty Data
112 request_message[5] = 0x00; // |
113 request_message[6] = 0x00; // |
114 request_message[7] = 0x00; // |
115 request_message[8] = 0x00; // |
116 request_message[9] = 0x00; // |
117 request_message[10] = 0x00; // |
118 request_message[11] = 0x00; // Empty Data
119
120 request_message[12] = (uint8_t) (request_message[0] + request_message[1] + request_message[2] +
121 request_message[3]); // Checksum (Lower byte of the other bytes sum)
122
123 ESP_LOGV(TAG, "Request datapacket Nr %x", data_id);
124 this->write_array(request_message, sizeof(request_message));
125 this->flush();
126}
127
128void DalyBmsComponent::decode_data_(std::vector<uint8_t> data) {
129 auto it = data.begin();
130
131 while ((it = std::find(it, data.end(), 0xA5)) != data.end()) {
132 if (data.end() - it >= DALY_FRAME_SIZE && it[1] == 0x01) {
133 const uint8_t checksum = sum_bytes(&*it, 12);
134
135 if (checksum == it[12]) {
136 switch (it[2]) {
137#ifdef USE_SENSOR
138 case DALY_REQUEST_BATTERY_LEVEL:
139 PUBLISH_STATE(this->voltage_sensor_, (float) encode_uint16(it[4], it[5]) / 10);
140 PUBLISH_STATE(this->current_sensor_, (float) (encode_uint16(it[8], it[9]) - DALY_CURRENT_OFFSET) / 10);
141 PUBLISH_STATE(this->battery_level_sensor_, (float) encode_uint16(it[10], it[11]) / 10);
142 break;
143
144 case DALY_REQUEST_MIN_MAX_VOLTAGE:
145 PUBLISH_STATE(this->max_cell_voltage_sensor_, (float) encode_uint16(it[4], it[5]) / 1000);
146 PUBLISH_STATE(this->max_cell_voltage_number_sensor_, it[6]);
147 PUBLISH_STATE(this->min_cell_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
148 PUBLISH_STATE(this->min_cell_voltage_number_sensor_, it[9]);
149 break;
150
151 case DALY_REQUEST_MIN_MAX_TEMPERATURE:
152 PUBLISH_STATE(this->max_temperature_sensor_, it[4] - DALY_TEMPERATURE_OFFSET);
153 PUBLISH_STATE(this->max_temperature_probe_number_sensor_, it[5]);
154 PUBLISH_STATE(this->min_temperature_sensor_, it[6] - DALY_TEMPERATURE_OFFSET);
155 PUBLISH_STATE(this->min_temperature_probe_number_sensor_, it[7]);
156 break;
157#endif
158 case DALY_REQUEST_MOS:
159#ifdef USE_TEXT_SENSOR
160 if (this->status_text_sensor_ != nullptr) {
161 switch (it[4]) {
162 case 0:
163 this->status_text_sensor_->publish_state("Stationary");
164 break;
165 case 1:
166 this->status_text_sensor_->publish_state("Charging");
167 break;
168 case 2:
169 this->status_text_sensor_->publish_state("Discharging");
170 break;
171 default:
172 break;
173 }
174 }
175#endif
176#ifdef USE_BINARY_SENSOR
177 PUBLISH_STATE(this->charging_mos_enabled_binary_sensor_, it[5]);
178 PUBLISH_STATE(this->discharging_mos_enabled_binary_sensor_, it[6]);
179#endif
180#ifdef USE_SENSOR
181 PUBLISH_STATE(this->remaining_capacity_sensor_, (float) encode_uint32(it[8], it[9], it[10], it[11]) / 1000);
182#endif
183 break;
184
185#ifdef USE_SENSOR
186 case DALY_REQUEST_STATUS:
187 PUBLISH_STATE(this->cells_number_sensor_, it[4]);
188 break;
189
190 case DALY_REQUEST_TEMPERATURE:
191 if (it[4] == 1) {
192 PUBLISH_STATE(this->temperature_1_sensor_, it[5] - DALY_TEMPERATURE_OFFSET);
193 PUBLISH_STATE(this->temperature_2_sensor_, it[6] - DALY_TEMPERATURE_OFFSET);
194 }
195 break;
196
197 case DALY_REQUEST_CELL_VOLTAGE:
198 switch (it[4]) {
199 case 1:
200 PUBLISH_STATE(this->cell_1_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
201 PUBLISH_STATE(this->cell_2_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
202 PUBLISH_STATE(this->cell_3_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
203 break;
204 case 2:
205 PUBLISH_STATE(this->cell_4_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
206 PUBLISH_STATE(this->cell_5_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
207 PUBLISH_STATE(this->cell_6_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
208 break;
209 case 3:
210 PUBLISH_STATE(this->cell_7_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
211 PUBLISH_STATE(this->cell_8_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
212 PUBLISH_STATE(this->cell_9_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
213 break;
214 case 4:
215 PUBLISH_STATE(this->cell_10_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
216 PUBLISH_STATE(this->cell_11_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
217 PUBLISH_STATE(this->cell_12_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
218 break;
219 case 5:
220 PUBLISH_STATE(this->cell_13_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
221 PUBLISH_STATE(this->cell_14_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
222 PUBLISH_STATE(this->cell_15_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
223 break;
224 case 6:
225 PUBLISH_STATE(this->cell_16_voltage_sensor_, (float) encode_uint16(it[5], it[6]) / 1000);
226 PUBLISH_STATE(this->cell_17_voltage_sensor_, (float) encode_uint16(it[7], it[8]) / 1000);
227 PUBLISH_STATE(this->cell_18_voltage_sensor_, (float) encode_uint16(it[9], it[10]) / 1000);
228 break;
229 }
230 break;
231#endif
232 default:
233 break;
234 }
235 } else {
236 ESP_LOGW(TAG, "Checksum-Error on Packet %x", it[4]);
237 }
238 std::advance(it, DALY_FRAME_SIZE);
239 } else {
240 std::advance(it, 1);
241 }
242 }
243}
244
245} // namespace esphome::daly_bms
uint8_t checksum
Definition bl0906.h:3
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
std::vector< uint8_t > data_
Definition daly_bms.h:82
void decode_data_(std::vector< uint8_t > data)
Definition daly_bms.cpp:128
void request_data_(uint8_t data_id)
Definition daly_bms.cpp:104
UARTFlushResult flush()
Definition uart.h:96
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
ESPHOME_LOG_TAG(TAG, "daly_bms")
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
Definition helpers.h:939
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
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t