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