react on switch from Mqtt

This commit is contained in:
c3ma 2021-07-09 22:51:50 +02:00
parent df39c09c50
commit 2d91f91290
3 changed files with 63 additions and 22 deletions

View File

@ -13,6 +13,7 @@
#define PLANT_CTRL_H
#include "HomieTypes.h"
#include <HomieNode.hpp>
#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

View File

@ -133,21 +133,10 @@ void Plant::activatePump(void)
}
}
void Plant::advertise(void)
{
// Advertise topics
this->mPlant->advertise("switch").setName("Pump 1").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 (range.isRange) {
return false; // only one switch is present
}
if ((value.equals("ON")) || (value.equals("On")) || (value.equals("on")) || (value.equals("true"))) {
this->activatePump();
@ -158,6 +147,18 @@ bool Plant::switchHandler(const HomieRange& range, const String& value) {
} else {
return false;
}
}
}
*/
void Plant::setSwitchHandler(HomieInternals::PropertyInputHandler f) {
this->mPump.settable(f);
}
void Plant::advertise(void)
{
// Advertise topics
mPump = this->mPlant->advertise("switch").setName("Pump").setDatatype("boolean");
this->mPlant->advertise("lastPump").setName("lastPump").setDatatype("number").setUnit("unixtime");
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");
}

View File

@ -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)