Merge branch 'master' of https://github.com/0110/PlantCtrl
This commit is contained in:
commit
f29a5e3d0b
@ -46,7 +46,6 @@
|
|||||||
|
|
||||||
#define MIN_TIME_RUNNING 5UL /**< Amount of seconds the controller must stay awoken */
|
#define MIN_TIME_RUNNING 5UL /**< Amount of seconds the controller must stay awoken */
|
||||||
#define MAX_PLANTS 7
|
#define MAX_PLANTS 7
|
||||||
#define EMPTY_LIPO_MULTIPL 3 /**< Multiplier to increase time for sleeping when lipo is empty */
|
|
||||||
#define MINIMUM_LIPO_VOLT 3.6f /**< Minimum voltage of the Lipo, that must be present */
|
#define MINIMUM_LIPO_VOLT 3.6f /**< Minimum voltage of the Lipo, that must be present */
|
||||||
#define NO_LIPO_VOLT 2.0f /**< No Lipo connected */
|
#define NO_LIPO_VOLT 2.0f /**< No Lipo connected */
|
||||||
#define MINIMUM_SOLAR_VOLT 4.0f /**< Minimum voltage of the sun, to detect daylight */
|
#define MINIMUM_SOLAR_VOLT 4.0f /**< Minimum voltage of the sun, to detect daylight */
|
||||||
@ -59,4 +58,7 @@
|
|||||||
|
|
||||||
#define MAX_CONFIG_SETTING_ITEMS 50 /**< Parameter, that can be configured in Homie */
|
#define MAX_CONFIG_SETTING_ITEMS 50 /**< Parameter, that can be configured in Homie */
|
||||||
|
|
||||||
|
#define PANIK_MODE_DEEPSLEEP (60*60*5U) /**< 5 hours in usecond */
|
||||||
|
#define PANIK_MODE_DEEPSLEEP_US (PANIK_MODE_DEEPSLEEP*1000*1000)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -37,7 +37,6 @@ HomieNode stayAlive("stay", "alive", "alive");
|
|||||||
/**
|
/**
|
||||||
*********************************** Settings *******************************
|
*********************************** Settings *******************************
|
||||||
*/
|
*/
|
||||||
|
|
||||||
HomieSetting<long> deepSleepTime("deepsleep", "time in milliseconds to sleep (0 deactivats it)");
|
HomieSetting<long> deepSleepTime("deepsleep", "time in milliseconds to sleep (0 deactivats it)");
|
||||||
HomieSetting<long> deepSleepNightTime("nightsleep", "time in milliseconds to sleep (0 uses same setting: deepsleep at night, too)");
|
HomieSetting<long> deepSleepNightTime("nightsleep", "time in milliseconds to sleep (0 uses same setting: deepsleep at night, too)");
|
||||||
HomieSetting<long> wateringDeepSleep("pumpdeepsleep", "time seconds to sleep, while a pump is running");
|
HomieSetting<long> wateringDeepSleep("pumpdeepsleep", "time seconds to sleep, while a pump is running");
|
||||||
|
@ -13,13 +13,12 @@
|
|||||||
#define PLANT_CTRL_H
|
#define PLANT_CTRL_H
|
||||||
|
|
||||||
#include "HomieTypes.h"
|
#include "HomieTypes.h"
|
||||||
|
#include "RunningMedian.h"
|
||||||
|
|
||||||
class Plant {
|
class Plant {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int mValue = 0; /**< Value of the moist sensor */
|
RunningMedian moistureRaw = RunningMedian(5);
|
||||||
|
|
||||||
int mAnalogValue=0; /**< moist sensor values, used for a calculation */
|
|
||||||
HomieNode* mPlant = NULL;
|
HomieNode* mPlant = NULL;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -45,14 +44,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
void addSenseValue(int analogValue);
|
void addSenseValue(int analogValue);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculate the value based on the information
|
|
||||||
* @see amountMeasurePoints
|
|
||||||
* Internal memory, used by addSenseValue will be resetted
|
|
||||||
* @return int analog value
|
|
||||||
*/
|
|
||||||
void calculateSensorValue(int amountMeasurePoints);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the Sensor Pin of the analog measuring
|
* @brief Get the Sensor Pin of the analog measuring
|
||||||
*
|
*
|
||||||
@ -67,7 +58,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
int getPumpPin() { return mPinPump; }
|
int getPumpPin() { return mPinPump; }
|
||||||
|
|
||||||
int getSensorValue() { return mValue; }
|
int getSensorValue() { return moistureRaw.getMedian(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a plant is too dry and needs some water.
|
* @brief Check if a plant is too dry and needs some water.
|
||||||
@ -76,7 +67,7 @@ public:
|
|||||||
* @return false
|
* @return false
|
||||||
*/
|
*/
|
||||||
bool isPumpRequired() {
|
bool isPumpRequired() {
|
||||||
return (this->mSetting->pSensorDry != NULL) && (this->mValue < this->mSetting->pSensorDry->get());
|
return (this->mSetting->pSensorDry != NULL) && (this->moistureRaw.getMedian() < this->mSetting->pSensorDry->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
HomieInternals::SendingPromise& setProperty(const String& property) const {
|
HomieInternals::SendingPromise& setProperty(const String& property) const {
|
||||||
@ -84,10 +75,6 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void init(void);
|
void init(void);
|
||||||
|
|
||||||
long getSettingSensorDry() {
|
|
||||||
return this->mSetting->pSensorDry->get();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -19,3 +19,4 @@ board_build.partitions = defaultWithSmallerSpiffs.csv
|
|||||||
lib_deps = ArduinoJson@6.16.1
|
lib_deps = ArduinoJson@6.16.1
|
||||||
https://github.com/homieiot/homie-esp8266.git#v3.0
|
https://github.com/homieiot/homie-esp8266.git#v3.0
|
||||||
OneWire
|
OneWire
|
||||||
|
DallasTemperature
|
||||||
|
@ -20,7 +20,7 @@ Plant::Plant(int pinSensor, int pinPump,int plantId, HomieNode* plant, PlantSett
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Plant::init(void) {
|
void Plant::init(void) {
|
||||||
this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT);
|
this->mSetting->pSensorDry->setDefaultValue(4095);
|
||||||
this->mSetting->pSensorDry->setValidator([] (long candidate) {
|
this->mSetting->pSensorDry->setValidator([] (long candidate) {
|
||||||
return (((candidate >= 0) && (candidate <= 4095) ) || candidate == DEACTIVATED_PLANT);
|
return (((candidate >= 0) && (candidate <= 4095) ) || candidate == DEACTIVATED_PLANT);
|
||||||
});
|
});
|
||||||
@ -41,10 +41,5 @@ void Plant::init(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Plant::addSenseValue(int analog) {
|
void Plant::addSenseValue(int analog) {
|
||||||
this->mAnalogValue += analog;
|
this->moistureRaw.add(analog);
|
||||||
}
|
|
||||||
|
|
||||||
void Plant::calculateSensorValue(int amountMeasurePoints) {
|
|
||||||
this->mValue = this->mAnalogValue / amountMeasurePoints;
|
|
||||||
this->mAnalogValue = 0;
|
|
||||||
}
|
}
|
@ -17,6 +17,7 @@
|
|||||||
#include "esp_sleep.h"
|
#include "esp_sleep.h"
|
||||||
#include "RunningMedian.h"
|
#include "RunningMedian.h"
|
||||||
#include <arduino-timer.h>
|
#include <arduino-timer.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minutes */
|
const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minutes */
|
||||||
|
|
||||||
@ -28,6 +29,7 @@ const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minut
|
|||||||
|
|
||||||
/********************* non volatile enable after deepsleep *******************************/
|
/********************* non volatile enable after deepsleep *******************************/
|
||||||
|
|
||||||
|
RTC_DATA_ATTR long gotoMode2AfterThisTimestamp = 0;
|
||||||
RTC_DATA_ATTR long rtcDeepSleepTime = 0; /**< Time, when the microcontroller shall be up again */
|
RTC_DATA_ATTR long rtcDeepSleepTime = 0; /**< Time, when the microcontroller shall be up again */
|
||||||
RTC_DATA_ATTR long rtcLastActive0 = 0;
|
RTC_DATA_ATTR long rtcLastActive0 = 0;
|
||||||
RTC_DATA_ATTR long rtcMoistureTrigger0 = 0; /**<Level for the moisture sensor */
|
RTC_DATA_ATTR long rtcMoistureTrigger0 = 0; /**<Level for the moisture sensor */
|
||||||
@ -46,21 +48,14 @@ RTC_DATA_ATTR long rtcMoistureTrigger6 = 0; /**<Level for the moisture sensor
|
|||||||
RTC_DATA_ATTR int lastPumpRunning = 0;
|
RTC_DATA_ATTR int lastPumpRunning = 0;
|
||||||
RTC_DATA_ATTR long lastWaterValue = 0;
|
RTC_DATA_ATTR long lastWaterValue = 0;
|
||||||
|
|
||||||
|
const char* ntpServer = "pool.ntp.org";
|
||||||
|
|
||||||
bool warmBoot = true;
|
bool warmBoot = true;
|
||||||
bool mode3Active = false; /**< Controller must not sleep */
|
bool mode3Active = false; /**< Controller must not sleep */
|
||||||
|
|
||||||
|
|
||||||
bool mLoopInited = false;
|
|
||||||
bool mDeepSleep = false;
|
|
||||||
|
|
||||||
int plantSensor1 = 0;
|
int plantSensor1 = 0;
|
||||||
|
|
||||||
int lipoSenor = -1;
|
|
||||||
int lipoSensorValues = 0;
|
|
||||||
int solarSensor = -1;
|
|
||||||
int solarSensorValues = 0;
|
|
||||||
|
|
||||||
int mWaterGone = -1; /**< Amount of centimeter, where no water is seen */
|
int mWaterGone = -1; /**< Amount of centimeter, where no water is seen */
|
||||||
int readCounter = 0;
|
int readCounter = 0;
|
||||||
int mButtonClicks = 0;
|
int mButtonClicks = 0;
|
||||||
@ -91,57 +86,87 @@ Plant mPlants[MAX_PLANTS] = {
|
|||||||
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6, &plant6, &mSetting6)
|
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6, &plant6, &mSetting6)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
float getBatteryVoltage(){
|
||||||
|
return ADC_5V_TO_3V3(lipoRawSensor.getAverage());
|
||||||
|
}
|
||||||
|
|
||||||
|
float getSolarVoltage(){
|
||||||
|
return SOLAR_VOLT(solarRawSensor.getAverage());
|
||||||
|
}
|
||||||
|
|
||||||
void readSystemSensors() {
|
void readSystemSensors() {
|
||||||
lipoRawSensor.add(analogRead(SENSOR_LIPO));
|
lipoRawSensor.add(analogRead(SENSOR_LIPO));
|
||||||
solarRawSensor.add(analogRead(SENSOR_SOLAR));
|
solarRawSensor.add(analogRead(SENSOR_SOLAR));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int determineNextPump();
|
int determineNextPump();
|
||||||
void setLastActivationForPump(int pumpId, long time);
|
void setLastActivationForPump(int pumpId, long time);
|
||||||
|
|
||||||
|
|
||||||
//FIXME real impl
|
|
||||||
long getCurrentTime(){
|
long getCurrentTime(){
|
||||||
return 1337;
|
struct timeval tv_now;
|
||||||
|
gettimeofday(&tv_now, NULL);
|
||||||
|
return tv_now.tv_sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
//wait till homie flushed mqtt ect.
|
//wait till homie flushed mqtt ect.
|
||||||
bool prepareSleep(void *) {
|
bool prepareSleep(void *) {
|
||||||
//FIXME wait till pending mqtt is done, then start sleep via event or whatever
|
//FIXME wait till pending mqtt is done, then start sleep via event or whatever
|
||||||
//Homie.prepareToSleep();
|
//Homie.prepareToSleep();
|
||||||
|
bool queueIsEmpty = true;
|
||||||
|
if(queueIsEmpty){
|
||||||
|
esp_deep_sleep_start();
|
||||||
|
}
|
||||||
return true; // repeat? true there is something in the queue to be done
|
return true; // repeat? true there is something in the queue to be done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void espDeepSleepFor(long seconds){
|
||||||
|
delay(1500);
|
||||||
|
gpio_deep_sleep_hold_en();
|
||||||
|
gpio_hold_en(GPIO_NUM_13); //pump pwr
|
||||||
|
//gpio_hold_en(GPIO_NUM_23); //p0
|
||||||
|
//FIXME fix for outher outputs
|
||||||
|
|
||||||
|
|
||||||
|
Serial.print("Going to sleep for ");
|
||||||
|
Serial.print(seconds);
|
||||||
|
Serial.println(" seconds");
|
||||||
|
esp_sleep_enable_timer_wakeup( (seconds * 1000U * 1000U) );
|
||||||
|
wait4sleep.in(500, prepareSleep);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void mode2MQTT(){
|
void mode2MQTT(){
|
||||||
if (deepSleepTime.get()) {
|
readSystemSensors();
|
||||||
Serial << "sleeping for " << deepSleepTime.get() << endl;
|
|
||||||
}
|
configTime(0, 0, ntpServer);
|
||||||
/* Publish default values */
|
|
||||||
|
digitalWrite(OUTPUT_PUMP, LOW);
|
||||||
|
for(int i=0; i < MAX_PLANTS; i++) {
|
||||||
|
digitalWrite(mPlants[i].mPinPump, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deepSleepTime.get()) {
|
||||||
|
Serial << "sleeping for " << deepSleepTime.get() << endl;
|
||||||
|
}
|
||||||
|
/* Publish default values */
|
||||||
|
|
||||||
if(lastPumpRunning != -1){
|
if(lastPumpRunning != -1){
|
||||||
long waterDiff = mWaterGone-lastWaterValue;
|
long waterDiff = mWaterGone-lastWaterValue;
|
||||||
//TODO attribute used water in ml to plantid
|
//TODO attribute used water in ml to plantid
|
||||||
}
|
}
|
||||||
|
for(int i=0; i < MAX_PLANTS; i++) {
|
||||||
|
mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 ));
|
||||||
|
}
|
||||||
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - mWaterGone ));
|
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - mWaterGone ));
|
||||||
Serial << "W : " << mWaterGone << " cm (" << String(waterLevelMax.get() - mWaterGone ) << "%)" << endl;
|
Serial << "W : " << mWaterGone << " cm (" << String(waterLevelMax.get() - mWaterGone ) << "%)" << endl;
|
||||||
lastWaterValue = mWaterGone;
|
lastWaterValue = mWaterGone;
|
||||||
|
|
||||||
if (mWaterGone <= waterLevelMin.get()) {
|
sensorLipo.setProperty("percent").send( String(100 * lipoRawSensor.getAverage() / 4095) );
|
||||||
/* let the ESP sleep qickly, as nothing must be done */
|
sensorLipo.setProperty("volt").send( String(getBatteryVoltage()) );
|
||||||
if ((millis() >= (MIN_TIME_RUNNING * MS_TO_S)) && (deepSleepTime.get() > 0)) {
|
sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage() ) / 4095));
|
||||||
Serial << "No W" << endl;
|
sensorSolar.setProperty("volt").send( String(getSolarVoltage()) );
|
||||||
/* in 500 microseconds */
|
|
||||||
wait4sleep.in(500, prepareSleep);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sensorLipo.setProperty("percent").send( String(100 * lipoSenor / 4095) );
|
|
||||||
sensorLipo.setProperty("volt").send( String(ADC_5V_TO_3V3(lipoSenor)) );
|
|
||||||
sensorSolar.setProperty("percent").send(String((100 * solarSensor ) / 4095));
|
|
||||||
sensorSolar.setProperty("volt").send( String(SOLAR_VOLT(solarSensor)) );
|
|
||||||
|
|
||||||
float temp[2] = { TEMP_INIT_VALUE, TEMP_INIT_VALUE };
|
float temp[2] = { TEMP_INIT_VALUE, TEMP_INIT_VALUE };
|
||||||
float* pFloat = temp;
|
float* pFloat = temp;
|
||||||
@ -161,20 +186,39 @@ void mode2MQTT(){
|
|||||||
|
|
||||||
bool lipoTempWarning = abs(temp[0] - temp[1]) > 5;
|
bool lipoTempWarning = abs(temp[0] - temp[1]) > 5;
|
||||||
if(lipoTempWarning){
|
if(lipoTempWarning){
|
||||||
wait4sleep.in(500, prepareSleep);
|
Serial.println("Lipo temp incorrect, panic mode deepsleep");
|
||||||
|
espDeepSleepFor(PANIK_MODE_DEEPSLEEP);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
digitalWrite(OUTPUT_PUMP, LOW);
|
bool hasWater = true;//FIXMEmWaterGone > waterLevelMin.get();
|
||||||
for(int i=0; i < MAX_PLANTS; i++) {
|
//FIXME no water warning message
|
||||||
digitalWrite(mPlants[i].mPinPump, LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
lastPumpRunning = determineNextPump();
|
lastPumpRunning = determineNextPump();
|
||||||
if(lastPumpRunning != -1){
|
if(lastPumpRunning != -1 && !hasWater){
|
||||||
|
Serial.println("Want to pump but no water");
|
||||||
|
}
|
||||||
|
if(lastPumpRunning != -1 && hasWater){
|
||||||
|
digitalWrite(OUTPUT_PUMP, HIGH);
|
||||||
setLastActivationForPump(lastPumpRunning, getCurrentTime());
|
setLastActivationForPump(lastPumpRunning, getCurrentTime());
|
||||||
digitalWrite(mPlants[lastPumpRunning].mPinPump, HIGH);
|
digitalWrite(mPlants[lastPumpRunning].mPinPump, HIGH);
|
||||||
}
|
}
|
||||||
|
if(lastPumpRunning == -1 || !hasWater){
|
||||||
|
if(getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE){
|
||||||
|
gotoMode2AfterThisTimestamp = getCurrentTime()+deepSleepNightTime.get();
|
||||||
|
Serial.println("No pumps to activate and low light, deepSleepNight");
|
||||||
|
espDeepSleepFor(deepSleepNightTime.get());
|
||||||
|
}else {
|
||||||
|
gotoMode2AfterThisTimestamp = getCurrentTime()+deepSleepTime.get();
|
||||||
|
Serial.println("No pumps to activate, deepSleep");
|
||||||
|
espDeepSleepFor(deepSleepTime.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
}else {
|
||||||
|
gotoMode2AfterThisTimestamp = 0;
|
||||||
|
Serial.println("Running pump, watering deepsleep");
|
||||||
|
espDeepSleepFor(wateringDeepSleep.get());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMoistureTrigger(int plantId, long value){
|
void setMoistureTrigger(int plantId, long value){
|
||||||
@ -255,7 +299,9 @@ long getLastActivationForPump(int plantId){
|
|||||||
* These sensors (ADC2) can only be read when no Wifi is used.
|
* These sensors (ADC2) can only be read when no Wifi is used.
|
||||||
*/
|
*/
|
||||||
void readSensors() {
|
void readSensors() {
|
||||||
Serial << "rs" << endl;
|
Serial << "Read Sensors" << endl;
|
||||||
|
|
||||||
|
readSystemSensors();
|
||||||
|
|
||||||
/* activate all sensors */
|
/* activate all sensors */
|
||||||
pinMode(OUTPUT_SENSOR, OUTPUT);
|
pinMode(OUTPUT_SENSOR, OUTPUT);
|
||||||
@ -305,15 +351,26 @@ void readSensors() {
|
|||||||
digitalWrite(OUTPUT_SENSOR, LOW);
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Homie.getMqttClient().disconnect();
|
//Homie.getMqttClient().disconnect();
|
||||||
|
|
||||||
void onHomieEvent(const HomieEvent& event) {
|
void onHomieEvent(const HomieEvent& event) {
|
||||||
const String OFF = String("OFF");
|
const String OFF = String("OFF");
|
||||||
switch(event.type) {
|
switch(event.type) {
|
||||||
|
case HomieEventType::SENDING_STATISTICS:
|
||||||
|
mode2MQTT();
|
||||||
|
Homie.getLogger() << "My statistics" << endl;
|
||||||
|
break;
|
||||||
case HomieEventType::MQTT_READY:
|
case HomieEventType::MQTT_READY:
|
||||||
|
//wait for rtc sync?
|
||||||
|
rtcDeepSleepTime = deepSleepTime.get();
|
||||||
|
Serial << rtcDeepSleepTime << " ms ds" << endl;
|
||||||
|
|
||||||
|
//saveguard, should be overriden in mode2MQTT normally
|
||||||
|
esp_sleep_enable_timer_wakeup( (rtcDeepSleepTime * 1000U) );
|
||||||
|
|
||||||
|
mode2MQTT();
|
||||||
|
Homie.getLogger() << "MQTT 1" << endl;
|
||||||
|
|
||||||
plant0.setProperty("switch").send(OFF);
|
plant0.setProperty("switch").send(OFF);
|
||||||
plant1.setProperty("switch").send(OFF);
|
plant1.setProperty("switch").send(OFF);
|
||||||
plant2.setProperty("switch").send(OFF);
|
plant2.setProperty("switch").send(OFF);
|
||||||
@ -322,46 +379,48 @@ void onHomieEvent(const HomieEvent& event) {
|
|||||||
plant5.setProperty("switch").send(OFF);
|
plant5.setProperty("switch").send(OFF);
|
||||||
plant6.setProperty("switch").send(OFF);
|
plant6.setProperty("switch").send(OFF);
|
||||||
|
|
||||||
//wait for rtc sync?
|
|
||||||
rtcDeepSleepTime = deepSleepTime.get();
|
|
||||||
if(!mode3Active){
|
|
||||||
mode2MQTT();
|
|
||||||
}
|
|
||||||
Homie.getLogger() << "MQTT 1" << endl;
|
|
||||||
break;
|
break;
|
||||||
case HomieEventType::READY_TO_SLEEP:
|
case HomieEventType::READY_TO_SLEEP:
|
||||||
Homie.getLogger() << "rtsleep" << endl;
|
Homie.getLogger() << "rtsleep" << endl;
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
break;
|
break;
|
||||||
|
case HomieEventType::OTA_STARTED:
|
||||||
|
digitalWrite(OUTPUT_SENSOR, HIGH);
|
||||||
|
mode3Active=true;
|
||||||
|
break;
|
||||||
|
case HomieEventType::OTA_SUCCESSFUL:
|
||||||
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int determineNextPump(){
|
int determineNextPump(){
|
||||||
float solarValue = solarRawSensor.getMedian();
|
float solarValue = getSolarVoltage();
|
||||||
bool isLowLight =(ADC_5V_TO_3V3(solarValue) > SOLAR_CHARGE_MIN_VOLTAGE || ADC_5V_TO_3V3(solarValue) < SOLAR_CHARGE_MAX_VOLTAGE);
|
bool isLowLight =(solarValue > SOLAR_CHARGE_MIN_VOLTAGE || solarValue < SOLAR_CHARGE_MAX_VOLTAGE);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//FIXME instead of for, use sorted by last activation index to ensure equal runtime?
|
//FIXME instead of for, use sorted by last activation index to ensure equal runtime?
|
||||||
for(int i=0; i < MAX_PLANTS; i++) {
|
for(int i=0; i < MAX_PLANTS; i++) {
|
||||||
mPlants[i].calculateSensorValue(AMOUNT_SENOR_QUERYS);
|
|
||||||
mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 ));
|
|
||||||
long lastActivation = getLastActivationForPump(i);
|
long lastActivation = getLastActivationForPump(i);
|
||||||
long sinceLastActivation = getCurrentTime()-lastActivation;
|
long sinceLastActivation = getCurrentTime()-lastActivation;
|
||||||
//this pump is in cooldown skip it and disable low power mode trigger for it
|
//this pump is in cooldown skip it and disable low power mode trigger for it
|
||||||
if(mPlants[i].mSetting->pPumpCooldownInHours->get() > sinceLastActivation / 3600 / 1000){
|
if(mPlants[i].mSetting->pPumpCooldownInHours->get() > sinceLastActivation / 3600){
|
||||||
setMoistureTrigger(i, DEACTIVATED_PLANT);
|
Serial.println("Skipping due to cooldown");
|
||||||
continue;
|
//setMoistureTrigger(i, DEACTIVATED_PLANT);
|
||||||
|
//continue;
|
||||||
}
|
}
|
||||||
//skip as it is not low light
|
//skip as it is not low light
|
||||||
if(!isLowLight && mPlants[i].mSetting->pPumpOnlyWhenLowLight->get()){
|
if(!isLowLight && mPlants[i].mSetting->pPumpOnlyWhenLowLight->get()){
|
||||||
|
Serial.println("Skipping due to light");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mPlants->isPumpRequired()){
|
if(mPlants->isPumpRequired()){
|
||||||
|
Serial.println("Requested pumpin");
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
Serial.println("No pump required");
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -415,13 +474,13 @@ bool switchGeneralPumpHandler(const int pump, const HomieRange& range, const Str
|
|||||||
*/
|
*/
|
||||||
bool aliveHandler(const HomieRange& range, const String& value) {
|
bool aliveHandler(const HomieRange& range, const String& value) {
|
||||||
if (range.isRange) return false; // only one controller is present
|
if (range.isRange) return false; // only one controller is present
|
||||||
|
Serial << value << endl;
|
||||||
if (value.equals("ON") || value.equals("On") || value.equals("1")) {
|
if (value.equals("ON") || value.equals("On") || value.equals("1")) {
|
||||||
mode3Active=true;
|
mode3Active=true;
|
||||||
} else {
|
} else {
|
||||||
mode3Active=false;
|
mode3Active=false;
|
||||||
esp_deep_sleep_start();
|
|
||||||
}
|
}
|
||||||
Serial << (mode3Active ? "stayalive" : "") << endl;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -472,15 +531,19 @@ void systemInit(){
|
|||||||
Homie_setFirmware("PlantControl", FIRMWARE_VERSION);
|
Homie_setFirmware("PlantControl", FIRMWARE_VERSION);
|
||||||
|
|
||||||
// Set default values
|
// Set default values
|
||||||
deepSleepTime.setDefaultValue(300000); /* 5 minutes in milliseconds */
|
|
||||||
deepSleepNightTime.setDefaultValue(0);
|
//in seconds
|
||||||
wateringDeepSleep.setDefaultValue(60000); /* 1 minute in milliseconds */
|
deepSleepTime.setDefaultValue(10);
|
||||||
|
deepSleepNightTime.setDefaultValue(30);
|
||||||
|
wateringDeepSleep.setDefaultValue(5);
|
||||||
|
|
||||||
waterLevelMax.setDefaultValue(1000); /* 100cm in mm */
|
waterLevelMax.setDefaultValue(1000); /* 100cm in mm */
|
||||||
waterLevelMin.setDefaultValue(50); /* 5cm in mm */
|
waterLevelMin.setDefaultValue(50); /* 5cm in mm */
|
||||||
waterLevelWarn.setDefaultValue(500); /* 50cm in mm */
|
waterLevelWarn.setDefaultValue(500); /* 50cm in mm */
|
||||||
waterLevelVol.setDefaultValue(5000); /* 5l in ml */
|
waterLevelVol.setDefaultValue(5000); /* 5l in ml */
|
||||||
|
|
||||||
Homie.setLoopFunction(homieLoop);
|
Homie.setLoopFunction(homieLoop);
|
||||||
|
Homie.onEvent(onHomieEvent);
|
||||||
Homie.setup();
|
Homie.setup();
|
||||||
|
|
||||||
mConfigured = Homie.isConfigured();
|
mConfigured = Homie.isConfigured();
|
||||||
@ -551,6 +614,12 @@ void systemInit(){
|
|||||||
|
|
||||||
bool mode1(){
|
bool mode1(){
|
||||||
Serial.println("m1");
|
Serial.println("m1");
|
||||||
|
Serial << getCurrentTime() << " curtime" << endl;
|
||||||
|
|
||||||
|
if(rtcDeepSleepTime > 0){
|
||||||
|
esp_sleep_enable_timer_wakeup( (rtcDeepSleepTime * 1000U) );
|
||||||
|
}
|
||||||
|
|
||||||
readSensors();
|
readSensors();
|
||||||
//queue sensor values for
|
//queue sensor values for
|
||||||
|
|
||||||
@ -598,7 +667,16 @@ bool mode1(){
|
|||||||
}
|
}
|
||||||
//check how long it was already in mode1 if to long goto mode2
|
//check how long it was already in mode1 if to long goto mode2
|
||||||
|
|
||||||
//TODO evaluate if something is to do
|
long cTime = getCurrentTime();
|
||||||
|
if(cTime < 100000){
|
||||||
|
Serial.println("Starting mode 2 due to missing ntp");
|
||||||
|
//missing ntp time boot to mode3
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(gotoMode2AfterThisTimestamp < cTime){
|
||||||
|
Serial.println("Starting mode 2 after specified mode1 time");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -636,6 +714,7 @@ void setup() {
|
|||||||
}
|
}
|
||||||
/* read button */
|
/* read button */
|
||||||
pinMode(BUTTON, INPUT);
|
pinMode(BUTTON, INPUT);
|
||||||
|
pinMode(OUTPUT_PUMP, OUTPUT);
|
||||||
|
|
||||||
/* Disable Wifi and bluetooth */
|
/* Disable Wifi and bluetooth */
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
@ -652,26 +731,12 @@ void setup() {
|
|||||||
|
|
||||||
// Big TODO use here the settings in RTC_Memory
|
// Big TODO use here the settings in RTC_Memory
|
||||||
|
|
||||||
// Configure Deep Sleep:
|
//Panik mode, the Lipo is empty, sleep a long long time:
|
||||||
if (mConfigured && (deepSleepNightTime.get() > 0) &&
|
if ((getBatteryVoltage() < MINIMUM_LIPO_VOLT) &&
|
||||||
( SOLAR_VOLT(solarSensor) < MINIMUM_SOLAR_VOLT)) {
|
(getBatteryVoltage() > NO_LIPO_VOLT)) {
|
||||||
Serial << deepSleepNightTime.get() << "ms ds " << SOLAR_VOLT(solarSensor) << "V" << endl;
|
Serial << PANIK_MODE_DEEPSLEEP << " s lipo " << getBatteryVoltage() << "V" << endl;
|
||||||
uint64_t usSleepTime = deepSleepNightTime.get() * 1000U;
|
esp_sleep_enable_timer_wakeup(PANIK_MODE_DEEPSLEEP_US);
|
||||||
esp_sleep_enable_timer_wakeup(usSleepTime);
|
esp_deep_sleep_start();
|
||||||
}else if (mConfigured && deepSleepTime.get()) {
|
|
||||||
Serial << deepSleepTime.get() << " ms ds" << endl;
|
|
||||||
uint64_t usSleepTime = deepSleepTime.get() * 1000U;
|
|
||||||
esp_sleep_enable_timer_wakeup(usSleepTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mConfigured &&
|
|
||||||
(ADC_5V_TO_3V3(lipoSenor) < MINIMUM_LIPO_VOLT) &&
|
|
||||||
(ADC_5V_TO_3V3(lipoSenor) > NO_LIPO_VOLT) &&
|
|
||||||
(deepSleepTime.get()) ) {
|
|
||||||
long sleepEmptyLipo = (deepSleepTime.get() * EMPTY_LIPO_MULTIPL);
|
|
||||||
Serial << sleepEmptyLipo << " ms lipo " << ADC_5V_TO_3V3(lipoSenor) << "V" << endl;
|
|
||||||
esp_sleep_enable_timer_wakeup(sleepEmptyLipo * 1000U);
|
|
||||||
mDeepSleep = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mode1()){
|
if(mode1()){
|
||||||
@ -692,7 +757,7 @@ void loop() {
|
|||||||
Homie.loop();
|
Homie.loop();
|
||||||
|
|
||||||
if(millis() > 30000 && !mode3Active){
|
if(millis() > 30000 && !mode3Active){
|
||||||
Serial << (millis()/ 1000) << " ds watchdog" << endl;
|
Serial << (millis()/ 1000) << "s alive" << endl;
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user