From 093b7c7fc38359e87b7c539c96200e643ba563e9 Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 16 Feb 2021 22:52:47 +0100 Subject: [PATCH] Corrected pin configuration to 0.7final PCB --- esp32/include/ControllerConfiguration.h | 31 ++++--- esp32/src/main.cpp | 111 ++++-------------------- 2 files changed, 32 insertions(+), 110 deletions(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 0917159..646ab75 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -46,7 +46,7 @@ /** \addtogroup Configuration * @{ */ -#define FIRMWARE_VERSION "1.0.10" +#define FIRMWARE_VERSION "1.1.0" #define ADC_TO_VOLT(adc) ((adc) * 3.3 ) / 4095) #define ADC_TO_VOLT_WITH_MULTI(adc, multi) (((adc)*3.3 * (multi)) / 4095) @@ -58,23 +58,21 @@ #define BATTSENSOR_INDEX_BATTERY 1 #define MS_TO_S 1000 -#define SENSOR_LIPO 34 /**< GPIO 34 (ADC1) */ -#define SENSOR_SOLAR 35 /**< GPIO 35 (ADC1) */ -#define SENSOR_PLANT0 12 /**< GPIO 32 (ADC1) */ -#define SENSOR_PLANT1 14/**< GPIO 33 (ADC1) */ -#define SENSOR_PLANT2 27 /**< GPIO 25 (ADC2) */ +#define SENSOR_PLANT0 32 /**< GPIO 32 (ADC1) */ +#define SENSOR_PLANT1 33 /**< GPIO 33 (ADC1) */ +#define SENSOR_PLANT2 25 /**< GPIO 25 (ADC2) */ #define SENSOR_PLANT3 26 /**< GPIO 26 (ADC2) */ -#define SENSOR_PLANT4 25 /**< GPIO 27 (ADC2) */ -#define SENSOR_PLANT5 14 /**< GPIO 14 (ADC2) */ -#define SENSOR_PLANT6 12 /**< GPIO 12 (ADC2) */ +#define SENSOR_PLANT4 27 /**< GPIO 27 (ADC2) */ +#define SENSOR_PLANT5 39 /**< SENSOR_VIN */ +#define SENSOR_PLANT6 36 /**< SENSOR_VP */ -#define OUTPUT_PUMP0 15 /**< GPIO 23 */ -#define OUTPUT_PUMP1 5 /**< GPIO 22 */ -#define OUTPUT_PUMP2 18 /**< GPIO 21 */ +#define OUTPUT_PUMP0 17 /**< GPIO 17 */ +#define OUTPUT_PUMP1 5 /**< GPIO 5 */ +#define OUTPUT_PUMP2 18 /**< GPIO 18 */ #define OUTPUT_PUMP3 19 /**< GPIO 19 */ -#define OUTPUT_PUMP4 21 /**< GPIO 18 */ -#define OUTPUT_PUMP5 22 /**< GPIO 5 */ -#define OUTPUT_PUMP6 23 /**< GPIO 15 */ +#define OUTPUT_PUMP4 21 /**< GPIO 21 */ +#define OUTPUT_PUMP5 22 /**< GPIO 22 */ +#define OUTPUT_PUMP6 23 /**< GPIO 23 */ #define OUTPUT_SENSOR 16 /**< GPIO 16 - Enable Sensors */ #define OUTPUT_PUMP 13 /**< GPIO 13 - Enable Pumps */ @@ -89,6 +87,8 @@ #define MINIMUM_SOLAR_VOLT 4.0f /**< Minimum voltage of the sun, to detect daylight */ #define SOLAR_CHARGE_MIN_VOLTAGE 7 /**< Sun is rising (morning detected) */ #define SOLAR_CHARGE_MAX_VOLTAGE 9 /**< Sun is shining (noon) */ +#define VOLT_MAX_BATT 4.2f +#define VOLT_MAX_SOLAR 20.0f #define MAX_CONFIG_SETTING_ITEMS 50 /**< Parameter, that can be configured in Homie */ @@ -104,7 +104,6 @@ #define TEMPERATUR_TIMEOUT 3000 /**< 3 Seconds timeout for the temperatur sensors */ #define TEMP_SENSOR_MEASURE_SERIES 5 -#define VOLT_SENSOR_MEASURE_SERIES 5 /* @} */ #endif \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 50e726e..84c3465 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -87,11 +87,9 @@ int readCounter = 0; bool mConfigured = false; long nextBlink = 0; /**< Time needed in main loop to support expected blink code */ -RunningMedian lipoRawSensor = RunningMedian(VOLT_SENSOR_MEASURE_SERIES); -RunningMedian solarRawSensor = RunningMedian(VOLT_SENSOR_MEASURE_SERIES); RunningMedian waterRawSensor = RunningMedian(5); -RunningMedian lipoTempSensor = RunningMedian(TEMP_SENSOR_MEASURE_SERIES); -RunningMedian waterTempSensor = RunningMedian(TEMP_SENSOR_MEASURE_SERIES); +float mTempLipo = 0.0f; +float mTempWater = 0.0f; float mBatteryVoltage = 0.0f; float mSolarVoltage = 0.0f; float mChipTemp = 0.0f; @@ -181,8 +179,6 @@ void readSystemSensors() int timeoutTemp = millis() + TEMPERATUR_TIMEOUT; int sensorCount = 0; - rtcLastLipoTemp = lipoTempSensor.getAverage(); - rtcLastWaterTemp = waterTempSensor.getAverage(); /* Required to read the temperature at least once */ while (sensorCount == 0 && millis() < timeoutTemp) @@ -211,14 +207,10 @@ void readSystemSensors() mSolarVoltage = battery.getVoltage(BATTSENSOR_INDEX_SOLAR) * SOLAR_VOLT_FACTOR; mBatteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY); mChipTemp = battery.getTemperature(); - for (int i = 0; i < VOLT_SENSOR_MEASURE_SERIES; i++) - { - lipoRawSensor.add(analogRead(SENSOR_LIPO)); - solarRawSensor.add(analogRead(SENSOR_SOLAR)); - } - Serial << "Lipo " << lipoRawSensor.getAverage() << " -> " << mBatteryVoltage << endl; rtcLastBatteryVoltage = mBatteryVoltage; rtcLastSolarVoltage = mSolarVoltage; + rtcLastLipoTemp = mTempLipo; + rtcLastWaterTemp = mTempWater; } long getCurrentTime() @@ -324,87 +316,24 @@ void mode2MQTT() Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << "%)" << endl; lastWaterValue = waterRawSensor.getAverage(); - sensorLipo.setProperty("percent").send(String(100 * lipoRawSensor.getAverage() / 4095)); + sensorLipo.setProperty("percent").send(String(100 * mBatteryVoltage / VOLT_MAX_BATT)); sensorLipo.setProperty("volt").send(String(mBatteryVoltage)); - sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage()) / 4095)); + sensorSolar.setProperty("percent").send(String(100 * mSolarVoltage / VOLT_MAX_SOLAR)); sensorSolar.setProperty("volt").send(String(mSolarVoltage)); startupReason.setProperty("startupReason").send(String(wakeUpReason)); rtcLipoTempIndex = lipoSensorIndex.get(); rtcWaterTempIndex = waterSensorIndex.get(); - float lipoTempCurrent = lipoTempSensor.getMedian(); - float t2 = NAN; - if (! isnan(lipoTempCurrent)) - { - sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(lipoTempCurrent)); - Serial << "Lipo Temperatur " << lipoTempCurrent << " °C " << endl; + + sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(mTempLipo)); + Serial << "Lipo Temperatur " << mTempLipo << " °C " << endl; - t2 = waterTempSensor.getMedian(); - if (! isnan(t2)) - { - sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(t2)); - Serial << "Water Temperatur " << lipoTempCurrent << " °C " << endl; - } - //give mqtt time, use via publish callback instead? - delay(100); - } else { - int j=0; - /* Activate the Sensors and measure the temperature again */ - /* activate all sensors */ - pinMode(OUTPUT_SENSOR, OUTPUT); - digitalWrite(OUTPUT_SENSOR, HIGH); + sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(mTempWater)); + Serial << "Water Temperatur " << mTempWater << " °C " << endl; - delay(100); - sensors.begin(); - - for(j=0; j < TEMP_SENSOR_MEASURE_SERIES && sensors.getDeviceCount() == 0; j++) { - delay(100); - sensors.begin(); - Serial << "Reset 1-Wire Bus" << endl; - // Setup Battery sensor DS2438 - battery.begin(); - } - - for(j=0; j < TEMP_SENSOR_MEASURE_SERIES && isnan(lipoTempCurrent); j++) { - delay(200); - readTemp(); - lipoTempCurrent = lipoTempSensor.getMedian(); - t2 = waterTempSensor.getMedian(); - Serial << "Temperatur Lipo:" << lipoTempCurrent << " °C Water : " << t2 << " °C" << endl; - } - - if (! isnan(lipoTempCurrent)) - { - sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(lipoTempCurrent)); - Serial << "Lipo Temperatur " << lipoTempCurrent << " °C " << endl; - - t2 = waterTempSensor.getMedian(); - if (! isnan(t2)) - { - sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(t2)); - Serial << "Water Temperatur " << lipoTempCurrent << " °C " << endl; - } - } - - if (! isnan(mChipTemp)) { - sensorTemp.setProperty(TEMPERATUR_SENSOR_CHIP).send(String(mChipTemp)); - Serial << "Chip Temperatur " << mChipTemp << " °C " << endl; - } - - /* deactivate the sensors */ - digitalWrite(OUTPUT_SENSOR, LOW); - } - - if (! isnan(lipoTempCurrent) && ! isnan(t2)) { - bool lipoTempWarning = (lipoTempCurrent != LIPO_MAX_TEMPERATUR) && abs(lipoTempCurrent - t2) > LIPO_MAX_TEMPERATUR_DIFF; - if (lipoTempWarning) - { - Serial.println("Lipo temp incorrect, panic mode deepsleep TODO"); - //espDeepSleepFor(PANIK_MODE_DEEPSLEEP); - //return; - } - } + sensorTemp.setProperty(TEMPERATUR_SENSOR_CHIP).send(String(mChipTemp)); + Serial << "Chip Temperatur " << mChipTemp << " °C " << endl; for (int i = 0; i < MAX_PLANTS; i++) { @@ -534,9 +463,7 @@ int readTemp() { { if (rtcLipoTempIndex != -1) { - float temp1Raw = sensors.getTempCByIndex(rtcLipoTempIndex); - //Serial << "lipoTempCurrent: " << temp1Raw << endl; - lipoTempSensor.add(temp1Raw); + mTempLipo = sensors.getTempCByIndex(rtcLipoTempIndex); } else { @@ -552,9 +479,8 @@ int readTemp() { if (sensorCount > 1 && rtcWaterTempIndex != -1) { - float temp2Raw = sensors.getTempCByIndex(rtcWaterTempIndex); + mTempWater = sensors.getTempCByIndex(rtcWaterTempIndex); //Serial << "waterTempCurrent: " << temp2Raw << endl; - waterTempSensor.add(temp2Raw); } readAgain--; @@ -623,12 +549,12 @@ bool readSensors() // check if chip needs to start into full operational mode leaveMode1 |= readTemp(); - if (abs(lipoTempSensor.getAverage() - rtcLastLipoTemp) > TEMPERATURE_DELTA_TRIGGER_IN_C) + if (abs(mTempWater - rtcLastLipoTemp) > TEMPERATURE_DELTA_TRIGGER_IN_C) { leaveMode1 = true; wakeUpReason = WAKEUP_REASON_TEMP1_CHANGE; } - if (abs(waterTempSensor.getAverage() - rtcLastWaterTemp) > TEMPERATURE_DELTA_TRIGGER_IN_C) + if (abs(mTempWater - rtcLastWaterTemp) > TEMPERATURE_DELTA_TRIGGER_IN_C) { wakeUpReason = WAKEUP_REASON_TEMP2_CHANGE; leaveMode1 = true; @@ -928,9 +854,6 @@ void setup() mPlants[i].init(); } - /* Intialize inputs and outputs */ - pinMode(SENSOR_LIPO, ANALOG); - pinMode(SENSOR_SOLAR, ANALOG); /* read button */ pinMode(BUTTON, INPUT);