ESPHome  2024.4.1
resistance_sensor.cpp
Go to the documentation of this file.
1 #include "resistance_sensor.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace resistance {
6 
7 static const char *const TAG = "resistance";
8 
10  LOG_SENSOR("", "Resistance Sensor", this);
11  ESP_LOGCONFIG(TAG, " Configuration: %s", this->configuration_ == UPSTREAM ? "UPSTREAM" : "DOWNSTREAM");
12  ESP_LOGCONFIG(TAG, " Resistor: %.2fΩ", this->resistor_);
13  ESP_LOGCONFIG(TAG, " Reference Voltage: %.1fV", this->reference_voltage_);
14 }
15 void ResistanceSensor::process_(float value) {
16  if (std::isnan(value)) {
17  this->publish_state(NAN);
18  return;
19  }
20  float res = 0;
21  switch (this->configuration_) {
22  case UPSTREAM:
23  if (value == 0.0f) {
24  res = NAN;
25  } else {
26  res = (this->reference_voltage_ - value) / value;
27  }
28  break;
29  case DOWNSTREAM:
30  if (value == this->reference_voltage_) {
31  res = NAN;
32  } else {
33  res = value / (this->reference_voltage_ - value);
34  }
35  break;
36  }
37 
38  res *= this->resistor_;
39  ESP_LOGD(TAG, "'%s' - Resistance %.1fΩ", this->name_.c_str(), res);
40  this->publish_state(res);
41 }
42 
43 } // namespace resistance
44 } // namespace esphome
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
ResistanceConfiguration configuration_
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