espHome-NBS-files/external_components/analog_orp/analog_orp.cpp

71 lines
1.8 KiB
C++
Raw Normal View History

2024-05-10 10:37:19 +00:00
#include "analog_orp.h"
namespace esphome {
namespace analog_orp {
static const char *const TAG = "sensor.analog_orp";
void ChlorineSensor::set_pin(InternalGPIOPin *pin){
this->pin = pin;
}
void ChlorineSensor::set_zero_point(int zero_point){
this->zero_point_int = zero_point;
this->zero_point = ((float) zero_point / 1024.0f) * 3.3f; // Base Value for Clorine in Volt
}
void ChlorineSensor::set_print_raw(bool print_raw){
this->print_raw = print_raw;
}
void ChlorineSensor::set_inverted(bool inverted){
this->inverted = inverted;
}
void ChlorineSensor::setup(){
ESP_LOGCONFIG(TAG, "Setting up AnalogOrp '%s'...", this->get_name().c_str());
pin->setup();
ESP_LOGCONFIG(TAG, "AnalogOrp '%s' setup finished!", this->get_name().c_str());
}
void ChlorineSensor::update() {
float value_v = this->sample();
ESP_LOGV(TAG, "'%s': Got orp value=%.4fmV", this->get_name().c_str(), value_v);
this->publish_state(value_v);
}
float ChlorineSensor::get_setup_priority() const { return setup_priority::DATA; }
void ChlorineSensor::dump_config(){
LOG_SENSOR("", "AnalogOrp Sensor", this);
LOG_UPDATE_INTERVAL(this);
LOG_PIN(" Pin: ", pin);
ESP_LOGCONFIG(TAG, " Zero Point: %d", zero_point_int);
ESP_LOGCONFIG(TAG, " Inverted: %s", inverted ? "true" : "false");
ESP_LOGCONFIG(TAG, " Print Raw: %s", print_raw ? "true" : "false");
}
/*
Nullwert : 680
*/
float ChlorineSensor::sample(){
float raw_read = (float) analogRead(pin->get_pin());
if(print_raw){
ESP_LOGD(TAG, "analogRead read a raw value of: %.0f", raw_read);
}
float raw_voltage = (raw_read / 1024.0f) * 3.3f;
float messured_voltage = raw_voltage - zero_point;
if(inverted){
messured_voltage = messured_voltage * -1.0f;
}
float milli_volt = messured_voltage * 1000.0f;
return milli_volt;
}
}
}