7static const char *
const TAG =
"bl0940";
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}};
35 if (
read_array((uint8_t *) &buffer,
sizeof(buffer))) {
40 ESP_LOGW(TAG,
"Junk on wire. Throwing away partial message");
49 uint8_t *
raw = (uint8_t *) data;
50 for (
uint32_t i = 0; i <
sizeof(*data) - 1; i++) {
55 ESP_LOGW(TAG,
"Invalid checksum! 0x%02X != 0x%02X",
checksum, data->checksum);
108 for (
auto *i : BL0940_INIT) {
143 ESP_LOGD(TAG,
"external calibration values restored to initial state");
148 ESP_LOGV(TAG,
"update current calibration state: %f", this->
current_cal_);
153 ESP_LOGV(TAG,
"update voltage calibration state: %f", this->
voltage_cal_);
158 ESP_LOGV(TAG,
"update power calibration state: %f", this->
power_cal_);
163 ESP_LOGV(TAG,
"update energy calibration state: %f", this->
energy_cal_);
168 ESP_LOGV(TAG,
"Recalibrating reference values");
183 "Recalibrated reference values:\n"
194 float converted_temp = ((float) 170 / 448) * (tb / 2 - 32) - 45;
195 if (sensor !=
nullptr) {
197 ESP_LOGD(TAG,
"Invalid temperature change. Sensor: '%s', Old temperature: %f, New temperature: %f",
203 return converted_temp;
208 if (data->frame_header != BL0940_PACKET_HEADER) {
209 ESP_LOGI(TAG,
"Invalid data. Header mismatch: %d", data->frame_header);
219 this->prev_cf_cnt_ =
cf_cnt;
221 float v_rms = (
uint24_t) data->v_rms / this->voltage_reference_cal_;
222 float i_rms = (
uint24_t) data->i_rms / this->current_reference_cal_;
223 float watt = (
int24_t) data->watt / this->power_reference_cal_;
242 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,
243 total_energy_consumption,
tps1,
tps2);
250 " READ CMD: 0x%02X\n"
251 " WRITE CMD: 0x%02X\n"
252 " ------------------\n"
253 " Current reference: %f\n"
254 " Energy reference: %f\n"
255 " Power reference: %f\n"
256 " Voltage reference: %f\n",
262 " Current calibration: %f\n"
263 " Energy calibration: %f\n"
264 " Power calibration: %f\n"
265 " Voltage calibration: %f\n",
const StringRef & get_name() const
constexpr const char * c_str() const
float calculate_calibration_value_(float state)
number::Number * current_calibration_number_
sensor::Sensor * external_temperature_sensor_
sensor::Sensor * current_sensor_
sensor::Sensor * internal_temperature_sensor_
float update_temp_(sensor::Sensor *sensor, uint16_le_t packed_temperature) const
number::Number * energy_calibration_number_
number::Number * power_calibration_number_
float max_temperature_diff_
float current_reference_cal_
bool legacy_mode_enabled_
void current_calibration_callback_(float state)
void dump_config() override
void voltage_calibration_callback_(float state)
sensor::Sensor * voltage_sensor_
sensor::Sensor * energy_sensor_
number::Number * voltage_calibration_number_
float calculate_energy_reference_()
void energy_calibration_callback_(float state)
float calculate_power_reference_()
bool validate_checksum_(DataPacket *data)
float power_reference_cal_
sensor::Sensor * power_sensor_
void received_package_(DataPacket *data)
float energy_reference_cal_
float voltage_reference_cal_
void power_calibration_callback_(float state)
NumberCall & set_value(float value)
void add_on_state_callback(F &&callback)
Base-class for all sensors.
void publish_state(float state)
Publish a new state to the front-end.
float get_state() const
Getter-syntax for .state.
optional< std::array< uint8_t, N > > read_array()
void write_byte(uint8_t data)
void write_array(const uint8_t *data, size_t len)
void HOT delay(uint32_t ms)
24-bit signed integer type, transparently converting to 32-bit.
24-bit unsigned integer type, transparently converting to 32-bit.