ESPHome 2026.3.2
Loading...
Searching...
No Matches
bl0906.cpp
Go to the documentation of this file.
1#include "bl0906.h"
2#include "constants.h"
3
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace bl0906 {
8
9static const char *const TAG = "bl0906";
10
11constexpr uint32_t to_uint32_t(ube24_t input) { return input.h << 16 | input.m << 8 | input.l; }
12
13constexpr int32_t to_int32_t(sbe24_t input) {
14 return static_cast<int32_t>(encode_uint32((uint8_t) input.h, input.m, input.l, 0)) >> 8;
15}
16
17// The SUM byte is (Addr+Data_L+Data_M+Data_H)&0xFF negated;
18constexpr uint8_t bl0906_checksum(const uint8_t address, const DataPacket *data) {
19 return (address + data->l + data->m + data->h) ^ 0xFF;
20}
21
22void BL0906::loop() {
23 if (this->current_channel_ == UINT8_MAX) {
24 return;
25 }
26
27 while (this->available())
28 this->flush();
29
30 if (this->current_channel_ == 0) {
31 // Temperature
32 this->read_data_(BL0906_TEMPERATURE, BL0906_TREF, this->temperature_sensor_);
33 } else if (this->current_channel_ == 1) {
34 this->read_data_(BL0906_I_1_RMS, BL0906_IREF, this->current_1_sensor_);
35 this->read_data_(BL0906_WATT_1, BL0906_PREF, this->power_1_sensor_);
36 this->read_data_(BL0906_CF_1_CNT, BL0906_EREF, this->energy_1_sensor_);
37 } else if (this->current_channel_ == 2) {
38 this->read_data_(BL0906_I_2_RMS, BL0906_IREF, this->current_2_sensor_);
39 this->read_data_(BL0906_WATT_2, BL0906_PREF, this->power_2_sensor_);
40 this->read_data_(BL0906_CF_2_CNT, BL0906_EREF, this->energy_2_sensor_);
41 } else if (this->current_channel_ == 3) {
42 this->read_data_(BL0906_I_3_RMS, BL0906_IREF, this->current_3_sensor_);
43 this->read_data_(BL0906_WATT_3, BL0906_PREF, this->power_3_sensor_);
44 this->read_data_(BL0906_CF_3_CNT, BL0906_EREF, this->energy_3_sensor_);
45 } else if (this->current_channel_ == 4) {
46 this->read_data_(BL0906_I_4_RMS, BL0906_IREF, this->current_4_sensor_);
47 this->read_data_(BL0906_WATT_4, BL0906_PREF, this->power_4_sensor_);
48 this->read_data_(BL0906_CF_4_CNT, BL0906_EREF, this->energy_4_sensor_);
49 } else if (this->current_channel_ == 5) {
50 this->read_data_(BL0906_I_5_RMS, BL0906_IREF, this->current_5_sensor_);
51 this->read_data_(BL0906_WATT_5, BL0906_PREF, this->power_5_sensor_);
52 this->read_data_(BL0906_CF_5_CNT, BL0906_EREF, this->energy_5_sensor_);
53 } else if (this->current_channel_ == 6) {
54 this->read_data_(BL0906_I_6_RMS, BL0906_IREF, this->current_6_sensor_);
55 this->read_data_(BL0906_WATT_6, BL0906_PREF, this->power_6_sensor_);
56 this->read_data_(BL0906_CF_6_CNT, BL0906_EREF, this->energy_6_sensor_);
57 } else if (this->current_channel_ == UINT8_MAX - 2) {
58 // Frequency
59 this->read_data_(BL0906_FREQUENCY, BL0906_FREF, frequency_sensor_);
60 // Voltage
61 this->read_data_(BL0906_V_RMS, BL0906_UREF, voltage_sensor_);
62 } else if (this->current_channel_ == UINT8_MAX - 1) {
63 // Total power
64 this->read_data_(BL0906_WATT_SUM, BL0906_WATT, this->total_power_sensor_);
65 // Total Energy
66 this->read_data_(BL0906_CF_SUM_CNT, BL0906_CF, this->total_energy_sensor_);
67 } else {
68 this->current_channel_ = UINT8_MAX - 2; // Go to frequency and voltage
69 return;
70 }
71 this->current_channel_++;
72 this->handle_actions_();
73}
74
75void BL0906::setup() {
76 while (this->available())
77 this->flush();
78 this->write_array(USR_WRPROT_WITABLE, sizeof(USR_WRPROT_WITABLE));
79 // Calibration (1: register address; 2: value before calibration; 3: value after calibration)
80 this->bias_correction_(BL0906_RMSOS_1, 0.01600, 0); // Calibration current_1
81 this->bias_correction_(BL0906_RMSOS_2, 0.01500, 0);
82 this->bias_correction_(BL0906_RMSOS_3, 0.01400, 0);
83 this->bias_correction_(BL0906_RMSOS_4, 0.01300, 0);
84 this->bias_correction_(BL0906_RMSOS_5, 0.01200, 0);
85 this->bias_correction_(BL0906_RMSOS_6, 0.01200, 0); // Calibration current_6
86
87 this->write_array(USR_WRPROT_ONLYREAD, sizeof(USR_WRPROT_ONLYREAD));
88}
89
90void BL0906::update() { this->current_channel_ = 0; }
91
93 this->action_queue_.push_back(function);
94 return this->action_queue_.size();
95}
96
98 if (this->action_queue_.empty()) {
99 return;
100 }
101 ActionCallbackFuncPtr ptr_func = nullptr;
102 for (size_t i = 0; i < this->action_queue_.size(); i++) {
103 ptr_func = this->action_queue_[i];
104 if (ptr_func) {
105 ESP_LOGI(TAG, "HandleActionCallback[%zu]", i);
106 (this->*ptr_func)();
107 }
108 }
109
110 while (this->available()) {
111 this->read();
112 }
113
114 this->action_queue_.clear();
115}
116
117// Reset energy
119 this->write_array(BL0906_INIT[0], 6);
120 delay(1);
121 this->flush();
122
123 ESP_LOGW(TAG, "RMSOS:%02X%02X%02X%02X%02X%02X", BL0906_INIT[0][0], BL0906_INIT[0][1], BL0906_INIT[0][2],
124 BL0906_INIT[0][3], BL0906_INIT[0][4], BL0906_INIT[0][5]);
125}
126
127// Read data
128void BL0906::read_data_(const uint8_t address, const float reference, sensor::Sensor *sensor) {
129 if (sensor == nullptr) {
130 return;
131 }
132 DataPacket buffer;
133 ube24_t data_u24;
134 sbe24_t data_s24;
135 float value = 0;
136
137 bool signed_result = reference == BL0906_TREF || reference == BL0906_WATT || reference == BL0906_PREF;
138
139 this->write_byte(BL0906_READ_COMMAND);
140 this->write_byte(address);
141 if (!this->read_array((uint8_t *) &buffer, sizeof(buffer) - 1)) {
142 ESP_LOGW(TAG, "Read failed");
143 return;
144 }
145 if (bl0906_checksum(address, &buffer) != buffer.checksum) {
146 ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
147 while (read() >= 0)
148 ;
149 return;
150 }
151 if (signed_result) {
152 data_s24.l = buffer.l;
153 data_s24.m = buffer.m;
154 data_s24.h = buffer.h;
155 } else {
156 data_u24.l = buffer.l;
157 data_u24.m = buffer.m;
158 data_u24.h = buffer.h;
159 }
160 // Power
161 if (reference == BL0906_PREF) {
162 value = (float) to_int32_t(data_s24) * reference;
163 }
164
165 // Total power
166 if (reference == BL0906_WATT) {
167 value = (float) to_int32_t(data_s24) * reference;
168 }
169
170 // Voltage, current, power, total power
171 if (reference == BL0906_UREF || reference == BL0906_IREF || reference == BL0906_EREF || reference == BL0906_CF) {
172 value = (float) to_uint32_t(data_u24) * reference;
173 }
174
175 // Frequency
176 if (reference == BL0906_FREF) {
177 value = reference / (float) to_uint32_t(data_u24);
178 }
179 // Chip temperature
180 if (reference == BL0906_TREF) {
181 value = (float) to_int32_t(data_s24);
182 value = (value - 64) * 12.5 / 59 - 40;
183 }
184 sensor->publish_state(value);
185}
186
187// RMS offset correction
188void BL0906::bias_correction_(uint8_t address, float measurements, float correction) {
189 DataPacket data;
190 float ki = 12875 * 1 * (5.1 + 5.1) * 1000 / 2000 / 1.097; // Current coefficient
191 float i_rms0 = measurements * ki;
192 float i_rms = correction * ki;
193 int32_t value = (i_rms * i_rms - i_rms0 * i_rms0) / 256;
194 data.l = value & 0xFF;
195 data.m = (value >> 8) & 0xFF;
196 data.h = (value >> 16) & 0xFF;
197 data.address = bl0906_checksum(address, &data);
198 ESP_LOGV(TAG, "RMSOS:%02X%02X%02X%02X%02X%02X", BL0906_WRITE_COMMAND, address, data.l, data.m, data.h, data.address);
199 this->write_byte(BL0906_WRITE_COMMAND);
200 this->write_byte(address);
201 this->write_byte(data.l);
202 this->write_byte(data.m);
203 this->write_byte(data.h);
204 this->write_byte(data.address);
205}
206
207void BL0906::dump_config() {
208 ESP_LOGCONFIG(TAG, "BL0906:");
209 LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
210
211 LOG_SENSOR(" ", "Current1", this->current_1_sensor_);
212 LOG_SENSOR(" ", "Current2", this->current_2_sensor_);
213 LOG_SENSOR(" ", "Current3", this->current_3_sensor_);
214 LOG_SENSOR(" ", "Current4", this->current_4_sensor_);
215 LOG_SENSOR(" ", "Current5", this->current_5_sensor_);
216 LOG_SENSOR(" ", "Current6", this->current_6_sensor_);
217
218 LOG_SENSOR(" ", "Power1", this->power_1_sensor_);
219 LOG_SENSOR(" ", "Power2", this->power_2_sensor_);
220 LOG_SENSOR(" ", "Power3", this->power_3_sensor_);
221 LOG_SENSOR(" ", "Power4", this->power_4_sensor_);
222 LOG_SENSOR(" ", "Power5", this->power_5_sensor_);
223 LOG_SENSOR(" ", "Power6", this->power_6_sensor_);
224
225 LOG_SENSOR(" ", "Energy1", this->energy_1_sensor_);
226 LOG_SENSOR(" ", "Energy2", this->energy_2_sensor_);
227 LOG_SENSOR(" ", "Energy3", this->energy_3_sensor_);
228 LOG_SENSOR(" ", "Energy4", this->energy_4_sensor_);
229 LOG_SENSOR(" ", "Energy5", this->energy_5_sensor_);
230 LOG_SENSOR(" ", "Energy6", this->energy_6_sensor_);
231
232 LOG_SENSOR(" ", "Total Power", this->total_power_sensor_);
233 LOG_SENSOR(" ", "Total Energy", this->total_energy_sensor_);
234 LOG_SENSOR(" ", "Frequency", this->frequency_sensor_);
235 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
236}
237
238} // namespace bl0906
239} // namespace esphome
uint8_t address
Definition bl0906.h:4
uint24_le_t i_rms
Definition bl0940.h:2
virtual void loop()
This method will be called repeatedly.
Definition component.cpp:96
uint8_t current_channel_
Definition bl0906.h:82
void read_data_(uint8_t address, float reference, sensor::Sensor *sensor)
Definition bl0906.cpp:128
void bias_correction_(uint8_t address, float measurements, float correction)
Definition bl0906.cpp:188
size_t enqueue_action_(ActionCallbackFuncPtr function)
Definition bl0906.cpp:92
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
FlushResult flush()
Definition uart.h:48
void write_byte(uint8_t data)
Definition uart.h:18
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
constexpr uint8_t bl0906_checksum(const uint8_t address, const DataPacket *data)
Definition bl0906.cpp:18
void(BL0906::*)() ActionCallbackFuncPtr
Definition bl0906.h:39
const uint8_t BL0906_INIT[2][6]
Definition constants.h:115
constexpr int32_t to_int32_t(sbe24_t input)
Definition bl0906.cpp:13
constexpr uint32_t to_uint32_t(ube24_t input)
Definition bl0906.cpp:11
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:736
void HOT delay(uint32_t ms)
Definition core.cpp:28
static void uint32_t