13static const uint8_t BL0939_READ_COMMAND = 0x55;
14static const uint8_t BL0939_FULL_PACKET = 0xAA;
15static const uint8_t BL0939_PACKET_HEADER = 0x55;
17static const uint8_t BL0939_WRITE_COMMAND = 0xA5;
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;
25static constexpr uint8_t BL0939_INIT[6][6] PROGMEM = {
27 {BL0939_WRITE_COMMAND, BL0939_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x33},
29 {BL0939_WRITE_COMMAND, BL0939_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xEB},
31 {BL0939_WRITE_COMMAND, BL0939_REG_MODE, 0x00, 0x10, 0x00, 0x32},
33 {BL0939_WRITE_COMMAND, BL0939_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xF9},
35 {BL0939_WRITE_COMMAND, BL0939_REG_IA_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x16},
37 {BL0939_WRITE_COMMAND, BL0939_REG_IB_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x08}};
49 ESP_LOGW(TAG,
"Junk on wire. Throwing away partial message");
57 const uint8_t
checksum =
sum_bytes(data->raw,
sizeof(data->raw) - 1, BL0939_READ_COMMAND) ^ 0xFF;
59 ESP_LOGW(TAG,
"BL0939 invalid checksum! 0x%02X != 0x%02X",
checksum, data->checksum);
71 uint8_t cmd[
sizeof(BL0939_INIT[0])];
72 for (
const auto *i : BL0939_INIT) {
82 if (data->frame_header != BL0939_PACKET_HEADER) {
83 ESP_LOGI(TAG,
"Invalid data. Header mismatch: %d", data->frame_header);
96 float total_energy_consumption = a_energy_consumption + b_energy_consumption;
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);
107 if (this->internal_temperature_sensor_ !=
nullptr) {
108 const uint16_t raw_temperature =
encode_uint16(data->tps1.h, data->tps1.l) & 0x3FF;
110 const float temperature = (170.0f / 448.0f) * (raw_temperature / 2.0f - 32.0f) - 45.0f;
111 this->internal_temperature_sensor_->publish_state(
temperature);
115 "BL0939: U %fV, I1 %fA, I2 %fA, P1 %fW, P2 %fW, CntA %" PRId32
", CntB %" PRId32
", ∫P1 %fkWh, ∫P2 %fkWh",
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_);
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...
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.