From 2d91f91290dcc16552ec396ae25a51bd95869cfb Mon Sep 17 00:00:00 2001 From: c3ma Date: Fri, 9 Jul 2021 22:51:50 +0200 Subject: [PATCH] react on switch from Mqtt --- esp32/include/PlantCtrl.h | 8 ++++++-- esp32/src/PlantCtrl.cpp | 41 ++++++++++++++++++++------------------- esp32/src/main.cpp | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 22 deletions(-) diff --git a/esp32/include/PlantCtrl.h b/esp32/include/PlantCtrl.h index 09fcc77..3110413 100644 --- a/esp32/include/PlantCtrl.h +++ b/esp32/include/PlantCtrl.h @@ -13,6 +13,7 @@ #define PLANT_CTRL_H #include "HomieTypes.h" +#include #include "ControllerConfiguration.h" #include "RunningMedian.h" #include "MathUtils.h" @@ -23,6 +24,7 @@ class Plant private: RunningMedian moistureRaw = RunningMedian(5); HomieNode *mPlant = NULL; + HomieInternals::PropertyInterface mPump; int mPinSensor = 0; /**< Pin of the moist sensor */ int mPinPump = 0; /**< Pin of the pump */ bool mConnected = false; @@ -100,7 +102,6 @@ public: { return mPlant->setProperty(property); } - bool switchHandler(const HomieRange &range, const String &value); void init(void); @@ -131,8 +132,11 @@ public: return this->mSetting->pPumpOnlyWhenLowLight->get(); } - void publishState(String state); + + bool switchHandler(const HomieRange& range, const String& value); + + void setSwitchHandler(HomieInternals::PropertyInputHandler f); }; #endif diff --git a/esp32/src/PlantCtrl.cpp b/esp32/src/PlantCtrl.cpp index 6fb2e62..3ea76ee 100644 --- a/esp32/src/PlantCtrl.cpp +++ b/esp32/src/PlantCtrl.cpp @@ -133,31 +133,32 @@ void Plant::activatePump(void) } } +bool Plant::switchHandler(const HomieRange& range, const String& value) { + if (range.isRange) { + return false; // only one switch is present + } + + if ((value.equals("ON")) || (value.equals("On")) || (value.equals("on")) || (value.equals("true"))) { + this->activatePump(); + return true; + } else if ((value.equals("OFF")) || (value.equals("Off")) || (value.equals("off")) || (value.equals("false")) ) { + this->deactivatePump(); + return true; + } else { + return false; + } +} + +void Plant::setSwitchHandler(HomieInternals::PropertyInputHandler f) { + this->mPump.settable(f); +} + void Plant::advertise(void) { // Advertise topics - this->mPlant->advertise("switch").setName("Pump 1").setDatatype("boolean"); + mPump = this->mPlant->advertise("switch").setName("Pump").setDatatype("boolean"); this->mPlant->advertise("lastPump").setName("lastPump").setDatatype("number").setUnit("unixtime"); - //FIXME add .settable(this->switchHandler) this->mPlant->advertise("moist").setName("Percent").setDatatype("number").setUnit("%"); this->mPlant->advertise("moistraw").setName("adc").setDatatype("number").setUnit("3.3/4096V"); this->mPlant->advertise("state").setName("state").setDatatype("string"); } - -/* FIXME -bool Plant::switchHandler(const HomieRange& range, const String& value) { - if (range.isRange) return false; // only one switch is present - - - if ((value.equals("ON")) || (value.equals("On")) || (value.equals("on")) || (value.equals("true"))) { - this->activatePump(); - return true; - } else if ((value.equals("OFF")) || (value.equals("Off")) || (value.equals("off")) || (value.equals("false")) ) { - this->deactivatePump(); - return true; - } else { - return false; - } - } -} -*/ \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index a85835f..30093f7 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -510,6 +510,34 @@ void homieLoop() } } +bool switch1(const HomieRange& range, const String& value) { + return mPlants[0].switchHandler(range, value); +} + +bool switch2(const HomieRange& range, const String& value) { + return mPlants[1].switchHandler(range, value); +} + +bool switch3(const HomieRange& range, const String& value) { + return mPlants[2].switchHandler(range, value); +} + +bool switch4(const HomieRange& range, const String& value) { + return mPlants[3].switchHandler(range, value); +} + +bool switch5(const HomieRange& range, const String& value) { + return mPlants[4].switchHandler(range, value); +} + +bool switch6(const HomieRange& range, const String& value) { + return mPlants[5].switchHandler(range, value); +} + +bool switch7(const HomieRange& range, const String& value) { + return mPlants[6].switchHandler(range, value); +} + /** * @brief Startup function * Is called once, the controller is started @@ -619,6 +647,14 @@ void setup() { mPlants[i].advertise(); } + mPlants[0].setSwitchHandler(switch1); + mPlants[1].setSwitchHandler(switch2); + mPlants[2].setSwitchHandler(switch3); + mPlants[3].setSwitchHandler(switch4); + mPlants[4].setSwitchHandler(switch5); + mPlants[5].setSwitchHandler(switch6); + mPlants[6].setSwitchHandler(switch7); + sensorTemp.advertise(TEMPERATUR_SENSOR_LIPO) .setName(TEMPERATURE_NAME) .setDatatype(NUMBER_TYPE)