PlantCtrl/esp32/include/PlantCtrl.h

126 lines
2.8 KiB
C
Raw Normal View History

2020-09-07 18:18:46 +02:00
/**
* @file PlantCtrl.h
* @author your name (you@domain.com)
* @brief Abstraction to handle the Sensors
* @version 0.1
* @date 2020-05-27
*
* @copyright Copyright (c) 2020
*
*/
#ifndef PLANT_CTRL_H
#define PLANT_CTRL_H
2020-10-16 16:22:48 +02:00
#include "HomieTypes.h"
2020-10-21 20:46:09 +02:00
#include "RunningMedian.h"
class Plant
{
2020-09-07 18:18:46 +02:00
private:
2020-10-21 20:46:09 +02:00
RunningMedian moistureRaw = RunningMedian(5);
HomieNode *mPlant = NULL;
int mPinSensor = 0; /**< Pin of the moist sensor */
int mPinPump = 0; /**< Pin of the pump */
2020-10-23 16:20:34 +02:00
bool mConnected = false;
public:
PlantSettings_t *mSetting;
2020-09-07 18:18:46 +02:00
/**
* @brief Construct a new Plant object
*
* @param pinSensor Pin of the Sensor to use to measure moist
* @param pinPump Pin of the Pump to use
*/
Plant(int pinSensor, int pinPump,
int plantId,
HomieNode *plant,
PlantSettings_t *setting);
2020-09-07 18:18:46 +02:00
2020-10-23 16:20:34 +02:00
void postMQTTconnection(void);
void advertise(void);
2020-09-07 18:18:46 +02:00
/**
2020-10-23 16:20:34 +02:00
* @brief Measure a new analog moister value
2020-09-07 18:18:46 +02:00
*
*/
2020-10-23 16:20:34 +02:00
void addSenseValue(void);
2020-10-23 16:20:34 +02:00
void deactivatePump(void);
2020-09-07 18:18:46 +02:00
2020-10-23 16:20:34 +02:00
void activatePump(void);
2020-09-07 18:18:46 +02:00
/**
* @brief Check if a plant is too dry and needs some water.
*
* @return true
* @return false
*/
bool isPumpRequired()
{
bool isDry = getCurrentMoisture() > getSettingsMoisture();
bool isActive = isPumpTriggerActive();
return isDry && isActive;
}
bool isPumpTriggerActive()
{
return this->mSetting->pSensorDry->get() != DEACTIVATED_PLANT;
}
float getCurrentMoisture()
{
if(moistureRaw.getCount()==0){
return MISSING_SENSOR;
}
return this->moistureRaw.getMedian();
}
long getSettingsMoisture()
{
if (this->mSetting->pSensorDry != NULL)
{
return this->mSetting->pSensorDry->get();
}
else
{
return DEACTIVATED_PLANT;
}
}
HomieInternals::SendingPromise &setProperty(const String &property) const
{
return mPlant->setProperty(property);
}
bool switchHandler(const HomieRange &range, const String &value);
2020-10-16 18:25:02 +02:00
void init(void);
2020-10-23 16:20:34 +02:00
2020-10-23 16:47:40 +02:00
/** @fn bool isInCooldown(long sinceLastActivation)
* @brief determine, if the plant was recently casted
* @param sinceLastActivation timestamp of last time
*/
bool isInCooldown(long sinceLastActivation)
{
2020-10-23 16:47:40 +02:00
/* if the time difference is greater than one month, we know these are initial values */
if (sinceLastActivation > (60 * 60 * 24 * 30))
{
2020-10-23 16:47:40 +02:00
return false;
}
return (getCooldownInSeconds() > sinceLastActivation);
}
long getCooldownInSeconds(){
return this->mSetting->pPumpCooldownInHours->get()*60*60;
2020-10-23 16:20:34 +02:00
}
bool isAllowedOnlyAtLowLight(void)
{
2020-10-23 16:20:34 +02:00
return this->mSetting->pPumpOnlyWhenLowLight->get();
}
2020-09-07 18:18:46 +02:00
};
#endif