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"
|
2021-07-09 22:51:50 +02:00
|
|
|
#include <HomieNode.hpp>
|
2021-07-09 21:50:51 +02:00
|
|
|
#include "ControllerConfiguration.h"
|
2020-10-21 20:46:09 +02:00
|
|
|
#include "RunningMedian.h"
|
2021-07-09 21:50:51 +02:00
|
|
|
#include "MathUtils.h"
|
2020-09-21 19:40:01 +02:00
|
|
|
|
2021-08-29 20:45:50 +02:00
|
|
|
#define MOISTURE_MEASUREMENT_DURATION 400 /** ms */
|
2021-07-21 21:23:58 +02:00
|
|
|
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
class Plant
|
|
|
|
{
|
2020-09-07 18:18:46 +02:00
|
|
|
|
|
|
|
private:
|
2020-11-04 21:57:40 +01:00
|
|
|
HomieNode *mPlant = NULL;
|
2021-07-09 22:51:50 +02:00
|
|
|
HomieInternals::PropertyInterface mPump;
|
2021-07-21 21:23:58 +02:00
|
|
|
int32_t mMoisture_freq = 0;
|
2020-11-04 21:57:40 +01:00
|
|
|
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;
|
2020-12-20 17:00:44 +01:00
|
|
|
int mPlantId = -1;
|
2020-10-23 16:20:34 +02:00
|
|
|
|
|
|
|
public:
|
2020-11-04 21:57:40 +01:00
|
|
|
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
|
|
|
|
*/
|
2020-09-21 19:40:01 +02:00
|
|
|
Plant(int pinSensor, int pinPump,
|
2020-11-04 21:57:40 +01:00
|
|
|
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
|
|
|
*
|
|
|
|
*/
|
2021-07-21 21:23:58 +02:00
|
|
|
void startMoistureMeasurement(void);
|
|
|
|
void stopMoistureMeasurement(void);
|
2020-11-04 21:57:40 +01:00
|
|
|
|
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
|
|
|
|
*/
|
2020-11-04 21:57:40 +01:00
|
|
|
bool isPumpRequired()
|
|
|
|
{
|
2021-07-09 21:50:51 +02:00
|
|
|
bool isDry = getCurrentMoisture() > getSetting2Moisture();
|
2020-11-04 21:10:22 +01:00
|
|
|
bool isActive = isPumpTriggerActive();
|
|
|
|
return isDry && isActive;
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
bool isPumpTriggerActive()
|
|
|
|
{
|
2021-07-09 21:50:51 +02:00
|
|
|
long current = this->mSetting->pSensorDry->get();
|
|
|
|
return !equalish(current,DEACTIVATED_PLANT);
|
2020-11-04 21:10:22 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
float getCurrentMoisture()
|
2020-11-04 23:28:08 +01:00
|
|
|
{
|
2021-07-21 21:34:14 +02:00
|
|
|
if(mMoisture_freq < MOIST_SENSOR_MIN_FRQ){
|
|
|
|
return MISSING_SENSOR;
|
|
|
|
}
|
2021-07-21 21:23:58 +02:00
|
|
|
return mMoisture_freq;
|
2020-11-04 21:10:22 +01:00
|
|
|
}
|
2021-07-09 21:50:51 +02:00
|
|
|
|
|
|
|
long getSetting2Moisture()
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
|
|
|
if (this->mSetting->pSensorDry != NULL)
|
|
|
|
{
|
2021-08-17 16:55:52 +02:00
|
|
|
//1 is totally wet, 0 is try, 0 is MOIST_SENSOR_MAX_FRQ, 1 is MOIST_SENSOR_MIN_FRQ
|
|
|
|
float factor = (this->mSetting->pSensorDry->get());
|
|
|
|
return map(factor,0,100,MOIST_SENSOR_MAX_FRQ,MOIST_SENSOR_MIN_FRQ);
|
2020-11-04 21:57:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-04 21:10:22 +01:00
|
|
|
return DEACTIVATED_PLANT;
|
|
|
|
}
|
2020-09-21 19:40:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
HomieInternals::SendingPromise &setProperty(const String &property) const
|
|
|
|
{
|
2020-09-21 19:40:01 +02:00
|
|
|
return mPlant->setProperty(property);
|
|
|
|
}
|
2020-10-16 18:25:02 +02:00
|
|
|
|
|
|
|
void init(void);
|
2020-10-23 16:20:34 +02:00
|
|
|
|
2021-04-07 21:26:11 +02:00
|
|
|
long getCooldownInSeconds() {
|
|
|
|
return this->mSetting->pPumpCooldownInHours->get()*60*60;
|
|
|
|
}
|
2020-10-23 16:47:40 +02:00
|
|
|
|
2021-04-07 21:26:11 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the Hours when pumping should start
|
|
|
|
*
|
|
|
|
* @return hour
|
|
|
|
*/
|
|
|
|
int getHoursStart() {
|
|
|
|
return this->mSetting->pPumpAllowedHourRangeStart->get();
|
2020-11-04 23:28:08 +01:00
|
|
|
}
|
|
|
|
|
2021-04-07 21:26:11 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the Hours when pumping should end
|
|
|
|
*
|
|
|
|
* @return hour
|
|
|
|
*/
|
|
|
|
int getHoursEnd() {
|
|
|
|
return this->mSetting->pPumpAllowedHourRangeEnd->get();
|
2020-10-23 16:20:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
bool isAllowedOnlyAtLowLight(void)
|
|
|
|
{
|
2020-10-23 16:20:34 +02:00
|
|
|
return this->mSetting->pPumpOnlyWhenLowLight->get();
|
|
|
|
}
|
2021-07-01 22:06:50 +02:00
|
|
|
|
|
|
|
void publishState(String state);
|
2021-07-09 22:51:50 +02:00
|
|
|
|
|
|
|
bool switchHandler(const HomieRange& range, const String& value);
|
|
|
|
|
|
|
|
void setSwitchHandler(HomieInternals::PropertyInputHandler f);
|
2021-10-01 23:46:37 +02:00
|
|
|
|
|
|
|
long getPumpDuration() {
|
|
|
|
return this->mSetting->pPumpDuration->get();
|
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|