Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
hempy.h
Go to the documentation of this file.
1#pragma once
2
3#include "esphome.h" // Include ESPHome macros and functionality
4#include "esphome/core/component.h"
5#include "esphome/core/log.h"
6#include "esphome/components/sensor/sensor.h"
7#include "esphome/components/number/number.h"
8#include "esphome/components/switch/switch.h"
9#include "esphome/components/hx711/hx711.h"
10
11namespace esphome
12{
13 namespace hempy
14 {
15
16 enum class HempyStates
17 {
18 DISABLED, // Watering logic is disabled by the user. Can be re-enabled by starting or stopping the watering pump.
19 DRY, // The plant needs manual watering, the pump possibly failed. Stop the manual watering when the system beeps, that means Max weight is reached
20 IDLE, // Hempy bucket weight within limits (between Start watering - Max weight)
21 WATERING, // Water pump currently running, triggered when Start watering weight is reached, or if weight drops below Wet weight - Evaporation target
22 DRAINING // Wait for the Hempy bucket to drain, measures the bucket's weight loss. The sum of the total weight loss during DRAINING cycles is compared against DrainTarget
23 };
24
25 class HempyBucket : public PollingComponent
26 {
27 public:
28 HempyBucket(std::string name, text_sensor::TextSensor *state_sensor, hx711::HX711Sensor *weight_sensor, number::Number *start_watering_weight, number::Number *watering_increments, number::Number *max_watering_weight, number::Number *manual_watering_time, number::Number *pump_timeout, number::Number *drain_wait_time, number::Number *drain_target_weight, number::Number *evaporation_target_weight, sensor::Sensor *dry_weight, sensor::Sensor *wet_weight, switch_::Switch *waterPump, uint32_t update_interval) : PollingComponent(update_interval), DefaultUpdateInterval(update_interval), Name(name), StateSensor(state_sensor), WeightSensor(weight_sensor), StartWateringWeight(start_watering_weight), WateringIncrement(watering_increments), MaxWateringWeight(max_watering_weight), ManualWateringTime(manual_watering_time), MaxWateringTime(pump_timeout), DrainWaitTime(drain_wait_time), DrainTargetWeight(drain_target_weight), EvaporationTargetWeight(evaporation_target_weight), DryWeight(dry_weight), WetWeight(wet_weight), WaterPump(waterPump) {}
29 void setup() override;
30 void update() override;
31 void refresh(); // Update the weight sensor before calling update()
32 void update_interval(uint32_t miliseconds); // How often to call update(). Changes the Polling interval of the component
33 void update_state(HempyStates NewState, bool Force = false);
34 const char *to_text_state(HempyStates state);
35 bool is_watering_active();
36 void toggle_watering_logic(int8_t RequestedState = -1); // Enables or disables weight based watering (Useful when working with the plant). SuspendForMinutes: Automatically re-enable watering after (X minutes)
37 void start_watering(); // Start watering (re-enables watering logic)
38 void stop_watering(); // Stops watering
39 void toggle_watering(); // Triggers watering (re-enables watering logic), or stops watering if it is in progress
40 void disable_watering(); // Disable watering logic
41 void update_next_watering_weight(float weight); // Force update the next watering weight (Called when Start Water Weight is changed on the dashboard)
42 void update_evaporation_target(float EvaporationTarget); // Recalculates watering weight if WetWeight is known
43 float update_average(float NewValue); // Calculate the average of floats passed as NewValue. AverageQueueSize defines how many historical readings to keep
44 float get_average_weight(); // Returns current Average weight as a float number
45 HempyStates State{HempyStates::IDLE}; // Stores the current state of the hempy bucket
46 //static void set_active_waterings_limit(uint32_t limit) { ActiveWateringsLimit = limit;}
47 //static void set_active_waterings_limit(uint32_t limit); // Called once at boot
48
49 private:
50 //static uint32_t ActiveWaterings; // Tracks how many Hempy objects are watering simultaneously
51 //static uint32_t ActiveWateringsLimit; // Max simultaneously running pumps
52 std::string Name; // Name of the object
53 text_sensor::TextSensor *StateSensor; // Register a sensor to publish current state: IDLE/WATERING/DRAINING/DISABLED
54 hx711::HX711Sensor *WeightSensor; // Weight sensor object
55 number::Number *StartWateringWeight; // When the bucket weight drops below this -> Start the watering process
56 number::Number *WateringIncrement; // How much water to pump in one cycle, then wait for DrainWaitTime seconds before either starting a new pump cycle (DrainTargetWeight not reached) or considering the watering done (DrainTargetWeight reached)
57 number::Number *MaxWateringWeight; // Safety limit: Disable watering when bucket weight goes above this -> Consider the drain hose clogged and disable the watering logic
58 number::Number *MaxWateringTime; // Safety limit: Maximum total time the pump can run during watering. If the drain target is not hit before the timeout -> Consider the pump broken and disable the watering logic
59 number::Number *ManualWateringTime; // Maximum time a manual watering from DRY Weight to Wet or Max weight can take in seconds
60 number::Number *DrainWaitTime; // How long to wait between watering cycles for the water to drain in to the waste reservoir
61 number::Number *DrainTargetWeight; // Target weight reduction before watering is considered complete (drain-to-waste system)
62 number::Number *EvaporationTargetWeight; // How much weight should the bucket loose before starting another watering. When a watering is complete the wet weight - Evaporation target will give the next start watering weight. Calculated after every watering. After boot, before the first watering "start_watering_weight" is used.
63 switch_::Switch *WaterPump; // Reference to the relay controlling the water pump
64 sensor::Sensor *DryWeight; // Start watering when bucket weight drops below (Initially equals to StartWateringWeight, then calculated after each watering using EvaporationTargetWeight)
65 sensor::Sensor *WetWeight; // Weight measured after watering
66 bool ManualWateringDetected = false; // Set to true in DRY state, when weight increase is detected (manual watering in progress)
67 uint32_t ManualWateringStarted = 0; // Stores when the manual watering has started (When weight starts to increase in DRY mode)
68 uint32_t StateTimer = 0; // Track how much time is spent in one State
69 uint32_t PumpOnTimer = 0; // Track how long watering pump is on continuously (one water-drain cycle)
70 uint32_t WateringTimer = 0; // Track how long watering pump is on in total (all water-drain cycles)
71 float StateWeight = 0; // Used to store the weight of the bucket when entering a new state
72 float DrainProgress = 0; // Tracks how much water have drained away during the WATERING-DRAINING cycles
73 bool UpdateInProgress = false; // True while the state of the hempy bucket is updating (update_state running)
74 uint32_t DefaultUpdateInterval = 1000; // Stores the update_interval set in the YAML file in miliseconds
75 float AverageWeight = 0; // Stores the last calculated average weight sensor reading
76 static constexpr int AverageQueueSize = 15; // How many readings to use for average calculation
77 float AverageReadings[AverageQueueSize] = {0}; // Zero initialized array containing the previous readings
78 uint AverageCurrent = 0; // Keeps track of the next array item to update (to be used in circular buffer)
79 float AverageTotal = 0; // Sum of the readings stored in AverageReadings
80 bool AverageReset = true; // At first run (or when Reset=true), the average is (re)set as the NewValue
81 };
82
83 } // namespace hempy
84} // namespace esphome
const char * to_text_state(HempyStates state)
Definition hempy.cpp:177
void update() override
Definition hempy.cpp:16
void update_state(HempyStates NewState, bool Force=false)
Definition hempy.cpp:54
void update_evaporation_target(float EvaporationTarget)
Definition hempy.cpp:250
HempyBucket(std::string name, text_sensor::TextSensor *state_sensor, hx711::HX711Sensor *weight_sensor, number::Number *start_watering_weight, number::Number *watering_increments, number::Number *max_watering_weight, number::Number *manual_watering_time, number::Number *pump_timeout, number::Number *drain_wait_time, number::Number *drain_target_weight, number::Number *evaporation_target_weight, sensor::Sensor *dry_weight, sensor::Sensor *wet_weight, switch_::Switch *waterPump, uint32_t update_interval)
Definition hempy.h:28
void toggle_watering_logic(int8_t RequestedState=-1)
Definition hempy.cpp:201
void update_next_watering_weight(float weight)
Definition hempy.cpp:241
void update_interval(uint32_t miliseconds)
Definition hempy.cpp:44
void setup() override
Definition hempy.cpp:8
float update_average(float NewValue)
Definition hempy.cpp:263