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
|
2022-03-05 10:31:43 +01:00
|
|
|
*
|
2020-09-07 18:18:46 +02:00
|
|
|
* @copyright Copyright (c) 2020
|
2022-03-05 10:31:43 +01:00
|
|
|
*
|
2020-09-07 18:18:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
2022-02-12 05:26:54 +01:00
|
|
|
#include "MQTTUtils.h"
|
|
|
|
#include "LogDefines.h"
|
2022-03-05 10:31:43 +01:00
|
|
|
#include "SHT2x.h"
|
2020-09-21 19:40:01 +02:00
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
#define ANALOG_REREADS 5
|
|
|
|
#define MOISTURE_MEASUREMENT_DURATION 400 /** ms */
|
2021-10-26 21:50:55 +02:00
|
|
|
#define PWM_FREQ 50000
|
|
|
|
#define PWM_BITS 8
|
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;
|
2022-02-12 05:26:54 +01:00
|
|
|
RunningMedian mMoisture_raw = RunningMedian(ANALOG_REREADS);
|
2022-03-05 10:31:43 +01:00
|
|
|
RunningMedian mTemperature_degree = RunningMedian(ANALOG_REREADS);
|
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;
|
2022-03-05 10:31:43 +01:00
|
|
|
SENSOR_MODE mSensorMode;
|
|
|
|
SHT2x sht20;
|
|
|
|
|
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
|
2022-03-05 10:31:43 +01:00
|
|
|
*
|
2020-09-07 18:18:46 +02:00
|
|
|
* @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,
|
2022-03-05 10:31:43 +01:00
|
|
|
PlantSettings_t *setting, SENSOR_MODE mode);
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-23 16:20:34 +02:00
|
|
|
void postMQTTconnection(void);
|
|
|
|
|
|
|
|
void advertise(void);
|
|
|
|
|
2022-03-05 10:31:43 +01:00
|
|
|
// for sensor that might take any time
|
2022-02-12 05:26:54 +01:00
|
|
|
void blockingMoistureMeasurement(void);
|
2022-03-05 10:31:43 +01:00
|
|
|
// 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-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
|
|
|
|
2022-03-05 10:31:43 +01:00
|
|
|
String getSensorModeString()
|
|
|
|
{
|
2022-02-19 02:24:19 +01:00
|
|
|
SENSOR_MODE mode = getSensorMode();
|
|
|
|
return SENSOR_STRING[mode];
|
|
|
|
}
|
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
bool isHydroponic()
|
|
|
|
{
|
2021-10-22 19:39:42 +02:00
|
|
|
long current = this->mSetting->pSensorDry->get();
|
2022-02-12 05:26:54 +01:00
|
|
|
return equalish(current, HYDROPONIC_MODE);
|
|
|
|
}
|
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
SENSOR_MODE getSensorMode()
|
2022-02-12 05:26:54 +01:00
|
|
|
{
|
2022-03-05 10:31:43 +01:00
|
|
|
return mSensorMode;
|
2021-10-22 19:39:42 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
/**
|
|
|
|
* @brief Check if a plant is too dry and needs some water.
|
2022-03-05 10:31:43 +01:00
|
|
|
*
|
|
|
|
* @return true
|
|
|
|
* @return false
|
2020-09-07 18:18:46 +02:00
|
|
|
*/
|
2020-11-04 21:57:40 +01:00
|
|
|
bool isPumpRequired()
|
|
|
|
{
|
2022-02-12 05:26:54 +01:00
|
|
|
if (isHydroponic())
|
|
|
|
{
|
2022-03-05 10:31:43 +01:00
|
|
|
// hydroponic only uses timer based controll
|
2021-10-22 19:39:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
2022-02-12 05:26:54 +01:00
|
|
|
bool isDry = getCurrentMoisturePCT() < getTargetMoisturePCT();
|
2020-11-04 21:10:22 +01:00
|
|
|
bool isActive = isPumpTriggerActive();
|
|
|
|
return isDry && isActive;
|
2022-02-12 05:26:54 +01:00
|
|
|
}
|
2021-10-22 19:39:42 +02:00
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
bool isPumpTriggerActive()
|
|
|
|
{
|
2021-07-09 21:50:51 +02:00
|
|
|
long current = this->mSetting->pSensorDry->get();
|
2022-02-12 05:26:54 +01:00
|
|
|
return !equalish(current, DEACTIVATED_PLANT);
|
2020-11-04 21:10:22 +01:00
|
|
|
}
|
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
float getTargetMoisturePCT()
|
|
|
|
{
|
|
|
|
if (isPumpTriggerActive())
|
|
|
|
{
|
|
|
|
return this->mSetting->pSensorDry->get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return DEACTIVATED_PLANT;
|
2021-07-21 21:34:14 +02:00
|
|
|
}
|
2020-11-04 21:10:22 +01:00
|
|
|
}
|
2021-07-09 21:50:51 +02:00
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
float getCurrentMoisturePCT()
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2022-03-05 10:31:43 +01:00
|
|
|
switch (getSensorMode())
|
|
|
|
{
|
2022-02-19 02:24:19 +01:00
|
|
|
case NONE:
|
2022-02-12 05:26:54 +01:00
|
|
|
return DEACTIVATED_PLANT;
|
2022-02-19 02:24:19 +01:00
|
|
|
case CAPACITIVE_FREQUENCY:
|
|
|
|
return mapf(mMoisture_raw.getMedian(), MOIST_SENSOR_MAX_FRQ, MOIST_SENSOR_MIN_FRQ, 0, 100);
|
|
|
|
case ANALOG_RESISTANCE_PROBE:
|
2022-04-29 10:14:50 +02:00
|
|
|
return mapf(mMoisture_raw.getMedian(), ANALOG_SENSOR_MAX_MV, ANALOG_SENSOR_MIN_MV, 0, 100);
|
2020-11-04 21:10:22 +01:00
|
|
|
}
|
2022-03-05 10:31:43 +01:00
|
|
|
return MISSING_SENSOR;
|
2020-09-21 19:40:01 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
float getCurrentMoistureRaw()
|
|
|
|
{
|
2022-03-05 10:31:43 +01:00
|
|
|
if (getSensorMode() == CAPACITIVE_FREQUENCY)
|
|
|
|
{
|
|
|
|
if (mMoisture_raw.getMedian() < MOIST_SENSOR_MIN_FRQ)
|
2022-02-12 05:26:54 +01:00
|
|
|
{
|
|
|
|
return MISSING_SENSOR;
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 10:31:43 +01:00
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
return mMoisture_raw.getMedian();
|
|
|
|
}
|
|
|
|
|
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);
|
2022-02-19 02:24:19 +01:00
|
|
|
void initSensors(void);
|
2020-10-23 16:20:34 +02:00
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
long getCooldownInSeconds()
|
|
|
|
{
|
2021-10-27 01:42:01 +02:00
|
|
|
return this->mSetting->pPumpCooldownInSeconds->get();
|
2021-04-07 21:26:11 +02:00
|
|
|
}
|
2020-10-23 16:47:40 +02:00
|
|
|
|
2021-04-07 21:26:11 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the Hours when pumping should start
|
2022-03-05 10:31:43 +01:00
|
|
|
*
|
|
|
|
* @return hour
|
2021-04-07 21:26:11 +02:00
|
|
|
*/
|
2022-02-12 05:26:54 +01:00
|
|
|
int getHoursStart()
|
|
|
|
{
|
2021-04-07 21:26:11 +02:00
|
|
|
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
|
2022-03-05 10:31:43 +01:00
|
|
|
*
|
|
|
|
* @return hour
|
2021-04-07 21:26:11 +02:00
|
|
|
*/
|
2022-02-12 05:26:54 +01:00
|
|
|
int getHoursEnd()
|
|
|
|
{
|
2021-04-07 21:26:11 +02:00
|
|
|
return this->mSetting->pPumpAllowedHourRangeEnd->get();
|
2020-10-23 16:20:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
bool isAllowedOnlyAtLowLight(void)
|
|
|
|
{
|
2022-02-12 05:26:54 +01:00
|
|
|
if (this->isHydroponic())
|
|
|
|
{
|
2021-10-27 00:40:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
2022-02-12 05:26:54 +01: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
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
long getPumpDuration()
|
|
|
|
{
|
2021-10-01 23:46:37 +02:00
|
|
|
return this->mSetting->pPumpDuration->get();
|
|
|
|
}
|
2022-04-27 21:19:26 +02:00
|
|
|
long getPumpMl()
|
|
|
|
{
|
|
|
|
return this->mSetting->pPumpMl->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|