From 056990076ebc7300bfef2376ed9f9189c02bf102 Mon Sep 17 00:00:00 2001 From: Nicolas Bachschwell Date: Fri, 10 May 2024 12:37:19 +0200 Subject: [PATCH] Updated chlorine Pump --- chlorPump.yaml | 51 +++++++++++++ clorPump.yaml | 37 ---------- external_components/analog_orp/__init__.py | 0 external_components/analog_orp/analog_orp.cpp | 71 +++++++++++++++++++ external_components/analog_orp/analog_orp.h | 33 +++++++++ external_components/analog_orp/sensor.py | 50 +++++++++++++ 6 files changed, 205 insertions(+), 37 deletions(-) create mode 100644 chlorPump.yaml delete mode 100644 clorPump.yaml create mode 100644 external_components/analog_orp/__init__.py create mode 100644 external_components/analog_orp/analog_orp.cpp create mode 100644 external_components/analog_orp/analog_orp.h create mode 100644 external_components/analog_orp/sensor.py diff --git a/chlorPump.yaml b/chlorPump.yaml new file mode 100644 index 0000000..46acb39 --- /dev/null +++ b/chlorPump.yaml @@ -0,0 +1,51 @@ +esphome: + name: chlorine-pump + project: + name: nbsgamesat.chlorine-pump + version: "0.2" + +esp8266: + board: nodemcuv2 + +# Enable logging +logger: + +ota: + password: !secret cp_password + +external_components: + - source: + type: local + path: external_components/ + components: [ analog_orp ] + +api: + encryption: + key: !secret cp_key + +wifi: + ssid: !secret wifi_ssid + password: !secret wifi_password + fast_connect: true + +output: + - platform: gpio + pin: D3 + id: pump_switch + +binary_sensor: + - platform: gpio + id: pool_pump + pin: D2 + +sensor: + - platform: analog_orp + name: Chlorine + id: chlorine_sensor + pin: A0 + zero_point: 640 + inverted: true + update_interval: 2s +# GPIO A0 to acd + +# Here is space for any display implementation that could possibliy be. Have fun OK \ No newline at end of file diff --git a/clorPump.yaml b/clorPump.yaml deleted file mode 100644 index ec5a52b..0000000 --- a/clorPump.yaml +++ /dev/null @@ -1,37 +0,0 @@ -esphome: - name: Clor_Pump - platform: ESP8266 - board: nodemcuv2 - -# Enable logging -logger: - -ota: - password: !secret cp_password - -api: - encryption: - key: !secret cp_key - -wifi: - ssid: !secret wifi_ssid - password: !secret wifi_passwd - fast_connect: true - -switch: - - platform: gpio - pin: D3 - id: pump_switch - - platform: custom - name: Clor Pump - -button: - - platform: custom - name: prime - -binary_sensor: - - platform: gpio - id: pool_pump - pin: D2 - -# Here is space for any display implementation that could possibliy be. Have fun OK \ No newline at end of file diff --git a/external_components/analog_orp/__init__.py b/external_components/analog_orp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/external_components/analog_orp/analog_orp.cpp b/external_components/analog_orp/analog_orp.cpp new file mode 100644 index 0000000..6df9de6 --- /dev/null +++ b/external_components/analog_orp/analog_orp.cpp @@ -0,0 +1,71 @@ +#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; +} + +} +} \ No newline at end of file diff --git a/external_components/analog_orp/analog_orp.h b/external_components/analog_orp/analog_orp.h new file mode 100644 index 0000000..3273458 --- /dev/null +++ b/external_components/analog_orp/analog_orp.h @@ -0,0 +1,33 @@ +#pragma once + +#include "esphome.h" +#include "esphome/components/sensor/sensor.h" +#include "esphome/core/component.h" +#include "esphome/core/gpio.h" + +namespace esphome { +namespace analog_orp { + +class ChlorineSensor: public PollingComponent, public sensor::Sensor { + public: + void set_pin(InternalGPIOPin *pin); + void set_zero_point(int zero_point); + void set_print_raw(bool print_raw); + void set_inverted(bool inverted); + void setup() override; + void update() override; + float sample(); + void dump_config() override; + float get_setup_priority() const; + protected: + InternalGPIOPin *pin; + bool inverted; + bool print_raw; + float zero_point; + int zero_point_int; + +}; + + +} +} \ No newline at end of file diff --git a/external_components/analog_orp/sensor.py b/external_components/analog_orp/sensor.py new file mode 100644 index 0000000..af266a1 --- /dev/null +++ b/external_components/analog_orp/sensor.py @@ -0,0 +1,50 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import sensor +from esphome.components.adc import sensor as adc +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_VOLTAGE, + STATE_CLASS_MEASUREMENT +) + +chlorine_sensor_ns = cg.esphome_ns.namespace('analog_orp') +ChlorineSensor = chlorine_sensor_ns.class_('ChlorineSensor', cg.PollingComponent, sensor.Sensor) + +CONF_SENSOR_PIN = "pin" +CONF_INVERTED = "inverted" +CONF_PRINT_RAW = "print_raw" +CONF_ZERO_POINT = "zero_point" + +UNIT_MILLI_VOLT = "mV" + +ICON_LIGHTNING_BOLT="mdi:lightning-bolt" + +CONFIG_SCHEMA = cv.All( + sensor.sensor_schema( + ChlorineSensor, + unit_of_measurement=UNIT_MILLI_VOLT, + accuracy_decimals=2, + device_class=DEVICE_CLASS_VOLTAGE, + state_class=STATE_CLASS_MEASUREMENT, + icon=ICON_LIGHTNING_BOLT + ).extend({ + cv.Required(CONF_SENSOR_PIN): adc.validate_adc_pin, + cv.Required(CONF_ZERO_POINT): cv.Range(min=0, max=1023), + cv.Optional(CONF_INVERTED, False): cv.boolean, + cv.Optional(CONF_PRINT_RAW, False): cv.boolean + } + ).extend(cv.polling_component_schema("60s")) +) + +def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + yield cg.register_component(var, config) + yield sensor.register_sensor(var, config) + + sensor_pin = yield cg.gpio_pin_expression(config[CONF_SENSOR_PIN]) + cg.add(var.set_pin(sensor_pin)) + + cg.add(var.set_inverted(config[CONF_INVERTED])) + cg.add(var.set_print_raw(config[CONF_PRINT_RAW])) + cg.add(var.set_zero_point(config[CONF_ZERO_POINT])) \ No newline at end of file