diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 8afcebc..9816301 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -64,10 +64,13 @@ #define OUTPUT_ENABLE_SENSOR GPIO_NUM_14 /**< GPIO 14 - Enable Sensors */ #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 */ +#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_SCL GPIO_NUM_16 /**< GPIO 16 - water sensor SCL */ +#endif #define BUTTON GPIO_NUM_0 /**< GPIO 0 - Fix button of NodeMCU */ #define CUSTOM1_PIN1 GPIO_NUM_34 /** direct gpio */ @@ -85,7 +88,11 @@ #define FLOWMETER_PULSES_PER_ML 2.2 #define FIRMWARE_FEATURE1 "Flow" #else + #ifdef ANALOG_WATER + #define FIRMWARE_FEATURE1 "Water" + #else #define FIRMWARE_FEATURE1 "" + #endif #endif #ifdef TIMED_LIGHT_PIN @@ -96,7 +103,7 @@ #define FIRMWARE_BASENAME "PlantControl" #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_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 ESP_STALE_TIMEOUT (MQTT_TIMEOUT+(700*1000)) +#define ESP_ADC_MAX 4095 #define MAX_PLANTS 7 #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_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_JSON_CONFIG_FILE_SIZE_CUSTOM 2500 diff --git a/esp32/include/HomieConfiguration.h b/esp32/include/HomieConfiguration.h index bcfd672..c26a218 100644 --- a/esp32/include/HomieConfiguration.h +++ b/esp32/include/HomieConfiguration.h @@ -75,9 +75,6 @@ HomieNode stayAlive("stay", "alive", "alive"); /**< Necessary for Mqtt Active C HomieSetting deepSleepTime("sleep", "time in seconds to sleep"); HomieSetting deepSleepNightTime("nightsleep", "time in seconds to sleep (0 uses same setting: deepsleep at night, too)"); HomieSetting pumpIneffectiveWarning("pumpConsecutiveWarn", "if the pump was triggered this amount directly after each cooldown, without resolving dryness, warn"); -HomieSetting waterLevelMax("tankmax", "distance (mm) at maximum water level"); -HomieSetting waterLevelMin("tankmin", "distance (mm) at minimum water level (pumps still covered)"); -HomieSetting waterLevelWarn("tankwarn", "warn (mm) if below this water level %"); HomieSetting waterLevelVol("tankVolume", "(ml) between minimum and maximum"); HomieSetting lipoSensorAddr("lipoDSAddr", "1wire address for lipo temperature sensor"); HomieSetting waterSensorAddr("tankDSAddr", "1wire address for water temperature sensor"); diff --git a/esp32/platformio.ini b/esp32/platformio.ini index af2722e..043b088 100644 --- a/esp32/platformio.ini +++ b/esp32/platformio.ini @@ -22,6 +22,7 @@ build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY -DPLANT6_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE -DTIMED_LIGHT_PIN=CUSTOM1_PIN5 -DFLOWMETER_PIN=CUSTOM1_PIN1 + -DANALOG_WATER=GPIO_NUM_34 board_build.partitions = defaultWithSmallerSpiffs.csv diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 817cf9c..e58c320 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -51,7 +51,10 @@ extern "C" bool verifyRollbackLater(){ * DEFINES ******************************************************************************/ #define AMOUNT_SENOR_QUERYS 8 +#ifdef ANALOG_WATER +#else #define MAX_TANK_DEPTH 5000 +#endif #define REBOOT_LOOP_DETECTION_ERROR 5 /****************************************************************************** @@ -354,68 +357,35 @@ void readPowerSwitchedSensors() Plant plant = mPlants[i]; 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; break; - } - case ANALOG_RESISTANCE_PROBE : { + case ANALOG_RESISTANCE_PROBE : Serial << "Plant " << i << " measurement: " << mPlants[i].getCurrentMoistureRaw() << " mV " << mPlants[i].getCurrentMoisturePCT() << "%" << endl; break; - } - case NONE : { - - } + default: + case NONE: + break; } } - Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL); - // Source: https://www.st.com/resource/en/datasheet/vl53l0x.pdf - tankSensor.setAddress(0x52); - tankSensor.setBus(&Wire); - delay(50); - Serial << "Distance sensor init" << endl; - long start = millis(); - bool distanceReady = false; - while ((start + WATERSENSOR_TIMEOUT) > millis()) + #ifdef ANALOG_WATER + Serial << "Analog water measurement " << WATERSENSOR_CYCLE << " cycles.." << endl; + Serial.flush(); + + /* Read analog Water sensor*/ + for(int i=0; i < WATERSENSOR_CYCLE; i++) { - 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++) - - { - if (!tankSensor.timeoutOccurred()) - { - 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 */ digitalWrite(OUTPUT_ENABLE_SENSOR, LOW); @@ -792,6 +762,10 @@ void safeSetup() 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"); if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS) { @@ -819,9 +793,6 @@ void safeSetup() deepSleepNightTime.setDefaultValue(600); 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 */ lipoSensorAddr.setDefaultValue(""); waterSensorAddr.setDefaultValue(""); @@ -1082,7 +1053,7 @@ void plantcontrol() 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); float chipTemp = battery.getTemperature(); @@ -1093,14 +1064,11 @@ void plantcontrol() /* Publish water values, if available */ 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())) { + /* 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())); } } @@ -1141,11 +1109,7 @@ bool isLowLight = (mSolarVoltage <= SOLAR_CHARGE_MAX_VOLTAGE); if (waterRawSensor.getCount() > 0) { //surface of water is still nearer the sensor than required to cover the pumps - hasWater = waterRawSensor.getAverage() < waterLevelMin.get(); - if (waterRawSensor.getAverage() > waterLevelMax.get()) { - log(LOG_LEVEL_ERROR, LOG_PUMP_FULLTANK_MESSAGE, LOG_PUMP_FULLTANK_CODE); - hasWater = true; - } + hasWater = waterRawSensor.getAverage() > WATER_LEVEL_MINIMUM; } // FIXME no water warning message