fixed deepsleep procedure, homie shutdown, ensure pinhold is same as pin

This commit is contained in:
Your Name 2021-05-24 15:50:04 +02:00
parent 92005c1c33
commit 50d07a3c02
3 changed files with 71 additions and 62 deletions

View File

@ -46,33 +46,33 @@
/** \addtogroup GPIO Settings
* @{
*/
#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 27 /**< GPIO 27 (ADC2) */
#define SENSOR_PLANT5 39 /**< SENSOR_VIN */
#define SENSOR_PLANT6 36 /**< SENSOR_VP */
#define SENSOR_PLANT0 GPIO_NUM_32 /**< GPIO 32 (ADC1) */
#define SENSOR_PLANT1 GPIO_NUM_33 /**< GPIO 33 (ADC1) */
#define SENSOR_PLANT2 GPIO_NUM_25 /**< GPIO 25 (ADC2) */
#define SENSOR_PLANT3 GPIO_NUM_26 /**< GPIO 26 (ADC2) */
#define SENSOR_PLANT4 GPIO_NUM_27 /**< GPIO 27 (ADC2) */
#define SENSOR_PLANT5 GPIO_NUM_39 /**< SENSOR_VIN */
#define SENSOR_PLANT6 GPIO_NUM_36 /**< SENSOR_VP */
#define OUTPUT_PUMP0 15 /**< GPIO 15 */
#define OUTPUT_PUMP1 5 /**< GPIO 5 */
#define OUTPUT_PUMP2 18 /**< GPIO 18 */
#define OUTPUT_PUMP3 19 /**< GPIO 19 */
#define OUTPUT_PUMP4 21 /**< GPIO 21 */
#define OUTPUT_PUMP5 22 /**< GPIO 22 */
#define OUTPUT_PUMP6 23 /**< GPIO 23 */
#define OUTPUT_PUMP0 GPIO_NUM_15 /**< GPIO 15 */
#define OUTPUT_PUMP1 GPIO_NUM_5 /**< GPIO 5 */
#define OUTPUT_PUMP2 GPIO_NUM_18 /**< GPIO 18 */
#define OUTPUT_PUMP3 GPIO_NUM_19 /**< GPIO 19 */
#define OUTPUT_PUMP4 GPIO_NUM_21 /**< GPIO 21 */
#define OUTPUT_PUMP5 GPIO_NUM_22 /**< GPIO 22 */
#define OUTPUT_PUMP6 GPIO_NUM_23 /**< GPIO 23 */
#define OUTPUT_ENABLE_SENSOR 14 /**< GPIO 14 - Enable Sensors */
#define OUTPUT_ENABLE_PUMP 13 /**< GPIO 13 - Enable Pumps */
#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 4 /**< GPIO 12 - Temperatur sensor, Battery and other cool onewire stuff */
#define SENSOR_TANK_ECHO 16 /**< GPIO 16 - echo feedback of water sensor */
#define SENSOR_TANK_TRG 17 /**< GPIO 17 - trigger for water sensor */
#define BUTTON 0 /**< GPIO 0 - Fix button of NodeMCU */
#define CUSTOM1_PIN3 2 /**< GPIO 2 - Custom GPIO controlling a MOSFET, connected to GND */
#define CUSTOM1_PIN2 12 /**< GPIO 4 - custom GPIO directly connected to GPIO header */
#define I2C1_PIN2 34 /**< GPIO 34 - I2C */
#define I2C1_PIN3 35 /**< GPIO 35 - I2C */
#define SENSOR_ONEWIRE GPIO_NUM_4 /**< GPIO 12 - Temperatur sensor, Battery and other cool onewire stuff */
#define SENSOR_TANK_ECHO GPIO_NUM_16 /**< GPIO 16 - echo feedback of water sensor */
#define SENSOR_TANK_TRG GPIO_NUM_17 /**< GPIO 17 - trigger for water sensor */
#define BUTTON GPIO_NUM_0 /**< GPIO 0 - Fix button of NodeMCU */
#define CUSTOM1_PIN3 GPIO_NUM_2 /**< GPIO 2 - Custom GPIO controlling a MOSFET, connected to GND */
#define CUSTOM1_PIN2 GPIO_NUM_12 /**< GPIO 4 - custom GPIO directly connected to GPIO header */
#define I2C1_PIN2 GPIO_NUM_34 /**< GPIO 34 - I2C */
#define I2C1_PIN3 GPIO_NUM_35 /**< GPIO 35 - I2C */
/* @} */
/** \addtogroup Configuration

View File

@ -17,9 +17,9 @@ board_build.partitions = defaultWithSmallerSpiffs.csv
; the latest development brankitchen-lightch (convention V3.0.x)
lib_deps = ArduinoJson@6.16.1
https://github.com/homieiot/homie-esp8266.git#v3.0
OneWire
DallasTemperature
https://github.com/homieiot/homie-esp8266.git#develop
[platformio]

View File

@ -38,14 +38,14 @@
******************************************************************************/
int determineNextPump();
void plantcontrol();
void plantcontrol(boolean withHomie);
void readPowerSwitchedSensors();
/******************************************************************************
* NON VOLATILE VARIABLES in DEEP SLEEP
******************************************************************************/
RTC_DATA_ATTR int lastPumpRunning = 0; /**< store last successfully waterd plant */
RTC_DATA_ATTR int lastPumpRunning = -1; /**< store last successfully waterd plant */
RTC_DATA_ATTR long lastWaterValue = 0; /**< to calculate the used water per plant */
RTC_DATA_ATTR long rtcLastWateringPlant[MAX_PLANTS] = { 0 };
@ -54,7 +54,6 @@ RTC_DATA_ATTR long rtcLastWateringPlant[MAX_PLANTS] = { 0 };
* LOCAL VARIABLES
******************************************************************************/
bool volatile mDownloadMode = false; /**< Controller must not sleep */
bool volatile mDeepsleep = false; /**< about to sleep, clearing the todolist of the controller */
bool volatile mSensorsRead = false; /**< Sensors are read without Wifi or MQTT */
bool mConfigured = false;
@ -99,7 +98,7 @@ int getCurrentHour()
return info.tm_hour;
}
void espDeepSleepFor(long seconds, bool activatePump = false)
void espDeepSleepFor(long seconds, bool activatePump, bool withHomieShutdown)
{
if (mDownloadMode)
{
@ -127,12 +126,12 @@ void espDeepSleepFor(long seconds, bool activatePump = false)
{
esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_ON);
gpio_deep_sleep_hold_en();
gpio_hold_en(GPIO_NUM_13); //pump pwr
gpio_hold_en(OUTPUT_ENABLE_PUMP); //pump pwr
}
else
{
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
gpio_hold_dis(GPIO_NUM_13); //pump pwr
gpio_hold_dis(OUTPUT_ENABLE_PUMP); //pump pwr
gpio_deep_sleep_hold_dis();
digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
digitalWrite(OUTPUT_ENABLE_SENSOR, LOW);
@ -149,12 +148,19 @@ void espDeepSleepFor(long seconds, bool activatePump = false)
Serial.println(" seconds");
esp_sleep_enable_timer_wakeup((seconds * 1000U * 1000U));
mDeepsleep = true;
if(withHomieShutdown){
Homie.prepareToSleep();
}else {
Serial << "Bye offline mode" << endl;
Serial.flush();
esp_deep_sleep_start();
}
}
//requires homie being started
void readOneWireSensors(boolean withMQTT){
void readOneWireSensors(bool withMQTT){
int sensorCount = sensors.getDS18Count();
Serial << "Read OneWire" << endl;
Serial.flush();
@ -252,6 +258,11 @@ void onHomieEvent(const HomieEvent &event)
{
switch (event.type)
{
case HomieEventType::READY_TO_SLEEP:
Serial << "Bye homie mode" << endl;
Serial.flush();
esp_deep_sleep_start();
break;
case HomieEventType::SENDING_STATISTICS:
break;
case HomieEventType::MQTT_READY:
@ -264,13 +275,13 @@ void onHomieEvent(const HomieEvent &event)
Serial.printf("NTP Setup with server %s\r\n", ntpServer.get());
configTime(0, 0, ntpServer.get());
Serial << "Setup plants" << endl;
Serial << "publish plants mqtt" << endl;
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].postMQTTconnection();
}
plantcontrol();
plantcontrol(true);
break;
case HomieEventType::OTA_STARTED:
Homie.getLogger() << "OTA started" << endl;
@ -523,9 +534,7 @@ void loop()
digitalWrite(OUTPUT_ENABLE_SENSOR, !digitalRead(OUTPUT_ENABLE_SENSOR));
}
}
else if (!mDeepsleep)
{
else {
unsigned long timeSinceSetup = millis() - setupFinishedTimestamp;
if ((timeSinceSetup > MQTT_TIMEOUT) && (!mSensorsRead)) {
mSensorsRead = true;
@ -533,18 +542,12 @@ void loop()
WiFi.mode(WIFI_OFF);
Serial <<"Wifi mode set to " << WIFI_OFF << " mqqt was no reached within " << timeSinceSetup << "ms , fallback to offline mode " << endl;
Serial.flush();
plantcontrol();
plantcontrol(false);
}
}
else
{
Serial << "Bye" << endl;
Serial.flush();
esp_deep_sleep_start();
}
/** Timeout always stopping the ESP -> no endless power consumption */
if (millis() > 30000 && !mDownloadMode)
if (millis() > 60000 && !mDownloadMode)
{
Serial << (millis() / 1000) << "not terminated watchdog reset" << endl;
Serial.flush();
@ -557,7 +560,7 @@ void loop()
* @fn plantcontrol
* Main function, doing the logic
*/
void plantcontrol()
void plantcontrol(bool withHomie)
{
if (lastPumpRunning != -1)
{
@ -589,26 +592,32 @@ void plantcontrol()
mPlants[i].setProperty("moist").send(String(pct));
mPlants[i].setProperty("moistraw").send(String(raw));
}
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - waterRawSensor.getAverage()));
Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << "%)" << endl;
lastWaterValue = waterRawSensor.getAverage();
float batteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY);
float chipTemp = battery.getTemperature();
sensorLipo.setProperty("percent").send(String(100 * batteryVoltage / VOLT_MAX_BATT));
sensorLipo.setProperty("volt").send(String(batteryVoltage));
sensorLipo.setProperty("current").send(String(battery.getCurrent()));
sensorLipo.setProperty("Ah").send(String(battery.getAh()));
sensorLipo.setProperty("ICA").send(String(battery.getICA()));
sensorLipo.setProperty("DCA").send(String(battery.getDCA()));
sensorLipo.setProperty("CCA").send(String(battery.getCCA()));
sensorSolar.setProperty("volt").send(String(mSolarVoltage));
sensorTemp.setProperty(TEMPERATUR_SENSOR_CHIP).send(String(chipTemp));
Serial << "Chip Temperatur " << chipTemp << " °C " << endl;
if(withHomie){
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - waterRawSensor.getAverage()));
sensorLipo.setProperty("percent").send(String(100 * batteryVoltage / VOLT_MAX_BATT));
sensorLipo.setProperty("volt").send(String(batteryVoltage));
sensorLipo.setProperty("current").send(String(battery.getCurrent()));
sensorLipo.setProperty("Ah").send(String(battery.getAh()));
sensorLipo.setProperty("ICA").send(String(battery.getICA()));
sensorLipo.setProperty("DCA").send(String(battery.getDCA()));
sensorLipo.setProperty("CCA").send(String(battery.getCCA()));
sensorSolar.setProperty("volt").send(String(mSolarVoltage));
sensorTemp.setProperty(TEMPERATUR_SENSOR_CHIP).send(String(chipTemp));
} else {
Serial.println("Skipping MQTT, offline mode");
Serial.flush();
}
bool hasWater = true; //FIXMEmWaterGone > waterLevelMin.get();
//FIXME no water warning message
lastPumpRunning = determineNextPump();
@ -637,18 +646,18 @@ void plantcontrol()
{
Serial.print(mSolarVoltage);
Serial.println("V! No pumps to activate and low light, deepSleepNight");
espDeepSleepFor(deepSleepNightTime.get());
espDeepSleepFor(deepSleepNightTime.get(), false, withHomie);
}
else
{
Serial.println("No pumps to activate, deepSleep");
espDeepSleepFor(deepSleepTime.get());
espDeepSleepFor(deepSleepTime.get(), false ,withHomie);
}
}
else
{
Serial.println("Running pump, watering deepsleep");
espDeepSleepFor(wateringDeepSleep.get(), true);
espDeepSleepFor(wateringDeepSleep.get(), true, withHomie);
}
}