fix for capacitive sensors not working, enum introduction for sensormode

This commit is contained in:
c3ma 2022-02-19 02:24:19 +01:00
parent d0320beaa7
commit a0f8df7016
6 changed files with 192 additions and 177 deletions

View File

@ -22,7 +22,9 @@
"array": "cpp", "array": "cpp",
"tuple": "cpp", "tuple": "cpp",
"utility": "cpp", "utility": "cpp",
"fstream": "cpp" "fstream": "cpp",
"ostream": "cpp",
"sstream": "cpp"
} }
} }
} }

View File

@ -1,51 +1,6 @@
/**
* @file ControllerConfiguration.h
* @author your name (you@domain.com)
* @brief
* @version 0.1
* @date 2020-05-30
*
* @copyright Copyright (c) 2020
*
* \mainpage Configuration of the controller
* @{
* Describe the used PINs of the controller
*
* @subpage Controller
*
* @subpage Homie
*
* @subpage Configuration
*
* There are several modes in the controller
* \dot
* digraph Operationmode {
* ranksep=.75;
* poweroff [ label="off" ];
* mode1 [ label="Mode 1 - Sensor only", shape=box, width=2 ];
* mode2 [ label="Mode 2 - Wifi enabled", shape=box ];
* mode3 [ label="Mode 3 - Stay alive", shape=box ];
* mode1 -> mode2 [ label="wakeup reason", fontsize=10 ];
* mode1 -> mode2 [ label="Time duration", fontsize=10 ];
* mode2 -> mode3 [ label="Over the Air Update", fontsize=10 ];
* mode3 -> mode2 [ label="Over the Air Finished", fontsize=10 ];
* mode3 -> mode2 [ label="Mqtt Command", fontsize=10 ];
* mode2 -> mode3 [ label="Mqtt Command", fontsize=10 ];
* poweroff -> mode1 [ label="deep sleep wakeup", fontsize=10 ];
* mode1 -> poweroff [ label="enter deep sleep", fontsize=10 ];
* mode2 -> poweroff [ label="Mqtt queue empty", fontsize=10 ];
* }
* \enddot
*
* Before entering Deep sleep the controller is configured with an wakeup time.
*
* @}
*/
#ifndef CONTROLLER_CONFIG_H #ifndef CONTROLLER_CONFIG_H
#define CONTROLLER_CONFIG_H #define CONTROLLER_CONFIG_H
/** \addtogroup GPIO Settings
* @{
*/
#define SENSOR_PLANT0 GPIO_NUM_32 /**< GPIO 32 (ADC1) */ #define SENSOR_PLANT0 GPIO_NUM_32 /**< GPIO 32 (ADC1) */
#define SENSOR_PLANT1 GPIO_NUM_33 /**< GPIO 33 (ADC1) */ #define SENSOR_PLANT1 GPIO_NUM_33 /**< GPIO 33 (ADC1) */
#define SENSOR_PLANT2 GPIO_NUM_25 /**< GPIO 25 (ADC2) */ #define SENSOR_PLANT2 GPIO_NUM_25 /**< GPIO 25 (ADC2) */
@ -77,12 +32,8 @@
#define I2C1_SDA GPIO_NUM_34 /**< GPIO 34 - I2C */ #define I2C1_SDA GPIO_NUM_34 /**< GPIO 34 - I2C */
#define I2C1_SCL GPIO_NUM_35 /**< GPIO 35 - I2C */ #define I2C1_SCL GPIO_NUM_35 /**< GPIO 35 - I2C */
/* @} */
/** \addtogroup Configuration #define FIRMWARE_VERSION "sw 2.2 hw 0.10b"
* @{
*/
#define FIRMWARE_VERSION "sw 2.1 hw 0.10b"
#define TIMED_LIGHT_PIN CUSTOM1_PIN5 #define TIMED_LIGHT_PIN CUSTOM1_PIN5
#define FLOWMETER_PIN CUSTOM1_PIN1 #define FLOWMETER_PIN CUSTOM1_PIN1

View File

@ -13,9 +13,21 @@
#include <Homie.h> #include <Homie.h>
#define SENSOR_NONE 0 #define FOREACH_SENSOR(SENSOR) \
#define SENSOR_CAPACITIVE_FREQUENCY_MOD 1 SENSOR(NONE) \
#define SENSOR_ANALOG_RESISTANCE_PROBE 2 SENSOR(CAPACITIVE_FREQUENCY) \
SENSOR(ANALOG_RESISTANCE_PROBE) \
#define GENERATE_ENUM(ENUM) ENUM,
#define GENERATE_STRING(STRING) #STRING,
enum SENSOR_MODE {
FOREACH_SENSOR(GENERATE_ENUM)
};
static const char *SENSOR_STRING[] = {
FOREACH_SENSOR(GENERATE_STRING)
};
//plant pump is deactivated, but sensor values are still recorded and published //plant pump is deactivated, but sensor values are still recorded and published
#define DEACTIVATED_PLANT -1 #define DEACTIVATED_PLANT -1

View File

@ -64,15 +64,22 @@ public:
void activatePump(void); void activatePump(void);
String getSensorModeString(){
SENSOR_MODE mode = getSensorMode();
return SENSOR_STRING[mode];
}
bool isHydroponic() bool isHydroponic()
{ {
long current = this->mSetting->pSensorDry->get(); long current = this->mSetting->pSensorDry->get();
return equalish(current, HYDROPONIC_MODE); return equalish(current, HYDROPONIC_MODE);
} }
long isSensorMode(int sensorMode) SENSOR_MODE getSensorMode()
{ {
return this->mSetting->pSensorMode->get() == sensorMode; int raw_mode = this->mSetting->pSensorMode->get();
SENSOR_MODE sensorType = static_cast<SENSOR_MODE>(raw_mode);
return sensorType;
} }
/** /**
@ -113,18 +120,14 @@ public:
float getCurrentMoisturePCT() float getCurrentMoisturePCT()
{ {
if (isSensorMode(SENSOR_NONE)) switch (getSensorMode()){
{ case NONE:
return DEACTIVATED_PLANT; return DEACTIVATED_PLANT;
} case CAPACITIVE_FREQUENCY:
if (isSensorMode(SENSOR_CAPACITIVE_FREQUENCY_MOD))
{
return mapf(mMoisture_raw.getMedian(), MOIST_SENSOR_MAX_FRQ, MOIST_SENSOR_MIN_FRQ, 0, 100); return mapf(mMoisture_raw.getMedian(), MOIST_SENSOR_MAX_FRQ, MOIST_SENSOR_MIN_FRQ, 0, 100);
} case ANALOG_RESISTANCE_PROBE:
else if (isSensorMode(SENSOR_ANALOG_RESISTANCE_PROBE))
{
return mapf(mMoisture_raw.getMedian(), ANALOG_SENSOR_MAX_MV, ANALOG_SENSOR_MIN_MV, 0, 100); return mapf(mMoisture_raw.getMedian(), ANALOG_SENSOR_MAX_MV, ANALOG_SENSOR_MIN_MV, 0, 100);
} else { default:
log(LOG_LEVEL_ERROR, LOG_SENSORMODE_UNKNOWN, LOG_SENSORMODE_UNKNOWN_CODE); log(LOG_LEVEL_ERROR, LOG_SENSORMODE_UNKNOWN, LOG_SENSORMODE_UNKNOWN_CODE);
return DEACTIVATED_PLANT; return DEACTIVATED_PLANT;
} }
@ -132,9 +135,8 @@ public:
float getCurrentMoistureRaw() float getCurrentMoistureRaw()
{ {
if (isSensorMode(SENSOR_CAPACITIVE_FREQUENCY_MOD)) if(getSensorMode() == CAPACITIVE_FREQUENCY){
{ if (mMoisture_raw.getMedian() < MOIST_SENSOR_MIN_FRQ)
if (mMoisture_raw.getMedian() < MOIST_SENSOR_MIN_FRQ)
{ {
return MISSING_SENSOR; return MISSING_SENSOR;
} }
@ -149,6 +151,7 @@ public:
} }
void init(void); void init(void);
void initSensors(void);
long getCooldownInSeconds() long getCooldownInSeconds()
{ {

View File

@ -27,42 +27,33 @@ void Plant::init(void)
{ {
/* Initialize Home Settings validator */ /* Initialize Home Settings validator */
this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT); this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT);
this->mSetting->pSensorDry->setValidator([](long candidate) { this->mSetting->pSensorDry->setValidator([](long candidate)
return (((candidate >= 0.0) && (candidate <= 100.0)) || equalish(candidate,DEACTIVATED_PLANT) || equalish(candidate,HYDROPONIC_MODE)); { return (((candidate >= 0.0) && (candidate <= 100.0)) || equalish(candidate, DEACTIVATED_PLANT) || equalish(candidate, HYDROPONIC_MODE)); });
});
this->mSetting->pSensorMode->setDefaultValue(SENSOR_NONE); this->mSetting->pSensorMode->setDefaultValue(NONE);
this->mSetting->pSensorMode->setValidator([](long candidate) { this->mSetting->pSensorMode->setValidator([](long candidate)
return candidate == SENSOR_NONE || candidate == SENSOR_CAPACITIVE_FREQUENCY_MOD || candidate == SENSOR_ANALOG_RESISTANCE_PROBE; { return candidate == NONE || candidate == CAPACITIVE_FREQUENCY || candidate == ANALOG_RESISTANCE_PROBE; });
});
this->mSetting->pPumpAllowedHourRangeStart->setDefaultValue(8); // start at 8:00 this->mSetting->pPumpAllowedHourRangeStart->setDefaultValue(8); // start at 8:00
this->mSetting->pPumpAllowedHourRangeStart->setValidator([](long candidate) { this->mSetting->pPumpAllowedHourRangeStart->setValidator([](long candidate)
return ((candidate >= 0) && (candidate <= 23)); { return ((candidate >= 0) && (candidate <= 23)); });
});
this->mSetting->pPumpAllowedHourRangeEnd->setDefaultValue(20); // stop pumps at 20:00 this->mSetting->pPumpAllowedHourRangeEnd->setDefaultValue(20); // stop pumps at 20:00
this->mSetting->pPumpAllowedHourRangeEnd->setValidator([](long candidate) { this->mSetting->pPumpAllowedHourRangeEnd->setValidator([](long candidate)
return ((candidate >= 0) && (candidate <= 23)); { return ((candidate >= 0) && (candidate <= 23)); });
});
this->mSetting->pPumpOnlyWhenLowLight->setDefaultValue(true); this->mSetting->pPumpOnlyWhenLowLight->setDefaultValue(true);
this->mSetting->pPumpCooldownInSeconds->setDefaultValue(60*60); // 1 hour this->mSetting->pPumpCooldownInSeconds->setDefaultValue(60 * 60); // 1 hour
this->mSetting->pPumpCooldownInSeconds->setValidator([](long candidate) { this->mSetting->pPumpCooldownInSeconds->setValidator([](long candidate)
return (candidate >= 0); { return (candidate >= 0); });
});
this->mSetting->pPumpDuration->setDefaultValue(30); this->mSetting->pPumpDuration->setDefaultValue(30);
this->mSetting->pPumpDuration->setValidator([](long candidate) { this->mSetting->pPumpDuration->setValidator([](long candidate)
return ((candidate >= 0) && (candidate <= 1000)); { return ((candidate >= 0) && (candidate <= 1000)); });
});
this->mSetting->pPumpMl->setDefaultValue(0); this->mSetting->pPumpMl->setDefaultValue(0);
this->mSetting->pPumpMl->setValidator([](long candidate) { this->mSetting->pPumpMl->setValidator([](long candidate)
return ((candidate >= 0) && (candidate <= 5000)); { return ((candidate >= 0) && (candidate <= 5000)); });
});
this->mSetting->pPumpPowerLevel->setDefaultValue(100); this->mSetting->pPumpPowerLevel->setDefaultValue(100);
this->mSetting->pPumpPowerLevel->setValidator([](long candidate) { this->mSetting->pPumpPowerLevel->setValidator([](long candidate)
return ((candidate >= 0) && (candidate <= 100)); { return ((candidate >= 0) && (candidate <= 100)); });
});
/* Initialize Hardware */ /* Initialize Hardware */
Serial.println("Init PWM controller for pump " + String(mPinPump) + "=" + String(OUTPUT)); Serial.println("Init PWM controller for pump " + String(mPinPump) + "=" + String(OUTPUT));
@ -74,12 +65,16 @@ void Plant::init(void)
Serial.println("Set GPIO mode " + String(mPinSensor) + "=" + String(ANALOG)); Serial.println("Set GPIO mode " + String(mPinSensor) + "=" + String(ANALOG));
Serial.flush(); Serial.flush();
pinMode(this->mPinSensor, INPUT); pinMode(this->mPinSensor, INPUT);
}
if(isSensorMode(SENSOR_CAPACITIVE_FREQUENCY_MOD)){ void Plant::initSensors(void){
switch (getSensorMode())
{
case CAPACITIVE_FREQUENCY:
{
pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_0 + this->mPlantId);
pcnt_unit_t unit = (pcnt_unit_t) (PCNT_UNIT_0 + this->mPlantId); pcnt_config_t pcnt_config = {}; // Instancia PCNT config
pcnt_config_t pcnt_config = { }; // Instancia PCNT config
pcnt_config.pulse_gpio_num = this->mPinSensor; // Configura GPIO para entrada dos pulsos 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.ctrl_gpio_num = PCNT_PIN_NOT_USED; // Configura GPIO para controle da contagem
@ -92,64 +87,98 @@ void Plant::init(void)
pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // PCNT - modo hctrl - se HIGH conta incrementando pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // PCNT - modo hctrl - se HIGH conta incrementando
pcnt_unit_config(&pcnt_config); // Configura o contador PCNT pcnt_unit_config(&pcnt_config); // Configura o contador PCNT
pcnt_counter_pause(unit); // Pausa o contador PCNT pcnt_counter_pause(unit); // Pausa o contador PCNT
pcnt_counter_clear(unit); // Zera o contador PCNT pcnt_counter_clear(unit); // Zera o contador PCNT
Serial.println("Setup Counter " + String(mPinPump) + "=" + String(LOW)); Serial.println("Setup Counter " + String(mPinPump) + "=" + String(LOW));
} else if (isSensorMode(SENSOR_ANALOG_RESISTANCE_PROBE)){ break;
adcAttachPin(this->mPinSensor);
} else if (isSensorMode(SENSOR_NONE)){
//nothing to do here
} else {
log(LOG_LEVEL_ERROR, LOG_SENSORMODE_UNKNOWN, LOG_SENSORMODE_UNKNOWN_CODE);
} }
case ANALOG_RESISTANCE_PROBE:
{
adcAttachPin(this->mPinSensor);
break;
}
case NONE:
{
//do nothing
break;
}
}
} }
void Plant::blockingMoistureMeasurement(void) { void Plant::blockingMoistureMeasurement(void)
if(isSensorMode(SENSOR_ANALOG_RESISTANCE_PROBE)){ {
for(int i = 0;i<ANALOG_REREADS;i++){ switch (getSensorMode())
{
case ANALOG_RESISTANCE_PROBE:
{
for (int i = 0; i < ANALOG_REREADS; i++)
{
this->mMoisture_raw.add(analogReadMilliVolts(this->mPinSensor)); this->mMoisture_raw.add(analogReadMilliVolts(this->mPinSensor));
delay(5); delay(5);
break;
} }
}else if(isSensorMode(SENSOR_CAPACITIVE_FREQUENCY_MOD) || isSensorMode(SENSOR_NONE)){ }
case CAPACITIVE_FREQUENCY:
case NONE:
{
//nothing to do here //nothing to do here
} else { break;
log(LOG_LEVEL_ERROR, LOG_SENSORMODE_UNKNOWN, LOG_SENSORMODE_UNKNOWN_CODE); }
} }
} }
void Plant::startMoistureMeasurement(void)
void Plant::startMoistureMeasurement(void) { {
if(isSensorMode(SENSOR_CAPACITIVE_FREQUENCY_MOD)){ switch (getSensorMode())
pcnt_unit_t unit = (pcnt_unit_t) (PCNT_UNIT_0 + this->mPlantId); {
case CAPACITIVE_FREQUENCY:
{
pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_0 + this->mPlantId);
pcnt_counter_resume(unit); pcnt_counter_resume(unit);
} else if (isSensorMode(SENSOR_ANALOG_RESISTANCE_PROBE) || isSensorMode(SENSOR_NONE)){ break;
//nothing to do here }
} else { case ANALOG_RESISTANCE_PROBE:
log(LOG_LEVEL_ERROR, LOG_SENSORMODE_UNKNOWN, LOG_SENSORMODE_UNKNOWN_CODE); case NONE:
{
//do nothing here
}
} }
} }
void Plant::stopMoistureMeasurement(void) { void Plant::stopMoistureMeasurement(void)
if(isSensorMode(SENSOR_CAPACITIVE_FREQUENCY_MOD)){ {
switch (getSensorMode())
{
case CAPACITIVE_FREQUENCY:
{
int16_t pulses; int16_t pulses;
pcnt_unit_t unit = (pcnt_unit_t) (PCNT_UNIT_0 + this->mPlantId); pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_0 + this->mPlantId);
pcnt_counter_pause(unit); pcnt_counter_pause(unit);
esp_err_t result = pcnt_get_counter_value(unit, &pulses); esp_err_t result = pcnt_get_counter_value(unit, &pulses);
pcnt_counter_clear(unit); pcnt_counter_clear(unit);
if(result != ESP_OK){ if (result != ESP_OK)
{
log(LOG_LEVEL_ERROR, LOG_HARDWARECOUNTER_ERROR_MESSAGE, LOG_HARDWARECOUNTER_ERROR_CODE); log(LOG_LEVEL_ERROR, LOG_HARDWARECOUNTER_ERROR_MESSAGE, LOG_HARDWARECOUNTER_ERROR_CODE);
this-> mMoisture_raw.clear(); this->mMoisture_raw.clear();
this -> mMoisture_raw.add(-1); this->mMoisture_raw.add(-1);
} else { }
else
{
this->mMoisture_raw.add(pulses * (1000 / MOISTURE_MEASUREMENT_DURATION)); this->mMoisture_raw.add(pulses * (1000 / MOISTURE_MEASUREMENT_DURATION));
} }
}else if (isSensorMode(SENSOR_ANALOG_RESISTANCE_PROBE) || isSensorMode(SENSOR_NONE)){ break;
}
case ANALOG_RESISTANCE_PROBE:
{
//nothing to do here //nothing to do here
} else { break;
log(LOG_LEVEL_ERROR, LOG_SENSORMODE_UNKNOWN, LOG_SENSORMODE_UNKNOWN_CODE); }
case NONE:
{
break;
}
} }
} }
@ -193,7 +222,8 @@ void Plant::deactivatePump(void)
} }
} }
void Plant::publishState(String state) { void Plant::publishState(String state)
{
if (this->mConnected) if (this->mConnected)
{ {
this->mPlant->setProperty("state").send(state); this->mPlant->setProperty("state").send(state);
@ -206,7 +236,7 @@ void Plant::activatePump(void)
Serial << "activating pump " << plantId << endl; Serial << "activating pump " << plantId << endl;
long desiredPowerLevelPercent = this->mSetting->pPumpPowerLevel->get(); long desiredPowerLevelPercent = this->mSetting->pPumpPowerLevel->get();
ledcWrite(this->mPlantId, desiredPowerLevelPercent*PWM_BITS); ledcWrite(this->mPlantId, desiredPowerLevelPercent * PWM_BITS);
if (this->mConnected) if (this->mConnected)
{ {
const String OFF = String("ON"); const String OFF = String("ON");
@ -215,23 +245,31 @@ void Plant::activatePump(void)
} }
} }
bool Plant::switchHandler(const HomieRange& range, const String& value) { bool Plant::switchHandler(const HomieRange &range, const String &value)
if (range.isRange) { {
if (range.isRange)
{
return false; // only one switch is present return false; // only one switch is present
} }
if ((value.equals("ON")) || (value.equals("On")) || (value.equals("on")) || (value.equals("true"))) { if ((value.equals("ON")) || (value.equals("On")) || (value.equals("on")) || (value.equals("true")))
{
this->activatePump(); this->activatePump();
return true; return true;
} else if ((value.equals("OFF")) || (value.equals("Off")) || (value.equals("off")) || (value.equals("false")) ) { }
else if ((value.equals("OFF")) || (value.equals("Off")) || (value.equals("off")) || (value.equals("false")))
{
this->deactivatePump(); this->deactivatePump();
return true; return true;
} else { }
else
{
return false; return false;
} }
} }
void Plant::setSwitchHandler(HomieInternals::PropertyInputHandler f) { void Plant::setSwitchHandler(HomieInternals::PropertyInputHandler f)
{
this->mPump.settable(f); this->mPump.settable(f);
} }

View File

@ -254,6 +254,10 @@ void readOneWireSensors()
*/ */
void readPowerSwitchedSensors() void readPowerSwitchedSensors()
{ {
for (int i = 0; i < MAX_PLANTS; i++)
{
Serial << "Sensor " << i << " mode: " << mPlants[i].getSensorModeString() << endl;
}
digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH); digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH);
delay(50); delay(50);
for (int i = 0; i < MAX_PLANTS; i++) for (int i = 0; i < MAX_PLANTS; i++)
@ -274,21 +278,20 @@ void readPowerSwitchedSensors()
for (int i = 0; i < MAX_PLANTS; i++) for (int i = 0; i < MAX_PLANTS; i++)
{ {
if (mPlants[i].isSensorMode(SENSOR_CAPACITIVE_FREQUENCY_MOD)) Plant plant = mPlants[i];
switch (plant.getSensorMode())
{ {
Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentMoistureRaw() << " hz" << endl; case CAPACITIVE_FREQUENCY: {
Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentMoistureRaw() << " hz " << mPlants[i].getCurrentMoisturePCT() << "%" << endl;
break;
} }
else if (mPlants[i].isSensorMode(SENSOR_ANALOG_RESISTANCE_PROBE)) case ANALOG_RESISTANCE_PROBE : {
{ Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentMoistureRaw() << " mV" << mPlants[i].getCurrentMoisturePCT() << "%" << endl;
Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentMoistureRaw() << " mV" << endl; break;
} }
else if (mPlants[i].isSensorMode(SENSOR_NONE)) case NONE : {
{
Serial << "Plant " << i << " measurement: no sensor configured" << endl;
} }
else
{
log(LOG_LEVEL_ERROR, LOG_SENSORMODE_UNKNOWN, LOG_SENSORMODE_UNKNOWN_CODE);
} }
} }
@ -760,6 +763,12 @@ void safeSetup()
Homie.setup(); Homie.setup();
/* Intialize Plant */
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].initSensors();
}
/************************* Start One-Wire bus ***************/ /************************* Start One-Wire bus ***************/
int tempInitStartTime = millis(); int tempInitStartTime = millis();
uint8_t sensorCount = 0U; uint8_t sensorCount = 0U;