8 static const char *
const TAG =
"bl0939";
12 static const uint8_t BL0939_READ_COMMAND = 0x55;
13 static const uint8_t BL0939_FULL_PACKET = 0xAA;
14 static const uint8_t BL0939_PACKET_HEADER = 0x55;
16 static const uint8_t BL0939_WRITE_COMMAND = 0xA5;
17 static const uint8_t BL0939_REG_IA_FAST_RMS_CTRL = 0x10;
18 static const uint8_t BL0939_REG_IB_FAST_RMS_CTRL = 0x1E;
19 static const uint8_t BL0939_REG_MODE = 0x18;
20 static const uint8_t BL0939_REG_SOFT_RESET = 0x19;
21 static const uint8_t BL0939_REG_USR_WRPROT = 0x1A;
22 static const uint8_t BL0939_REG_TPS_CTRL = 0x1B;
26 {BL0939_WRITE_COMMAND, BL0939_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x33},
28 {BL0939_WRITE_COMMAND, BL0939_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xEB},
30 {BL0939_WRITE_COMMAND, BL0939_REG_MODE, 0x00, 0x10, 0x00, 0x32},
32 {BL0939_WRITE_COMMAND, BL0939_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xF9},
34 {BL0939_WRITE_COMMAND, BL0939_REG_IA_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x16},
36 {BL0939_WRITE_COMMAND, BL0939_REG_IB_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x08}};
43 if (
read_array((uint8_t *) &buffer,
sizeof(buffer))) {
48 ESP_LOGW(TAG,
"Junk on wire. Throwing away partial message");
55 uint8_t
checksum = BL0939_READ_COMMAND;
57 for (uint32_t i = 0; i <
sizeof(data->
raw) - 1; i++) {
58 checksum += data->
raw[i];
62 ESP_LOGW(TAG,
"BL0939 invalid checksum! 0x%02X != 0x%02X", checksum, data->
checksum);
74 for (
auto *i : BL0939_INIT) {
84 ESP_LOGI(TAG,
"Invalid data. Header mismatch: %d", data->
frame_header);
97 float total_energy_consumption = a_energy_consumption + b_energy_consumption;
125 "BL0939: U %fV, I1 %fA, I2 %fA, P1 %fW, P2 %fW, CntA %" PRId32
", CntB %" PRId32
", ∫P1 %fkWh, ∫P2 %fkWh",
126 v_rms, ia_rms, ib_rms, a_watt, b_watt, cfa_cnt, cfb_cnt, a_energy_consumption, b_energy_consumption);
130 ESP_LOGCONFIG(TAG,
"BL0939:");
sensor::Sensor * current_sensor_1_
optional< std::array< uint8_t, N > > read_array()
const uint8_t BL0939_INIT[6][6]
void write_array(const uint8_t *data, size_t len)
void write_byte(uint8_t data)
sensor::Sensor * power_sensor_2_
void dump_config() override
static int32_t to_int32_t(sbe24_t input)
sensor::Sensor * power_sensor_1_
static uint32_t to_uint32_t(ube24_t input)
sensor::Sensor * voltage_sensor_
void publish_state(float state)
Publish a new state to the front-end.
sensor::Sensor * energy_sensor_2_
void received_package_(const DataPacket *data) const
static bool validate_checksum(const DataPacket *data)
sensor::Sensor * energy_sensor_1_
sensor::Sensor * current_sensor_2_
Implementation of SPI Controller mode.
sensor::Sensor * energy_sensor_sum_
void IRAM_ATTR HOT delay(uint32_t ms)