2020-09-07 18:18:46 +02:00
|
|
|
/**
|
|
|
|
* @file PlantCtrl.cpp
|
|
|
|
* @author your name (you@domain.com)
|
2022-03-05 10:31:43 +01:00
|
|
|
* @brief
|
2020-09-07 18:18:46 +02:00
|
|
|
* @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
|
|
|
|
|
|
|
*/
|
|
|
|
#include "PlantCtrl.h"
|
2020-11-04 23:28:08 +01:00
|
|
|
#include "ControllerConfiguration.h"
|
2021-07-01 21:19:51 +02:00
|
|
|
#include "TimeUtils.h"
|
2022-02-19 02:24:19 +01:00
|
|
|
#include "driver/pcnt.h"
|
2022-03-05 10:31:43 +01:00
|
|
|
#include "MQTTUtils.h"
|
2021-07-09 21:50:51 +02:00
|
|
|
|
2022-03-05 10:31:43 +01:00
|
|
|
Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting, SENSOR_MODE mode)
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2020-10-14 20:07:56 +02:00
|
|
|
this->mPinSensor = pinSensor;
|
|
|
|
this->mPinPump = pinPump;
|
2020-10-16 16:22:48 +02:00
|
|
|
this->mPlant = plant;
|
2020-11-04 21:57:40 +01:00
|
|
|
this->mSetting = setting;
|
2020-12-20 17:00:44 +01:00
|
|
|
this->mPlantId = plantId;
|
2022-03-05 10:31:43 +01:00
|
|
|
this->mSensorMode = mode;
|
2020-10-16 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void Plant::init(void)
|
|
|
|
{
|
2020-10-23 16:20:34 +02:00
|
|
|
/* Initialize Home Settings validator */
|
2020-10-28 21:20:46 +01:00
|
|
|
this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT);
|
2022-02-19 02:24:19 +01:00
|
|
|
this->mSetting->pSensorDry->setValidator([](long candidate)
|
2022-05-20 01:00:04 +02:00
|
|
|
{ return (((candidate >= 0.0) && (candidate <= 100.0)) || equalish(candidate, DEACTIVATED_PLANT) || equalish(candidate, HYDROPONIC_MODE) || equalish(candidate, TIMER_ONLY)); });
|
2022-02-12 05:26:54 +01:00
|
|
|
|
2020-10-16 18:40:39 +02:00
|
|
|
this->mSetting->pPumpAllowedHourRangeStart->setDefaultValue(8); // start at 8:00
|
2022-02-19 02:24:19 +01:00
|
|
|
this->mSetting->pPumpAllowedHourRangeStart->setValidator([](long candidate)
|
|
|
|
{ return ((candidate >= 0) && (candidate <= 23)); });
|
2020-10-16 18:40:39 +02:00
|
|
|
this->mSetting->pPumpAllowedHourRangeEnd->setDefaultValue(20); // stop pumps at 20:00
|
2022-02-19 02:24:19 +01:00
|
|
|
this->mSetting->pPumpAllowedHourRangeEnd->setValidator([](long candidate)
|
|
|
|
{ return ((candidate >= 0) && (candidate <= 23)); });
|
2022-04-29 10:45:59 +02:00
|
|
|
this->mSetting->pPumpOnlyWhenLowLight->setDefaultValue(false);
|
2022-02-19 02:24:19 +01:00
|
|
|
this->mSetting->pPumpCooldownInSeconds->setDefaultValue(60 * 60); // 1 hour
|
|
|
|
this->mSetting->pPumpCooldownInSeconds->setValidator([](long candidate)
|
|
|
|
{ return (candidate >= 0); });
|
2020-10-23 16:20:34 +02:00
|
|
|
|
2021-12-03 19:36:10 +01:00
|
|
|
this->mSetting->pPumpDuration->setDefaultValue(30);
|
2022-02-19 02:24:19 +01:00
|
|
|
this->mSetting->pPumpDuration->setValidator([](long candidate)
|
|
|
|
{ return ((candidate >= 0) && (candidate <= 1000)); });
|
2022-04-27 21:19:26 +02:00
|
|
|
this->mSetting->pPumpMl->setDefaultValue(1000);
|
2022-02-19 02:24:19 +01:00
|
|
|
this->mSetting->pPumpMl->setValidator([](long candidate)
|
|
|
|
{ return ((candidate >= 0) && (candidate <= 5000)); });
|
2021-10-26 21:50:55 +02:00
|
|
|
this->mSetting->pPumpPowerLevel->setDefaultValue(100);
|
2022-02-19 02:24:19 +01:00
|
|
|
this->mSetting->pPumpPowerLevel->setValidator([](long candidate)
|
|
|
|
{ return ((candidate >= 0) && (candidate <= 100)); });
|
2021-10-01 23:46:37 +02:00
|
|
|
|
2020-10-23 16:20:34 +02:00
|
|
|
/* Initialize Hardware */
|
2021-10-26 21:50:55 +02:00
|
|
|
ledcSetup(this->mPlantId, PWM_FREQ, PWM_BITS);
|
|
|
|
ledcAttachPin(mPinPump, this->mPlantId);
|
|
|
|
ledcWrite(this->mPlantId, 0);
|
2021-07-21 21:23:58 +02:00
|
|
|
pinMode(this->mPinSensor, INPUT);
|
2022-02-19 02:24:19 +01:00
|
|
|
}
|
2021-10-27 01:42:01 +02:00
|
|
|
|
2022-03-05 10:31:43 +01:00
|
|
|
void Plant::initSensors(void)
|
|
|
|
{
|
2022-02-19 02:24:19 +01:00
|
|
|
switch (getSensorMode())
|
|
|
|
{
|
2023-02-12 12:51:52 +01:00
|
|
|
case FREQUENCY_MOD_RESISTANCE_PROBE:
|
2022-02-19 02:24:19 +01:00
|
|
|
{
|
2022-02-12 05:26:54 +01:00
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_0 + this->mPlantId);
|
|
|
|
pcnt_config_t pcnt_config = {}; // Instancia PCNT config
|
2021-07-21 21:23:58 +02:00
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
pcnt_config.pulse_gpio_num = this->mPinSensor; // Configura GPIO para entrada dos pulsos
|
|
|
|
pcnt_config.ctrl_gpio_num = PCNT_PIN_NOT_USED; // Configura GPIO para controle da contagem
|
|
|
|
pcnt_config.unit = unit; // Unidade de contagem PCNT - 0
|
|
|
|
pcnt_config.channel = PCNT_CHANNEL_0; // Canal de contagem PCNT - 0
|
|
|
|
pcnt_config.counter_h_lim = INT16_MAX; // Limite maximo de contagem - 20000
|
|
|
|
pcnt_config.pos_mode = PCNT_COUNT_INC; // Incrementa contagem na subida do pulso
|
|
|
|
pcnt_config.neg_mode = PCNT_COUNT_DIS; // Incrementa contagem na descida do pulso
|
|
|
|
pcnt_config.lctrl_mode = PCNT_MODE_KEEP; // PCNT - modo lctrl desabilitado
|
|
|
|
pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // PCNT - modo hctrl - se HIGH conta incrementando
|
|
|
|
pcnt_unit_config(&pcnt_config); // Configura o contador PCNT
|
2021-07-21 21:23:58 +02:00
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
pcnt_counter_pause(unit); // Pausa o contador PCNT
|
|
|
|
pcnt_counter_clear(unit); // Zera o contador PCNT
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ANALOG_RESISTANCE_PROBE:
|
|
|
|
{
|
2022-02-12 05:26:54 +01:00
|
|
|
adcAttachPin(this->mPinSensor);
|
2022-02-19 02:24:19 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NONE:
|
|
|
|
{
|
2022-03-05 10:31:43 +01:00
|
|
|
// do nothing
|
2022-02-19 02:24:19 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-02-12 05:26:54 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-21 21:23:58 +02:00
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
void Plant::blockingMoistureMeasurement(void)
|
|
|
|
{
|
|
|
|
switch (getSensorMode())
|
|
|
|
{
|
|
|
|
case ANALOG_RESISTANCE_PROBE:
|
|
|
|
{
|
|
|
|
for (int i = 0; i < ANALOG_REREADS; i++)
|
|
|
|
{
|
|
|
|
this->mMoisture_raw.add(analogReadMilliVolts(this->mPinSensor));
|
2022-02-12 05:26:54 +01:00
|
|
|
delay(5);
|
|
|
|
}
|
2022-03-05 10:31:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-02-12 12:51:52 +01:00
|
|
|
case FREQUENCY_MOD_RESISTANCE_PROBE:
|
2022-02-19 02:24:19 +01:00
|
|
|
case NONE:
|
|
|
|
{
|
2022-03-05 10:31:43 +01:00
|
|
|
// nothing to do here
|
2022-02-19 02:24:19 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-02-12 05:26:54 +01:00
|
|
|
}
|
2020-10-23 16:20:34 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
void Plant::startMoistureMeasurement(void)
|
|
|
|
{
|
|
|
|
switch (getSensorMode())
|
|
|
|
{
|
2023-02-12 12:51:52 +01:00
|
|
|
case FREQUENCY_MOD_RESISTANCE_PROBE:
|
2022-02-19 02:24:19 +01:00
|
|
|
{
|
|
|
|
pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_0 + this->mPlantId);
|
2022-02-12 05:26:54 +01:00
|
|
|
pcnt_counter_resume(unit);
|
2022-02-19 02:24:19 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ANALOG_RESISTANCE_PROBE:
|
|
|
|
case NONE:
|
|
|
|
{
|
2022-03-05 10:31:43 +01:00
|
|
|
// do nothing here
|
2022-02-19 02:24:19 +01:00
|
|
|
}
|
2022-02-12 05:26:54 +01:00
|
|
|
}
|
2021-07-01 20:39:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
void Plant::stopMoistureMeasurement(void)
|
|
|
|
{
|
|
|
|
switch (getSensorMode())
|
|
|
|
{
|
2023-02-12 12:51:52 +01:00
|
|
|
case FREQUENCY_MOD_RESISTANCE_PROBE:
|
2022-02-19 02:24:19 +01:00
|
|
|
{
|
2022-02-12 05:26:54 +01:00
|
|
|
int16_t pulses;
|
2022-02-19 02:24:19 +01:00
|
|
|
pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_0 + this->mPlantId);
|
|
|
|
pcnt_counter_pause(unit);
|
2022-02-12 05:26:54 +01:00
|
|
|
esp_err_t result = pcnt_get_counter_value(unit, &pulses);
|
|
|
|
pcnt_counter_clear(unit);
|
2022-02-19 02:24:19 +01:00
|
|
|
if (result != ESP_OK)
|
|
|
|
{
|
2022-02-12 05:26:54 +01:00
|
|
|
log(LOG_LEVEL_ERROR, LOG_HARDWARECOUNTER_ERROR_MESSAGE, LOG_HARDWARECOUNTER_ERROR_CODE);
|
2022-02-19 02:24:19 +01:00
|
|
|
this->mMoisture_raw.clear();
|
|
|
|
this->mMoisture_raw.add(-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-12 05:26:54 +01:00
|
|
|
this->mMoisture_raw.add(pulses * (1000 / MOISTURE_MEASUREMENT_DURATION));
|
|
|
|
}
|
2022-02-19 02:24:19 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ANALOG_RESISTANCE_PROBE:
|
|
|
|
case NONE:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2021-10-06 21:24:34 +02:00
|
|
|
}
|
2020-10-23 16:20:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void Plant::postMQTTconnection(void)
|
|
|
|
{
|
2020-10-23 16:20:34 +02:00
|
|
|
const String OFF = String("OFF");
|
2020-11-04 21:57:40 +01:00
|
|
|
this->mConnected = true;
|
2020-10-23 16:20:34 +02:00
|
|
|
this->mPlant->setProperty("switch").send(OFF);
|
2021-07-09 21:50:51 +02:00
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
float pct = getCurrentMoisturePCT();
|
|
|
|
float raw = getCurrentMoistureRaw();
|
2021-07-09 21:50:51 +02:00
|
|
|
if (equalish(raw, MISSING_SENSOR))
|
|
|
|
{
|
2022-02-19 02:24:19 +01:00
|
|
|
pct = 0;
|
2021-07-09 21:50:51 +02:00
|
|
|
}
|
|
|
|
if (pct < 0)
|
|
|
|
{
|
2022-02-19 02:24:19 +01:00
|
|
|
pct = 0;
|
2021-07-09 21:50:51 +02:00
|
|
|
}
|
|
|
|
if (pct > 100)
|
|
|
|
{
|
2022-02-19 02:24:19 +01:00
|
|
|
pct = 100;
|
2021-07-09 21:50:51 +02:00
|
|
|
}
|
|
|
|
|
2022-02-12 05:26:54 +01:00
|
|
|
this->mPlant->setProperty("moist").send(String(pct));
|
2022-02-19 02:51:36 +01:00
|
|
|
this->mPlant->setProperty("sensormode").send(getSensorModeString());
|
2021-07-09 21:50:51 +02:00
|
|
|
this->mPlant->setProperty("moistraw").send(String(raw));
|
2022-02-12 05:26:54 +01:00
|
|
|
this->mPlant->setProperty("moisttrigger").send(String(getTargetMoisturePCT()));
|
2020-10-23 16:20:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void Plant::deactivatePump(void)
|
|
|
|
{
|
2021-05-26 21:46:33 +02:00
|
|
|
int plantId = this->mPlantId;
|
|
|
|
Serial << "deactivating pump " << plantId << endl;
|
2021-10-26 21:50:55 +02:00
|
|
|
ledcWrite(this->mPlantId, 0);
|
2020-11-04 21:57:40 +01:00
|
|
|
if (this->mConnected)
|
|
|
|
{
|
2020-10-23 16:20:34 +02:00
|
|
|
const String OFF = String("OFF");
|
|
|
|
this->mPlant->setProperty("switch").send(OFF);
|
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
void Plant::publishState(String state)
|
|
|
|
{
|
2021-07-01 22:06:50 +02:00
|
|
|
if (this->mConnected)
|
|
|
|
{
|
|
|
|
this->mPlant->setProperty("state").send(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void Plant::activatePump(void)
|
|
|
|
{
|
2021-05-26 21:46:33 +02:00
|
|
|
int plantId = this->mPlantId;
|
2022-02-19 02:24:19 +01:00
|
|
|
|
2021-05-26 21:46:33 +02:00
|
|
|
Serial << "activating pump " << plantId << endl;
|
2021-10-26 21:50:55 +02:00
|
|
|
long desiredPowerLevelPercent = this->mSetting->pPumpPowerLevel->get();
|
2022-02-19 02:24:19 +01:00
|
|
|
ledcWrite(this->mPlantId, desiredPowerLevelPercent * PWM_BITS);
|
2020-11-04 21:57:40 +01:00
|
|
|
if (this->mConnected)
|
|
|
|
{
|
2020-10-23 16:20:34 +02:00
|
|
|
const String OFF = String("ON");
|
|
|
|
this->mPlant->setProperty("switch").send(OFF);
|
2021-07-01 21:19:51 +02:00
|
|
|
this->mPlant->setProperty("lastPump").send(String(getCurrentTime()));
|
2020-10-23 16:20:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
bool Plant::switchHandler(const HomieRange &range, const String &value)
|
|
|
|
{
|
|
|
|
if (range.isRange)
|
|
|
|
{
|
|
|
|
return false; // only one switch is present
|
2021-07-09 22:51:50 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
if ((value.equals("ON")) || (value.equals("On")) || (value.equals("on")) || (value.equals("true")))
|
|
|
|
{
|
2021-07-09 22:51:50 +02:00
|
|
|
this->activatePump();
|
|
|
|
return true;
|
2022-02-19 02:24:19 +01:00
|
|
|
}
|
|
|
|
else if ((value.equals("OFF")) || (value.equals("Off")) || (value.equals("off")) || (value.equals("false")))
|
|
|
|
{
|
2021-07-09 22:51:50 +02:00
|
|
|
this->deactivatePump();
|
|
|
|
return true;
|
2022-02-19 02:24:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-09 22:51:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 02:24:19 +01:00
|
|
|
void Plant::setSwitchHandler(HomieInternals::PropertyInputHandler f)
|
|
|
|
{
|
2021-07-09 22:51:50 +02:00
|
|
|
this->mPump.settable(f);
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void Plant::advertise(void)
|
|
|
|
{
|
2020-10-23 16:20:34 +02:00
|
|
|
// Advertise topics
|
2022-02-19 02:24:19 +01:00
|
|
|
mPump = this->mPlant->advertise("switch").setName("Pump").setDatatype("Boolean");
|
2022-12-20 00:07:10 +01:00
|
|
|
this->mPlant->advertise("lastPump").setName("lastPump").setDatatype("Integer").setUnit("unixtime").setRetained(true);
|
|
|
|
this->mPlant->advertise("moist").setName("Percent").setDatatype("Float").setUnit("%").setRetained(true);
|
2023-03-03 19:58:55 +01:00
|
|
|
this->mPlant->advertise("moistraw").setName("frequency").setDatatype("Float").setUnit("hz").setRetained(true);
|
2022-12-20 00:07:10 +01:00
|
|
|
this->mPlant->advertise("state").setName("state").setDatatype("String").setRetained(true);
|
2022-03-05 10:31:43 +01:00
|
|
|
|
2020-10-23 16:20:34 +02:00
|
|
|
}
|