ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ct_clamp_sensor.cpp
Go to the documentation of this file.
1#include "ct_clamp_sensor.h"
2
3#include "esphome/core/log.h"
4#include <cinttypes>
5#include <cmath>
6
8
9ESPHOME_LOG_TAG(TAG, "ct_clamp");
10
11static constexpr uint32_t READ_TIMEOUT_ID = 0;
12
14 LOG_SENSOR("", "CT Clamp Sensor", this);
15 ESP_LOGCONFIG(TAG, " Sample Duration: %.2fs", this->sample_duration_ / 1e3f);
16 LOG_UPDATE_INTERVAL(this);
17}
18
20 // Update only starts the sampling phase, in loop() the actual sampling is happening.
21
22 // Request a high loop() execution interval during sampling phase.
23 this->high_freq_.start();
24
25 // Set timeout for ending sampling phase
26 this->set_timeout(READ_TIMEOUT_ID, this->sample_duration_, [this]() {
27 this->is_sampling_ = false;
28 this->high_freq_.stop();
29
30 if (this->num_samples_ == 0) {
31 // Shouldn't happen, but let's not crash if it does.
32 this->publish_state(NAN);
33 return;
34 }
35
36 const float rms_ac_dc_squared = this->sample_squared_sum_ / this->num_samples_;
37 const float rms_dc = this->sample_sum_ / this->num_samples_;
38 const float rms_ac_squared = rms_ac_dc_squared - rms_dc * rms_dc;
39 float rms_ac = 0;
40 if (rms_ac_squared > 0)
41 rms_ac = std::sqrt(rms_ac_squared);
42 ESP_LOGD(TAG, "'%s' - Raw AC Value: %.3fA after %" PRIu32 " different samples (%" PRIu32 " SPS)",
43 LOG_STR_ARG(this->get_log_name()), rms_ac, this->num_samples_,
44 1000 * this->num_samples_ / this->sample_duration_);
45 this->publish_state(rms_ac);
46 });
47
48 // Set sampling values
49 this->last_value_ = 0.0;
50 this->num_samples_ = 0;
51 this->sample_sum_ = 0.0f;
52 this->sample_squared_sum_ = 0.0f;
53 this->is_sampling_ = true;
54}
55
57 if (!this->is_sampling_)
58 return;
59
60 // Perform a single sample
61 float value = this->source_->sample();
62 if (std::isnan(value))
63 return;
64
65 // Assuming a sine wave, avoid requesting values faster than the ADC can provide them
66 if (this->last_value_ == value)
67 return;
68 this->last_value_ = value;
69
70 this->num_samples_++;
71 this->sample_sum_ += value;
72 this->sample_squared_sum_ += value * value;
73}
74
75} // namespace esphome::ct_clamp
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
void stop()
Stop running the loop continuously.
Definition helpers.cpp:841
void start()
Start running the loop continuously.
Definition helpers.cpp:835
voltage_sampler::VoltageSampler * source_
The sampling source to read values from.
float last_value_
The DC offset of the circuit.
uint32_t sample_duration_
Duration in ms of the sampling phase.
HighFrequencyLoopRequester high_freq_
High Frequency loop() requester used during sampling phase.
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
virtual float sample()=0
Get a voltage reading, in V.
ESPHOME_LOG_TAG(TAG, "ct_clamp")
static void uint32_t