esphome/esphome/components/ain4_20ma/ain4_20ma.cpp

32 lines
706 B
C++
Raw Normal View History

#include "ain4_20ma.h"
#include "esphome/core/log.h"
namespace esphome {
2024-05-02 11:57:06 +02:00
namespace ain4_20ma {
static const char *const TAG = "ain4_20ma";
2024-05-02 11:01:51 +02:00
void Ain420maComponent::dump_config() {
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() {
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) {
ESP_LOGE(TAG, "Error reading data from AIN4-20mA");
this->publish_state(NAN);
2024-05-01 17:13:19 +02:00
} else {
uint16_t value;
value = data[0] | (data[1] << 8);
this->publish_state(value);
}
}
2024-05-02 11:57:06 +02:00
} // namespace ain4_20ma
} // namespace esphome