ESPHome  2024.3.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 <cmath>
5 
6 namespace esphome {
7 namespace ct_clamp {
8 
9 static const char *const TAG = "ct_clamp";
10 
12  LOG_SENSOR("", "CT Clamp Sensor", this);
13  ESP_LOGCONFIG(TAG, " Sample Duration: %.2fs", this->sample_duration_ / 1e3f);
14  LOG_UPDATE_INTERVAL(this);
15 }
16 
18  // Update only starts the sampling phase, in loop() the actual sampling is happening.
19 
20  // Request a high loop() execution interval during sampling phase.
21  this->high_freq_.start();
22 
23  // Set timeout for ending sampling phase
24  this->set_timeout("read", this->sample_duration_, [this]() {
25  this->is_sampling_ = false;
26  this->high_freq_.stop();
27 
28  if (this->num_samples_ == 0) {
29  // Shouldn't happen, but let's not crash if it does.
30  this->publish_state(NAN);
31  return;
32  }
33 
34  const float rms_ac_dc_squared = this->sample_squared_sum_ / this->num_samples_;
35  const float rms_dc = this->sample_sum_ / this->num_samples_;
36  const float rms_ac_squared = rms_ac_dc_squared - rms_dc * rms_dc;
37  float rms_ac = 0;
38  if (rms_ac_squared > 0)
39  rms_ac = std::sqrt(rms_ac_squared);
40  ESP_LOGD(TAG, "'%s' - Raw AC Value: %.3fA after %d different samples (%d SPS)", this->name_.c_str(), rms_ac,
41  this->num_samples_, 1000 * this->num_samples_ / this->sample_duration_);
42  this->publish_state(rms_ac);
43  });
44 
45  // Set sampling values
46  this->last_value_ = 0.0;
47  this->num_samples_ = 0;
48  this->sample_sum_ = 0.0f;
49  this->sample_squared_sum_ = 0.0f;
50  this->is_sampling_ = true;
51 }
52 
54  if (!this->is_sampling_)
55  return;
56 
57  // Perform a single sample
58  float value = this->source_->sample();
59  if (std::isnan(value))
60  return;
61 
62  // Assuming a sine wave, avoid requesting values faster than the ADC can provide them
63  if (this->last_value_ == value)
64  return;
65  this->last_value_ = value;
66 
67  this->num_samples_++;
68  this->sample_sum_ += value;
69  this->sample_squared_sum_ += value * value;
70 }
71 
72 } // namespace ct_clamp
73 } // 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:547
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:553
constexpr const char * c_str() const
Definition: string_ref.h:68
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7