ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bl0939.cpp
Go to the documentation of this file.
1#include "bl0939.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
5#include <cinttypes>
6
7namespace esphome::bl0939 {
8
9ESPHOME_LOG_TAG(TAG, "bl0939");
10
11// https://www.belling.com.cn/media/file_object/bel_product/BL0939/datasheet/BL0939_V1.2_cn.pdf
12// (unfortunately chinese, but the protocol can be understood with some translation tool)
13static const uint8_t BL0939_READ_COMMAND = 0x55; // 0x5{A4,A3,A2,A1}
14static const uint8_t BL0939_FULL_PACKET = 0xAA;
15static const uint8_t BL0939_PACKET_HEADER = 0x55;
16
17static const uint8_t BL0939_WRITE_COMMAND = 0xA5; // 0xA{A4,A3,A2,A1}
18static const uint8_t BL0939_REG_IA_FAST_RMS_CTRL = 0x10;
19static const uint8_t BL0939_REG_IB_FAST_RMS_CTRL = 0x1E;
20static const uint8_t BL0939_REG_MODE = 0x18;
21static const uint8_t BL0939_REG_SOFT_RESET = 0x19;
22static const uint8_t BL0939_REG_USR_WRPROT = 0x1A;
23static const uint8_t BL0939_REG_TPS_CTRL = 0x1B;
24
25static constexpr uint8_t BL0939_INIT[6][6] PROGMEM = {
26 // Reset to default
27 {BL0939_WRITE_COMMAND, BL0939_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x33},
28 // Enable User Operation Write
29 {BL0939_WRITE_COMMAND, BL0939_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xEB},
30 // 0x0100 = CF_UNABLE energy pulse, AC_FREQ_SEL 50Hz, RMS_UPDATE_SEL 800mS
31 {BL0939_WRITE_COMMAND, BL0939_REG_MODE, 0x00, 0x10, 0x00, 0x32},
32 // 0x47FF = Over-current and leakage alarm on, Automatic temperature measurement, Interval 100mS
33 {BL0939_WRITE_COMMAND, BL0939_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xF9},
34 // 0x181C = Half cycle, Fast RMS threshold 6172
35 {BL0939_WRITE_COMMAND, BL0939_REG_IA_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x16},
36 // 0x181C = Half cycle, Fast RMS threshold 6172
37 {BL0939_WRITE_COMMAND, BL0939_REG_IB_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x08}};
38
41 if (!this->available()) {
42 return;
43 }
44 if (read_array((uint8_t *) &buffer, sizeof(buffer))) {
47 }
48 } else {
49 ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
50 while (read() >= 0)
51 ;
52 }
53}
54
56 // Whole package but checksum
57 const uint8_t checksum = sum_bytes(data->raw, sizeof(data->raw) - 1, BL0939_READ_COMMAND) ^ 0xFF;
58 if (checksum != data->checksum) {
59 ESP_LOGW(TAG, "BL0939 invalid checksum! 0x%02X != 0x%02X", checksum, data->checksum);
60 }
61 return checksum == data->checksum;
62}
63
65 this->flush();
66 this->write_byte(BL0939_READ_COMMAND);
67 this->write_byte(BL0939_FULL_PACKET);
68}
69
71 uint8_t cmd[sizeof(BL0939_INIT[0])];
72 for (const auto *i : BL0939_INIT) {
73 progmem_memcpy(cmd, i, sizeof(cmd));
74 this->write_array(cmd, sizeof(cmd));
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 PUBLISH_STATE(this->voltage_sensor_, v_rms);
99 PUBLISH_STATE(this->current_1_sensor_, ia_rms);
100 PUBLISH_STATE(this->current_2_sensor_, ib_rms);
101 PUBLISH_STATE(this->power_1_sensor_, a_watt);
102 PUBLISH_STATE(this->power_2_sensor_, b_watt);
103 PUBLISH_STATE(this->energy_1_sensor_, a_energy_consumption);
104 PUBLISH_STATE(this->energy_2_sensor_, b_energy_consumption);
105 PUBLISH_STATE(this->energy_sum_sensor_, total_energy_consumption);
106
107 if (this->internal_temperature_sensor_ != nullptr) {
108 const uint16_t raw_temperature = encode_uint16(data->tps1.h, data->tps1.l) & 0x3FF;
109 // Datasheet temperature conversion: Tx = (170 / 448) * (TB / 2 - 32) - 45.
110 const float temperature = (170.0f / 448.0f) * (raw_temperature / 2.0f - 32.0f) - 45.0f;
111 this->internal_temperature_sensor_->publish_state(temperature);
112 }
113
114 ESP_LOGV(TAG,
115 "BL0939: U %fV, I1 %fA, I2 %fA, P1 %fW, P2 %fW, CntA %" PRId32 ", CntB %" PRId32 ", ∫P1 %fkWh, ∫P2 %fkWh",
116 v_rms, ia_rms, ib_rms, a_watt, b_watt, cfa_cnt, cfb_cnt, a_energy_consumption, b_energy_consumption);
117}
118
119void BL0939::dump_config() { // NOLINT(readability-function-cognitive-complexity)
120 ESP_LOGCONFIG(TAG,
121 "BL0939:\n"
122 " Mode: %s",
123 this->mode_ == BL0939Mode::BL0939_MODE_CURRENT_TRANSFORMER ? LOG_STR_LITERAL("current_transformer")
124 : LOG_STR_LITERAL("direct"));
125 LOG_SENSOR("", "Voltage", this->voltage_sensor_);
126 LOG_SENSOR("", "Current 1", this->current_1_sensor_);
127 LOG_SENSOR("", "Current 2", this->current_2_sensor_);
128 LOG_SENSOR("", "Power 1", this->power_1_sensor_);
129 LOG_SENSOR("", "Power 2", this->power_2_sensor_);
130 LOG_SENSOR("", "Energy 1", this->energy_1_sensor_);
131 LOG_SENSOR("", "Energy 2", this->energy_2_sensor_);
132 LOG_SENSOR("", "Energy sum", this->energy_sum_sensor_);
133 LOG_SENSOR("", "Internal Temperature", this->internal_temperature_sensor_);
134}
135
136uint32_t BL0939::to_uint32_t(ube24_t input) { return encode_uint24(input.h, input.m, input.l); }
137
138int32_t BL0939::to_int32_t(sbe24_t input) { return sign_extend<24>(encode_uint24(input.h, input.m, input.l)); }
139
140} // 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:64
static int32_t to_int32_t(sbe24_t input)
Definition bl0939.cpp:138
void setup() override
Definition bl0939.cpp:70
void loop() override
Definition bl0939.cpp:39
static bool validate_checksum(const DataPacket *data)
Definition bl0939.cpp:55
static uint32_t to_uint32_t(ube24_t input)
Definition bl0939.cpp:136
void received_package_(const DataPacket *data) const
Definition bl0939.cpp:80
void dump_config() override
Definition bl0939.cpp:119
UARTFlushResult flush()
Definition uart.h:96
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:43
void write_byte(uint8_t data)
Definition uart.h:22
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "bl0939")
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
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:1047
constexpr int32_t sign_extend(uint32_t value)
Sign-extend the low BITS bits of value, e.g. sign_extend<24>(0xFFFFFE) == -2. Higher bits are ignored...
Definition helpers.h:1057
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
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12