mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-22 05:43:28 +02:00
41b73ff892
* Add CT Clamp component * Update lint * Some more fixes * Make updates to work as an analog sensor consumer * Remove unused imports Update lint suggestions * Move setup_priority to header * Remove unused calibration value * Remove Unique ID - Will be auto generated * Update to use loop and not slow down main loop Co-authored-by: Otto Winter <otto@otto-winter.com>
47 lines
1.4 KiB
C++
47 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include "esphome/core/component.h"
|
|
#include "esphome/core/esphal.h"
|
|
#include "esphome/components/sensor/sensor.h"
|
|
#include "esphome/components/voltage_sampler/voltage_sampler.h"
|
|
|
|
namespace esphome {
|
|
namespace ct_clamp {
|
|
|
|
class CTClampSensor : public sensor::Sensor, public PollingComponent {
|
|
public:
|
|
void update() override;
|
|
void loop() override;
|
|
void dump_config() override;
|
|
float get_setup_priority() const override { return setup_priority::DATA; }
|
|
|
|
void set_sample_duration(uint32_t sample_duration) { sample_duration_ = sample_duration; }
|
|
void set_source(voltage_sampler::VoltageSampler *source) { source_ = source; }
|
|
|
|
protected:
|
|
/// High Frequency loop() requester used during sampling phase.
|
|
HighFrequencyLoopRequester high_freq_;
|
|
|
|
/// Duration in ms of the sampling phase.
|
|
uint32_t sample_duration_;
|
|
/// The sampling source to read values from.
|
|
voltage_sampler::VoltageSampler *source_;
|
|
|
|
/** The DC offset of the circuit.
|
|
*
|
|
* Diagram: https://learn.openenergymonitor.org/electricity-monitoring/ct-sensors/interface-with-arduino
|
|
*
|
|
* This is automatically calculated with an exponential moving average/digital low pass filter.
|
|
*
|
|
* 0.5 is a good initial approximation to start with for most ESP8266 setups.
|
|
*/
|
|
float offset_ = 0.5f;
|
|
|
|
float sample_sum_ = 0.0f;
|
|
uint32_t num_samples_ = 0;
|
|
bool is_sampling_ = false;
|
|
};
|
|
|
|
} // namespace ct_clamp
|
|
} // namespace esphome
|