8 static const char *
const TAG =
"bl0942";
10 static const uint8_t BL0942_READ_COMMAND = 0x58;
11 static const uint8_t BL0942_FULL_PACKET = 0xAA;
12 static const uint8_t BL0942_PACKET_HEADER = 0x55;
14 static const uint8_t BL0942_WRITE_COMMAND = 0xA8;
15 static const uint8_t BL0942_REG_I_FAST_RMS_CTRL = 0x10;
16 static const uint8_t BL0942_REG_MODE = 0x18;
17 static const uint8_t BL0942_REG_SOFT_RESET = 0x19;
18 static const uint8_t BL0942_REG_USR_WRPROT = 0x1A;
19 static const uint8_t BL0942_REG_TPS_CTRL = 0x1B;
24 {BL0942_WRITE_COMMAND, BL0942_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x38},
26 {BL0942_WRITE_COMMAND, BL0942_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xF0},
28 {BL0942_WRITE_COMMAND, BL0942_REG_MODE, 0x00, 0x10, 0x00, 0x37},
30 {BL0942_WRITE_COMMAND, BL0942_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xFE},
32 {BL0942_WRITE_COMMAND, BL0942_REG_I_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x1B}};
39 if (
read_array((uint8_t *) &buffer,
sizeof(buffer))) {
44 ESP_LOGW(TAG,
"Junk on wire. Throwing away partial message");
51 uint8_t
checksum = BL0942_READ_COMMAND;
53 uint8_t *
raw = (uint8_t *) data;
54 for (uint32_t i = 0; i <
sizeof(*data) - 1; i++) {
58 if (checksum != data->checksum) {
59 ESP_LOGW(TAG,
"BL0942 invalid checksum! 0x%02X != 0x%02X", checksum, data->checksum);
61 return checksum == data->checksum;
71 for (
auto *i : BL0942_INIT) {
81 ESP_LOGI(TAG,
"Invalid data. Header mismatch: %d", data->
frame_header);
108 ESP_LOGV(TAG,
"BL0942: U %fV, I %fA, P %fW, Cnt %" PRId32
", ∫P %fkWh, frequency %fHz, status 0x%08X", v_rms,
i_rms,
109 watt,
cf_cnt, total_energy_consumption, frequency, data->
status);
113 ESP_LOGCONFIG(TAG,
"BL0942:");
sensor::Sensor * frequency_sensor_
24-bit unsigned integer type, transparently converting to 32-bit.
optional< std::array< uint8_t, N > > read_array()
void write_array(const uint8_t *data, size_t len)
const uint8_t BL0942_INIT[5][6]
void write_byte(uint8_t data)
sensor::Sensor * power_sensor_
static bool validate_checksum(DataPacket *data)
sensor::Sensor * current_sensor_
24-bit signed integer type, transparently converting to 32-bit.
void publish_state(float state)
Publish a new state to the front-end.
sensor::Sensor * voltage_sensor_
sensor::Sensor * energy_sensor_
void dump_config() override
Implementation of SPI Controller mode.
void received_package_(DataPacket *data)
void IRAM_ATTR HOT delay(uint32_t ms)