code cleanups
This commit is contained in:
7
esp32/src/MathUtil.cpp
Normal file
7
esp32/src/MathUtil.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
#include "MathUtils.h"
|
||||
#include <Arduino.h>
|
||||
bool equalish(double x, double y)
|
||||
{
|
||||
return (abs(x - y) < 0.5);
|
||||
}
|
||||
@@ -13,6 +13,12 @@
|
||||
#include "PlantCtrl.h"
|
||||
#include "ControllerConfiguration.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)
|
||||
{
|
||||
@@ -27,8 +33,8 @@ void Plant::init(void)
|
||||
{
|
||||
/* Initialize Home Settings validator */
|
||||
this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT);
|
||||
this->mSetting->pSensorDry->setValidator([](long candidate) {
|
||||
return (((candidate >= 0) && (candidate <= 4095)) || candidate == DEACTIVATED_PLANT);
|
||||
this->mSetting->pSensorDry->setValidator([](double candidate) {
|
||||
return (((candidate >= 0.0) && (candidate <= 100.0)) || equalish(candidate,DEACTIVATED_PLANT));
|
||||
});
|
||||
this->mSetting->pPumpAllowedHourRangeStart->setDefaultValue(8); // start at 8:00
|
||||
this->mSetting->pPumpAllowedHourRangeStart->setValidator([](long candidate) {
|
||||
@@ -63,6 +69,7 @@ void Plant::clearMoisture(void){
|
||||
void Plant::addSenseValue(void)
|
||||
{
|
||||
int raw = analogRead(this->mPinSensor);
|
||||
Serial << "plant bla " << raw << endl;
|
||||
if(raw < MOIST_SENSOR_MAX_ADC && raw > MOIST_SENSOR_MIN_ADC){
|
||||
this->moistureRaw.add(raw);
|
||||
}
|
||||
@@ -73,6 +80,25 @@ void Plant::postMQTTconnection(void)
|
||||
const String OFF = String("OFF");
|
||||
this->mConnected = true;
|
||||
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)
|
||||
|
||||
@@ -260,7 +260,7 @@ void readOneWireSensors()
|
||||
void readPowerSwitchedSensors()
|
||||
{
|
||||
digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH);
|
||||
delay(10);
|
||||
delay(500);
|
||||
for (int i = 0; i < MAX_PLANTS; i++)
|
||||
{
|
||||
mPlants[i].clearMoisture();
|
||||
@@ -272,7 +272,7 @@ void readPowerSwitchedSensors()
|
||||
{
|
||||
mPlants[i].addSenseValue();
|
||||
}
|
||||
delay(2);
|
||||
delay(20);
|
||||
}
|
||||
|
||||
waterRawSensor.clear();
|
||||
@@ -372,10 +372,6 @@ void onHomieEvent(const HomieEvent &event)
|
||||
|
||||
configTime(0, 0, ntpServer.get());
|
||||
|
||||
for (int i = 0; i < MAX_PLANTS; i++)
|
||||
{
|
||||
mPlants[i].postMQTTconnection();
|
||||
}
|
||||
{
|
||||
getTopic(TEST_TOPIC, testopic)
|
||||
Homie.getMqttClient()
|
||||
@@ -436,7 +432,7 @@ int determineNextPump()
|
||||
log(LOG_LEVEL_DEBUG, String(String(i) + " No pump required: due to light"), LOG_DEBUG_CODE);
|
||||
continue;
|
||||
}
|
||||
if (plant.getCurrentMoisture() == MISSING_SENSOR)
|
||||
if (equalish(plant.getCurrentMoisture(),MISSING_SENSOR))
|
||||
{
|
||||
plant.publishState("nosensor");
|
||||
log(LOG_LEVEL_ERROR, String(String(i) + " No pump possible: missing sensor"), LOG_MISSING_PUMP);
|
||||
@@ -599,8 +595,7 @@ void setup()
|
||||
// Set default values
|
||||
|
||||
//in seconds
|
||||
deepSleepTime.setDefaultValue(600).setValidator([](long candidate)
|
||||
{ return (candidate > 0) && (candidate < (60 * 60 * 2) /** 2h max sleep */); });
|
||||
deepSleepTime.setDefaultValue(600).setValidator([](long candidate) { return (candidate > 0) && (candidate < (60 * 60 * 2) /** 2h max sleep */); });
|
||||
deepSleepNightTime.setDefaultValue(600);
|
||||
wateringDeepSleep.setDefaultValue(5);
|
||||
ntpServer.setDefaultValue("pool.ntp.org");
|
||||
@@ -764,29 +759,16 @@ void plantcontrol()
|
||||
Serial << "Plant" << lastPumpRunning << ": Water diff " << waterDiff << " mm" << endl;
|
||||
}
|
||||
|
||||
readOneWireSensors();
|
||||
|
||||
for (int i = 0; i < MAX_PLANTS; i++)
|
||||
if (mAliveWasRead)
|
||||
{
|
||||
long raw = mPlants[i].getCurrentMoisture();
|
||||
long pct = 100 - map(raw, MOIST_SENSOR_MIN_ADC, MOIST_SENSOR_MAX_ADC, 0, 100);
|
||||
if (raw == MISSING_SENSOR)
|
||||
for (int i = 0; i < MAX_PLANTS; i++)
|
||||
{
|
||||
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;
|
||||
lastWaterValue = waterRawSensor.getAverage();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user