Add CT Clamp component (#559)

* 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>
This commit is contained in:
Jesse Hills
2019-05-29 21:15:08 +12:00
committed by Otto Winter
parent 15cb0e4ff3
commit 3089ffa8e7
5 changed files with 146 additions and 0 deletions
@@ -0,0 +1,46 @@
#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