mirror of
https://github.com/athom-tech/esp32-configs.git
synced 2026-07-27 19:56:13 +00:00
Add files via upload
This commit is contained in:
@@ -0,0 +1,342 @@
|
||||
#include "bl0906.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace bl0906 {
|
||||
|
||||
static const char *const TAG = "bl0906";
|
||||
uint8_t USR_WRPROT_Witable[6]={0xCA, 0x9E, 0x55, 0x55, 0x00, 0xB7};
|
||||
uint8_t USR_WRPROT_Onlyread[6]={0xCA, 0x9E, 0x00, 0x00, 0x00, 0x61};
|
||||
|
||||
static const uint8_t BL0906_READ_COMMAND = 0x35;
|
||||
static const uint8_t BL0906_WRITE_COMMAND = 0xCA;
|
||||
|
||||
//Register address
|
||||
//Voltage
|
||||
static const uint8_t BL0906_V_RMS = 0x16;
|
||||
|
||||
//Total power
|
||||
static const uint8_t BL0906_WATT_SUM = 0X2C;
|
||||
|
||||
//Current1~6
|
||||
static const uint8_t BL0906_I_1_RMS = 0x0D; //current_1
|
||||
static const uint8_t BL0906_I_2_RMS = 0x0E;
|
||||
static const uint8_t BL0906_I_3_RMS = 0x0F;
|
||||
static const uint8_t BL0906_I_4_RMS = 0x10;
|
||||
static const uint8_t BL0906_I_5_RMS = 0x13;
|
||||
static const uint8_t BL0906_I_6_RMS = 0x14; //current_6
|
||||
|
||||
//Power1~6
|
||||
static const uint8_t BL0906_WATT_1 = 0X23; //power_1
|
||||
static const uint8_t BL0906_WATT_2 = 0X24;
|
||||
static const uint8_t BL0906_WATT_3 = 0X25;
|
||||
static const uint8_t BL0906_WATT_4 = 0X26;
|
||||
static const uint8_t BL0906_WATT_5 = 0X29;
|
||||
static const uint8_t BL0906_WATT_6 = 0X2A; //power_6
|
||||
|
||||
//Active pulse count, unsigned
|
||||
static const uint8_t BL0906_CF_1_CNT = 0X30; //Channel_1
|
||||
static const uint8_t BL0906_CF_2_CNT = 0X31;
|
||||
static const uint8_t BL0906_CF_3_CNT = 0X32;
|
||||
static const uint8_t BL0906_CF_4_CNT = 0X33;
|
||||
static const uint8_t BL0906_CF_5_CNT = 0X36;
|
||||
static const uint8_t BL0906_CF_6_CNT = 0X37; //Channel_6
|
||||
|
||||
//Total active pulse count, unsigned
|
||||
static const uint8_t BL0906_CF_SUM_CNT = 0X39;
|
||||
|
||||
//Voltage frequency cycle
|
||||
static const uint8_t BL0906_FREQUENCY = 0X4E;
|
||||
|
||||
//Internal temperature
|
||||
static const uint8_t BL0906_TEMPERATURE = 0X5E;
|
||||
|
||||
//Calibration register
|
||||
//RMS gain adjustment register
|
||||
static const uint8_t BL0906_RMSGN_1 = 0x6D; //Channel_1
|
||||
static const uint8_t BL0906_RMSGN_2 = 0x6E;
|
||||
static const uint8_t BL0906_RMSGN_3 = 0x6F;
|
||||
static const uint8_t BL0906_RMSGN_4 = 0x70;
|
||||
static const uint8_t BL0906_RMSGN_5 = 0x73;
|
||||
static const uint8_t BL0906_RMSGN_6 = 0x74; //Channel_6
|
||||
|
||||
//RMS offset correction register
|
||||
static const uint8_t BL0906_RMSOS_1 = 0x78; //Channel_1
|
||||
static const uint8_t BL0906_RMSOS_2 = 0x79;
|
||||
static const uint8_t BL0906_RMSOS_3 = 0x7A;
|
||||
static const uint8_t BL0906_RMSOS_4 = 0x7B;
|
||||
static const uint8_t BL0906_RMSOS_5 = 0x7E;
|
||||
static const uint8_t BL0906_RMSOS_6 = 0x7F; //Channel_6
|
||||
|
||||
//Active power gain adjustment register
|
||||
static const uint8_t BL0906_WATTGN_1 = 0xB7; //Channel_1
|
||||
static const uint8_t BL0906_WATTGN_2 = 0xB8;
|
||||
static const uint8_t BL0906_WATTGN_3 = 0xB9;
|
||||
static const uint8_t BL0906_WATTGN_4 = 0xBA;
|
||||
static const uint8_t BL0906_WATTGN_5 = 0xBD;
|
||||
static const uint8_t BL0906_WATTGN_6 = 0xBE; //Channel_6
|
||||
|
||||
void BL0906::loop() {}
|
||||
|
||||
void BL0906::setup() {
|
||||
while (this->available())
|
||||
this->flush();
|
||||
this-> write_array(USR_WRPROT_Witable,sizeof(USR_WRPROT_Witable));
|
||||
//Calibration (1: register address; 2: value before calibration; 3: value after calibration)
|
||||
Bias_correction(BL0906_RMSOS_1, 0.01600, 0); //Calibration current_1
|
||||
Bias_correction(BL0906_RMSOS_2, 0.01500, 0);
|
||||
Bias_correction(BL0906_RMSOS_3, 0.01400, 0);
|
||||
Bias_correction(BL0906_RMSOS_4, 0.01300, 0);
|
||||
Bias_correction(BL0906_RMSOS_5, 0.01200, 0);
|
||||
Bias_correction(BL0906_RMSOS_6, 0.01200, 0); //Calibration current_6
|
||||
|
||||
// gain_correction(BL0906_RMSGN_1, 2.15000, 2.148, BL0906_ki); //RMS gain adjustment current_1
|
||||
// gain_correction(BL0906_RMSGN_2, 2.15100, 2.148, BL0906_ki);
|
||||
// gain_correction(BL0906_RMSGN_3, 2.15200, 2.148, BL0906_ki);
|
||||
// gain_correction(BL0906_RMSGN_4, 2.14500, 2.148, BL0906_ki);
|
||||
// gain_correction(BL0906_RMSGN_5, 2.14600, 2.148, BL0906_ki);
|
||||
// gain_correction(BL0906_RMSGN_6, 2.14600, 2.148, BL0906_ki); //RMS gain adjustment current_6
|
||||
|
||||
// gain_correction(BL0906_WATTGN_1, 15.13427, 14.5, BL0906_Kp); //Active power gain adjustment power_1
|
||||
// gain_correction(BL0906_WATTGN_2, 15.23937, 14.5, BL0906_Kp);
|
||||
// gain_correction(BL0906_WATTGN_3, 15.44956, 14.5, BL0906_Kp);
|
||||
// gain_correction(BL0906_WATTGN_4, 16.57646, 14.5, BL0906_Kp);
|
||||
// gain_correction(BL0906_WATTGN_5, 15.27440, 14.5, BL0906_Kp);
|
||||
// gain_correction(BL0906_WATTGN_6, 31.75744, 14.5, BL0906_Kp); //Active power gain adjustment power_6
|
||||
this-> write_array(USR_WRPROT_Onlyread,sizeof(USR_WRPROT_Onlyread));
|
||||
}
|
||||
|
||||
|
||||
void BL0906::update() {
|
||||
while (this->available())
|
||||
this->flush();
|
||||
//Frequency
|
||||
read_data(BL0906_FREQUENCY, frequency_reference_, frequency_sensor_ );
|
||||
//Temperature
|
||||
read_data(BL0906_TEMPERATURE, temperature_reference_, temperature_sensor_);
|
||||
//Voltage
|
||||
read_data(BL0906_V_RMS, voltage_reference_, voltage_sensor_);
|
||||
//Current
|
||||
read_data(BL0906_I_1_RMS, current_reference_, current_sensor_1_ );
|
||||
read_data(BL0906_I_2_RMS, current_reference_, current_sensor_2_ );
|
||||
read_data(BL0906_I_3_RMS, current_reference_, current_sensor_3_ );
|
||||
read_data(BL0906_I_4_RMS, current_reference_, current_sensor_4_ );
|
||||
read_data(BL0906_I_5_RMS, current_reference_, current_sensor_5_ );
|
||||
read_data(BL0906_I_6_RMS, current_reference_, current_sensor_6_ );
|
||||
//Power
|
||||
read_data(BL0906_WATT_1, power_reference_, power_sensor_1_ );
|
||||
read_data(BL0906_WATT_2, power_reference_, power_sensor_2_ );
|
||||
read_data(BL0906_WATT_3, power_reference_, power_sensor_3_ );
|
||||
read_data(BL0906_WATT_4, power_reference_, power_sensor_4_ );
|
||||
read_data(BL0906_WATT_5, power_reference_, power_sensor_5_ );
|
||||
read_data(BL0906_WATT_6, power_reference_, power_sensor_6_ );
|
||||
//Total power
|
||||
read_data(BL0906_WATT_SUM, sum_power_reference_, power_sensor_sum_ );
|
||||
//Energy
|
||||
read_data(BL0906_CF_1_CNT, energy_reference_, energy_sensor_1_ );
|
||||
read_data(BL0906_CF_2_CNT, energy_reference_, energy_sensor_2_ );
|
||||
read_data(BL0906_CF_3_CNT, energy_reference_, energy_sensor_3_ );
|
||||
read_data(BL0906_CF_4_CNT, energy_reference_, energy_sensor_4_ );
|
||||
read_data(BL0906_CF_5_CNT, energy_reference_, energy_sensor_5_ );
|
||||
read_data(BL0906_CF_6_CNT, energy_reference_, energy_sensor_6_ );
|
||||
//Total Energy
|
||||
read_data(BL0906_CF_SUM_CNT, sum_energy_reference_, energy_sensor_sum_ );
|
||||
}
|
||||
|
||||
//The SUM byte is (Addr+Data_L+Data_M+Data_H)&0xFF negated;
|
||||
uint8_t bl0906_checksum(const uint8_t address, const DataPacket *data) {
|
||||
return (address + data->l + data->m + data->h) ^ 0xFF;
|
||||
}
|
||||
|
||||
//Read data
|
||||
void BL0906::read_data(const uint8_t address, const float reference, sensor::Sensor *sensor_) {
|
||||
DataPacket buffer;
|
||||
ube24_t data_u24;
|
||||
sbe24_t data_s24;
|
||||
float value=0;
|
||||
|
||||
//Power
|
||||
if (reference == power_reference_){
|
||||
this-> write_byte(BL0906_READ_COMMAND);
|
||||
this-> write_byte(address);
|
||||
if (this-> read_array((uint8_t *) &buffer, sizeof(buffer)-1)) {
|
||||
if (bl0906_checksum(address,&buffer)==buffer.checksum) {
|
||||
data_s24.l = buffer.l;
|
||||
data_s24.m = buffer.m;
|
||||
data_s24.h = buffer.h;
|
||||
value = (float) to_int32_t(data_s24) * reference;
|
||||
if (sensor_ != nullptr) {
|
||||
sensor_->publish_state(value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Junk on wire. Throwing away partial power message");
|
||||
while (read() >= 0)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
//Total power
|
||||
if (reference == sum_power_reference_){
|
||||
this-> write_byte(BL0906_READ_COMMAND);
|
||||
this-> write_byte(address);
|
||||
if (this-> read_array((uint8_t *) &buffer, sizeof(buffer)-1)) {
|
||||
if (bl0906_checksum(address,&buffer)==buffer.checksum) {
|
||||
data_s24.l = buffer.l;
|
||||
data_s24.m = buffer.m;
|
||||
data_s24.h = buffer.h;
|
||||
value = (float) to_int32_t(data_s24) * reference;
|
||||
if (sensor_ != nullptr) {
|
||||
sensor_->publish_state(value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Junk on wire. Throwing away partial power message");
|
||||
while (read() >= 0)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
//Voltage, current, power, total power
|
||||
if (reference == voltage_reference_ || reference == current_reference_ || reference == energy_reference_ || reference == sum_energy_reference_) {
|
||||
this-> write_byte(BL0906_READ_COMMAND);
|
||||
this-> write_byte(address);
|
||||
if (this-> read_array((uint8_t *) &buffer, sizeof(buffer)-1)) {
|
||||
//ESP_LOGW(TAG, "checksum= 0x%02X", bl0906_checksum(BL0906_V_RMS,&buffer));
|
||||
if (bl0906_checksum(address,&buffer)==buffer.checksum) {
|
||||
data_u24.l = buffer.l;
|
||||
data_u24.m = buffer.m;
|
||||
data_u24.h = buffer.h;
|
||||
value = (float) to_uint32_t(data_u24) * reference;
|
||||
if (sensor_ != nullptr) {
|
||||
sensor_->publish_state(value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
|
||||
while (read() >= 0)
|
||||
;
|
||||
}
|
||||
}
|
||||
//Frequency
|
||||
if (reference == frequency_reference_) {
|
||||
this-> write_byte(BL0906_READ_COMMAND);
|
||||
this-> write_byte(address);
|
||||
if (this-> read_array((uint8_t *) &buffer, sizeof(buffer)-1)) {
|
||||
if (bl0906_checksum(address,&buffer)==buffer.checksum) {
|
||||
data_u24.l = buffer.l;
|
||||
data_u24.m = buffer.m;
|
||||
data_u24.h = buffer.h;
|
||||
value = reference / (float) to_uint32_t(data_u24);
|
||||
if (sensor_ != nullptr) {
|
||||
sensor_->publish_state(value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Junk on wire. Throwing frequency message");
|
||||
while (read() >= 0)
|
||||
;
|
||||
}
|
||||
}
|
||||
//Chip temperature
|
||||
if (reference == temperature_reference_) {
|
||||
this-> write_byte(BL0906_READ_COMMAND);
|
||||
this-> write_byte(address);
|
||||
if (this-> read_array((uint8_t *) &buffer, sizeof(buffer)-1)) {
|
||||
if (bl0906_checksum(address,&buffer)==buffer.checksum) {
|
||||
data_s24.l = buffer.l;
|
||||
data_s24.m = buffer.m;
|
||||
data_s24.h = buffer.h;
|
||||
value = (float) to_int32_t(data_s24);
|
||||
value = (value - 64) *12.5/59-40;
|
||||
if (sensor_ != nullptr) {
|
||||
sensor_->publish_state(value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Junk on wire. Throwing frequency message");
|
||||
while (read() >= 0)
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
//RMS offset correction
|
||||
void BL0906::Bias_correction(const uint8_t address, const float measurements, const float Correction) {
|
||||
DataPacket data;
|
||||
float ki = 12875*1*(5.1+5.1)*1000/2000/1.097; //Current coefficient
|
||||
float I_RMS0 = measurements * ki;
|
||||
float I_RMS = Correction * ki;
|
||||
int32_t value = (I_RMS*I_RMS-I_RMS0*I_RMS0) / 256;
|
||||
data.l = value << 24 >>24;
|
||||
data.m = value << 16 >>24;
|
||||
if (value < 0) {
|
||||
data.h = (value << 8 >>24)|0b10000000;
|
||||
}
|
||||
data.address = bl0906_checksum(address, &data);
|
||||
ESP_LOGW(TAG, "RMSOS:%02X%02X%02X%02X%02X%02X",BL0906_WRITE_COMMAND,address,data.l ,data.m, data.h,data.address );
|
||||
this-> write_byte(BL0906_WRITE_COMMAND);
|
||||
this-> write_byte(address);
|
||||
this-> write_byte(data.l);
|
||||
this-> write_byte(data.m);
|
||||
this-> write_byte(data.h);
|
||||
this-> write_byte(data.address);
|
||||
}
|
||||
//Gain adjustment
|
||||
void BL0906::gain_correction(const uint8_t address, const float measurements, const float Correction, const float coefficient) {
|
||||
DataPacket data;
|
||||
float I_RMS0 = measurements * coefficient;
|
||||
float I_RMS = Correction * coefficient;
|
||||
float rms_gn = int((I_RMS/I_RMS0-1) * 65536);
|
||||
int16_t value;
|
||||
if (rms_gn <= -32767){
|
||||
value = -32767;
|
||||
} else {
|
||||
value = int(rms_gn);
|
||||
}
|
||||
data.h = 0xFF;
|
||||
data.m = value >> 8;
|
||||
data.l = value << 8 >>8;
|
||||
data.address = bl0906_checksum(address, &data);
|
||||
//ESP_LOGW(TAG, "RMSOS:%02X%02X%02X%02X%02X%02X",BL0906_WRITE_COMMAND,address,data.l ,data.m, data.h,data.address );
|
||||
this-> write_byte(BL0906_WRITE_COMMAND);
|
||||
this-> write_byte(address);
|
||||
this-> write_byte(data.l);
|
||||
this-> write_byte(data.m);
|
||||
this-> write_byte(data.h);
|
||||
this-> write_byte(data.address);
|
||||
}
|
||||
|
||||
void BL0906::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "BL0906:");
|
||||
LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
|
||||
LOG_SENSOR(" ", "Current1", this->current_sensor_1_);
|
||||
LOG_SENSOR(" ", "Current2", this->current_sensor_2_);
|
||||
LOG_SENSOR(" ", "Current3", this->current_sensor_3_);
|
||||
LOG_SENSOR(" ", "Current4", this->current_sensor_4_);
|
||||
LOG_SENSOR(" ", "Current5", this->current_sensor_5_);
|
||||
LOG_SENSOR(" ", "Current6", this->current_sensor_6_);
|
||||
|
||||
LOG_SENSOR(" ", "Power1", this->power_sensor_1_);
|
||||
LOG_SENSOR(" ", "Power2", this->power_sensor_2_);
|
||||
LOG_SENSOR(" ", "Power3", this->power_sensor_3_);
|
||||
LOG_SENSOR(" ", "Power4", this->power_sensor_4_);
|
||||
LOG_SENSOR(" ", "Power5", this->power_sensor_5_);
|
||||
LOG_SENSOR(" ", "Power6", this->power_sensor_6_);
|
||||
|
||||
LOG_SENSOR(" ", "Energy1", this->energy_sensor_1_);
|
||||
LOG_SENSOR(" ", "Energy2", this->energy_sensor_2_);
|
||||
LOG_SENSOR(" ", "Energy3", this->energy_sensor_3_);
|
||||
LOG_SENSOR(" ", "Energy4", this->energy_sensor_4_);
|
||||
LOG_SENSOR(" ", "Energy5", this->energy_sensor_5_);
|
||||
LOG_SENSOR(" ", "Energy6", this->energy_sensor_6_);
|
||||
LOG_SENSOR(" ", "Energy sum", this->energy_sensor_sum_);
|
||||
LOG_SENSOR(" ", "Frequency", this->frequency_sensor_);
|
||||
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
||||
}
|
||||
|
||||
uint32_t BL0906::to_uint32_t(ube24_t input) { return input.h << 16 | input.m << 8 | input.l; }
|
||||
|
||||
int32_t BL0906::to_int32_t(sbe24_t input) { return input.h << 16 | input.m << 8 | input.l; }
|
||||
|
||||
} // namespace bl0906
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,137 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
|
||||
// https://www.belling.com.cn/media/file_object/bel_product/BL0906/datasheet/BL0906_V1.02_cn.pdf
|
||||
// https://www.belling.com.cn/media/file_object/bel_product/BL0906/guide/BL0906%20APP%20Note_V1.02.pdf
|
||||
|
||||
namespace esphome {
|
||||
namespace bl0906 {
|
||||
|
||||
//Total power conversion
|
||||
static const float BL0906_WATT = 16*1.097*1.097*(20000+20000+20000+20000+20000)/(40.41259*((5.1+5.1)*1000/2000)*1*100*1*1000);
|
||||
//Total Energy conversion
|
||||
static const float BL0906_CF =16*4194304*0.032768*16/(3600000*16*(40.4125*((5.1+5.1)*1000/2000)*1*100*1*1000/(1.097*1.097*(20000+20000+20000+20000+20000))));
|
||||
//Frequency conversion
|
||||
static const float BL0906_FREF = 10000000;
|
||||
//Temperature conversion
|
||||
static const float BL0906_TREF = 12.5/59-40;
|
||||
//Current conversion
|
||||
static const float BL0906_IREF = 1.097/(12875*1*(5.1+5.1)*1000/2000);
|
||||
//Voltage conversion
|
||||
static const float BL0906_UREF = 1.097*(20000+20000+20000+20000+20000)/(13162*1*100*1000);
|
||||
//Power conversion
|
||||
static const float BL0906_PREF = 1.097*1.097*(20000+20000+20000+20000+20000)/(40.41259*((5.1+5.1)*1000/2000)*1*100*1*1000);
|
||||
//Energy conversion
|
||||
static const float BL0906_EREF = 4194304*0.032768*16/(3600000*16*(40.4125*((5.1+5.1)*1000/2000)*1*100*1*1000/(1.097*1.097*(20000+20000+20000+20000+20000))));
|
||||
//Current coefficient
|
||||
static const float BL0906_ki = 12875*1*(5.1+5.1)*1000/2000/1.097;
|
||||
//Power coefficient
|
||||
static const float BL0906_Kp=40.4125*((5.1+5.1)*1000/2000)*1*100*1*1000/1.097/1.097/(20000+20000+20000+20000+20000);
|
||||
|
||||
struct DataPacket { // NOLINT(altera-struct-pack-align)
|
||||
uint8_t l;
|
||||
uint8_t m;
|
||||
uint8_t h;
|
||||
uint8_t checksum; // checksum
|
||||
uint8_t address;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct ube24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
|
||||
uint8_t l;
|
||||
uint8_t m;
|
||||
uint8_t h;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct sbe24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
|
||||
uint8_t l;
|
||||
uint8_t m;
|
||||
int8_t h;
|
||||
} __attribute__((packed));
|
||||
|
||||
class BL0906 : public PollingComponent, public uart::UARTDevice {
|
||||
public:
|
||||
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
|
||||
void set_current_sensor_1(sensor::Sensor *current_sensor_1) { current_sensor_1_ = current_sensor_1; }
|
||||
void set_current_sensor_2(sensor::Sensor *current_sensor_2) { current_sensor_2_ = current_sensor_2; }
|
||||
void set_current_sensor_3(sensor::Sensor *current_sensor_3) { current_sensor_3_ = current_sensor_3; }
|
||||
void set_current_sensor_4(sensor::Sensor *current_sensor_4) { current_sensor_4_ = current_sensor_4; }
|
||||
void set_current_sensor_5(sensor::Sensor *current_sensor_5) { current_sensor_5_ = current_sensor_5; }
|
||||
void set_current_sensor_6(sensor::Sensor *current_sensor_6) { current_sensor_6_ = current_sensor_6; }
|
||||
void set_power_sensor_1(sensor::Sensor *power_sensor_1) { power_sensor_1_ = power_sensor_1; }
|
||||
void set_power_sensor_2(sensor::Sensor *power_sensor_2) { power_sensor_2_ = power_sensor_2; }
|
||||
void set_power_sensor_3(sensor::Sensor *power_sensor_3) { power_sensor_3_ = power_sensor_3; }
|
||||
void set_power_sensor_4(sensor::Sensor *power_sensor_4) { power_sensor_4_ = power_sensor_4; }
|
||||
void set_power_sensor_5(sensor::Sensor *power_sensor_5) { power_sensor_5_ = power_sensor_5; }
|
||||
void set_power_sensor_6(sensor::Sensor *power_sensor_6) { power_sensor_6_ = power_sensor_6; }
|
||||
void set_power_sensor_sum(sensor::Sensor *power_sensor_sum) { power_sensor_sum_ = power_sensor_sum; }
|
||||
void set_energy_sensor_1(sensor::Sensor *energy_sensor_1) { energy_sensor_1_ = energy_sensor_1; }
|
||||
void set_energy_sensor_2(sensor::Sensor *energy_sensor_2) { energy_sensor_2_ = energy_sensor_2; }
|
||||
void set_energy_sensor_3(sensor::Sensor *energy_sensor_3) { energy_sensor_3_ = energy_sensor_3; }
|
||||
void set_energy_sensor_4(sensor::Sensor *energy_sensor_4) { energy_sensor_4_ = energy_sensor_4; }
|
||||
void set_energy_sensor_5(sensor::Sensor *energy_sensor_5) { energy_sensor_5_ = energy_sensor_5; }
|
||||
void set_energy_sensor_6(sensor::Sensor *energy_sensor_6) { energy_sensor_6_ = energy_sensor_6; }
|
||||
void set_energy_sensor_sum(sensor::Sensor *energy_sensor_sum) { energy_sensor_sum_ = energy_sensor_sum; }
|
||||
void set_frequency_sensor(sensor::Sensor *frequency_sensor) { frequency_sensor_ = frequency_sensor; }
|
||||
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
|
||||
void loop() override;
|
||||
|
||||
void update() override;
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
sensor::Sensor *voltage_sensor_{nullptr};
|
||||
sensor::Sensor *current_sensor_1_{nullptr};
|
||||
sensor::Sensor *current_sensor_2_{nullptr};
|
||||
sensor::Sensor *current_sensor_3_{nullptr};
|
||||
sensor::Sensor *current_sensor_4_{nullptr};
|
||||
sensor::Sensor *current_sensor_5_{nullptr};
|
||||
sensor::Sensor *current_sensor_6_{nullptr};
|
||||
sensor::Sensor *power_sensor_1_{nullptr};
|
||||
sensor::Sensor *power_sensor_2_{nullptr};
|
||||
sensor::Sensor *power_sensor_3_{nullptr};
|
||||
sensor::Sensor *power_sensor_4_{nullptr};
|
||||
sensor::Sensor *power_sensor_5_{nullptr};
|
||||
sensor::Sensor *power_sensor_6_{nullptr};
|
||||
sensor::Sensor *power_sensor_sum_{nullptr};
|
||||
sensor::Sensor *energy_sensor_1_{nullptr};
|
||||
sensor::Sensor *energy_sensor_2_{nullptr};
|
||||
sensor::Sensor *energy_sensor_3_{nullptr};
|
||||
sensor::Sensor *energy_sensor_4_{nullptr};
|
||||
sensor::Sensor *energy_sensor_5_{nullptr};
|
||||
sensor::Sensor *energy_sensor_6_{nullptr};
|
||||
sensor::Sensor *energy_sensor_sum_{nullptr};
|
||||
sensor::Sensor *frequency_sensor_{nullptr};
|
||||
sensor::Sensor *temperature_sensor_{nullptr};
|
||||
|
||||
float sum_energy_reference_ = BL0906_CF;
|
||||
|
||||
float sum_power_reference_ = BL0906_WATT;
|
||||
|
||||
float power_reference_ = BL0906_PREF;
|
||||
|
||||
float voltage_reference_ = BL0906_UREF;
|
||||
|
||||
float current_reference_ = BL0906_IREF;
|
||||
|
||||
float energy_reference_ = BL0906_EREF;
|
||||
|
||||
float frequency_reference_ = BL0906_FREF;
|
||||
|
||||
float temperature_reference_ = BL0906_TREF;
|
||||
|
||||
static uint32_t to_uint32_t(ube24_t input);
|
||||
|
||||
static int32_t to_int32_t(sbe24_t input);
|
||||
|
||||
void read_data(const uint8_t address, const float reference, sensor::Sensor *sensor_);
|
||||
|
||||
void Bias_correction(const uint8_t address, const float measurements, const float Correction) ;
|
||||
|
||||
void gain_correction(const uint8_t address, const float measurements, const float Correction, const float coefficient) ;
|
||||
};
|
||||
} // namespace bl0906
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,224 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import sensor, uart
|
||||
from esphome.const import (
|
||||
UNIT_PERCENT,
|
||||
CONF_ID,
|
||||
)
|
||||
|
||||
|
||||
DEPENDENCIES = ["uart"]
|
||||
AUTO_LOAD = ["bl0906"]
|
||||
|
||||
bl0906_ns = cg.esphome_ns.namespace("bl0906")
|
||||
BL0906 = bl0906_ns.class_(
|
||||
"BL0906", cg.PollingComponent, uart.UARTDevice
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(BL0906),
|
||||
cv.Optional("Frequency"): sensor.sensor_schema(
|
||||
accuracy_decimals = 0,
|
||||
device_class = "frequency",
|
||||
unit_of_measurement = "Hz"
|
||||
),
|
||||
cv.GenerateID(): cv.declare_id(BL0906),
|
||||
cv.Optional("Temperature"): sensor.sensor_schema(
|
||||
accuracy_decimals = 0,
|
||||
device_class = "temperature",
|
||||
unit_of_measurement = "℃"
|
||||
),
|
||||
cv.GenerateID(): cv.declare_id(BL0906),
|
||||
cv.Optional("Voltage"): sensor.sensor_schema(
|
||||
accuracy_decimals = 0,
|
||||
device_class = "voltage",
|
||||
unit_of_measurement = "V"
|
||||
),
|
||||
cv.Optional("Current_1"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "current",
|
||||
unit_of_measurement = "A"
|
||||
),
|
||||
cv.Optional("Current_2"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "current",
|
||||
unit_of_measurement = "A"
|
||||
),
|
||||
cv.Optional("Current_3"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "current",
|
||||
unit_of_measurement = "A"
|
||||
),
|
||||
cv.Optional("Current_4"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "current",
|
||||
unit_of_measurement = "A"
|
||||
),
|
||||
cv.Optional("Current_5"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "current",
|
||||
unit_of_measurement = "A"
|
||||
),
|
||||
cv.Optional("Current_6"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "current",
|
||||
unit_of_measurement = "A"
|
||||
),
|
||||
cv.Optional("Power_1"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "power",
|
||||
unit_of_measurement = "W"
|
||||
),
|
||||
cv.Optional("Power_2"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "power",
|
||||
unit_of_measurement = "W"
|
||||
),
|
||||
cv.Optional("Power_3"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "power",
|
||||
unit_of_measurement = "W"
|
||||
),
|
||||
cv.Optional("Power_4"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "power",
|
||||
unit_of_measurement = "W"
|
||||
),
|
||||
cv.Optional("Power_5"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "power",
|
||||
unit_of_measurement = "W"
|
||||
),
|
||||
cv.Optional("Power_6"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "power",
|
||||
unit_of_measurement = "W"
|
||||
),
|
||||
cv.Optional("Power_sum"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "power",
|
||||
unit_of_measurement = "W"
|
||||
),
|
||||
cv.Optional("Energy_1"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "energy",
|
||||
state_class = "total",
|
||||
unit_of_measurement = "kWh"
|
||||
),
|
||||
cv.Optional("Energy_2"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "energy",
|
||||
state_class = "total",
|
||||
unit_of_measurement = "kWh" ),
|
||||
cv.Optional("Energy_3"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "energy",
|
||||
state_class = "total",
|
||||
unit_of_measurement = "kWh"
|
||||
),
|
||||
cv.Optional("Energy_4"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "energy",
|
||||
state_class = "total",
|
||||
unit_of_measurement = "kWh"
|
||||
),
|
||||
cv.Optional("Energy_5"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "energy",
|
||||
state_class = "total",
|
||||
unit_of_measurement = "kWh"
|
||||
),
|
||||
cv.Optional("Energy_6"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "energy",
|
||||
state_class = "total",
|
||||
unit_of_measurement = "kWh"
|
||||
),
|
||||
cv.Optional("Energy_sum"): sensor.sensor_schema(
|
||||
accuracy_decimals = 3,
|
||||
device_class = "energy",
|
||||
state_class = "total",
|
||||
unit_of_measurement = "kWh"
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(uart.UART_DEVICE_SCHEMA)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
)
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await uart.register_uart_device(var, config)
|
||||
if "Frequency" in config:
|
||||
sens = await sensor.new_sensor(config["Frequency"])
|
||||
cg.add(var.set_frequency_sensor(sens))
|
||||
if "Temperature" in config:
|
||||
sens = await sensor.new_sensor(config["Temperature"])
|
||||
cg.add(var.set_temperature_sensor(sens))
|
||||
if "Voltage" in config:
|
||||
sens = await sensor.new_sensor(config["Voltage"])
|
||||
cg.add(var.set_voltage_sensor(sens))
|
||||
if "Current_1" in config:
|
||||
sens = await sensor.new_sensor(config["Current_1"])
|
||||
cg.add(var.set_current_sensor_1(sens))
|
||||
if "Current_2" in config:
|
||||
sens = await sensor.new_sensor(config["Current_2"])
|
||||
cg.add(var.set_current_sensor_2(sens))
|
||||
if "Current_3" in config:
|
||||
sens = await sensor.new_sensor(config["Current_3"])
|
||||
cg.add(var.set_current_sensor_3(sens))
|
||||
if "Current_4" in config:
|
||||
sens = await sensor.new_sensor(config["Current_4"])
|
||||
cg.add(var.set_current_sensor_4(sens))
|
||||
if "Current_5" in config:
|
||||
sens = await sensor.new_sensor(config["Current_5"])
|
||||
cg.add(var.set_current_sensor_5(sens))
|
||||
if "Current_6" in config:
|
||||
sens = await sensor.new_sensor(config["Current_6"])
|
||||
cg.add(var.set_current_sensor_6(sens))
|
||||
if "Power_1" in config:
|
||||
sens = await sensor.new_sensor(config["Power_1"])
|
||||
cg.add(var.set_power_sensor_1(sens))
|
||||
if "Power_2" in config:
|
||||
sens = await sensor.new_sensor(config["Power_2"])
|
||||
cg.add(var.set_power_sensor_2(sens))
|
||||
if "Power_3" in config:
|
||||
sens = await sensor.new_sensor(config["Power_3"])
|
||||
cg.add(var.set_power_sensor_3(sens))
|
||||
if "Power_4" in config:
|
||||
sens = await sensor.new_sensor(config["Power_4"])
|
||||
cg.add(var.set_power_sensor_4(sens))
|
||||
if "Power_5" in config:
|
||||
sens = await sensor.new_sensor(config["Power_5"])
|
||||
cg.add(var.set_power_sensor_5(sens))
|
||||
if "Power_6" in config:
|
||||
sens = await sensor.new_sensor(config["Power_6"])
|
||||
cg.add(var.set_power_sensor_6(sens))
|
||||
if "Power_sum" in config:
|
||||
sens = await sensor.new_sensor(config["Power_sum"])
|
||||
cg.add(var.set_power_sensor_sum(sens))
|
||||
if "Energy_1" in config:
|
||||
sens = await sensor.new_sensor(config["Energy_1"])
|
||||
cg.add(var.set_energy_sensor_1(sens))
|
||||
if "Energy_2" in config:
|
||||
sens = await sensor.new_sensor(config["Energy_2"])
|
||||
cg.add(var.set_energy_sensor_2(sens))
|
||||
if "Energy_3" in config:
|
||||
sens = await sensor.new_sensor(config["Energy_3"])
|
||||
cg.add(var.set_energy_sensor_3(sens))
|
||||
if "Energy_4" in config:
|
||||
sens = await sensor.new_sensor(config["Energy_4"])
|
||||
cg.add(var.set_energy_sensor_4(sens))
|
||||
if "Energy_5" in config:
|
||||
sens = await sensor.new_sensor(config["Energy_5"])
|
||||
cg.add(var.set_energy_sensor_5(sens))
|
||||
if "Energy_6" in config:
|
||||
sens = await sensor.new_sensor(config["Energy_6"])
|
||||
cg.add(var.set_energy_sensor_6(sens))
|
||||
if "Energy_sum" in config:
|
||||
sens = await sensor.new_sensor(config["Energy_sum"])
|
||||
cg.add(var.set_energy_sensor_sum(sens))
|
||||
|
||||
Reference in New Issue
Block a user