From fd6d54697cd87ca0149536e7a19cb6dff662cdca Mon Sep 17 00:00:00 2001 From: Nicolas Bachschwell Date: Fri, 24 May 2024 16:00:34 +0200 Subject: [PATCH] Added all the changes necessary to run chlorine pump again --- chlorPump.yaml | 44 ++++++++++++------- external_components/chlorine_pump/__init__.py | 21 ++++++--- .../chlorine_pump/pump_cycle.h | 26 +++++------ 3 files changed, 54 insertions(+), 37 deletions(-) diff --git a/chlorPump.yaml b/chlorPump.yaml index f76e5e5..591ccf7 100644 --- a/chlorPump.yaml +++ b/chlorPump.yaml @@ -2,11 +2,11 @@ esphome: name: chlorine-pump project: name: nbsgamesat.chlorine-pump - version: "0.2" + version: "0.7" on_boot: priority: 600 then: - output.turn_on: whatever + output.turn_on: wifi_status_led esp8266: board: nodemcuv2 @@ -21,7 +21,7 @@ external_components: - source: type: local path: external_components/ - components: [ analog_orp, chlorine_pump ] + components: [ chlorine_pump ] globals: - id: last_pump_state @@ -40,7 +40,9 @@ wifi: password: !secret wifi_password fast_connect: true on_connect: - output.turn_off: whatever + output.turn_off: wifi_status_led + on_disconnect: + output.turn_on: wifi_status_led output: - platform: gpio @@ -50,7 +52,7 @@ output: pin: number: D0 inverted: true - id: whatever + id: wifi_status_led binary_sensor: - platform: gpio @@ -137,20 +139,22 @@ text_sensor: id: cycle_text_info sensor: - - platform: analog_orp + - platform: adc name: Chlorine id: chlorine_sensor + unit_of_measurement: "mV" + icon: "mdi:test-tube" pin: A0 - zero_point: 673 - inverted: true - update_interval: 1s -# print_raw: true - average: - mesurements: 15 - send_state_every: 10 -# on_value_read: -# - lambda: |- -# ESP_LOGD("WHAT", "Chlorine_value, %.1f", x); + update_interval: 200ms + filters: + - multiply: 3.3 + - offset: -1.5 + - multiply: 1000.0 + - median: + window_size: 25 + send_every: 25 + send_first_at: 25 + - offset: 30.0 - platform: hx711 name: "Chlorine Canister Levels" dout_pin: D5 @@ -163,13 +167,19 @@ sensor: - 47608 -> 0 - 590566 -> 100 unit_of_measurement: "%" + - platform: template + name: debug_last_raw_value + id: debug_last_raw_value + accuracy_decimals: 0 + unit_of_measurement: "points" chlorine_pump: pump: pump_switch sensor: chlorine_sensor id: chlorine_pump_component target: 700 - disable_clock: true + disable_clock: false + proportional_band: 400 on_pump_value: - lambda: |- if(pState != id(last_pump_state)){ diff --git a/external_components/chlorine_pump/__init__.py b/external_components/chlorine_pump/__init__.py index fedcac0..6028e90 100644 --- a/external_components/chlorine_pump/__init__.py +++ b/external_components/chlorine_pump/__init__.py @@ -1,8 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import automation -from esphome.components import output -from esphome.components.analog_orp.sensor import ChlorineSensor +from esphome.components import output, sensor from esphome.const import ( CONF_ID, CONF_TRIGGER_ID @@ -22,6 +21,7 @@ CONF_PUMP_VALUE = "on_pump_value" CONF_CYCLE_START = "on_cycle_start" CONF_DISABLE_CLOCK="disable_clock" CONF_TARGET="target" +CONF_PUMP_TIME_DIVIDER = "pump_time_divider" def to_proportional_band(value): try: @@ -29,7 +29,7 @@ def to_proportional_band(value): except (TypeError, ValueError): # pylint: disable=raise-missing-from raise cv.Invalid(f"") - return cv.one_of(20, 50, 100, 150, 200, 250)(value) + return cv.one_of(20, 50, 100, 150, 200, 300, 400)(value) ChlorSensorOutputTrigger = chlorine_pump_ns.class_( "ChlorSensorOutputTrigger", automation.Trigger.template(cg.bool_, cg.int_) @@ -42,15 +42,17 @@ PrimeAction = chlorine_pump_ns.class_("ChlorinePrime", automation.Action) StartAction = chlorine_pump_ns.class_("ChlorineStart", automation.Action) StopAction = chlorine_pump_ns.class_("ChlorineStop", automation.Action) SetTargetAction = chlorine_pump_ns.class_("ChlorineSetTarget", automation.Action) +ManualDoseAction = chlorine_pump_ns.class_("ChlorineDose", automation.Action) CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(ChlorinePump), cv.Required(CONF_PUMP_OUT): cv.use_id(output.BinaryOutput), - cv.Optional(CONF_SENSOR): cv.use_id(ChlorineSensor), + cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor), cv.Optional(CONF_PUMP_CYCLE_TIME, default=360): cv.int_range(min=30, max=1400), cv.Optional(CONF_PUMP_PROPORTIONAL_BAND, default=200): to_proportional_band, cv.Optional(CONF_DISABLE_CLOCK, default=False): cv.boolean, cv.Optional(CONF_TARGET, 700): cv.int_range(300, 1400), + cv.Optional(CONF_PUMP_TIME_DIVIDER, 2): cv.float_range(1, 5), cv.Optional(CONF_PUMP_VALUE): automation.validate_automation({ cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorSensorOutputTrigger), } @@ -82,6 +84,11 @@ async def stop_action_to_code(config, action_id, template_arg, args): paren = await cg.get_variable(config[CONF_ID]) return cg.new_Pvariable(action_id, template_arg, paren) +@automation.register_action("chlorine_pump.manual_dose", StopAction, ACTION_SCHEMA) +async def stop_action_to_code(config, action_id, template_arg, args): + paren = await cg.get_variable(config[CONF_ID]) + return cg.new_Pvariable(action_id, template_arg, paren) + @automation.register_action("chlorine_pump.set_target", SetTargetAction, ACTION_SCHEMA.extend({ cv.Required(CONF_TARGET): cv.templatable(cv.float_), })) @@ -100,9 +107,9 @@ async def to_code(config): #btnReset = yield cg.gpio_pin_expression(config[CONF_SENSOR_PIN]) #cg.add(var.set_input_pin(btnReset)) - - sensor = await cg.get_variable(config[CONF_SENSOR]) - cg.add(var.set_sensor(sensor)) + if CONF_SENSOR in config: + sensor = await cg.get_variable(config[CONF_SENSOR]) + cg.add(var.set_sensor(sensor)) pump_out = await cg.get_variable(config[CONF_PUMP_OUT]) cg.add(var.set_pump_out(pump_out)) diff --git a/external_components/chlorine_pump/pump_cycle.h b/external_components/chlorine_pump/pump_cycle.h index 98b96dc..97f9baf 100644 --- a/external_components/chlorine_pump/pump_cycle.h +++ b/external_components/chlorine_pump/pump_cycle.h @@ -2,7 +2,6 @@ #include "esphome/core/automation.h" #include "esphome/components/gpio/output/gpio_binary_output.h" -#include "esphome/components/analog_orp/analog_orp.h" static const char *const TAG = "chlorine_pump"; @@ -12,7 +11,7 @@ namespace chlorine_pump { class ChlorinePump : public Component { protected: - analog_orp::ChlorineSensor *sensor_; + sensor::Sensor *sensor_; gpio::GPIOBinaryOutput *pump_out; int cycle_time; int prop_band; @@ -24,6 +23,7 @@ class ChlorinePump : public Component { int tOn = 0; int tOff = 0; float target_ = 700.0f; + float tOn_divider_; public: @@ -35,9 +35,12 @@ class ChlorinePump : public Component { void disable_clock(bool disable_clock){ this->disable_clock_ = disable_clock; } - void set_sensor(analog_orp::ChlorineSensor *sensor){ + void set_sensor(sensor::Sensor *sensor){ this->sensor_ = sensor; } + void set_pump_time_divivder(float pump_time_divider){ + this->tOn_divider_ = pump_time_divider; + } void set_pump_out(gpio::GPIOBinaryOutput *pump_out){ this->pump_out = pump_out; } @@ -54,10 +57,7 @@ class ChlorinePump : public Component { void setup() override { last_action = millis(); if(disable_clock_ && sensor_ != nullptr){ - if(!sensor_->has_averager()) sensor_->add_on_state_callback([=](float val) -> void { - this->tick_time(val); - }); - if(sensor_->has_averager()) sensor_->add_average_change_callback([=](float val) -> void { + sensor_->add_on_state_callback([=](float val) -> void { this->tick_time(val); }); } @@ -119,14 +119,13 @@ class ChlorinePump : public Component { if(tOn == 0 && tOff == 0 && state){ int seconds = calculatePumpTimeSeconds(last_mesurement); + tOn = seconds; + tOff = cycle_time - tOn; + if(seconds == 0){ tOn = 0; - tOff = 30; + tOff = 360; } - - tOn = seconds; - tOff = cycle_time - tOn; - ESP_LOGD(TAG, "Time => tOn: %d, tOff: %d", tOn, tOff); this->callback_cycle_.call(tOn, tOff); @@ -149,7 +148,8 @@ class ChlorinePump : public Component { if(millis() - last_action > 1000){ - tick_time(sensor_->get_state()); + if(sensor_->has_state()) + tick_time(sensor_->get_state()); setMillisPrecies(1000); }