mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-21 05:23:27 +02:00
Add NTC and resistance sensor (#560)
* Add NTC and resistance sensor Fixes https://github.com/esphome/feature-requests/issues/248 * Fix * Fix platformio4 moved get_project_dir
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
#include "resistance_sensor.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace resistance {
|
||||
|
||||
static const char *TAG = "resistance";
|
||||
|
||||
void ResistanceSensor::dump_config() {
|
||||
LOG_SENSOR("", "Resistance Sensor", this);
|
||||
ESP_LOGCONFIG(TAG, " Configuration: %s", this->configuration_ == UPSTREAM ? "UPSTREAM" : "DOWNSTREAM");
|
||||
ESP_LOGCONFIG(TAG, " Resistor: %.2fΩ", this->resistor_);
|
||||
ESP_LOGCONFIG(TAG, " Reference Voltage: %.1fV", this->reference_voltage_);
|
||||
}
|
||||
void ResistanceSensor::process_(float value) {
|
||||
if (isnan(value)) {
|
||||
this->publish_state(NAN);
|
||||
return;
|
||||
}
|
||||
float res = 0;
|
||||
switch (this->configuration_) {
|
||||
case UPSTREAM:
|
||||
if (value == 0.0f)
|
||||
res = NAN;
|
||||
else
|
||||
res = (this->reference_voltage_ - value) / value;
|
||||
break;
|
||||
case DOWNSTREAM:
|
||||
if (value == this->reference_voltage_)
|
||||
res = NAN;
|
||||
else
|
||||
res = value / (this->reference_voltage_ - value);
|
||||
break;
|
||||
}
|
||||
|
||||
res *= this->resistor_;
|
||||
ESP_LOGD(TAG, "'%s' - Resistance %.1fΩ", this->name_.c_str(), res);
|
||||
this->publish_state(res);
|
||||
}
|
||||
|
||||
} // namespace resistance
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user