8static const char *
const TAG =
"bl0940";
10static const uint8_t BL0940_FULL_PACKET = 0xAA;
11static const uint8_t BL0940_PACKET_HEADER = 0x55;
13static const uint8_t BL0940_REG_I_FAST_RMS_CTRL = 0x10;
14static const uint8_t BL0940_REG_MODE = 0x18;
15static const uint8_t BL0940_REG_SOFT_RESET = 0x19;
16static const uint8_t BL0940_REG_USR_WRPROT = 0x1A;
17static const uint8_t BL0940_REG_TPS_CTRL = 0x1B;
19static const uint8_t BL0940_INIT[5][5] = {
21 {BL0940_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x38},
23 {BL0940_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xF0},
25 {BL0940_REG_MODE, 0x00, 0x10, 0x00, 0x37},
27 {BL0940_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xFE},
29 {BL0940_REG_I_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x1B}};
36 if (
read_array((uint8_t *) &buffer,
sizeof(buffer))) {
41 ESP_LOGW(TAG,
"Junk on wire. Throwing away partial message");
50 uint8_t *
raw = (uint8_t *) data;
51 for (uint32_t i = 0; i <
sizeof(*data) - 1; i++) {
56 ESP_LOGW(TAG,
"Invalid checksum! 0x%02X != 0x%02X",
checksum, data->
checksum);
109 for (
auto *i : BL0940_INIT) {
144 ESP_LOGD(TAG,
"external calibration values restored to initial state");
149 ESP_LOGV(TAG,
"update current calibration state: %f", this->
current_cal_);
154 ESP_LOGV(TAG,
"update voltage calibration state: %f", this->
voltage_cal_);
159 ESP_LOGV(TAG,
"update power calibration state: %f", this->
power_cal_);
164 ESP_LOGV(TAG,
"update energy calibration state: %f", this->
energy_cal_);
169 ESP_LOGV(TAG,
"Recalibrating reference values");
184 "Recalibrated reference values:\n"
185 "Voltage: %f\n, Current: %f\n, Power: %f\n, Energy: %f\n",
192 float converted_temp = ((float) 170 / 448) * (tb / 2 - 32) - 45;
193 if (sensor !=
nullptr) {
195 ESP_LOGD(TAG,
"Invalid temperature change. Sensor: '%s', Old temperature: %f, New temperature: %f",
201 return converted_temp;
207 ESP_LOGI(TAG,
"Invalid data. Header mismatch: %d", data->
frame_header);
217 this->prev_cf_cnt_ =
cf_cnt;
240 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,
241 total_energy_consumption,
tps1,
tps2);
248 " READ CMD: 0x%02X\n"
249 " WRITE CMD: 0x%02X\n"
250 " ------------------\n"
251 " Current reference: %f\n"
252 " Energy reference: %f\n"
253 " Power reference: %f\n"
254 " Voltage reference: %f\n",
260 " Current calibration: %f\n"
261 " Energy calibration: %f\n"
262 " Power calibration: %f\n"
263 " 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(std::function< void(float)> &&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)
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR 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.