Activate Sensors and Pumps if Lipo is charged

This commit is contained in:
Ollo 2024-05-11 15:22:49 +02:00
parent cd1e88e6e2
commit aa7cb31302
2 changed files with 51 additions and 40 deletions

View File

@ -127,7 +127,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_VERSIONNMUMBER "3.011" #define FIRMWARE_VERSIONNMUMBER "3.012"
#ifdef HWREVISION07 #ifdef HWREVISION07
#define FIRMWARE_VERSION FIRMWARE_VERSIONNMUMBER " HW0.7" #define FIRMWARE_VERSION FIRMWARE_VERSIONNMUMBER " HW0.7"
@ -158,6 +158,7 @@
#define SOLAR_CHARGE_MAX_VOLTAGE 9 /**< Sun is shining (noon) */ #define SOLAR_CHARGE_MAX_VOLTAGE 9 /**< Sun is shining (noon) */
#define SOLAR_MAX_VOLTAGE_POSSIBLE 100 /**< higher values are treated as not connected sensor */ #define SOLAR_MAX_VOLTAGE_POSSIBLE 100 /**< higher values are treated as not connected sensor */
#define VOLT_MAX_BATT 4.2f #define VOLT_MAX_BATT 4.2f
#define VOLT_SENSORS_BATT 3.6f /**< Minimum battery voltage for sensor (and pump) usage */
#define VOLT_MIN_BATT 3.2f /**< Minimum battery voltage for normal operation */ #define VOLT_MIN_BATT 3.2f /**< Minimum battery voltage for normal operation */
#define LOWVOLT_SLEEP_FACTOR 6 /**< Factor for nightsleep delay, if the battery drops below minimum (@see VOLT_MIN_BATT) */ #define LOWVOLT_SLEEP_FACTOR 6 /**< Factor for nightsleep delay, if the battery drops below minimum (@see VOLT_MIN_BATT) */
#define LOWVOLT_SLEEP_MINIMUM 7200 /**< At low voltage sleep at least for 120 minutes (two hours) */ #define LOWVOLT_SLEEP_MINIMUM 7200 /**< At low voltage sleep at least for 120 minutes (two hours) */

View File

@ -80,9 +80,9 @@ RTC_DATA_ATTR long consecutiveWateringPlant[MAX_PLANTS] = {0};
/****************************************************************************** /******************************************************************************
* LOCAL VARIABLES * LOCAL VARIABLES
******************************************************************************/ ******************************************************************************/
bool volatile mDownloadMode = false; /**< Controller must not sleep */ bool volatile vmDownloadMode = false; /**< Controller must not sleep */
bool volatile mSensorsRead = false; /**< Sensors are read without Wifi or MQTT */ bool volatile vmSensorsRead = false; /**< Sensors are read without Wifi or MQTT */
int volatile pumpToRun = -1; /** pump to run at the end of the cycle */ int volatile vmPumpToRun = -1; /** pump to run at the end of the cycle */
int volatile selfTestPumpRun = -1; /** pump to run at the end of the cycle */ int volatile selfTestPumpRun = -1; /** pump to run at the end of the cycle */
bool mConfigured = false; bool mConfigured = false;
@ -177,7 +177,7 @@ void espDeepSleep(bool afterPump = false)
{ {
/* download mode or normal operation should restore device status*/ /* download mode or normal operation should restore device status*/
finsihedCycleSucessfully(); finsihedCycleSucessfully();
if (mDownloadMode) if (vmDownloadMode)
{ {
log(LOG_LEVEL_DEBUG, "abort deepsleep, DownloadMode active", LOG_DEBUG_CODE); log(LOG_LEVEL_DEBUG, "abort deepsleep, DownloadMode active", LOG_DEBUG_CODE);
return; return;
@ -403,12 +403,12 @@ void onHomieEvent(const HomieEvent &event)
case HomieEventType::SENDING_STATISTICS: case HomieEventType::SENDING_STATISTICS:
break; break;
case HomieEventType::MQTT_READY: case HomieEventType::MQTT_READY:
if (mSensorsRead) if (vmSensorsRead)
{ {
Serial.printf("Timeout occured... too late!\r\n"); Serial.printf("Timeout occured... too late!\r\n");
return; return;
} }
mSensorsRead = true; // MQTT is working, deactivate timeout logic vmSensorsRead = true; // MQTT is working, deactivate timeout logic
configTime(UTC_OFFSET_DE, UTF_OFFSET_DE_DST, ntpServer.get()); configTime(UTC_OFFSET_DE, UTF_OFFSET_DE_DST, ntpServer.get());
startMQTTRoundtripTest(); startMQTTRoundtripTest();
@ -419,7 +419,7 @@ void onHomieEvent(const HomieEvent &event)
mPlants[i].deactivatePump(); mPlants[i].deactivatePump();
} }
otaRunning = true; otaRunning = true;
mDownloadMode = true; vmDownloadMode = true;
break; break;
case HomieEventType::OTA_SUCCESSFUL: case HomieEventType::OTA_SUCCESSFUL:
digitalWrite(OUTPUT_ENABLE_SENSOR, LOW); digitalWrite(OUTPUT_ENABLE_SENSOR, LOW);
@ -511,7 +511,7 @@ int determineNextPump(bool isLowLight)
} }
else else
{ {
if (mDownloadMode) if (vmDownloadMode)
{ {
plant.publishState(PLANTSTATE_NUM_ACTIVE_SUPESSED, PLANTSTATE_STR_ACTIVE_SUPESSED); plant.publishState(PLANTSTATE_NUM_ACTIVE_SUPESSED, PLANTSTATE_STR_ACTIVE_SUPESSED);
} }
@ -571,15 +571,15 @@ bool aliveHandler(const HomieRange &range, const String &value)
if (value.equals("ON") || value.equals("On") || value.equals("1")) if (value.equals("ON") || value.equals("On") || value.equals("1"))
{ {
mDownloadMode = true; vmDownloadMode = true;
} }
else else
{ {
if (mDownloadMode) if (vmDownloadMode)
{ {
esp_restart(); esp_restart();
} }
mDownloadMode = false; vmDownloadMode = false;
} }
return true; return true;
@ -656,11 +656,11 @@ void initPumpLogic()
pcnt_counter_resume(unit); pcnt_counter_resume(unit);
#endif #endif
pumpStartTime = millis(); pumpStartTime = millis();
pumpTarget = millis() + (mPlants[pumpToRun].getPumpDuration() * 1000); pumpTarget = millis() + (mPlants[vmPumpToRun].getPumpDuration() * 1000);
#ifdef FLOWMETER_PIN #ifdef FLOWMETER_PIN
log(LOG_LEVEL_INFO, "Starting pump " + String(pumpToRun) + " for " + String(mPlants[pumpToRun].getPumpDuration()) + "s or " + String(pumpTargetMl) + "ml", LOG_PUMP_STARTED_CODE); log(LOG_LEVEL_INFO, "Starting pump " + String(pumpToRun) + " for " + String(mPlants[pumpToRun].getPumpDuration()) + "s or " + String(pumpTargetMl) + "ml", LOG_PUMP_STARTED_CODE);
#else #else
log(LOG_LEVEL_INFO, "Starting pump " + String(pumpToRun) + " for " + String(mPlants[pumpToRun].getPumpDuration()) + "s", LOG_PUMP_STARTED_CODE); log(LOG_LEVEL_INFO, "Starting pump " + String(vmPumpToRun) + " for " + String(mPlants[vmPumpToRun].getPumpDuration()) + "s", LOG_PUMP_STARTED_CODE);
#endif #endif
@ -670,7 +670,7 @@ void initPumpLogic()
delay(100); delay(100);
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1);
mPlants[pumpToRun].activatePump(); mPlants[vmPumpToRun].activatePump();
} }
void pumpActiveLoop() void pumpActiveLoop()
@ -681,7 +681,7 @@ void pumpActiveLoop()
{ {
initPumpLogic(); initPumpLogic();
pumpStarted = true; pumpStarted = true;
rtcLastWateringPlant[pumpToRun] = getCurrentTime(); rtcLastWateringPlant[vmPumpToRun] = getCurrentTime();
} }
bool mqttUpdateTick = false; bool mqttUpdateTick = false;
@ -723,12 +723,12 @@ void pumpActiveLoop()
if (millis() > pumpTarget) if (millis() > pumpTarget)
{ {
mPlants[pumpToRun].setProperty("watertime").send(String(duration)); mPlants[vmPumpToRun].setProperty("watertime").send(String(duration));
targetReached = true; targetReached = true;
} }
else if (mqttUpdateTick) else if (mqttUpdateTick)
{ {
mPlants[pumpToRun].setProperty("watertime").send(String(duration)); mPlants[vmPumpToRun].setProperty("watertime").send(String(duration));
} }
if (targetReached) if (targetReached)
@ -736,11 +736,11 @@ void pumpActiveLoop()
// disable all // disable all
digitalWrite(OUTPUT_ENABLE_PUMP, LOW); digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
mPlants[pumpToRun].deactivatePump(); mPlants[vmPumpToRun].deactivatePump();
// disable loop, to prevent multi processing // disable loop, to prevent multi processing
pumpStarted = false; pumpStarted = false;
// if runtime is larger than cooldown, else it would run continously // if runtime is larger than cooldown, else it would run continously
rtcLastWateringPlant[pumpToRun] = getCurrentTime(); rtcLastWateringPlant[vmPumpToRun] = getCurrentTime();
espDeepSleep(true); espDeepSleep(true);
} }
} }
@ -830,10 +830,6 @@ void safeSetup()
{ {
mPlants[i].initSensors(); mPlants[i].initSensors();
} }
readPowerSwitchedSensors();
Serial << "Reading Homie Config..." << endl;
Homie.setup();
/************************* Start One-Wire bus ***************/ /************************* Start One-Wire bus ***************/
int tempInitStartTime = millis(); int tempInitStartTime = millis();
@ -847,6 +843,17 @@ void safeSetup()
sensorCount = sensors.getDS18Count(); sensorCount = sensors.getDS18Count();
delay(50); delay(50);
} }
mBatteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY);
if (mBatteryVoltage >= VOLT_SENSORS_BATT)
{
/* read all sensors with additional power source */
readPowerSwitchedSensors();
}
/* Start Homie and Wifi */
Serial << "Reading Homie Config..." << endl;
Homie.setup();
Serial << "DS18S20 count: " << sensorCount << " found in " << (millis() - tempInitStartTime) << " ms" << endl; Serial << "DS18S20 count: " << sensorCount << " found in " << (millis() - tempInitStartTime) << " ms" << endl;
Serial.flush(); Serial.flush();
@ -929,7 +936,7 @@ void safeSetup()
delay(100); delay(100);
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1);
Serial.println("Initial Setup. Start Accesspoint..."); Serial.println("Initial Setup. Start Accesspoint...");
mDownloadMode = true; vmDownloadMode = true;
} }
stayAlive.advertise("alive").setName("Alive").setDatatype(NUMBER_TYPE).settable(aliveHandler); stayAlive.advertise("alive").setName("Alive").setDatatype(NUMBER_TYPE).settable(aliveHandler);
setupFinishedTimestamp = millis(); setupFinishedTimestamp = millis();
@ -965,7 +972,7 @@ void selfTest()
if (selfTestPumpRun >= 0 && selfTestPumpRun < MAX_PLANTS) if (selfTestPumpRun >= 0 && selfTestPumpRun < MAX_PLANTS)
{ {
Serial << "self test mode pump deactivate " << pumpToRun << endl; Serial << "self test mode pump deactivate " << vmPumpToRun << endl;
Serial.flush(); Serial.flush();
mPlants[selfTestPumpRun].deactivatePump(); mPlants[selfTestPumpRun].deactivatePump();
} }
@ -997,7 +1004,7 @@ void loop()
{ {
Homie.loop(); Homie.loop();
/* Toggel Senor LED to visualize mode 3 */ /* Toggel Senor LED to visualize mode 3 */
if (mDownloadMode) if (vmDownloadMode)
{ {
if (nextBlink < millis()) if (nextBlink < millis())
{ {
@ -1022,9 +1029,9 @@ void loop()
else else
{ {
unsigned long timeSinceSetup = millis() - setupFinishedTimestamp; unsigned long timeSinceSetup = millis() - setupFinishedTimestamp;
if ((timeSinceSetup > MQTT_TIMEOUT) && (!mSensorsRead)) if ((timeSinceSetup > MQTT_TIMEOUT) && (!vmSensorsRead))
{ {
mSensorsRead = true; vmSensorsRead = true;
/* Disable Wifi and put modem into sleep mode */ /* Disable Wifi and put modem into sleep mode */
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
Serial << "Wifi mode set to " << WIFI_OFF << " mqqt was no reached within " << timeSinceSetup << "ms , fallback to offline mode " << endl; Serial << "Wifi mode set to " << WIFI_OFF << " mqqt was no reached within " << timeSinceSetup << "ms , fallback to offline mode " << endl;
@ -1034,14 +1041,14 @@ void loop()
} }
/** Timeout always stopping the ESP -> no endless power consumption */ /** Timeout always stopping the ESP -> no endless power consumption */
if (millis() > ESP_STALE_TIMEOUT && !mDownloadMode) if (millis() > ESP_STALE_TIMEOUT && !vmDownloadMode)
{ {
Serial << (millis() / 1000) << "not terminated watchdog reset" << endl; Serial << (millis() / 1000) << "not terminated watchdog reset" << endl;
Serial.flush(); Serial.flush();
esp_restart(); esp_restart();
} }
if (pumpToRun != -1) if (vmPumpToRun != -1)
{ {
pumpActiveLoop(); pumpActiveLoop();
} }
@ -1066,7 +1073,6 @@ void plantcontrol()
Serial << "W : " << waterRawSensor.getAverage() << " mm " << endl; Serial << "W : " << waterRawSensor.getAverage() << " mm " << endl;
mBatteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY);
float chipTemp = battery.getTemperature(); float chipTemp = battery.getTemperature();
Serial << "Chip Temperatur " << chipTemp << " °C " << endl; Serial << "Chip Temperatur " << chipTemp << " °C " << endl;
@ -1144,18 +1150,22 @@ bool isLowLight = (mSolarVoltage <= SOLAR_CHARGE_MIN_VOLTAGE);
hasWater = waterRawSensor.getAverage() > WATER_LEVEL_MINIMUM; hasWater = waterRawSensor.getAverage() > WATER_LEVEL_MINIMUM;
} }
if (mBatteryVoltage >= VOLT_SENSORS_BATT)
{
// FIXME no water warning message // FIXME no water warning message
pumpToRun = determineNextPump(isLowLight); vmPumpToRun = determineNextPump(isLowLight);
}
// early aborts // early aborts
if (pumpToRun != -1) if (vmPumpToRun != -1)
{ {
if(isLiquid){ if(isLiquid){
if (hasWater) if (hasWater)
{ {
if (mDownloadMode) if (vmDownloadMode)
{ {
log(LOG_LEVEL_INFO, LOG_PUMP_AND_DOWNLOADMODE, LOG_PUMP_AND_DOWNLOADMODE_CODE); log(LOG_LEVEL_INFO, LOG_PUMP_AND_DOWNLOADMODE, LOG_PUMP_AND_DOWNLOADMODE_CODE);
pumpToRun = -1; vmPumpToRun = -1;
} else { } else {
/* Pump can be used :) */ /* Pump can be used :) */
} }
@ -1163,18 +1173,18 @@ bool isLowLight = (mSolarVoltage <= SOLAR_CHARGE_MIN_VOLTAGE);
else else
{ {
log(LOG_LEVEL_ERROR, LOG_PUMP_BUTNOTANK_MESSAGE, LOG_PUMP_BUTNOTANK_CODE); log(LOG_LEVEL_ERROR, LOG_PUMP_BUTNOTANK_MESSAGE, LOG_PUMP_BUTNOTANK_CODE);
pumpToRun = -1; vmPumpToRun = -1;
} }
} }
else else
{ {
log(LOG_LEVEL_ERROR, LOG_VERY_COLD_WATER, LOG_VERY_COLD_WATER_CODE); log(LOG_LEVEL_ERROR, LOG_VERY_COLD_WATER, LOG_VERY_COLD_WATER_CODE);
pumpToRun = -1; vmPumpToRun = -1;
} }
} }
// go directly to sleep, skipping the pump loop // go directly to sleep, skipping the pump loop
if (pumpToRun == -1) if (vmPumpToRun == -1)
{ {
espDeepSleep(); espDeepSleep();
} }