ESPHome  2022.12.8
bl0942.cpp
Go to the documentation of this file.
1 #include "bl0942.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace bl0942 {
6 
7 static const char *const TAG = "bl0942";
8 
9 static const uint8_t BL0942_READ_COMMAND = 0x58;
10 static const uint8_t BL0942_FULL_PACKET = 0xAA;
11 static const uint8_t BL0942_PACKET_HEADER = 0x55;
12 
13 static const uint8_t BL0942_WRITE_COMMAND = 0xA8;
14 static const uint8_t BL0942_REG_I_FAST_RMS_CTRL = 0x10;
15 static const uint8_t BL0942_REG_MODE = 0x18;
16 static const uint8_t BL0942_REG_SOFT_RESET = 0x19;
17 static const uint8_t BL0942_REG_USR_WRPROT = 0x1A;
18 static const uint8_t BL0942_REG_TPS_CTRL = 0x1B;
19 
20 // TODO: Confirm insialisation works as intended
21 const uint8_t BL0942_INIT[5][6] = {
22  // Reset to default
23  {BL0942_WRITE_COMMAND, BL0942_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x38},
24  // Enable User Operation Write
25  {BL0942_WRITE_COMMAND, BL0942_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xF0},
26  // 0x0100 = CF_UNABLE energy pulse, AC_FREQ_SEL 50Hz, RMS_UPDATE_SEL 800mS
27  {BL0942_WRITE_COMMAND, BL0942_REG_MODE, 0x00, 0x10, 0x00, 0x37},
28  // 0x47FF = Over-current and leakage alarm on, Automatic temperature measurement, Interval 100mS
29  {BL0942_WRITE_COMMAND, BL0942_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xFE},
30  // 0x181C = Half cycle, Fast RMS threshold 6172
31  {BL0942_WRITE_COMMAND, BL0942_REG_I_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x1B}};
32 
33 void BL0942::loop() {
34  DataPacket buffer;
35  if (!this->available()) {
36  return;
37  }
38  if (read_array((uint8_t *) &buffer, sizeof(buffer))) {
39  if (validate_checksum(&buffer)) {
40  received_package_(&buffer);
41  }
42  } else {
43  ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
44  while (read() >= 0)
45  ;
46  }
47 }
48 
50  uint8_t checksum = BL0942_READ_COMMAND;
51  // Whole package but checksum
52  uint8_t *raw = (uint8_t *) data;
53  for (uint32_t i = 0; i < sizeof(*data) - 1; i++) {
54  checksum += raw[i];
55  }
56  checksum ^= 0xFF;
57  if (checksum != data->checksum) {
58  ESP_LOGW(TAG, "BL0942 invalid checksum! 0x%02X != 0x%02X", checksum, data->checksum);
59  }
60  return checksum == data->checksum;
61 }
62 
64  this->flush();
65  this->write_byte(BL0942_READ_COMMAND);
66  this->write_byte(BL0942_FULL_PACKET);
67 }
68 
69 void BL0942::setup() {
70  for (auto *i : BL0942_INIT) {
71  this->write_array(i, 6);
72  delay(1);
73  }
74  this->flush();
75 }
76 
78  // Bad header
79  if (data->frame_header != BL0942_PACKET_HEADER) {
80  ESP_LOGI(TAG, "Invalid data. Header mismatch: %d", data->frame_header);
81  return;
82  }
83 
84  float v_rms = (uint24_t) data->v_rms / voltage_reference_;
85  float i_rms = (uint24_t) data->i_rms / current_reference_;
86  float watt = (int24_t) data->watt / power_reference_;
87  uint32_t cf_cnt = (uint24_t) data->cf_cnt;
88  float total_energy_consumption = cf_cnt / energy_reference_;
89  float frequency = 1000000.0f / data->frequency;
90 
91  if (voltage_sensor_ != nullptr) {
93  }
94  if (current_sensor_ != nullptr) {
96  }
97  if (power_sensor_ != nullptr) {
99  }
100  if (energy_sensor_ != nullptr) {
101  energy_sensor_->publish_state(total_energy_consumption);
102  }
103  if (frequency_sensor_ != nullptr) {
104  frequency_sensor_->publish_state(frequency);
105  }
106 
107  ESP_LOGV(TAG, "BL0942: U %fV, I %fA, P %fW, Cnt %d, ∫P %fkWh, frequency %f°Hz, status 0x%08X", v_rms, i_rms, watt,
108  cf_cnt, total_energy_consumption, frequency, data->status);
109 }
110 
111 void BL0942::dump_config() { // NOLINT(readability-function-cognitive-complexity)
112  ESP_LOGCONFIG(TAG, "BL0942:");
113  LOG_SENSOR("", "Voltage", this->voltage_sensor_);
114  LOG_SENSOR("", "Current", this->current_sensor_);
115  LOG_SENSOR("", "Power", this->power_sensor_);
116  LOG_SENSOR("", "Energy", this->energy_sensor_);
117  LOG_SENSOR("", "frequency", this->frequency_sensor_);
118 }
119 
120 } // namespace bl0942
121 } // namespace esphome
ube24_t i_rms
Definition: bl0940.h:21
void setup() override
Definition: bl0942.cpp:69
sensor::Sensor * frequency_sensor_
Definition: bl0942.h:52
24-bit unsigned integer type, transparently converting to 32-bit.
Definition: datatypes.h:32
uint8_t raw[35]
Definition: bl0939.h:19
ube24_t cf_cnt
Definition: bl0940.h:27
optional< std::array< uint8_t, N > > read_array()
Definition: uart.h:33
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
const uint8_t BL0942_INIT[5][6]
Definition: bl0942.cpp:21
void write_byte(uint8_t data)
Definition: uart.h:19
sensor::Sensor * power_sensor_
Definition: bl0942.h:50
void loop() override
Definition: bl0942.cpp:33
static bool validate_checksum(DataPacket *data)
Definition: bl0942.cpp:49
uint16_le_t frequency
Definition: bl0942.h:23
sensor::Sensor * current_sensor_
Definition: bl0942.h:47
24-bit signed integer type, transparently converting to 32-bit.
Definition: datatypes.h:38
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:72
void update() override
Definition: bl0942.cpp:63
sensor::Sensor * voltage_sensor_
Definition: bl0942.h:46
uint8_t checksum
Definition: bl0939.h:35
uint16_le_t frequency
Definition: bl0942.h:21
sensor::Sensor * energy_sensor_
Definition: bl0942.h:51
void dump_config() override
Definition: bl0942.cpp:111
Definition: a4988.cpp:4
void received_package_(DataPacket *data)
Definition: bl0942.cpp:77
float current_reference_
Definition: bl0942.h:59
sbe24_t watt
Definition: bl0940.h:25
ube24_t v_rms
Definition: bl0939.h:25
float voltage_reference_
Definition: bl0942.h:57
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27