analog water measurement added

This commit is contained in:
Ollo 2024-02-18 15:07:06 +01:00
parent 9167da5a82
commit 4f7e57988b
4 changed files with 45 additions and 73 deletions

View File

@ -64,10 +64,13 @@
#define OUTPUT_ENABLE_SENSOR GPIO_NUM_14 /**< GPIO 14 - Enable Sensors */ #define OUTPUT_ENABLE_SENSOR GPIO_NUM_14 /**< GPIO 14 - Enable Sensors */
#define OUTPUT_ENABLE_PUMP GPIO_NUM_13 /**< GPIO 13 - Enable Pumps */ #define OUTPUT_ENABLE_PUMP GPIO_NUM_13 /**< GPIO 13 - Enable Pumps */
#define SENSOR_ONEWIRE GPIO_NUM_4 /**< GPIO 12 - Temperatur sensor, Battery and other cool onewire stuff */ #define SENSOR_ONEWIRE GPIO_NUM_4 /**< GPIO 12 - Temperatur sensor, Battery and other cool onewire stuff */
#ifdef ANALOG_WATER
#define SENSOR_TANK_ANALOG ANALOG_WATER /**< GPIO 34 - analog water sensor (GPIO_NUM_34) */
#else
#define SENSOR_TANK_SDA GPIO_NUM_17 /**< GPIO 17 - water sensor SDA */ #define SENSOR_TANK_SDA GPIO_NUM_17 /**< GPIO 17 - water sensor SDA */
#define SENSOR_TANK_SCL GPIO_NUM_16 /**< GPIO 16 - water sensor SCL */ #define SENSOR_TANK_SCL GPIO_NUM_16 /**< GPIO 16 - water sensor SCL */
#endif
#define BUTTON GPIO_NUM_0 /**< GPIO 0 - Fix button of NodeMCU */ #define BUTTON GPIO_NUM_0 /**< GPIO 0 - Fix button of NodeMCU */
#define CUSTOM1_PIN1 GPIO_NUM_34 /** direct gpio */ #define CUSTOM1_PIN1 GPIO_NUM_34 /** direct gpio */
@ -85,7 +88,11 @@
#define FLOWMETER_PULSES_PER_ML 2.2 #define FLOWMETER_PULSES_PER_ML 2.2
#define FIRMWARE_FEATURE1 "Flow" #define FIRMWARE_FEATURE1 "Flow"
#else #else
#ifdef ANALOG_WATER
#define FIRMWARE_FEATURE1 "Water"
#else
#define FIRMWARE_FEATURE1 "" #define FIRMWARE_FEATURE1 ""
#endif
#endif #endif
#ifdef TIMED_LIGHT_PIN #ifdef TIMED_LIGHT_PIN
@ -96,7 +103,7 @@
#define FIRMWARE_BASENAME "PlantControl" #define FIRMWARE_BASENAME "PlantControl"
#define FIRMWARE_NAME FIRMWARE_BASENAME FIRMWARE_FEATURE1 FIRMWARE_FEATURE2 #define FIRMWARE_NAME FIRMWARE_BASENAME FIRMWARE_FEATURE1 FIRMWARE_FEATURE2
#define FIRMWARE_VERSION "2.71 HW0.10b" #define FIRMWARE_VERSION "3.00 HW0.10b"
#define MOIST_SENSOR_MAX_FRQ 5200 // 60kHz (500Hz margin) #define MOIST_SENSOR_MAX_FRQ 5200 // 60kHz (500Hz margin)
#define MOIST_SENSOR_MIN_FRQ 500 // 0.5kHz (500Hz margin) #define MOIST_SENSOR_MIN_FRQ 500 // 0.5kHz (500Hz margin)
@ -110,6 +117,7 @@
#define MQTT_TIMEOUT (1000 * 60) /**< After 10 seconds, MQTT is expected to be connected */ #define MQTT_TIMEOUT (1000 * 60) /**< After 10 seconds, MQTT is expected to be connected */
#define ESP_STALE_TIMEOUT (MQTT_TIMEOUT+(700*1000)) #define ESP_STALE_TIMEOUT (MQTT_TIMEOUT+(700*1000))
#define ESP_ADC_MAX 4095
#define MAX_PLANTS 7 #define MAX_PLANTS 7
#define SOLAR_CHARGE_MIN_VOLTAGE 7 /**< Sun is rising (morning detected) */ #define SOLAR_CHARGE_MIN_VOLTAGE 7 /**< Sun is rising (morning detected) */
@ -120,6 +128,8 @@
#define LOWVOLT_SLEEP_FACTOR 3 /**< Factor for nightsleep delay, if the battery drops below minimum (@see VOLT_MIN_BATT) */ #define LOWVOLT_SLEEP_FACTOR 3 /**< Factor for nightsleep delay, if the battery drops below minimum (@see VOLT_MIN_BATT) */
#define LOWVOLT_SLEEP_MINIMUM 1800 /**< At low voltage sleep at least for 30 minutes */ #define LOWVOLT_SLEEP_MINIMUM 1800 /**< At low voltage sleep at least for 30 minutes */
#define WATER_LEVEL_MINIMUM 500 /**< Minimum Analog value (1023 is the maximum)*/
#define MAX_CONFIG_SETTING_ITEMS 100 /**< Parameter, that can be configured in Homie */ #define MAX_CONFIG_SETTING_ITEMS 100 /**< Parameter, that can be configured in Homie */
#define MAX_JSON_CONFIG_FILE_SIZE_CUSTOM 2500 #define MAX_JSON_CONFIG_FILE_SIZE_CUSTOM 2500

View File

@ -75,9 +75,6 @@ HomieNode stayAlive("stay", "alive", "alive"); /**< Necessary for Mqtt Active C
HomieSetting<long> deepSleepTime("sleep", "time in seconds to sleep"); HomieSetting<long> deepSleepTime("sleep", "time in seconds to sleep");
HomieSetting<long> deepSleepNightTime("nightsleep", "time in seconds to sleep (0 uses same setting: deepsleep at night, too)"); HomieSetting<long> deepSleepNightTime("nightsleep", "time in seconds to sleep (0 uses same setting: deepsleep at night, too)");
HomieSetting<long> pumpIneffectiveWarning("pumpConsecutiveWarn", "if the pump was triggered this amount directly after each cooldown, without resolving dryness, warn"); HomieSetting<long> pumpIneffectiveWarning("pumpConsecutiveWarn", "if the pump was triggered this amount directly after each cooldown, without resolving dryness, warn");
HomieSetting<long> waterLevelMax("tankmax", "distance (mm) at maximum water level");
HomieSetting<long> waterLevelMin("tankmin", "distance (mm) at minimum water level (pumps still covered)");
HomieSetting<long> waterLevelWarn("tankwarn", "warn (mm) if below this water level %");
HomieSetting<long> waterLevelVol("tankVolume", "(ml) between minimum and maximum"); HomieSetting<long> waterLevelVol("tankVolume", "(ml) between minimum and maximum");
HomieSetting<const char *> lipoSensorAddr("lipoDSAddr", "1wire address for lipo temperature sensor"); HomieSetting<const char *> lipoSensorAddr("lipoDSAddr", "1wire address for lipo temperature sensor");
HomieSetting<const char *> waterSensorAddr("tankDSAddr", "1wire address for water temperature sensor"); HomieSetting<const char *> waterSensorAddr("tankDSAddr", "1wire address for water temperature sensor");

View File

@ -22,6 +22,7 @@ build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
-DPLANT6_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE -DPLANT6_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE
-DTIMED_LIGHT_PIN=CUSTOM1_PIN5 -DTIMED_LIGHT_PIN=CUSTOM1_PIN5
-DFLOWMETER_PIN=CUSTOM1_PIN1 -DFLOWMETER_PIN=CUSTOM1_PIN1
-DANALOG_WATER=GPIO_NUM_34
board_build.partitions = defaultWithSmallerSpiffs.csv board_build.partitions = defaultWithSmallerSpiffs.csv

View File

@ -51,7 +51,10 @@ extern "C" bool verifyRollbackLater(){
* DEFINES * DEFINES
******************************************************************************/ ******************************************************************************/
#define AMOUNT_SENOR_QUERYS 8 #define AMOUNT_SENOR_QUERYS 8
#ifdef ANALOG_WATER
#else
#define MAX_TANK_DEPTH 5000 #define MAX_TANK_DEPTH 5000
#endif
#define REBOOT_LOOP_DETECTION_ERROR 5 #define REBOOT_LOOP_DETECTION_ERROR 5
/****************************************************************************** /******************************************************************************
@ -354,68 +357,35 @@ void readPowerSwitchedSensors()
Plant plant = mPlants[i]; Plant plant = mPlants[i];
switch (plant.getSensorMode()) switch (plant.getSensorMode())
{ {
case FREQUENCY_MOD_RESISTANCE_PROBE: { case FREQUENCY_MOD_RESISTANCE_PROBE:
Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentMoistureRaw() << " hz " << mPlants[i].getCurrentMoisturePCT() << "%" << endl; Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentMoistureRaw() << " hz " << mPlants[i].getCurrentMoisturePCT() << "%" << endl;
break; break;
} case 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 " << mPlants[i].getCurrentMoisturePCT() << "%" << endl;
break; break;
} default:
case NONE : { case NONE:
break;
}
} }
} }
Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL); #ifdef ANALOG_WATER
// Source: https://www.st.com/resource/en/datasheet/vl53l0x.pdf Serial << "Analog water measurement " << WATERSENSOR_CYCLE << " cycles.." << endl;
tankSensor.setAddress(0x52); Serial.flush();
tankSensor.setBus(&Wire);
delay(50);
Serial << "Distance sensor init" << endl;
long start = millis();
bool distanceReady = false;
while ((start + WATERSENSOR_TIMEOUT) > millis())
{
if (tankSensor.init())
{
distanceReady = true;
break;
}
else
{
delay(200);
}
}
if (distanceReady)
{
waterRawSensor.clear();
tankSensor.setSignalRateLimit(0.1);
// increase laser pulse periods (defaults are 14 and 10 PCLKs)
tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
tankSensor.setMeasurementTimingBudget(200000);
Serial << "Distance sensor measuring" << endl;
for (int readCnt = 0; readCnt < WATERSENSOR_CYCLE; readCnt++)
{ /* Read analog Water sensor*/
if (!tankSensor.timeoutOccurred()) for(int i=0; i < WATERSENSOR_CYCLE; i++)
{
uint16_t distance = tankSensor.readRangeSingleMillimeters();
if (distance < MAX_TANK_DEPTH)
{
waterRawSensor.add(distance);
}
}
delay(50);
}
Serial << "Distance sensor " << waterRawSensor.getMedian() << " mm" << endl;
}
else
{ {
log(LOG_LEVEL_WARN, LOG_TANKSENSOR_FAIL_DETECT, LOG_TANKSENSOR_FAIL_DETECT_CODE); int value = analogRead(SENSOR_TANK_ANALOG);
Serial << "Round " << i << " measurement: " << value << " mV " << endl;
waterRawSensor.add(value);
delay(5);
} }
Serial << "Analog water measurement done" << endl;
#else
Serial << "Analog water measurement deactivated" << endl;
#endif
Serial.flush();
/* deactivate the sensors */ /* deactivate the sensors */
digitalWrite(OUTPUT_ENABLE_SENSOR, LOW); digitalWrite(OUTPUT_ENABLE_SENSOR, LOW);
@ -792,6 +762,10 @@ void safeSetup()
pinMode(OUTPUT_ENABLE_SENSOR, OUTPUT); pinMode(OUTPUT_ENABLE_SENSOR, OUTPUT);
#ifdef ANALOG_WATER
pinMode(SENSOR_TANK_ANALOG, INPUT);
#endif
static_assert(HomieInternals::MAX_CONFIG_SETTING_SIZE >= MAX_CONFIG_SETTING_ITEMS, "Limits.hpp not adjusted MAX_CONFIG_SETTING_ITEMS"); static_assert(HomieInternals::MAX_CONFIG_SETTING_SIZE >= MAX_CONFIG_SETTING_ITEMS, "Limits.hpp not adjusted MAX_CONFIG_SETTING_ITEMS");
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS) if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS)
{ {
@ -819,9 +793,6 @@ void safeSetup()
deepSleepNightTime.setDefaultValue(600); deepSleepNightTime.setDefaultValue(600);
ntpServer.setDefaultValue("pool.ntp.org"); ntpServer.setDefaultValue("pool.ntp.org");
/* waterLevelMax 1000 */ /* 100cm in mm */
waterLevelMin.setDefaultValue(50); /* 5cm in mm */
waterLevelWarn.setDefaultValue(500); /* 50cm in mm */
waterLevelVol.setDefaultValue(5000); /* 5l in ml */ waterLevelVol.setDefaultValue(5000); /* 5l in ml */
lipoSensorAddr.setDefaultValue(""); lipoSensorAddr.setDefaultValue("");
waterSensorAddr.setDefaultValue(""); waterSensorAddr.setDefaultValue("");
@ -1082,7 +1053,7 @@ void plantcontrol()
readOneWireSensors(); readOneWireSensors();
Serial << "W : " << waterRawSensor.getAverage() << " mm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << " mm left)" << endl; Serial << "W : " << waterRawSensor.getAverage() << " mm " << endl;
mBatteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY); mBatteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY);
float chipTemp = battery.getTemperature(); float chipTemp = battery.getTemperature();
@ -1093,14 +1064,11 @@ void plantcontrol()
/* Publish water values, if available */ /* Publish water values, if available */
if (waterRawSensor.getCount() > 0) if (waterRawSensor.getCount() > 0)
{ {
float remaining = (waterLevelMax.get() - waterRawSensor.getAverage());
if (!isnan(remaining))
{
/* measuring the distance from top -> smaller value means more water: */
sensorWater.setProperty("remaining").send(String(100.0 - (remaining/100)));
}
if (!isnan(waterRawSensor.getAverage())) if (!isnan(waterRawSensor.getAverage()))
{ {
/* measuring the distance from top -> smaller value means more water: */
sensorWater.setProperty("remaining").send(String(100.0 * (waterRawSensor.getAverage()/ESP_ADC_MAX)));
sensorWater.setProperty("distance").send(String(waterRawSensor.getAverage())); sensorWater.setProperty("distance").send(String(waterRawSensor.getAverage()));
} }
} }
@ -1141,11 +1109,7 @@ bool isLowLight = (mSolarVoltage <= SOLAR_CHARGE_MAX_VOLTAGE);
if (waterRawSensor.getCount() > 0) if (waterRawSensor.getCount() > 0)
{ {
//surface of water is still nearer the sensor than required to cover the pumps //surface of water is still nearer the sensor than required to cover the pumps
hasWater = waterRawSensor.getAverage() < waterLevelMin.get(); hasWater = waterRawSensor.getAverage() > WATER_LEVEL_MINIMUM;
if (waterRawSensor.getAverage() > waterLevelMax.get()) {
log(LOG_LEVEL_ERROR, LOG_PUMP_FULLTANK_MESSAGE, LOG_PUMP_FULLTANK_CODE);
hasWater = true;
}
} }
// FIXME no water warning message // FIXME no water warning message