code cleanups

This commit is contained in:
c3ma 2021-07-09 21:50:51 +02:00
parent 99703e5ad1
commit df39c09c50
8 changed files with 67 additions and 39 deletions

View File

@ -80,8 +80,8 @@
*/ */
#define FIRMWARE_VERSION "sw 1.3 hw 0.10" #define FIRMWARE_VERSION "sw 1.3 hw 0.10"
#define MOIST_SENSOR_MAX_ADC 1200 //swamp earth - 50 margin #define MOIST_SENSOR_MAX_ADC 2800 //swamp earth - 50 margin
#define MOIST_SENSOR_MIN_ADC 1800 //dry earth + 1500 margin #define MOIST_SENSOR_MIN_ADC 1200 //dry earth + 1500 margin
#define SOLAR_VOLT_FACTOR 11 #define SOLAR_VOLT_FACTOR 11
#define BATTSENSOR_INDEX_SOLAR 0 #define BATTSENSOR_INDEX_SOLAR 0

View File

@ -18,6 +18,7 @@
#define MAX_PLANTS 7 #define MAX_PLANTS 7
/** /**
* @name Homie Attributes * @name Homie Attributes
* generated Information * generated Information
@ -88,7 +89,7 @@ HomieSetting<const char *> ntpServer("ntpServer", "NTP server (pool.ntp.org as d
**/ **/
#define GENERATE_PLANT(plant, strplant) \ #define GENERATE_PLANT(plant, strplant) \
HomieSetting<long> mSensorDry##plant = HomieSetting<long>("dry" strplant, "Plant " strplant "- Moist sensor dry threshold"); \ HomieSetting<double> mSensorDry##plant = HomieSetting<double>("dry" strplant, "Plant " strplant "- Moist sensor dry %"); \
HomieSetting<long> mPumpAllowedHourRangeStart##plant = HomieSetting<long>("hourstart" strplant, "Plant" strplant " - Range pump allowed hour start (0-23)"); \ HomieSetting<long> mPumpAllowedHourRangeStart##plant = HomieSetting<long>("hourstart" strplant, "Plant" strplant " - Range pump allowed hour start (0-23)"); \
HomieSetting<long> mPumpAllowedHourRangeEnd##plant = HomieSetting<long>("hourend" strplant, "Plant" strplant " - Range pump allowed hour end (0-23)"); \ HomieSetting<long> mPumpAllowedHourRangeEnd##plant = HomieSetting<long>("hourend" strplant, "Plant" strplant " - Range pump allowed hour end (0-23)"); \
HomieSetting<bool> mPumpOnlyWhenLowLight##plant = HomieSetting<bool>("lowLight" strplant, "Plant" strplant " - Enable the Pump only, when there is no sunlight"); \ HomieSetting<bool> mPumpOnlyWhenLowLight##plant = HomieSetting<bool>("lowLight" strplant, "Plant" strplant " - Enable the Pump only, when there is no sunlight"); \

View File

@ -13,12 +13,12 @@
#include <Homie.h> #include <Homie.h>
#define DEACTIVATED_PLANT 5000 #define DEACTIVATED_PLANT -1
#define MISSING_SENSOR 5001 #define MISSING_SENSOR -2
typedef struct PlantSettings_t typedef struct PlantSettings_t
{ {
HomieSetting<long> *pSensorDry; HomieSetting<double> *pSensorDry;
HomieSetting<long> *pPumpAllowedHourRangeStart; HomieSetting<long> *pPumpAllowedHourRangeStart;
HomieSetting<long> *pPumpAllowedHourRangeEnd; HomieSetting<long> *pPumpAllowedHourRangeEnd;
HomieSetting<bool> *pPumpOnlyWhenLowLight; HomieSetting<bool> *pPumpOnlyWhenLowLight;

View File

@ -0,0 +1,7 @@
#ifndef MATHUTILS_H
#define MATHUTILS_H
bool equalish(double x, double y);
#endif

View File

@ -13,7 +13,9 @@
#define PLANT_CTRL_H #define PLANT_CTRL_H
#include "HomieTypes.h" #include "HomieTypes.h"
#include "ControllerConfiguration.h"
#include "RunningMedian.h" #include "RunningMedian.h"
#include "MathUtils.h"
class Plant class Plant
{ {
@ -62,14 +64,15 @@ public:
*/ */
bool isPumpRequired() bool isPumpRequired()
{ {
bool isDry = getCurrentMoisture() > getSettingsMoisture(); bool isDry = getCurrentMoisture() > getSetting2Moisture();
bool isActive = isPumpTriggerActive(); bool isActive = isPumpTriggerActive();
return isDry && isActive; return isDry && isActive;
} }
bool isPumpTriggerActive() bool isPumpTriggerActive()
{ {
return this->mSetting->pSensorDry->get() != DEACTIVATED_PLANT; long current = this->mSetting->pSensorDry->get();
return !equalish(current,DEACTIVATED_PLANT);
} }
float getCurrentMoisture() float getCurrentMoisture()
@ -79,11 +82,13 @@ public:
} }
return this->moistureRaw.getMedian(); return this->moistureRaw.getMedian();
} }
long getSettingsMoisture()
long getSetting2Moisture()
{ {
if (this->mSetting->pSensorDry != NULL) if (this->mSetting->pSensorDry != NULL)
{ {
return this->mSetting->pSensorDry->get(); float percent = (this->mSetting->pSensorDry->get());
return MOIST_SENSOR_MIN_ADC + ((MOIST_SENSOR_MAX_ADC - MOIST_SENSOR_MIN_ADC) * percent);
} }
else else
{ {

7
esp32/src/MathUtil.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "MathUtils.h"
#include <Arduino.h>
bool equalish(double x, double y)
{
return (abs(x - y) < 0.5);
}

View File

@ -13,6 +13,12 @@
#include "PlantCtrl.h" #include "PlantCtrl.h"
#include "ControllerConfiguration.h" #include "ControllerConfiguration.h"
#include "TimeUtils.h" #include "TimeUtils.h"
#include "MathUtils.h"
double mapf(double x, double in_min, double in_max, double out_min, double out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting) Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting)
{ {
@ -27,8 +33,8 @@ 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([](double candidate) {
return (((candidate >= 0) && (candidate <= 4095)) || candidate == DEACTIVATED_PLANT); return (((candidate >= 0.0) && (candidate <= 100.0)) || equalish(candidate,DEACTIVATED_PLANT));
}); });
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) {
@ -63,6 +69,7 @@ void Plant::clearMoisture(void){
void Plant::addSenseValue(void) void Plant::addSenseValue(void)
{ {
int raw = analogRead(this->mPinSensor); int raw = analogRead(this->mPinSensor);
Serial << "plant bla " << raw << endl;
if(raw < MOIST_SENSOR_MAX_ADC && raw > MOIST_SENSOR_MIN_ADC){ if(raw < MOIST_SENSOR_MAX_ADC && raw > MOIST_SENSOR_MIN_ADC){
this->moistureRaw.add(raw); this->moistureRaw.add(raw);
} }
@ -73,6 +80,25 @@ void Plant::postMQTTconnection(void)
const String OFF = String("OFF"); const String OFF = String("OFF");
this->mConnected = true; this->mConnected = true;
this->mPlant->setProperty("switch").send(OFF); this->mPlant->setProperty("switch").send(OFF);
long raw = getCurrentMoisture();
double pct = 100 - mapf(raw, MOIST_SENSOR_MIN_ADC, MOIST_SENSOR_MAX_ADC, 0, 100);
if (equalish(raw, MISSING_SENSOR))
{
pct = 0;
}
if (pct < 0)
{
pct = 0;
}
if (pct > 100)
{
pct = 100;
}
this->mPlant->setProperty("moist").send(String(round(pct*10)/10));
this->mPlant->setProperty("moistraw").send(String(raw));
} }
void Plant::deactivatePump(void) void Plant::deactivatePump(void)

View File

@ -260,7 +260,7 @@ void readOneWireSensors()
void readPowerSwitchedSensors() void readPowerSwitchedSensors()
{ {
digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH); digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH);
delay(10); delay(500);
for (int i = 0; i < MAX_PLANTS; i++) for (int i = 0; i < MAX_PLANTS; i++)
{ {
mPlants[i].clearMoisture(); mPlants[i].clearMoisture();
@ -272,7 +272,7 @@ void readPowerSwitchedSensors()
{ {
mPlants[i].addSenseValue(); mPlants[i].addSenseValue();
} }
delay(2); delay(20);
} }
waterRawSensor.clear(); waterRawSensor.clear();
@ -372,10 +372,6 @@ void onHomieEvent(const HomieEvent &event)
configTime(0, 0, ntpServer.get()); configTime(0, 0, ntpServer.get());
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].postMQTTconnection();
}
{ {
getTopic(TEST_TOPIC, testopic) getTopic(TEST_TOPIC, testopic)
Homie.getMqttClient() Homie.getMqttClient()
@ -436,7 +432,7 @@ int determineNextPump()
log(LOG_LEVEL_DEBUG, String(String(i) + " No pump required: due to light"), LOG_DEBUG_CODE); log(LOG_LEVEL_DEBUG, String(String(i) + " No pump required: due to light"), LOG_DEBUG_CODE);
continue; continue;
} }
if (plant.getCurrentMoisture() == MISSING_SENSOR) if (equalish(plant.getCurrentMoisture(),MISSING_SENSOR))
{ {
plant.publishState("nosensor"); plant.publishState("nosensor");
log(LOG_LEVEL_ERROR, String(String(i) + " No pump possible: missing sensor"), LOG_MISSING_PUMP); log(LOG_LEVEL_ERROR, String(String(i) + " No pump possible: missing sensor"), LOG_MISSING_PUMP);
@ -599,8 +595,7 @@ void setup()
// Set default values // Set default values
//in seconds //in seconds
deepSleepTime.setDefaultValue(600).setValidator([](long candidate) deepSleepTime.setDefaultValue(600).setValidator([](long candidate) { return (candidate > 0) && (candidate < (60 * 60 * 2) /** 2h max sleep */); });
{ return (candidate > 0) && (candidate < (60 * 60 * 2) /** 2h max sleep */); });
deepSleepNightTime.setDefaultValue(600); deepSleepNightTime.setDefaultValue(600);
wateringDeepSleep.setDefaultValue(5); wateringDeepSleep.setDefaultValue(5);
ntpServer.setDefaultValue("pool.ntp.org"); ntpServer.setDefaultValue("pool.ntp.org");
@ -764,29 +759,16 @@ void plantcontrol()
Serial << "Plant" << lastPumpRunning << ": Water diff " << waterDiff << " mm" << endl; Serial << "Plant" << lastPumpRunning << ": Water diff " << waterDiff << " mm" << endl;
} }
readOneWireSensors(); if (mAliveWasRead)
for (int i = 0; i < MAX_PLANTS; i++)
{ {
long raw = mPlants[i].getCurrentMoisture(); for (int i = 0; i < MAX_PLANTS; i++)
long pct = 100 - map(raw, MOIST_SENSOR_MIN_ADC, MOIST_SENSOR_MAX_ADC, 0, 100);
if (raw == MISSING_SENSOR)
{ {
pct = 0; mPlants[i].postMQTTconnection();
} }
if (pct < 0)
{
pct = 0;
}
if (pct > 100)
{
pct = 100;
}
mPlants[i].setProperty("moist").send(String(pct));
mPlants[i].setProperty("moistraw").send(String(raw));
} }
readOneWireSensors();
Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << "%)" << endl; Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << "%)" << endl;
lastWaterValue = waterRawSensor.getAverage(); lastWaterValue = waterRawSensor.getAverage();