9static const uint8_t BL0940_FULL_PACKET = 0xAA;
10static const uint8_t BL0940_PACKET_HEADER = 0x55;
12static const uint8_t BL0940_REG_I_FAST_RMS_CTRL = 0x10;
13static const uint8_t BL0940_REG_MODE = 0x18;
14static const uint8_t BL0940_REG_SOFT_RESET = 0x19;
15static const uint8_t BL0940_REG_USR_WRPROT = 0x1A;
16static const uint8_t BL0940_REG_TPS_CTRL = 0x1B;
18static const uint8_t BL0940_INIT[5][5] = {
20 {BL0940_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x38},
22 {BL0940_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xF0},
24 {BL0940_REG_MODE, 0x00, 0x10, 0x00, 0x37},
26 {BL0940_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xFE},
28 {BL0940_REG_I_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x1B}};
40 ESP_LOGW(TAG,
"Junk on wire. Throwing away partial message");
50 if (checksum != data->checksum) {
51 ESP_LOGW(TAG,
"Invalid checksum! 0x%02X != 0x%02X",
checksum, data->checksum);
65 if (this->voltage_calibration_number_ !=
nullptr) {
66 this->voltage_calibration_number_->add_on_state_callback(
68 if (this->voltage_calibration_number_->has_state()) {
73 if (this->current_calibration_number_ !=
nullptr) {
74 this->current_calibration_number_->add_on_state_callback(
76 if (this->current_calibration_number_->has_state()) {
81 if (this->power_calibration_number_ !=
nullptr) {
82 this->power_calibration_number_->add_on_state_callback(
84 if (this->power_calibration_number_->has_state()) {
89 if (this->energy_calibration_number_ !=
nullptr) {
90 this->energy_calibration_number_->add_on_state_callback(
92 if (this->energy_calibration_number_->has_state()) {
104 for (
auto *i : BL0940_INIT) {
124void BL0940::reset_calibration() {
125 ESP_LOGI(TAG,
"Resetting calibration defaults...");
127 if (this->current_calibration_number_ !=
nullptr && this->
current_cal_ != 1) {
128 this->current_calibration_number_->make_call().set_value(0).perform();
130 if (this->voltage_calibration_number_ !=
nullptr && this->
voltage_cal_ != 1) {
131 this->voltage_calibration_number_->make_call().set_value(0).perform();
133 if (this->power_calibration_number_ !=
nullptr && this->
power_cal_ != 1) {
134 this->power_calibration_number_->make_call().set_value(0).perform();
136 if (this->energy_calibration_number_ !=
nullptr && this->
energy_cal_ != 1) {
137 this->energy_calibration_number_->make_call().set_value(0).perform();
140 ESP_LOGD(TAG,
"external calibration values restored to initial state");
145 ESP_LOGV(TAG,
"update current calibration state: %f", this->
current_cal_);
150 ESP_LOGV(TAG,
"update voltage calibration state: %f", this->
voltage_cal_);
155 ESP_LOGV(TAG,
"update power calibration state: %f", this->
power_cal_);
160 ESP_LOGV(TAG,
"update energy calibration state: %f", this->
energy_cal_);
165 ESP_LOGV(TAG,
"Recalibrating reference values");
180 "Recalibrated reference values:\n"
191 float converted_temp = ((float) 170 / 448) * (tb / 2 - 32) - 45;
192 if (sensor !=
nullptr) {
194 ESP_LOGD(TAG,
"Invalid temperature change. Sensor: '%s', Old temperature: %f, New temperature: %f",
200 return converted_temp;
205 if (data->frame_header != BL0940_PACKET_HEADER) {
206 ESP_LOGI(TAG,
"Invalid data. Header mismatch: %d", data->frame_header);
216 this->prev_cf_cnt_ =
cf_cnt;
218 float v_rms = (
uint24_t) data->v_rms / this->voltage_reference_cal_;
219 float i_rms = (
uint24_t) data->i_rms / this->current_reference_cal_;
220 float watt = (
int24_t) data->watt / this->power_reference_cal_;
226 PUBLISH_STATE(this->voltage_sensor_,
v_rms);
227 PUBLISH_STATE(this->current_sensor_,
i_rms);
228 PUBLISH_STATE(this->power_sensor_,
watt);
229 PUBLISH_STATE(this->energy_sensor_, total_energy_consumption);
231 ESP_LOGV(TAG,
"BL0940: U %fV, I %fA, P %fW, Cnt %" PRId32
", ∫P %fkWh, T1 %f°C, T2 %f°C",
v_rms,
i_rms,
watt,
cf_cnt,
232 total_energy_consumption,
tps1,
tps2);
239 " READ CMD: 0x%02X\n"
240 " WRITE CMD: 0x%02X\n"
241 " ------------------\n"
242 " Current reference: %f\n"
243 " Energy reference: %f\n"
244 " Power reference: %f\n"
245 " Voltage reference: %f\n",
251 " Current calibration: %f\n"
252 " Energy calibration: %f\n"
253 " Power calibration: %f\n"
254 " Voltage calibration: %f\n",
257 LOG_SENSOR(
"",
"Voltage", this->voltage_sensor_);
258 LOG_SENSOR(
"",
"Current", this->current_sensor_);
259 LOG_SENSOR(
"",
"Power", this->power_sensor_);
260 LOG_SENSOR(
"",
"Energy", this->energy_sensor_);
261 LOG_SENSOR(
"",
"Internal temperature", this->internal_temperature_sensor_);
262 LOG_SENSOR(
"",
"External temperature", this->external_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...