8 static const char *
const TAG =
"ina226";
28 static const uint8_t INA226_REGISTER_CONFIG = 0x00;
29 static const uint8_t INA226_REGISTER_SHUNT_VOLTAGE = 0x01;
30 static const uint8_t INA226_REGISTER_BUS_VOLTAGE = 0x02;
31 static const uint8_t INA226_REGISTER_POWER = 0x03;
32 static const uint8_t INA226_REGISTER_CURRENT = 0x04;
33 static const uint8_t INA226_REGISTER_CALIBRATION = 0x05;
36 ESP_LOGCONFIG(TAG,
"Setting up INA226...");
45 uint16_t config = 0x0000;
48 config |= 0b0000001000000000;
51 config |= 0b0000000100000000;
54 config |= 0b0000000000100000;
57 config |= 0b0000000000000111;
65 uint32_t lsb =
static_cast<uint32_t
>(ceilf(this->
max_current_a_ * 1000000.0f / 32768));
71 ESP_LOGV(TAG,
" Using LSB=%u calibration=%u", lsb, calibration);
73 if (!this->
write_byte_16(INA226_REGISTER_CALIBRATION, calibration)) {
80 ESP_LOGCONFIG(TAG,
"INA226:");
84 ESP_LOGE(TAG,
"Communication with INA226 failed!");
87 LOG_UPDATE_INTERVAL(
this);
99 uint16_t raw_bus_voltage;
100 if (!this->
read_byte_16(INA226_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
104 float bus_voltage_v = int16_t(raw_bus_voltage) * 0.00125f;
109 uint16_t raw_shunt_voltage;
110 if (!this->
read_byte_16(INA226_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
114 float shunt_voltage_v = int16_t(raw_shunt_voltage) * 0.0000025f;
119 uint16_t raw_current;
120 if (!this->
read_byte_16(INA226_REGISTER_CURRENT, &raw_current)) {
124 float current_ma = int16_t(raw_current) * (this->
calibration_lsb_ / 1000.0f);
130 if (!this->
read_byte_16(INA226_REGISTER_POWER, &raw_power)) {
134 float power_mw = int16_t(raw_power) * (this->
calibration_lsb_ * 25.0f / 1000.0f);
sensor::Sensor * power_sensor_
bool read_byte_16(uint8_t a_register, uint16_t *data)
const float DATA
For components that import data from directly connected sensors like DHT.
uint32_t calibration_lsb_
sensor::Sensor * bus_voltage_sensor_
sensor::Sensor * current_sensor_
float get_setup_priority() const override
float shunt_resistance_ohm_
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
void status_set_warning()
virtual void mark_failed()
Mark this component as failed.
bool write_byte_16(uint8_t a_register, uint16_t data)
void dump_config() override
sensor::Sensor * shunt_voltage_sensor_
void IRAM_ATTR HOT delay(uint32_t ms)