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);
104 for (
auto *i : BL0940_INIT) {
125 ESP_LOGI(TAG,
"Resetting calibration defaults...");
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_;
239 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,
240 total_energy_consumption,
tps1,
tps2);
247 " READ CMD: 0x%02X\n"
248 " WRITE CMD: 0x%02X\n"
249 " ------------------\n"
250 " Current reference: %f\n"
251 " Energy reference: %f\n"
252 " Power reference: %f\n"
253 " Voltage reference: %f\n",
259 " Current calibration: %f\n"
260 " Energy calibration: %f\n"
261 " Power calibration: %f\n"
262 " Voltage calibration: %f\n",
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
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)
ESPHOME_LOG_TAG(TAG, "bl0940")
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...
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.