8 static const char *
const TAG =
"ads1115";
9 static const uint8_t ADS1115_REGISTER_CONVERSION = 0x00;
10 static const uint8_t ADS1115_REGISTER_CONFIG = 0x01;
12 static const uint8_t ADS1115_DATA_RATE_860_SPS = 0b111;
15 ESP_LOGCONFIG(TAG,
"Setting up ADS1115...");
17 if (!this->
read_byte_16(ADS1115_REGISTER_CONVERSION, &value)) {
22 ESP_LOGCONFIG(TAG,
"Configuring ADS1115...");
27 config |= 0b0000000000000000;
39 config |= 0b0000000000000000;
43 config |= 0b0000000100000000;
48 config |= ADS1115_DATA_RATE_860_SPS << 5;
52 config |= 0b0000000000000000;
56 config |= 0b0000000000000000;
60 config |= 0b0000000000000000;
64 config |= 0b0000000000000011;
73 ESP_LOGCONFIG(TAG,
"Setting up ADS1115...");
76 ESP_LOGE(TAG,
"Communication with ADS1115 failed!");
79 for (
auto *sensor : this->
sensors_) {
80 LOG_SENSOR(
" ",
"Sensor", sensor);
81 ESP_LOGCONFIG(TAG,
" Multiplexer: %u", sensor->get_multiplexer());
82 ESP_LOGCONFIG(TAG,
" Gain: %u", sensor->get_gain());
83 ESP_LOGCONFIG(TAG,
" Resolution: %u", sensor->get_resolution());
90 config &= 0b1000111111111111;
95 config &= 0b1111000111111111;
96 config |= (sensor->
get_gain() & 0b111) << 9;
100 config |= 0b1000000000000000;
117 uint32_t start =
millis();
118 while (this->
read_byte_16(ADS1115_REGISTER_CONFIG, &config) && (config >> 15) == 0) {
119 if (
millis() - start > 100) {
120 ESP_LOGW(TAG,
"Reading ADS1115 timed out");
129 uint16_t raw_conversion;
130 if (!this->
read_byte_16(ADS1115_REGISTER_CONVERSION, &raw_conversion)) {
136 bool negative = (raw_conversion >> 15) == 1;
145 raw_conversion |= 0x8000;
147 raw_conversion &= 0xF7FF;
151 auto signed_conversion =
static_cast<int16_t
>(raw_conversion);
157 millivolts = (signed_conversion * 6144) / divider;
160 millivolts = (signed_conversion * 4096) / divider;
163 millivolts = (signed_conversion * 2048) / divider;
166 millivolts = (signed_conversion * 1024) / divider;
169 millivolts = (signed_conversion * 512) / divider;
172 millivolts = (signed_conversion * 256) / divider;
179 return millivolts / 1e3f;
184 float v = this->parent_->request_measurement(
this);
185 if (!std::isnan(v)) {
186 ESP_LOGD(TAG,
"'%s': Got Voltage=%fV", this->get_name().c_str(), v);
187 this->publish_state(v);
bool read_byte_16(uint8_t a_register, uint16_t *data)
uint8_t get_multiplexer() const
float request_measurement(ADS1115Sensor *sensor)
Helper method to request a measurement from a sensor.
uint8_t get_resolution() const
uint32_t IRAM_ATTR HOT millis()
Internal holder class that is in instance of Sensor so that the hub can create individual sensors...
void dump_config() override
void status_clear_warning()
void status_set_warning()
void IRAM_ATTR HOT yield()
std::vector< ADS1115Sensor * > sensors_
virtual void mark_failed()
Mark this component as failed.
void setup() override
Set up the internal sensor array.
bool write_byte_16(uint8_t a_register, uint16_t data)
void IRAM_ATTR HOT delay(uint32_t ms)