SensESP 3.6.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensesp::AnalogInput Class Reference

Sensor for reading the MCU analog input pins. More...

#include <sensesp/sensors/analog_input.h>

Inheritance diagram for sensesp::AnalogInput:
[legend]

Public Member Functions

 AnalogInput (uint8_t pin=A0, unsigned int read_delay=200, const String &config_path="", float output_scale=1024.)
 
virtual ~AnalogInput ()
 
virtual bool to_json (JsonObject &root) override
 
virtual bool from_json (const JsonObject &config) override
 
- Public Member Functions inherited from sensesp::Sensor< T >
 Sensor (String config_path)
 
- Public Member Functions inherited from sensesp::SensorConfig
 SensorConfig (const String &config_path)
 
- Public Member Functions inherited from sensesp::Observable
 Observable ()
 
 Observable (Observable &&other)
 Move constructor.
 
void notify ()
 
int attach (std::function< void()> observer)
 Attach an observer callback.
 
void detach (int id)
 Remove a previously attached observer by its ID.
 
- Public Member Functions inherited from sensesp::FileSystemSaveable
 FileSystemSaveable (const String &config_path)
 
virtual bool load () override
 Load and populate the object from a persistent storage.
 
virtual bool save () override
 Save the object to a persistent storage.
 
virtual bool clear () override
 Delete the data from a persistent storage.
 
bool find_config_file (const String &config_path, String &filename)
 
- Public Member Functions inherited from sensesp::Saveable
 Saveable (const String &config_path)
 
virtual bool refresh ()
 Refresh the object. This may or may not access the persistent storage but is not expected to overwrite the object's state.
 
const String & get_config_path () const
 
- Public Member Functions inherited from sensesp::ValueProducer< T >
 ValueProducer ()
 
 ValueProducer (const T &initial_value)
 
virtual const T & get () const
 
template<typename VConsumer >
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< T, typenameVConsumer::input_type >::value, std::shared_ptr< VConsumer > >::type connect_to (std::shared_ptr< VConsumer > consumer)
 Connect a producer to a transform with a different input type.
 
template<typename VConsumer >
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< T, typenameVConsumer::input_type >::value, VConsumer * >::type connect_to (VConsumer *consumer)
 
template<typename VConsumer >
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< T, typenameVConsumer::input_type >::value, VConsumer * >::type connect_to (VConsumer &consumer)
 
void emit (const T &new_value)
 

Protected Member Functions

void update ()
 

Protected Attributes

uint8_t pin_ {}
 
unsigned int read_delay_
 
float output_scale_
 
std::unique_ptr< BaseAnalogReaderanalog_reader_ {}
 
reactesp::RepeatEvent * repeat_event_ = nullptr
 
- Protected Attributes inherited from sensesp::Saveable
const String config_path_
 
- Protected Attributes inherited from sensesp::ValueProducer< T >
output_
 

Detailed Description

Sensor for reading the MCU analog input pins.

Read an analog input pin at regular intervals and return the result scaled onto a given range. The reading is calibrated in millivolts using the Arduino core's analogReadMilliVolts(), so it compensates for the ESP32 ADC's nonlinearity. The ADCs are still noisy; for high accuracy, SensESP supports the ADS1015 and ADS1115 ADCs.

Unlike RepeatSensor, AnalogInput can expose its read delay in the web configuration UI. Give it a config_path and pass it to ConfigItem() if you want to adjust the read interval at run time.

Parameters
[in]pinThe GPIO pin to read. Which pins are usable depends on the ESP32 variant. Note that pins on ADC2 cannot be read while Wi-Fi is active, which in practice limits you to ADC1 pins (GPIO 32..39 on the original ESP32).
[in]read_delayTime delay between consecutive readings, in ms
[in]config_pathConfiguration path for the sensor
[in]output_scaleThe scale of the converted input value that AnalogInput produces. The raw value width of the ADC varies between ESP32 variants, so AnalogInput normalizes the calibrated input voltage against a 3.3 V full-scale reference and multiplies that fraction by 'output_scale'. Output is therefore in the range 0 to 'output_scale', and the same code behaves identically across variants. The default value is 1024. If you want your output expressed as a percentage, make this parameter 100. If you want the actual voltage at the pin, make it 3.3 – that holds regardless of the ADC attenuation set with analogSetAttenuation(), because the reading is calibrated in millivolts before it is scaled. If you want the voltage on the input side of a physical voltage divider, make this parameter the voltage that the divider maps onto the pin's full-scale range.

Definition at line 48 of file analog_input.h.

Constructor & Destructor Documentation

◆ AnalogInput()

sensesp::AnalogInput::AnalogInput ( uint8_t  pin = A0,
unsigned int  read_delay = 200,
const String &  config_path = "",
float  output_scale = 1024. 
)

Definition at line 10 of file analog_input.cpp.

Here is the call graph for this function:

◆ ~AnalogInput()

virtual sensesp::AnalogInput::~AnalogInput ( )
inlinevirtual

Definition at line 53 of file analog_input.h.

Here is the call graph for this function:

Member Function Documentation

◆ from_json()

bool sensesp::AnalogInput::from_json ( const JsonObject &  root)
overridevirtual

Deserializes the current object data from a JsonObject.

Reimplemented from sensesp::Serializable.

Definition at line 34 of file analog_input.cpp.

◆ to_json()

bool sensesp::AnalogInput::to_json ( JsonObject &  root)
overridevirtual

Serializes the current object data into a JsonObject.

Reimplemented from sensesp::Serializable.

Definition at line 29 of file analog_input.cpp.

◆ update()

void sensesp::AnalogInput::update ( )
protected

Definition at line 25 of file analog_input.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ analog_reader_

std::unique_ptr<BaseAnalogReader> sensesp::AnalogInput::analog_reader_ {}
protected

Definition at line 66 of file analog_input.h.

◆ output_scale_

float sensesp::AnalogInput::output_scale_
protected

Definition at line 65 of file analog_input.h.

◆ pin_

uint8_t sensesp::AnalogInput::pin_ {}
protected

Definition at line 63 of file analog_input.h.

◆ read_delay_

unsigned int sensesp::AnalogInput::read_delay_
protected

Definition at line 64 of file analog_input.h.

◆ repeat_event_

reactesp::RepeatEvent* sensesp::AnalogInput::repeat_event_ = nullptr
protected

Definition at line 67 of file analog_input.h.


The documentation for this class was generated from the following files: