ESPHome 2025.6.3
Loading...
Searching...
No Matches
adc_sensor_esp8266.cpp
Go to the documentation of this file.
1#ifdef USE_ESP8266
2
3#include "adc_sensor.h"
5#include "esphome/core/log.h"
6
7#ifdef USE_ADC_SENSOR_VCC
8#include <Esp.h>
9ADC_MODE(ADC_VCC)
10#else
11#include <Arduino.h>
12#endif // USE_ADC_SENSOR_VCC
13
14namespace esphome {
15namespace adc {
16
17static const char *const TAG = "adc.esp8266";
18
19void ADCSensor::setup() {
20 ESP_LOGCONFIG(TAG, "Running setup for '%s'", this->get_name().c_str());
21#ifndef USE_ADC_SENSOR_VCC
22 this->pin_->setup();
23#endif
24}
25
27 LOG_SENSOR("", "ADC Sensor", this);
28#ifdef USE_ADC_SENSOR_VCC
29 ESP_LOGCONFIG(TAG, " Pin: VCC");
30#else
31 LOG_PIN(" Pin: ", this->pin_);
32#endif // USE_ADC_SENSOR_VCC
33 ESP_LOGCONFIG(TAG,
34 " Samples: %i\n"
35 " Sampling mode: %s",
36 this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
37 LOG_UPDATE_INTERVAL(this);
38}
39
40float ADCSensor::sample() {
41 auto aggr = Aggregator(this->sampling_mode_);
42
43 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
44 uint32_t raw = 0;
45#ifdef USE_ADC_SENSOR_VCC
46 raw = ESP.getVcc(); // NOLINT(readability-static-accessed-through-instance)
47#else
48 raw = analogRead(this->pin_->get_pin()); // NOLINT
49#endif // USE_ADC_SENSOR_VCC
50 aggr.add_sample(raw);
51 }
52
53 if (this->output_raw_) {
54 return aggr.aggregate();
55 }
56 return aggr.aggregate() / 1024.0f;
57}
58
59std::string ADCSensor::unique_id() { return get_mac_address() + "-adc"; }
60
61} // namespace adc
62} // namespace esphome
63
64#endif // USE_ESP8266
ADC_MODE(ADC_VCC) namespace esphome
uint8_t raw[35]
Definition bl0939.h:0
const StringRef & get_name() const
virtual void setup()=0
virtual uint8_t get_pin() const =0
std::string unique_id() override
void setup() override
Setup ADC.
InternalGPIOPin * pin_
Definition adc_sensor.h:84
SamplingMode sampling_mode_
Definition adc_sensor.h:87
const LogString * sampling_mode_to_str(SamplingMode mode)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition helpers.cpp:726