From 2c8645121ea1d49d305ae8555807d117b9d18be2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 26 Oct 2021 21:50:55 +0200 Subject: [PATCH] added pump specific power level (requires controller that supports this or dc motor) --- esp32/include/ControllerConfiguration.h | 2 +- esp32/include/HomieConfiguration.h | 3 ++- esp32/include/HomieTypes.h | 1 + esp32/include/PlantCtrl.h | 2 ++ esp32/src/PlantCtrl.cpp | 17 +++++++++++++---- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 430c26e..f757744 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -98,7 +98,7 @@ #define BATTSENSOR_INDEX_BATTERY 1 #define MQTT_TIMEOUT (1000 * 60) /**< After 10 seconds, MQTT is expected to be connected */ -#define ESP_STALE_TIMEOUT (MQTT_TIMEOUT+(120*1000)) +#define ESP_STALE_TIMEOUT (MQTT_TIMEOUT+(700*1000)) #define MAX_PLANTS 7 #define SOLAR_CHARGE_MIN_VOLTAGE 7 /**< Sun is rising (morning detected) */ diff --git a/esp32/include/HomieConfiguration.h b/esp32/include/HomieConfiguration.h index 70002c9..293b4d8 100644 --- a/esp32/include/HomieConfiguration.h +++ b/esp32/include/HomieConfiguration.h @@ -106,7 +106,8 @@ HomieSetting ntpServer("ntpServer", "NTP server (pool.ntp.org as d 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"); \ - PlantSettings_t mSetting##plant = {&mSensorDry##plant, &mPumpAllowedHourRangeStart##plant, &mPumpAllowedHourRangeEnd##plant, &mPumpOnlyWhenLowLight##plant, &mPumpCooldownInMinutes##plant, &pPumpDuration##plant}; \ + 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}; \ /**< 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 7a87888..a92b7b5 100644 --- a/esp32/include/HomieTypes.h +++ b/esp32/include/HomieTypes.h @@ -28,6 +28,7 @@ typedef struct PlantSettings_t HomieSetting *pPumpOnlyWhenLowLight; HomieSetting *pPumpCooldownInMinutes; HomieSetting *pPumpDuration; + HomieSetting *pPumpPowerLevel; } PlantSettings_t; #endif \ No newline at end of file diff --git a/esp32/include/PlantCtrl.h b/esp32/include/PlantCtrl.h index f682f3c..bbf6a68 100644 --- a/esp32/include/PlantCtrl.h +++ b/esp32/include/PlantCtrl.h @@ -19,6 +19,8 @@ #include "MathUtils.h" #define MOISTURE_MEASUREMENT_DURATION 400 /** ms */ +#define PWM_FREQ 50000 +#define PWM_BITS 8 class Plant diff --git a/esp32/src/PlantCtrl.cpp b/esp32/src/PlantCtrl.cpp index 6c76256..c0833e6 100644 --- a/esp32/src/PlantCtrl.cpp +++ b/esp32/src/PlantCtrl.cpp @@ -55,12 +55,19 @@ void Plant::init(void) this->mSetting->pPumpDuration->setValidator([](long candidate) { return ((candidate >= 0) && (candidate <= 1000)); }); + this->mSetting->pPumpPowerLevel->setDefaultValue(100); + this->mSetting->pPumpPowerLevel->setValidator([](long candidate) { + return ((candidate >= 0) && (candidate <= 100)); + }); /* Initialize Hardware */ - Serial.println("Set GPIO mode " + String(mPinPump) + "=" + String(OUTPUT)); + Serial.println("Init PWM controller for pump " + String(mPinPump) + "=" + String(OUTPUT)); Serial.flush(); - pinMode(this->mPinPump, OUTPUT); + ledcSetup(this->mPlantId, PWM_FREQ, PWM_BITS); + ledcAttachPin(mPinPump, this->mPlantId); + ledcWrite(this->mPlantId, 0); + Serial.println("Set GPIO mode " + String(mPinSensor) + "=" + String(ANALOG)); Serial.flush(); pinMode(this->mPinSensor, INPUT); @@ -140,7 +147,7 @@ void Plant::deactivatePump(void) { int plantId = this->mPlantId; Serial << "deactivating pump " << plantId << endl; - digitalWrite(this->mPinPump, LOW); + ledcWrite(this->mPlantId, 0); if (this->mConnected) { const String OFF = String("OFF"); @@ -158,8 +165,10 @@ void Plant::publishState(String state) { void Plant::activatePump(void) { int plantId = this->mPlantId; + Serial << "activating pump " << plantId << endl; - digitalWrite(this->mPinPump, HIGH); + long desiredPowerLevelPercent = this->mSetting->pPumpPowerLevel->get(); + ledcWrite(this->mPlantId, desiredPowerLevelPercent*PWM_BITS); if (this->mConnected) { const String OFF = String("ON");