2020-09-07 18:18:46 +02:00
|
|
|
/**
|
|
|
|
* @file main.cpp
|
|
|
|
* @author Ollo
|
|
|
|
* @brief PlantControl
|
|
|
|
* @version 0.1
|
|
|
|
* @date 2020-05-01
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "PlantCtrl.h"
|
|
|
|
#include "ControllerConfiguration.h"
|
2020-10-16 16:22:48 +02:00
|
|
|
#include "HomieConfiguration.h"
|
2020-09-07 18:18:46 +02:00
|
|
|
#include "DS18B20.h"
|
|
|
|
#include <Homie.h>
|
2020-10-19 01:39:56 +02:00
|
|
|
#include "time.h"
|
2020-09-20 19:18:47 +02:00
|
|
|
#include "esp_sleep.h"
|
2020-10-16 19:26:05 +02:00
|
|
|
#include "RunningMedian.h"
|
2020-10-20 18:40:24 +02:00
|
|
|
#include <arduino-timer.h>
|
2020-10-21 16:43:26 +02:00
|
|
|
#include <stdint.h>
|
2020-09-07 18:18:46 +02:00
|
|
|
|
|
|
|
const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minutes */
|
|
|
|
|
|
|
|
#define AMOUNT_SENOR_QUERYS 8
|
|
|
|
#define SENSOR_QUERY_SHIFTS 3
|
|
|
|
#define SOLAR4SENSORS 6.0f
|
|
|
|
#define TEMP_INIT_VALUE -999.0f
|
|
|
|
#define TEMP_MAX_VALUE 85.0f
|
2020-11-01 14:30:26 +01:00
|
|
|
#define HalfHour 60
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-16 21:12:25 +02:00
|
|
|
/********************* non volatile enable after deepsleep *******************************/
|
|
|
|
|
2020-10-21 19:50:05 +02:00
|
|
|
RTC_DATA_ATTR long gotoMode2AfterThisTimestamp = 0;
|
2020-10-16 21:12:25 +02:00
|
|
|
RTC_DATA_ATTR long rtcDeepSleepTime = 0; /**< Time, when the microcontroller shall be up again */
|
2020-10-19 01:39:56 +02:00
|
|
|
RTC_DATA_ATTR long rtcLastActive0 = 0;
|
2020-10-16 21:12:25 +02:00
|
|
|
RTC_DATA_ATTR long rtcMoistureTrigger0 = 0; /**<Level for the moisture sensor */
|
2020-10-19 01:39:56 +02:00
|
|
|
RTC_DATA_ATTR long rtcLastActive1 = 0;
|
2020-10-16 21:12:25 +02:00
|
|
|
RTC_DATA_ATTR long rtcMoistureTrigger1 = 0; /**<Level for the moisture sensor */
|
2020-10-19 01:39:56 +02:00
|
|
|
RTC_DATA_ATTR long rtcLastActive2 = 0;
|
2020-10-16 21:12:25 +02:00
|
|
|
RTC_DATA_ATTR long rtcMoistureTrigger2 = 0; /**<Level for the moisture sensor */
|
2020-10-19 01:39:56 +02:00
|
|
|
RTC_DATA_ATTR long rtcLastActive3 = 0;
|
2020-10-16 21:12:25 +02:00
|
|
|
RTC_DATA_ATTR long rtcMoistureTrigger3 = 0; /**<Level for the moisture sensor */
|
2020-10-19 01:39:56 +02:00
|
|
|
RTC_DATA_ATTR long rtcLastActive4 = 0;
|
2020-10-16 21:12:25 +02:00
|
|
|
RTC_DATA_ATTR long rtcMoistureTrigger4 = 0; /**<Level for the moisture sensor */
|
2020-10-19 01:39:56 +02:00
|
|
|
RTC_DATA_ATTR long rtcLastActive5 = 0;
|
2020-10-16 21:12:25 +02:00
|
|
|
RTC_DATA_ATTR long rtcMoistureTrigger5 = 0; /**<Level for the moisture sensor */
|
2020-10-19 01:39:56 +02:00
|
|
|
RTC_DATA_ATTR long rtcLastActive6 = 0;
|
2020-10-16 21:12:25 +02:00
|
|
|
RTC_DATA_ATTR long rtcMoistureTrigger6 = 0; /**<Level for the moisture sensor */
|
2020-10-19 01:39:56 +02:00
|
|
|
RTC_DATA_ATTR int lastPumpRunning = 0;
|
|
|
|
RTC_DATA_ATTR long lastWaterValue = 0;
|
|
|
|
|
2020-10-16 20:36:07 +02:00
|
|
|
bool warmBoot = true;
|
2020-10-31 03:51:35 +01:00
|
|
|
bool volatile mode3Active = false; /**< Controller must not sleep */
|
|
|
|
bool volatile mDeepsleep = false;
|
2020-10-16 19:26:05 +02:00
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
int plantSensor1 = 0;
|
|
|
|
|
2020-09-20 19:18:47 +02:00
|
|
|
int mWaterGone = -1; /**< Amount of centimeter, where no water is seen */
|
2020-09-07 18:18:46 +02:00
|
|
|
int readCounter = 0;
|
2020-09-21 20:42:24 +02:00
|
|
|
bool mConfigured = false;
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-20 18:40:24 +02:00
|
|
|
|
|
|
|
auto wait4sleep = timer_create_default(); // create a timer with default settings
|
|
|
|
|
2020-09-20 19:18:47 +02:00
|
|
|
RTC_DATA_ATTR int gBootCount = 0;
|
2020-09-21 19:40:01 +02:00
|
|
|
RTC_DATA_ATTR int gCurrentPlant = 0; /**< Value Range: 1 ... 7 (0: no plant needs water) */
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-16 19:26:05 +02:00
|
|
|
RunningMedian lipoRawSensor = RunningMedian(5);
|
|
|
|
RunningMedian solarRawSensor = RunningMedian(5);
|
|
|
|
RunningMedian waterRawSensor = RunningMedian(5);
|
|
|
|
RunningMedian temp1 = RunningMedian(5);
|
|
|
|
RunningMedian temp2 = RunningMedian(5);
|
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
|
|
|
|
Ds18B20 dallas(SENSOR_DS18B20);
|
|
|
|
|
|
|
|
Plant mPlants[MAX_PLANTS] = {
|
2020-10-16 16:22:48 +02:00
|
|
|
Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0, &plant0, &mSetting0),
|
|
|
|
Plant(SENSOR_PLANT1, OUTPUT_PUMP1, 1, &plant1, &mSetting1),
|
|
|
|
Plant(SENSOR_PLANT2, OUTPUT_PUMP2, 2, &plant2, &mSetting2),
|
|
|
|
Plant(SENSOR_PLANT3, OUTPUT_PUMP3, 3, &plant3, &mSetting3),
|
|
|
|
Plant(SENSOR_PLANT4, OUTPUT_PUMP4, 4, &plant4, &mSetting4),
|
|
|
|
Plant(SENSOR_PLANT5, OUTPUT_PUMP5, 5, &plant5, &mSetting5),
|
|
|
|
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6, &plant6, &mSetting6)
|
2020-09-07 18:18:46 +02:00
|
|
|
};
|
|
|
|
|
2020-10-21 20:03:12 +02:00
|
|
|
float getBatteryVoltage(){
|
|
|
|
return ADC_5V_TO_3V3(lipoRawSensor.getAverage());
|
|
|
|
}
|
|
|
|
|
|
|
|
float getSolarVoltage(){
|
|
|
|
return SOLAR_VOLT(solarRawSensor.getAverage());
|
|
|
|
}
|
|
|
|
|
2020-10-31 19:25:13 +01:00
|
|
|
void setMoistureTrigger(int plantId, long value){
|
|
|
|
if(plantId == 0){
|
|
|
|
rtcMoistureTrigger0 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 1){
|
|
|
|
rtcMoistureTrigger1 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 2){
|
|
|
|
rtcMoistureTrigger2 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 3){
|
|
|
|
rtcMoistureTrigger3 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 4){
|
|
|
|
rtcMoistureTrigger4 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 5){
|
|
|
|
rtcMoistureTrigger5 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 6){
|
|
|
|
rtcMoistureTrigger6 = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 19:26:05 +02:00
|
|
|
void readSystemSensors() {
|
|
|
|
lipoRawSensor.add(analogRead(SENSOR_LIPO));
|
2020-10-21 18:21:44 +02:00
|
|
|
solarRawSensor.add(analogRead(SENSOR_SOLAR));
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 18:06:37 +02:00
|
|
|
int determineNextPump();
|
|
|
|
void setLastActivationForPump(int pumpId, long time);
|
|
|
|
|
|
|
|
|
|
|
|
long getCurrentTime(){
|
2020-10-21 19:50:05 +02:00
|
|
|
struct timeval tv_now;
|
|
|
|
gettimeofday(&tv_now, NULL);
|
|
|
|
return tv_now.tv_sec;
|
2020-10-20 18:06:37 +02:00
|
|
|
}
|
|
|
|
|
2020-10-23 16:47:40 +02:00
|
|
|
void espDeepSleepFor(long seconds, bool activatePump = false){
|
2020-10-31 03:51:35 +01:00
|
|
|
if(mode3Active){
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial << "abort deepsleep, mode3Active" << endl;
|
2020-10-31 03:51:35 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-10-31 19:25:13 +01:00
|
|
|
for(int i = 0;i<10;i++){
|
|
|
|
long cTime = getCurrentTime();
|
|
|
|
if(cTime < 100000){
|
|
|
|
Serial << "Wait for ntp" << endl;
|
|
|
|
delay(100);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 20:00:24 +01:00
|
|
|
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL,ESP_PD_OPTION_ON);
|
2020-10-31 19:25:13 +01:00
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
|
2020-10-23 16:47:40 +02:00
|
|
|
if (activatePump) {
|
2020-10-28 20:00:24 +01:00
|
|
|
gpio_deep_sleep_hold_en();
|
2020-10-23 16:47:40 +02:00
|
|
|
gpio_hold_en(GPIO_NUM_13); //pump pwr
|
2020-10-23 19:32:12 +02:00
|
|
|
} else {
|
2020-10-28 20:00:24 +01:00
|
|
|
gpio_hold_dis(GPIO_NUM_13); //pump pwr
|
2020-10-28 20:14:52 +01:00
|
|
|
gpio_deep_sleep_hold_dis();
|
2020-10-23 19:32:12 +02:00
|
|
|
digitalWrite(OUTPUT_PUMP, LOW);
|
|
|
|
for (int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
mPlants[i].deactivatePump();
|
|
|
|
}
|
2020-10-23 16:47:40 +02:00
|
|
|
}
|
2020-10-21 20:46:09 +02:00
|
|
|
//gpio_hold_en(GPIO_NUM_23); //p0
|
|
|
|
//FIXME fix for outher outputs
|
|
|
|
|
|
|
|
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial.print("Trying to sleep for ");
|
2020-10-21 19:50:05 +02:00
|
|
|
Serial.print(seconds);
|
|
|
|
Serial.println(" seconds");
|
|
|
|
esp_sleep_enable_timer_wakeup( (seconds * 1000U * 1000U) );
|
2020-10-31 03:51:35 +01:00
|
|
|
mDeepsleep=true;
|
2020-10-21 19:50:05 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 20:03:12 +02:00
|
|
|
|
|
|
|
|
2020-10-20 18:06:37 +02:00
|
|
|
void mode2MQTT(){
|
2020-10-21 18:21:44 +02:00
|
|
|
readSystemSensors();
|
|
|
|
|
2020-10-21 19:50:05 +02:00
|
|
|
digitalWrite(OUTPUT_PUMP, LOW);
|
2020-10-23 19:32:12 +02:00
|
|
|
for (int i=0; i < MAX_PLANTS; i++) {
|
2020-10-23 16:20:34 +02:00
|
|
|
mPlants[i].deactivatePump();
|
2020-10-21 19:50:05 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 20:46:09 +02:00
|
|
|
if (deepSleepTime.get()) {
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial << "deepsleep time is configured to " << deepSleepTime.get() << endl;
|
2020-10-21 20:46:09 +02:00
|
|
|
}
|
|
|
|
/* Publish default values */
|
2020-10-20 18:06:37 +02:00
|
|
|
|
|
|
|
if(lastPumpRunning != -1){
|
|
|
|
long waterDiff = mWaterGone-lastWaterValue;
|
|
|
|
//TODO attribute used water in ml to plantid
|
|
|
|
}
|
2020-10-21 20:46:09 +02:00
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 ));
|
|
|
|
}
|
2020-10-20 18:06:37 +02:00
|
|
|
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - mWaterGone ));
|
2020-10-20 21:35:28 +02:00
|
|
|
Serial << "W : " << mWaterGone << " cm (" << String(waterLevelMax.get() - mWaterGone ) << "%)" << endl;
|
2020-10-20 18:06:37 +02:00
|
|
|
lastWaterValue = mWaterGone;
|
|
|
|
|
2020-10-21 18:21:44 +02:00
|
|
|
sensorLipo.setProperty("percent").send( String(100 * lipoRawSensor.getAverage() / 4095) );
|
2020-10-21 20:03:12 +02:00
|
|
|
sensorLipo.setProperty("volt").send( String(getBatteryVoltage()) );
|
2020-10-21 18:21:44 +02:00
|
|
|
sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage() ) / 4095));
|
2020-10-21 20:03:12 +02:00
|
|
|
sensorSolar.setProperty("volt").send( String(getSolarVoltage()) );
|
2020-10-20 18:06:37 +02:00
|
|
|
|
2020-11-01 14:30:26 +01:00
|
|
|
|
2020-10-31 03:51:35 +01:00
|
|
|
float t1 = temp1.getMedian();
|
2020-11-01 14:30:26 +01:00
|
|
|
if (t1 != NAN) {
|
|
|
|
sensorTemp.setProperty("control").send( String(t1));
|
|
|
|
}
|
2020-10-31 03:51:35 +01:00
|
|
|
float t2 = temp2.getMedian();
|
2020-11-01 14:30:26 +01:00
|
|
|
if (t2 != NAN) {
|
|
|
|
sensorTemp.setProperty("temp").send( String(t2));
|
|
|
|
}
|
2020-10-20 18:06:37 +02:00
|
|
|
|
2020-10-31 19:25:13 +01:00
|
|
|
//give mqtt time, use via publish callback instead?
|
|
|
|
delay(100);
|
|
|
|
|
2020-10-31 03:51:35 +01:00
|
|
|
bool lipoTempWarning = t1!=85 && t2!=85 && abs(t1 - t2) > 10;
|
2020-10-20 18:06:37 +02:00
|
|
|
if(lipoTempWarning){
|
2020-10-31 03:51:35 +01:00
|
|
|
Serial.println("Lipo temp incorrect, panic mode deepsleep TODO");
|
|
|
|
//espDeepSleepFor(PANIK_MODE_DEEPSLEEP);
|
|
|
|
//return;
|
2020-10-20 18:06:37 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 19:25:13 +01:00
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
setMoistureTrigger(i, mPlants[i].mSetting->pSensorDry->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-21 20:46:09 +02:00
|
|
|
bool hasWater = true;//FIXMEmWaterGone > waterLevelMin.get();
|
2020-10-21 19:50:05 +02:00
|
|
|
//FIXME no water warning message
|
2020-10-20 18:06:37 +02:00
|
|
|
lastPumpRunning = determineNextPump();
|
2020-10-21 20:46:09 +02:00
|
|
|
if(lastPumpRunning != -1 && !hasWater){
|
|
|
|
Serial.println("Want to pump but no water");
|
|
|
|
}
|
2020-10-21 19:50:05 +02:00
|
|
|
if(lastPumpRunning != -1 && hasWater){
|
2020-10-21 20:46:09 +02:00
|
|
|
digitalWrite(OUTPUT_PUMP, HIGH);
|
2020-10-20 18:06:37 +02:00
|
|
|
setLastActivationForPump(lastPumpRunning, getCurrentTime());
|
2020-10-23 16:20:34 +02:00
|
|
|
mPlants[lastPumpRunning].activatePump();
|
2020-10-20 18:06:37 +02:00
|
|
|
}
|
2020-10-21 19:50:05 +02:00
|
|
|
if(lastPumpRunning == -1 || !hasWater){
|
2020-10-21 20:03:12 +02:00
|
|
|
if(getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE){
|
2020-10-21 19:50:05 +02:00
|
|
|
gotoMode2AfterThisTimestamp = getCurrentTime()+deepSleepNightTime.get();
|
|
|
|
Serial.println("No pumps to activate and low light, deepSleepNight");
|
|
|
|
espDeepSleepFor(deepSleepNightTime.get());
|
2020-10-31 19:25:13 +01:00
|
|
|
rtcDeepSleepTime = deepSleepNightTime.get();
|
2020-10-21 19:50:05 +02:00
|
|
|
}else {
|
|
|
|
gotoMode2AfterThisTimestamp = getCurrentTime()+deepSleepTime.get();
|
|
|
|
Serial.println("No pumps to activate, deepSleep");
|
|
|
|
espDeepSleepFor(deepSleepTime.get());
|
2020-10-31 19:25:13 +01:00
|
|
|
rtcDeepSleepTime = deepSleepTime.get();
|
2020-10-21 19:50:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}else {
|
|
|
|
gotoMode2AfterThisTimestamp = 0;
|
|
|
|
Serial.println("Running pump, watering deepsleep");
|
2020-10-23 16:47:40 +02:00
|
|
|
espDeepSleepFor(wateringDeepSleep.get(), true);
|
2020-10-21 19:50:05 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 18:06:37 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 19:25:13 +01:00
|
|
|
long getMoistureTrigger(int plantId){
|
2020-10-19 01:39:56 +02:00
|
|
|
if(plantId == 0){
|
2020-10-31 19:25:13 +01:00
|
|
|
return rtcMoistureTrigger0;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 1){
|
2020-10-31 19:25:13 +01:00
|
|
|
return rtcMoistureTrigger1;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 2){
|
2020-10-31 19:25:13 +01:00
|
|
|
return rtcMoistureTrigger2;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 3){
|
2020-10-31 19:25:13 +01:00
|
|
|
return rtcMoistureTrigger3;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 4){
|
2020-10-31 19:25:13 +01:00
|
|
|
return rtcMoistureTrigger4;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 5){
|
2020-10-31 19:25:13 +01:00
|
|
|
return rtcMoistureTrigger5;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 6){
|
2020-10-31 19:25:13 +01:00
|
|
|
return rtcMoistureTrigger6;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
2020-10-31 19:25:13 +01:00
|
|
|
return -1;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 19:25:13 +01:00
|
|
|
|
|
|
|
|
2020-10-19 01:39:56 +02:00
|
|
|
void setLastActivationForPump(int plantId, long value){
|
|
|
|
if(plantId == 0){
|
|
|
|
rtcLastActive0 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 1){
|
|
|
|
rtcLastActive1 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 2){
|
|
|
|
rtcLastActive2 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 3){
|
|
|
|
rtcLastActive3 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 4){
|
|
|
|
rtcLastActive4 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 5){
|
|
|
|
rtcLastActive5 = value;
|
|
|
|
}
|
|
|
|
if(plantId == 6){
|
|
|
|
rtcLastActive6 = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 18:06:37 +02:00
|
|
|
long getLastActivationForPump(int plantId){
|
2020-10-19 01:39:56 +02:00
|
|
|
if(plantId == 0){
|
2020-10-20 18:06:37 +02:00
|
|
|
return rtcLastActive0;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 1){
|
2020-10-20 18:06:37 +02:00
|
|
|
return rtcLastActive1;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 2){
|
2020-10-20 18:06:37 +02:00
|
|
|
return rtcLastActive2;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 3){
|
2020-10-20 18:06:37 +02:00
|
|
|
return rtcLastActive3;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 4){
|
2020-10-20 18:06:37 +02:00
|
|
|
return rtcLastActive4;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 5){
|
2020-10-20 18:06:37 +02:00
|
|
|
return rtcLastActive5;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
if(plantId == 6){
|
2020-10-20 18:06:37 +02:00
|
|
|
return rtcLastActive6;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-16 19:26:05 +02:00
|
|
|
/**
|
|
|
|
* @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
|
|
|
|
* These sensors (ADC2) can only be read when no Wifi is used.
|
|
|
|
*/
|
|
|
|
void readSensors() {
|
2020-10-21 18:21:44 +02:00
|
|
|
Serial << "Read Sensors" << endl;
|
|
|
|
|
|
|
|
readSystemSensors();
|
2020-10-16 19:26:05 +02:00
|
|
|
|
|
|
|
/* activate all sensors */
|
|
|
|
pinMode(OUTPUT_SENSOR, OUTPUT);
|
|
|
|
digitalWrite(OUTPUT_SENSOR, HIGH);
|
|
|
|
|
|
|
|
delay(100);
|
|
|
|
/* wait before reading something */
|
|
|
|
for (int readCnt=0;readCnt < AMOUNT_SENOR_QUERYS; readCnt++) {
|
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
2020-10-23 16:20:34 +02:00
|
|
|
mPlants[i].addSenseValue();
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 18:14:51 +02:00
|
|
|
Serial << "DS18B20" << endl;
|
2020-10-16 19:26:05 +02:00
|
|
|
/* Read the temperature sensors once, as first time 85 degree is returned */
|
2020-10-20 21:35:28 +02:00
|
|
|
Serial << "DS18B20" << String(dallas.readDevices()) << endl;
|
2020-10-16 19:26:05 +02:00
|
|
|
delay(200);
|
|
|
|
|
|
|
|
|
|
|
|
/* Required to read the temperature once */
|
2020-11-01 14:30:26 +01:00
|
|
|
float temp[2] = {TEMP_MAX_VALUE, TEMP_MAX_VALUE};
|
2020-10-16 19:26:05 +02:00
|
|
|
float* pFloat = temp;
|
2020-11-01 14:30:26 +01:00
|
|
|
int sensors = dallas.readAllTemperatures(pFloat, 2);
|
|
|
|
if (sensors > 0) {
|
2020-10-31 03:51:35 +01:00
|
|
|
Serial << "t1: " << String(temp[0]) << endl;
|
2020-11-01 14:30:26 +01:00
|
|
|
temp1.add(temp[0]);
|
|
|
|
}
|
|
|
|
if (sensors > 1) {
|
2020-10-31 03:51:35 +01:00
|
|
|
Serial << "t2: " << String(temp[1]) << endl;
|
2020-11-01 14:30:26 +01:00
|
|
|
temp2.add(temp[1]);
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
2020-11-01 14:30:26 +01:00
|
|
|
|
2020-10-16 19:26:05 +02:00
|
|
|
|
|
|
|
/* Use the Ultrasonic sensor to measure waterLevel */
|
|
|
|
|
|
|
|
digitalWrite(SENSOR_SR04_TRIG, LOW);
|
|
|
|
delayMicroseconds(2);
|
|
|
|
digitalWrite(SENSOR_SR04_TRIG, HIGH);
|
|
|
|
delayMicroseconds(10);
|
|
|
|
digitalWrite(SENSOR_SR04_TRIG, LOW);
|
|
|
|
float duration = pulseIn(SENSOR_SR04_ECHO, HIGH);
|
|
|
|
waterRawSensor.add((duration*.343)/2);
|
|
|
|
/* deactivate the sensors */
|
|
|
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 01:39:56 +02:00
|
|
|
//Homie.getMqttClient().disconnect();
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-19 01:39:56 +02:00
|
|
|
void onHomieEvent(const HomieEvent& event) {
|
|
|
|
switch(event.type) {
|
2020-10-21 18:14:51 +02:00
|
|
|
case HomieEventType::SENDING_STATISTICS:
|
|
|
|
Homie.getLogger() << "My statistics" << endl;
|
|
|
|
break;
|
2020-10-19 01:39:56 +02:00
|
|
|
case HomieEventType::MQTT_READY:
|
2020-11-01 20:29:24 +01:00
|
|
|
Serial.printf("NTP Setup with server %s\r\n", ntpServer.get());
|
|
|
|
configTime(0, 0, ntpServer.get());
|
2020-10-21 18:14:51 +02:00
|
|
|
//wait for rtc sync?
|
|
|
|
rtcDeepSleepTime = deepSleepTime.get();
|
2020-11-01 20:29:24 +01:00
|
|
|
Serial << "Setup plants" << endl;
|
2020-10-23 16:20:34 +02:00
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
mPlants[i].postMQTTconnection();
|
|
|
|
}
|
2020-10-28 20:31:18 +01:00
|
|
|
|
|
|
|
mode2MQTT();
|
2020-10-19 01:39:56 +02:00
|
|
|
break;
|
|
|
|
case HomieEventType::READY_TO_SLEEP:
|
2020-10-20 21:35:28 +02:00
|
|
|
Homie.getLogger() << "rtsleep" << endl;
|
2020-10-19 01:39:56 +02:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
break;
|
2020-10-21 16:43:26 +02:00
|
|
|
case HomieEventType::OTA_STARTED:
|
2020-11-01 14:30:26 +01:00
|
|
|
Homie.getLogger() << "OTA started" << endl;
|
2020-10-21 16:57:57 +02:00
|
|
|
digitalWrite(OUTPUT_SENSOR, HIGH);
|
2020-11-01 14:30:26 +01:00
|
|
|
digitalWrite(OUTPUT_PUMP, HIGH);
|
2020-10-28 20:31:18 +01:00
|
|
|
gpio_hold_dis(GPIO_NUM_13); //pump pwr
|
|
|
|
gpio_deep_sleep_hold_dis();
|
|
|
|
for (int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
mPlants[i].deactivatePump();
|
|
|
|
}
|
2020-10-21 16:43:26 +02:00
|
|
|
mode3Active=true;
|
|
|
|
break;
|
|
|
|
case HomieEventType::OTA_SUCCESSFUL:
|
2020-11-01 14:30:26 +01:00
|
|
|
Homie.getLogger() << "OTA successfull" << endl;
|
2020-10-21 16:57:57 +02:00
|
|
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
2020-10-23 16:32:05 +02:00
|
|
|
digitalWrite(OUTPUT_PUMP, LOW);
|
2020-10-28 20:31:18 +01:00
|
|
|
ESP.restart();
|
2020-10-21 16:43:26 +02:00
|
|
|
break;
|
2020-10-21 19:50:05 +02:00
|
|
|
default:
|
|
|
|
break;
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-19 01:39:56 +02:00
|
|
|
int determineNextPump(){
|
2020-10-21 20:03:12 +02:00
|
|
|
float solarValue = getSolarVoltage();
|
2020-10-21 20:46:09 +02:00
|
|
|
bool isLowLight =(solarValue > SOLAR_CHARGE_MIN_VOLTAGE || solarValue < SOLAR_CHARGE_MAX_VOLTAGE);
|
2020-10-19 01:39:56 +02:00
|
|
|
|
|
|
|
//FIXME instead of for, use sorted by last activation index to ensure equal runtime?
|
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
long lastActivation = getLastActivationForPump(i);
|
|
|
|
long sinceLastActivation = getCurrentTime()-lastActivation;
|
|
|
|
//this pump is in cooldown skip it and disable low power mode trigger for it
|
2020-10-23 16:20:34 +02:00
|
|
|
if(mPlants[i].isInCooldown(sinceLastActivation) ){
|
2020-10-23 16:32:05 +02:00
|
|
|
Serial.printf("%d Skipping due to cooldown\r\n", i);
|
|
|
|
setMoistureTrigger(i, DEACTIVATED_PLANT);
|
|
|
|
continue;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
//skip as it is not low light
|
2020-10-23 16:20:34 +02:00
|
|
|
if(!isLowLight && mPlants[i].isAllowedOnlyAtLowLight()){
|
2020-10-21 20:46:09 +02:00
|
|
|
Serial.println("Skipping due to light");
|
2020-10-19 01:39:56 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mPlants->isPumpRequired()){
|
2020-10-23 16:32:05 +02:00
|
|
|
Serial.printf("%d Requested pumping\r\n", i);
|
2020-10-19 01:39:56 +02:00
|
|
|
return i;
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
2020-10-23 19:32:12 +02:00
|
|
|
Serial.printf("%d No pump required\r\n", i);
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
2020-10-19 01:39:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-13 20:16:28 +02:00
|
|
|
/**
|
|
|
|
* @brief Handle Mqtt commands to keep controller alive
|
|
|
|
*
|
|
|
|
* @param range multiple transmitted values (not used for this function)
|
|
|
|
* @param value single value
|
|
|
|
* @return true when the command was parsed and executed succuessfully
|
|
|
|
* @return false on errors when parsing the request
|
|
|
|
*/
|
|
|
|
bool aliveHandler(const HomieRange& range, const String& value) {
|
|
|
|
if (range.isRange) return false; // only one controller is present
|
|
|
|
if (value.equals("ON") || value.equals("On") || value.equals("1")) {
|
2020-10-16 21:50:42 +02:00
|
|
|
mode3Active=true;
|
2020-10-13 20:16:28 +02:00
|
|
|
} else {
|
2020-10-16 21:50:42 +02:00
|
|
|
mode3Active=false;
|
2020-10-13 20:16:28 +02:00
|
|
|
}
|
2020-10-21 16:43:26 +02:00
|
|
|
|
2020-10-13 20:16:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-20 20:12:27 +02:00
|
|
|
void homieLoop(){
|
2020-11-01 20:29:24 +01:00
|
|
|
|
2020-10-20 20:12:27 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 20:36:07 +02:00
|
|
|
void systemInit(){
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
|
|
|
Homie_setFirmware("PlantControl", FIRMWARE_VERSION);
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-16 20:36:07 +02:00
|
|
|
// Set default values
|
2020-10-21 19:50:05 +02:00
|
|
|
|
|
|
|
//in seconds
|
2020-10-31 12:44:49 +01:00
|
|
|
deepSleepTime.setDefaultValue(60);
|
|
|
|
deepSleepNightTime.setDefaultValue(600);
|
2020-10-21 19:50:05 +02:00
|
|
|
wateringDeepSleep.setDefaultValue(5);
|
2020-11-01 20:17:21 +01:00
|
|
|
ntpServer.setDefaultValue("pool.ntp.org");
|
2020-10-21 19:50:05 +02:00
|
|
|
|
2020-10-28 21:06:34 +01:00
|
|
|
/* waterLevelMax 1000 */ /* 100cm in mm */
|
2020-10-16 18:40:39 +02:00
|
|
|
waterLevelMin.setDefaultValue(50); /* 5cm in mm */
|
|
|
|
waterLevelWarn.setDefaultValue(500); /* 50cm in mm */
|
|
|
|
waterLevelVol.setDefaultValue(5000); /* 5l in ml */
|
2020-09-21 20:42:24 +02:00
|
|
|
|
2020-10-20 20:12:27 +02:00
|
|
|
Homie.setLoopFunction(homieLoop);
|
2020-10-21 18:14:51 +02:00
|
|
|
Homie.onEvent(onHomieEvent);
|
2020-10-31 19:25:13 +01:00
|
|
|
//Homie.disableLogging();
|
2020-10-20 20:12:27 +02:00
|
|
|
Homie.setup();
|
|
|
|
|
2020-10-16 21:50:42 +02:00
|
|
|
mConfigured = Homie.isConfigured();
|
2020-10-14 19:01:13 +02:00
|
|
|
if (mConfigured) {
|
2020-10-23 16:20:34 +02:00
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
mPlants[i].advertise();
|
|
|
|
}
|
2020-09-21 20:42:24 +02:00
|
|
|
sensorTemp.advertise("control")
|
|
|
|
.setName("Temperature")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("°C");
|
|
|
|
sensorTemp.advertise("temp")
|
|
|
|
.setName("Temperature")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("°C");
|
|
|
|
|
|
|
|
sensorLipo.advertise("percent")
|
|
|
|
.setName("Percent")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("%");
|
|
|
|
sensorLipo.advertise("volt")
|
|
|
|
.setName("Volt")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("V");
|
|
|
|
|
|
|
|
sensorSolar.advertise("percent")
|
|
|
|
.setName("Percent")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("%");
|
|
|
|
sensorSolar.advertise("volt")
|
|
|
|
.setName("Volt")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("V");
|
|
|
|
sensorWater.advertise("remaining").setDatatype("number").setUnit("%");
|
|
|
|
}
|
2020-10-20 20:57:06 +02:00
|
|
|
stayAlive.advertise("alive").setName("Alive").setDatatype("number").settable(aliveHandler);
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-16 20:36:07 +02:00
|
|
|
bool mode1(){
|
2020-10-31 12:44:49 +01:00
|
|
|
Serial.println("==== Mode 1 ====");
|
2020-10-21 19:50:05 +02:00
|
|
|
Serial << getCurrentTime() << " curtime" << endl;
|
2020-10-21 18:14:51 +02:00
|
|
|
|
2020-10-16 19:26:05 +02:00
|
|
|
readSensors();
|
2020-10-19 01:39:56 +02:00
|
|
|
//queue sensor values for
|
2020-10-16 21:12:25 +02:00
|
|
|
|
2020-10-31 19:25:13 +01:00
|
|
|
if (rtcDeepSleepTime == 0){
|
|
|
|
Serial.println("1 missing rtc value, going to mode2");
|
2020-10-16 21:12:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-01 13:26:38 +01:00
|
|
|
for(int i = 0; i < MAX_PLANTS; i++){
|
2020-10-31 19:25:13 +01:00
|
|
|
long trigger =getMoistureTrigger(i);
|
2020-11-01 13:26:38 +01:00
|
|
|
if (trigger == 0) {
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial << "Missing rtc trigger " << i << endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(trigger == DEACTIVATED_PLANT){
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-01 13:26:38 +01:00
|
|
|
if(mPlants[i].getSensorValue() <= trigger){
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial << "plant dry starting mode 2" << i << endl;
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-16 21:12:25 +02:00
|
|
|
}
|
2020-10-31 19:25:13 +01:00
|
|
|
|
2020-10-19 01:39:56 +02:00
|
|
|
//check how long it was already in mode1 if to long goto mode2
|
2020-10-16 21:12:25 +02:00
|
|
|
|
2020-10-21 19:50:05 +02:00
|
|
|
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;
|
2020-10-31 19:25:13 +01:00
|
|
|
} else {
|
|
|
|
Serial << "Mode2 Timer " << gotoMode2AfterThisTimestamp << " curtime " << cTime << endl;
|
2020-10-21 19:50:05 +02:00
|
|
|
}
|
2020-10-16 20:36:07 +02:00
|
|
|
return false;
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void mode2(){
|
2020-10-31 12:44:49 +01:00
|
|
|
Serial.println("==== Mode 2 ====");
|
2020-10-16 20:36:07 +02:00
|
|
|
systemInit();
|
|
|
|
|
|
|
|
/* Jump into Mode 3, if not configured */
|
|
|
|
if (!mConfigured) {
|
2020-11-01 14:30:26 +01:00
|
|
|
Serial.println("==== Mode 3 ====");
|
2020-10-16 20:36:07 +02:00
|
|
|
mode3Active = true;
|
|
|
|
}
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Startup function
|
|
|
|
* Is called once, the controller is started
|
|
|
|
*/
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
|
|
|
Serial.setTimeout(1000); // Set timeout of 1 second
|
|
|
|
Serial << endl << endl;
|
|
|
|
/* Intialize Plant */
|
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
mPlants[i].init();
|
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
|
|
|
|
/* Intialize inputs and outputs */
|
2020-10-16 19:26:05 +02:00
|
|
|
pinMode(SENSOR_LIPO, ANALOG);
|
|
|
|
pinMode(SENSOR_SOLAR, ANALOG);
|
|
|
|
/* read button */
|
|
|
|
pinMode(BUTTON, INPUT);
|
2020-10-23 16:20:34 +02:00
|
|
|
|
|
|
|
/* Power pins */
|
2020-10-21 20:51:25 +02:00
|
|
|
pinMode(OUTPUT_PUMP, OUTPUT);
|
2020-10-16 21:50:42 +02:00
|
|
|
|
2020-10-16 19:26:05 +02:00
|
|
|
/* Disable Wifi and bluetooth */
|
|
|
|
WiFi.mode(WIFI_OFF);
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-16 19:26:05 +02:00
|
|
|
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS) {
|
2020-10-20 21:35:28 +02:00
|
|
|
//increase the config settings to 50 and the json to 3000
|
|
|
|
Serial << "Limits.hpp" << endl;
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 20:36:07 +02:00
|
|
|
// Big TODO use here the settings in RTC_Memory
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-21 18:33:38 +02:00
|
|
|
//Panik mode, the Lipo is empty, sleep a long long time:
|
2020-10-21 20:03:12 +02:00
|
|
|
if ((getBatteryVoltage() < MINIMUM_LIPO_VOLT) &&
|
|
|
|
(getBatteryVoltage() > NO_LIPO_VOLT)) {
|
|
|
|
Serial << PANIK_MODE_DEEPSLEEP << " s lipo " << getBatteryVoltage() << "V" << endl;
|
2020-10-21 19:50:05 +02:00
|
|
|
esp_sleep_enable_timer_wakeup(PANIK_MODE_DEEPSLEEP_US);
|
2020-10-28 20:00:24 +01:00
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL,ESP_PD_OPTION_ON);
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
|
2020-10-21 19:50:05 +02:00
|
|
|
esp_deep_sleep_start();
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
2020-10-20 18:41:41 +02:00
|
|
|
|
|
|
|
if(mode1()){
|
|
|
|
mode2();
|
|
|
|
} else {
|
2020-10-20 21:35:28 +02:00
|
|
|
Serial.println("nop");
|
2020-10-31 19:25:13 +01:00
|
|
|
espDeepSleepFor(rtcDeepSleepTime);
|
2020-10-20 18:41:41 +02:00
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Cyclic call
|
|
|
|
* Executs the Homie base functionallity or triggers sleeping, if requested.
|
|
|
|
*/
|
2020-10-16 19:26:05 +02:00
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
void loop() {
|
2020-10-31 19:25:13 +01:00
|
|
|
if (!mDeepsleep || mode3Active) {
|
2020-10-23 19:32:12 +02:00
|
|
|
Homie.loop();
|
|
|
|
} else {
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial << "Bye" << endl;
|
|
|
|
Serial.flush();
|
2020-10-23 19:32:12 +02:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
2020-10-16 20:36:07 +02:00
|
|
|
|
|
|
|
if(millis() > 30000 && !mode3Active){
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial << (millis()/ 1000) << "not terminated watchdog putting to sleep" << endl;
|
2020-10-16 20:36:07 +02:00
|
|
|
Serial.flush();
|
2020-10-31 19:25:13 +01:00
|
|
|
espDeepSleepFor(rtcDeepSleepTime);
|
2020-10-16 20:36:07 +02:00
|
|
|
}
|
2020-11-01 20:42:45 +01:00
|
|
|
|
|
|
|
/* Toggel Senor LED to visualize mode 3 */
|
|
|
|
if (mode3Active && (millis() % 100) == 0) {
|
|
|
|
digitalWrite(OUTPUT_SENSOR, ! digitalRead(OUTPUT_SENSOR));
|
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|