8 static const char *
const TAG =
"ina219";
28 static const uint8_t INA219_READ = 0x01;
29 static const uint8_t INA219_REGISTER_CONFIG = 0x00;
30 static const uint8_t INA219_REGISTER_SHUNT_VOLTAGE = 0x01;
31 static const uint8_t INA219_REGISTER_BUS_VOLTAGE = 0x02;
32 static const uint8_t INA219_REGISTER_POWER = 0x03;
33 static const uint8_t INA219_REGISTER_CURRENT = 0x04;
34 static const uint8_t INA219_REGISTER_CALIBRATION = 0x05;
37 ESP_LOGCONFIG(TAG,
"Setting up INA219...");
66 uint16_t config = 0x0000;
68 config |= 0b0000000000000111;
70 config |= 0b0000011110000000;
71 config |= 0b0000000001111000;
75 bool bus_32v_range = this->
max_voltage_v_ > 16.0f || shunt_max_voltage > 0.16f;
78 config |= 0b0010000000000000;
81 config |= 0b0000000000000000;
87 if (shunt_max_voltage * multiplier <= 0.02f) {
89 }
else if (shunt_max_voltage * multiplier <= 0.04f) {
91 }
else if (shunt_max_voltage * multiplier <= 0.08f) {
94 if (
int(shunt_max_voltage * multiplier * 100) > 16) {
96 " Max voltage across shunt resistor (resistance*current) exceeds %dmV. " 97 "This could damage the sensor!",
98 int(160 / multiplier));
103 config |= shunt_gain << 11;
104 ESP_LOGCONFIG(TAG,
" Using %dV-Range Shunt Gain=%dmV", bus_32v_range ? 32 : 16, 40 << shunt_gain);
110 auto min_lsb = uint32_t(ceilf(this->max_current_a_ * 1000000.0f / 0x8000));
111 auto max_lsb = uint32_t(floorf(this->max_current_a_ * 1000000.0f / 0x1000));
112 uint32_t lsb = min_lsb;
113 for (; lsb <= max_lsb; lsb++) {
114 float max_current_before_overflow = lsb * 0x7FFF / 1000000.0f;
115 if (this->max_current_a_ <= max_current_before_overflow)
120 ESP_LOGW(TAG,
" The requested current (%0.02fA) cannot be achieved without an overflow", this->max_current_a_);
125 ESP_LOGV(TAG,
" Using LSB=%" PRIu32
" calibration=%" PRIu32, lsb, calibration);
126 if (!this->
write_byte_16(INA219_REGISTER_CALIBRATION, calibration)) {
133 ESP_LOGCONFIG(TAG,
"INA219:");
134 LOG_I2C_DEVICE(
this);
137 ESP_LOGE(TAG,
"Communication with INA219 failed!");
140 LOG_UPDATE_INTERVAL(
this);
152 uint16_t raw_bus_voltage;
153 if (!this->
read_byte_16(INA219_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
157 raw_bus_voltage >>= 3;
158 float bus_voltage_v = int16_t(raw_bus_voltage) * 0.004f;
163 uint16_t raw_shunt_voltage;
164 if (!this->
read_byte_16(INA219_REGISTER_SHUNT_VOLTAGE, &raw_shunt_voltage)) {
168 float shunt_voltage_mv = int16_t(raw_shunt_voltage) * 0.01f;
173 uint16_t raw_current;
174 if (!this->
read_byte_16(INA219_REGISTER_CURRENT, &raw_current)) {
178 float current_ma = int16_t(raw_current) * (this->
calibration_lsb_ / 1000.0f);
184 if (!this->
read_byte_16(INA219_REGISTER_POWER, &raw_power)) {
188 float power_mw = int16_t(raw_power) * (this->
calibration_lsb_ * 20.0f / 1000.0f);
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.
void status_set_warning(const char *message="unspecified")
void dump_config() override
uint32_t calibration_lsb_
sensor::Sensor * current_sensor_
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
float get_setup_priority() const override
sensor::Sensor * bus_voltage_sensor_
sensor::Sensor * power_sensor_
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
float shunt_resistance_ohm_
bool write_byte_16(uint8_t a_register, uint16_t data)
sensor::Sensor * shunt_voltage_sensor_
void IRAM_ATTR HOT delay(uint32_t ms)