ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
bl0939.cpp
Go to the documentation of this file.
1#include "bl0939.h"
2#include "esphome/core/log.h"
3#include <cinttypes>
4
5namespace esphome::bl0939 {
6
7static const char *const TAG = "bl0939";
8
9// https://www.belling.com.cn/media/file_object/bel_product/BL0939/datasheet/BL0939_V1.2_cn.pdf
10// (unfortunately chinese, but the protocol can be understood with some translation tool)
11static const uint8_t BL0939_READ_COMMAND = 0x55; // 0x5{A4,A3,A2,A1}
12static const uint8_t BL0939_FULL_PACKET = 0xAA;
13static const uint8_t BL0939_PACKET_HEADER = 0x55;
14
15static const uint8_t BL0939_WRITE_COMMAND = 0xA5; // 0xA{A4,A3,A2,A1}
16static const uint8_t BL0939_REG_IA_FAST_RMS_CTRL = 0x10;
17static const uint8_t BL0939_REG_IB_FAST_RMS_CTRL = 0x1E;
18static const uint8_t BL0939_REG_MODE = 0x18;
19static const uint8_t BL0939_REG_SOFT_RESET = 0x19;
20static const uint8_t BL0939_REG_USR_WRPROT = 0x1A;
21static const uint8_t BL0939_REG_TPS_CTRL = 0x1B;
22
23const uint8_t BL0939_INIT[6][6] = {
24 // Reset to default
25 {BL0939_WRITE_COMMAND, BL0939_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x33},
26 // Enable User Operation Write
27 {BL0939_WRITE_COMMAND, BL0939_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xEB},
28 // 0x0100 = CF_UNABLE energy pulse, AC_FREQ_SEL 50Hz, RMS_UPDATE_SEL 800mS
29 {BL0939_WRITE_COMMAND, BL0939_REG_MODE, 0x00, 0x10, 0x00, 0x32},
30 // 0x47FF = Over-current and leakage alarm on, Automatic temperature measurement, Interval 100mS
31 {BL0939_WRITE_COMMAND, BL0939_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xF9},
32 // 0x181C = Half cycle, Fast RMS threshold 6172
33 {BL0939_WRITE_COMMAND, BL0939_REG_IA_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x16},
34 // 0x181C = Half cycle, Fast RMS threshold 6172
35 {BL0939_WRITE_COMMAND, BL0939_REG_IB_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x08}};
36
38 DataPacket buffer;
39 if (!this->available()) {
40 return;
41 }
42 if (read_array((uint8_t *) &buffer, sizeof(buffer))) {
43 if (validate_checksum(&buffer)) {
44 received_package_(&buffer);
45 }
46 } else {
47 ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
48 while (read() >= 0)
49 ;
50 }
51}
52
54 uint8_t checksum = BL0939_READ_COMMAND;
55 // Whole package but checksum
56 for (uint32_t i = 0; i < sizeof(data->raw) - 1; i++) {
57 checksum += data->raw[i];
58 }
59 checksum ^= 0xFF;
60 if (checksum != data->checksum) {
61 ESP_LOGW(TAG, "BL0939 invalid checksum! 0x%02X != 0x%02X", checksum, data->checksum);
62 }
63 return checksum == data->checksum;
64}
65
67 this->flush();
68 this->write_byte(BL0939_READ_COMMAND);
69 this->write_byte(BL0939_FULL_PACKET);
70}
71
73 for (auto *i : BL0939_INIT) {
74 this->write_array(i, 6);
75 delay(1);
76 }
77 this->flush();
78}
79
80void BL0939::received_package_(const DataPacket *data) const {
81 // Bad header
82 if (data->frame_header != BL0939_PACKET_HEADER) {
83 ESP_LOGI(TAG, "Invalid data. Header mismatch: %d", data->frame_header);
84 return;
85 }
86
87 float v_rms = (float) to_uint32_t(data->v_rms) / voltage_reference_;
88 float ia_rms = (float) to_uint32_t(data->ia_rms) / current_reference_;
89 float ib_rms = (float) to_uint32_t(data->ib_rms) / current_reference_;
90 float a_watt = (float) to_int32_t(data->a_watt) / power_reference_;
91 float b_watt = (float) to_int32_t(data->b_watt) / power_reference_;
92 int32_t cfa_cnt = to_int32_t(data->cfa_cnt);
93 int32_t cfb_cnt = to_int32_t(data->cfb_cnt);
94 float a_energy_consumption = (float) cfa_cnt / energy_reference_;
95 float b_energy_consumption = (float) cfb_cnt / energy_reference_;
96 float total_energy_consumption = a_energy_consumption + b_energy_consumption;
97
98 if (voltage_sensor_ != nullptr) {
100 }
101 if (current_sensor_1_ != nullptr) {
103 }
104 if (current_sensor_2_ != nullptr) {
106 }
107 if (power_sensor_1_ != nullptr) {
109 }
110 if (power_sensor_2_ != nullptr) {
112 }
113 if (energy_sensor_1_ != nullptr) {
114 energy_sensor_1_->publish_state(a_energy_consumption);
115 }
116 if (energy_sensor_2_ != nullptr) {
117 energy_sensor_2_->publish_state(b_energy_consumption);
118 }
119 if (energy_sensor_sum_ != nullptr) {
120 energy_sensor_sum_->publish_state(total_energy_consumption);
121 }
122
123 ESP_LOGV(TAG,
124 "BL0939: U %fV, I1 %fA, I2 %fA, P1 %fW, P2 %fW, CntA %" PRId32 ", CntB %" PRId32 ", ∫P1 %fkWh, ∫P2 %fkWh",
125 v_rms, ia_rms, ib_rms, a_watt, b_watt, cfa_cnt, cfb_cnt, a_energy_consumption, b_energy_consumption);
126}
127
128void BL0939::dump_config() { // NOLINT(readability-function-cognitive-complexity)
129 ESP_LOGCONFIG(TAG, "BL0939:");
130 LOG_SENSOR("", "Voltage", this->voltage_sensor_);
131 LOG_SENSOR("", "Current 1", this->current_sensor_1_);
132 LOG_SENSOR("", "Current 2", this->current_sensor_2_);
133 LOG_SENSOR("", "Power 1", this->power_sensor_1_);
134 LOG_SENSOR("", "Power 2", this->power_sensor_2_);
135 LOG_SENSOR("", "Energy 1", this->energy_sensor_1_);
136 LOG_SENSOR("", "Energy 2", this->energy_sensor_2_);
137 LOG_SENSOR("", "Energy sum", this->energy_sensor_sum_);
138}
139
140uint32_t BL0939::to_uint32_t(ube24_t input) { return input.h << 16 | input.m << 8 | input.l; }
141
142int32_t BL0939::to_int32_t(sbe24_t input) { return input.h << 16 | input.m << 8 | input.l; }
143
144} // namespace esphome::bl0939
uint8_t checksum
Definition bl0906.h:3
sbe24_t cfb_cnt
Definition bl0939.h:11
sbe24_t a_watt
Definition bl0939.h:8
sbe24_t cfa_cnt
Definition bl0939.h:10
ube24_t ia_rms
Definition bl0939.h:4
sbe24_t b_watt
Definition bl0939.h:9
ube24_t v_rms
Definition bl0939.h:6
ube24_t ib_rms
Definition bl0939.h:5
void update() override
Definition bl0939.cpp:66
static int32_t to_int32_t(sbe24_t input)
Definition bl0939.cpp:142
void setup() override
Definition bl0939.cpp:72
void loop() override
Definition bl0939.cpp:37
sensor::Sensor * energy_sensor_sum_
Definition bl0939.h:86
sensor::Sensor * voltage_sensor_
Definition bl0939.h:77
static bool validate_checksum(const DataPacket *data)
Definition bl0939.cpp:53
sensor::Sensor * current_sensor_2_
Definition bl0939.h:79
sensor::Sensor * energy_sensor_2_
Definition bl0939.h:85
sensor::Sensor * power_sensor_1_
Definition bl0939.h:82
static uint32_t to_uint32_t(ube24_t input)
Definition bl0939.cpp:140
void received_package_(const DataPacket *data) const
Definition bl0939.cpp:80
sensor::Sensor * energy_sensor_1_
Definition bl0939.h:84
void dump_config() override
Definition bl0939.cpp:128
sensor::Sensor * power_sensor_2_
Definition bl0939.h:83
sensor::Sensor * current_sensor_1_
Definition bl0939.h:78
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
UARTFlushResult flush()
Definition uart.h:48
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
void write_byte(uint8_t data)
Definition uart.h:18
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
const uint8_t BL0939_INIT[6][6]
Definition bl0939.cpp:23
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t