From af618e73f314539ec7c4189f3771f18c4799885c Mon Sep 17 00:00:00 2001 From: Empire Date: Fri, 19 Nov 2021 18:48:14 +0000 Subject: [PATCH] pump refactor round 1 --- esp32/include/ControllerConfiguration.h | 6 ++--- esp32/include/HomieConfiguration.h | 5 ++-- esp32/include/HomieTypes.h | 1 + esp32/src/main.cpp | 31 ++++++++++++++++++++----- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index f757744..a90624d 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -82,12 +82,12 @@ /** \addtogroup Configuration * @{ */ -#define FIRMWARE_VERSION "sw 2.0 hw 0.10b" +#define FIRMWARE_VERSION "sw 2.1 hw 0.10b" #define TIMED_LIGHT_PIN CUSTOM1_PIN5 -//#define FLOWMETER_PIN CUSTOM1_PIN1 +#define FLOWMETER_PIN CUSTOM1_PIN1 #ifdef FLOWMETER_PIN - #define FLOWMETER_FLOWFACTOR 22 /** F = 22 * Q;Q = L/min */ + #define FLOWMETER_FLOWFACTOR 23 /** F = 22 * Q;Q = L/min */ #endif #define MOIST_SENSOR_MAX_FRQ 60000 // 60kHz (500Hz margin) diff --git a/esp32/include/HomieConfiguration.h b/esp32/include/HomieConfiguration.h index 293b4d8..d6a4d07 100644 --- a/esp32/include/HomieConfiguration.h +++ b/esp32/include/HomieConfiguration.h @@ -105,9 +105,10 @@ HomieSetting ntpServer("ntpServer", "NTP server (pool.ntp.org as d HomieSetting mPumpAllowedHourRangeEnd##plant = HomieSetting("hourend" strplant, "Plant" strplant " - Range pump allowed hour end (0-23)"); \ HomieSetting mPumpOnlyWhenLowLight##plant = HomieSetting("lowLight" strplant, "Plant" strplant " - Enable the Pump only, when there is no sunlight"); \ HomieSetting mPumpCooldownInMinutes##plant = HomieSetting("delay" strplant, "Plant" strplant " - How long to wait until the pump is activated again (minutes)"); \ - HomieSetting pPumpDuration##plant = HomieSetting("pumpDuration" strplant, "Plant" strplant " - time seconds or ml (if using flowmeter) to water when pump is active"); \ + HomieSetting pPumpDuration##plant = HomieSetting("pumpDuration" strplant, "Plant" strplant " - time seconds to water when pump is active"); \ + HomieSetting pPumpMl##plant = HomieSetting("pumpAmount" strplant, "Plant" strplant " - ml (if using flowmeter) to water when pump is active"); \ HomieSetting pPowerLevel##plant = HomieSetting("powerLevel" strplant, "Plant" strplant " - pwm duty cycle in percent"); \ - PlantSettings_t mSetting##plant = {&mSensorDry##plant, &mPumpAllowedHourRangeStart##plant, &mPumpAllowedHourRangeEnd##plant, &mPumpOnlyWhenLowLight##plant, &mPumpCooldownInMinutes##plant, &pPumpDuration##plant, &pPowerLevel##plant}; \ + PlantSettings_t mSetting##plant = {&mSensorDry##plant, &mPumpAllowedHourRangeStart##plant, &mPumpAllowedHourRangeEnd##plant, &mPumpOnlyWhenLowLight##plant, &mPumpCooldownInMinutes##plant, &pPumpDuration##plant, &pPowerLevel##plant, &pPumpMl##plant}; \ /**< Generate all settings for one plant \ * \ * Feature to start pumping only at morning: @link{SOLAR_CHARGE_MIN_VOLTAGE} and @link{SOLAR_CHARGE_MAX_VOLTAGE} \ diff --git a/esp32/include/HomieTypes.h b/esp32/include/HomieTypes.h index 390fc4c..1d4d04e 100644 --- a/esp32/include/HomieTypes.h +++ b/esp32/include/HomieTypes.h @@ -29,6 +29,7 @@ typedef struct PlantSettings_t HomieSetting *pPumpCooldownInSeconds; HomieSetting *pPumpDuration; HomieSetting *pPumpPowerLevel; + HomieSetting *pPumpMl; } PlantSettings_t; #endif \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 08b3c96..e1a8785 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -69,7 +69,7 @@ RTC_DATA_ATTR long consecutiveWateringPlant[MAX_PLANTS] = {0}; bool volatile mDownloadMode = false; /**< Controller must not sleep */ bool volatile mSensorsRead = false; /**< Sensors are read without Wifi or MQTT */ int volatile pumpToRun = -1; /** pump to run at the end of the cycle */ -int volatile selfTestPumpRun = -1; /** pump to run at the end of the cycle */ +int volatile selfTestPumpRun = -1; /** pump to run at the end of the cycle */ bool mConfigured = false; long nextBlink = 0; /**< Time needed in main loop to support expected blink code */ @@ -80,6 +80,7 @@ unsigned long setupFinishedTimestamp; bool pumpStarted = false; long pumpTarget = -1; +long lastSendPumpUpdate = 0; #ifdef FLOWMETER_PIN long pumpTargetMl = -1; #endif @@ -568,10 +569,10 @@ void initPumpLogic() pcnt_counter_clear(unit); // Zera o contador PCNT pcnt_counter_resume(unit); -#else +#endif pumpTarget = millis() + (mPlants[pumpToRun].getPumpDuration() * 1000); log(LOG_LEVEL_INFO, "Starting pump " + String(pumpToRun) + " for " + String(mPlants[pumpToRun].getPumpDuration()) + "s", LOG_PUMP_STARTED_CODE); -#endif + //enable power WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); digitalWrite(OUTPUT_ENABLE_PUMP, HIGH); @@ -592,6 +593,13 @@ void pumpActiveLoop() rtcLastWateringPlant[pumpToRun] = getCurrentTime(); } + bool mqttUpdateTick = false; + if (lastSendPumpUpdate + 1000 < millis()) + { + lastSendPumpUpdate = millis(); + mqttUpdateTick = true; + } + #ifdef FLOWMETER_PIN int16_t pulses; pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_7); @@ -612,14 +620,25 @@ void pumpActiveLoop() { targetReached = true; pcnt_counter_pause(unit); + mPlants[pumpToRun].setProperty("waterusage").send(String(pumped)); + } + else if (mqttUpdateTick) + { + mPlants[pumpToRun].setProperty("waterusage").send(String(pumped)); } - mPlants[pumpToRun].setProperty("waterusage").send(String(pumped)); } -#else + + long duration = pumpTarget - millis(); if (millis() > pumpTarget) { + mPlants[pumpToRun].setProperty("watertime").send(String(duration)); targetReached = true; } + else if (mqttUpdateTick) + { + mPlants[pumpToRun].setProperty("watertime").send(String(duration)); + } + #endif if (targetReached) @@ -831,7 +850,7 @@ void setup() void selfTest() { - + if (selfTestPumpRun >= 0 && selfTestPumpRun < MAX_PLANTS) { Serial << "self test mode pump deactivate " << pumpToRun << endl;