ESPHome  2023.8.3
bl0939.cpp
Go to the documentation of this file.
1 #include "bl0939.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace bl0939 {
6 
7 static 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)
11 static const uint8_t BL0939_READ_COMMAND = 0x55; // 0x5{A4,A3,A2,A1}
12 static const uint8_t BL0939_FULL_PACKET = 0xAA;
13 static const uint8_t BL0939_PACKET_HEADER = 0x55;
14 
15 static const uint8_t BL0939_WRITE_COMMAND = 0xA5; // 0xA{A4,A3,A2,A1}
16 static const uint8_t BL0939_REG_IA_FAST_RMS_CTRL = 0x10;
17 static const uint8_t BL0939_REG_IB_FAST_RMS_CTRL = 0x1E;
18 static const uint8_t BL0939_REG_MODE = 0x18;
19 static const uint8_t BL0939_REG_SOFT_RESET = 0x19;
20 static const uint8_t BL0939_REG_USR_WRPROT = 0x1A;
21 static const uint8_t BL0939_REG_TPS_CTRL = 0x1B;
22 
23 const 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 
37 void BL0939::loop() {
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 
72 void BL0939::setup() {
73  for (auto *i : BL0939_INIT) {
74  this->write_array(i, 6);
75  delay(1);
76  }
77  this->flush();
78 }
79 
80 void BL0939::received_package_(const DataPacket *data) const {
81  // Bad header
82  if (data->frame_header != BL0939_PACKET_HEADER) {
83  ESP_LOGI("bl0939", "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("bl0939", "BL0939: U %fV, I1 %fA, I2 %fA, P1 %fW, P2 %fW, CntA %d, CntB %d, ∫P1 %fkWh, ∫P2 %fkWh", v_rms,
124  ia_rms, ib_rms, a_watt, b_watt, cfa_cnt, cfb_cnt, a_energy_consumption, b_energy_consumption);
125 }
126 
127 void BL0939::dump_config() { // NOLINT(readability-function-cognitive-complexity)
128  ESP_LOGCONFIG(TAG, "BL0939:");
129  LOG_SENSOR("", "Voltage", this->voltage_sensor_);
130  LOG_SENSOR("", "Current 1", this->current_sensor_1_);
131  LOG_SENSOR("", "Current 2", this->current_sensor_2_);
132  LOG_SENSOR("", "Power 1", this->power_sensor_1_);
133  LOG_SENSOR("", "Power 2", this->power_sensor_2_);
134  LOG_SENSOR("", "Energy 1", this->energy_sensor_1_);
135  LOG_SENSOR("", "Energy 2", this->energy_sensor_2_);
136  LOG_SENSOR("", "Energy sum", this->energy_sensor_sum_);
137 }
138 
139 uint32_t BL0939::to_uint32_t(ube24_t input) { return input.h << 16 | input.m << 8 | input.l; }
140 
141 int32_t BL0939::to_int32_t(sbe24_t input) { return input.h << 16 | input.m << 8 | input.l; }
142 
143 } // namespace bl0939
144 } // namespace esphome
sensor::Sensor * current_sensor_1_
Definition: bl0939.h:79
optional< std::array< uint8_t, N > > read_array()
Definition: uart.h:33
const uint8_t BL0939_INIT[6][6]
Definition: bl0939.cpp:23
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
void write_byte(uint8_t data)
Definition: uart.h:19
sensor::Sensor * power_sensor_2_
Definition: bl0939.h:84
void dump_config() override
Definition: bl0939.cpp:127
float voltage_reference_
Definition: bl0939.h:92
static int32_t to_int32_t(sbe24_t input)
Definition: bl0939.cpp:141
sbe24_t a_watt
Definition: bl0939.h:27
sensor::Sensor * power_sensor_1_
Definition: bl0939.h:83
sbe24_t b_watt
Definition: bl0939.h:28
static uint32_t to_uint32_t(ube24_t input)
Definition: bl0939.cpp:139
sbe24_t cfa_cnt
Definition: bl0939.h:29
sensor::Sensor * voltage_sensor_
Definition: bl0939.h:78
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
void loop() override
Definition: bl0939.cpp:37
ube24_t ia_rms
Definition: bl0939.h:23
sensor::Sensor * energy_sensor_2_
Definition: bl0939.h:86
void received_package_(const DataPacket *data) const
Definition: bl0939.cpp:80
uint8_t checksum
Definition: bl0939.h:35
static bool validate_checksum(const DataPacket *data)
Definition: bl0939.cpp:53
sensor::Sensor * energy_sensor_1_
Definition: bl0939.h:85
sensor::Sensor * current_sensor_2_
Definition: bl0939.h:80
void setup() override
Definition: bl0939.cpp:72
float current_reference_
Definition: bl0939.h:94
void update() override
Definition: bl0939.cpp:66
ube24_t ib_rms
Definition: bl0939.h:24
sensor::Sensor * energy_sensor_sum_
Definition: bl0939.h:87
ube24_t v_rms
Definition: bl0939.h:25
sbe24_t cfb_cnt
Definition: bl0939.h:30
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:28