PlantCtrl/esp32/include/PlantCtrl.h

203 lines
4.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"
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"
#include "MQTTUtils.h"
#include "LogDefines.h"
#define ANALOG_REREADS 5
#define MOISTURE_MEASUREMENT_DURATION 400 /** ms */
#define PWM_FREQ 50000
#define PWM_BITS 8
2021-07-21 21:23:58 +02:00
class Plant
{
2020-09-07 18:18:46 +02:00
private:
HomieNode *mPlant = NULL;
2021-07-09 22:51:50 +02:00
HomieInternals::PropertyInterface mPump;
RunningMedian mMoisture_raw = RunningMedian(ANALOG_REREADS);
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:
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);
//for sensor that might take any time
void blockingMoistureMeasurement(void);
//for sensor that need a start and a end in defined timing
2021-07-21 21:23:58 +02:00
void startMoistureMeasurement(void);
void stopMoistureMeasurement(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
String getSensorModeString(){
SENSOR_MODE mode = getSensorMode();
return SENSOR_STRING[mode];
}
bool isHydroponic()
{
long current = this->mSetting->pSensorDry->get();
return equalish(current, HYDROPONIC_MODE);
}
SENSOR_MODE getSensorMode()
{
int raw_mode = this->mSetting->pSensorMode->get();
SENSOR_MODE sensorType = static_cast<SENSOR_MODE>(raw_mode);
return sensorType;
}
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()
{
if (isHydroponic())
{
//hydroponic only uses timer based controll
return true;
}
bool isDry = getCurrentMoisturePCT() < getTargetMoisturePCT();
bool isActive = isPumpTriggerActive();
return isDry && isActive;
}
bool isPumpTriggerActive()
{
2021-07-09 21:50:51 +02:00
long current = this->mSetting->pSensorDry->get();
return !equalish(current, DEACTIVATED_PLANT);
}
float getTargetMoisturePCT()
{
if (isPumpTriggerActive())
{
return this->mSetting->pSensorDry->get();
}
else
{
return DEACTIVATED_PLANT;
2021-07-21 21:34:14 +02:00
}
}
2021-07-09 21:50:51 +02:00
float getCurrentMoisturePCT()
{
switch (getSensorMode()){
case NONE:
return DEACTIVATED_PLANT;
case CAPACITIVE_FREQUENCY:
return mapf(mMoisture_raw.getMedian(), MOIST_SENSOR_MAX_FRQ, MOIST_SENSOR_MIN_FRQ, 0, 100);
case ANALOG_RESISTANCE_PROBE:
return mapf(mMoisture_raw.getMedian(), ANALOG_SENSOR_MAX_MV, ANALOG_SENSOR_MIN_MV, 0, 100);
default:
log(LOG_LEVEL_ERROR, LOG_SENSORMODE_UNKNOWN, LOG_SENSORMODE_UNKNOWN_CODE);
return DEACTIVATED_PLANT;
}
}
float getCurrentMoistureRaw()
{
if(getSensorMode() == CAPACITIVE_FREQUENCY){
if (mMoisture_raw.getMedian() < MOIST_SENSOR_MIN_FRQ)
{
return MISSING_SENSOR;
}
}
return mMoisture_raw.getMedian();
}
HomieInternals::SendingPromise &setProperty(const String &property) const
{
return mPlant->setProperty(property);
}
2020-10-16 18:25:02 +02:00
void init(void);
void initSensors(void);
2020-10-23 16:20:34 +02:00
long getCooldownInSeconds()
{
return this->mSetting->pPumpCooldownInSeconds->get();
}
2020-10-23 16:47:40 +02:00
/**
* @brief Get the Hours when pumping should start
*
* @return hour
*/
int getHoursStart()
{
return this->mSetting->pPumpAllowedHourRangeStart->get();
}
/**
* @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
}
bool isAllowedOnlyAtLowLight(void)
{
if (this->isHydroponic())
{
return false;
}
2020-10-23 16:20:34 +02:00
return this->mSetting->pPumpOnlyWhenLowLight->get();
}
void publishState(String state);
2021-07-09 22:51:50 +02:00
bool switchHandler(const HomieRange &range, const String &value);
2021-07-09 22:51:50 +02:00
void setSwitchHandler(HomieInternals::PropertyInputHandler f);
2021-10-01 23:46:37 +02:00
long getPumpDuration()
{
2021-10-01 23:46:37 +02:00
return this->mSetting->pPumpDuration->get();
}
2020-09-07 18:18:46 +02:00
};
#endif