2024-05-01 16:41:09 +02:00
|
|
|
#include "ain4_20ma.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
2024-05-02 11:57:06 +02:00
|
|
|
namespace ain4_20ma {
|
2024-05-01 16:41:09 +02:00
|
|
|
|
|
|
|
static const char *const TAG = "ain4_20ma";
|
|
|
|
|
2024-05-02 11:01:51 +02:00
|
|
|
void Ain420maComponent::dump_config() {
|
2024-05-01 16:41:09 +02:00
|
|
|
ESP_LOGCONFIG(TAG, "AIN4-20mA Sensor:");
|
|
|
|
LOG_I2C_DEVICE(this);
|
|
|
|
LOG_UPDATE_INTERVAL(this);
|
|
|
|
LOG_SENSOR(" ", "Sensor:", this);
|
|
|
|
}
|
|
|
|
|
2024-05-02 11:01:51 +02:00
|
|
|
void Ain420maComponent::update() {
|
2024-05-01 16:41:09 +02:00
|
|
|
uint8_t data[2];
|
|
|
|
|
2024-05-02 10:29:05 +02:00
|
|
|
i2c::ErrorCode err = this->read_register(0x20, data, 2);
|
2024-05-02 07:31:21 +02:00
|
|
|
if (err != i2c::ERROR_OK) {
|
2024-05-01 16:41:09 +02:00
|
|
|
ESP_LOGE(TAG, "Error reading data from AIN4-20mA");
|
|
|
|
this->publish_state(NAN);
|
2024-05-01 17:13:19 +02:00
|
|
|
} else {
|
2024-05-01 16:41:09 +02:00
|
|
|
uint16_t value;
|
|
|
|
value = data[0] | (data[1] << 8);
|
|
|
|
this->publish_state(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 11:57:06 +02:00
|
|
|
} // namespace ain4_20ma
|
2024-05-01 16:41:09 +02:00
|
|
|
} // namespace esphome
|