2020-11-06 22:19:16 +01:00
|
|
|
/** \addtogroup Controller
|
|
|
|
* @{
|
|
|
|
*
|
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
|
|
|
|
*/
|
2020-12-21 16:12:46 +01:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* INCLUDES
|
|
|
|
******************************************************************************/
|
2020-09-07 18:18:46 +02:00
|
|
|
#include "PlantCtrl.h"
|
|
|
|
#include "ControllerConfiguration.h"
|
2020-10-16 16:22:48 +02:00
|
|
|
#include "HomieConfiguration.h"
|
2020-12-13 17:17:54 +01:00
|
|
|
#include "DallasTemperature.h"
|
2020-09-07 18:18:46 +02:00
|
|
|
#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-11-28 01:45:57 +01:00
|
|
|
#include "WakeReason.h"
|
2020-10-21 16:43:26 +02:00
|
|
|
#include <stdint.h>
|
2020-11-06 21:00:11 +01:00
|
|
|
#include <math.h>
|
2020-12-13 17:17:54 +01:00
|
|
|
#include <OneWire.h>
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-12-21 16:12:46 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* DEFINES
|
|
|
|
******************************************************************************/
|
2020-11-04 21:57:40 +01:00
|
|
|
#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
|
|
|
|
#define HalfHour 60
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-12-21 16:12:46 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* TYPE DEFS
|
|
|
|
******************************************************************************/
|
2020-11-06 21:00:11 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
long lastActive; /**< Timestamp, a pump was activated */
|
|
|
|
long moistTrigger; /**< Trigger value of the moist sensor */
|
|
|
|
long moisture; /**< last measured moist value */
|
2020-11-06 19:15:43 +01:00
|
|
|
|
|
|
|
} rtc_plant_t;
|
|
|
|
|
2020-12-21 16:12:46 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* FUNCTION PROTOTYPES
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
int determineNextPump();
|
|
|
|
void setLastActivationForPump(int pumpId, long time);
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* NON VOLATILE VARIABLES in DEEP SLEEP
|
|
|
|
******************************************************************************/
|
|
|
|
|
2020-11-06 23:29:46 +01:00
|
|
|
RTC_DATA_ATTR rtc_plant_t rtcPlant[MAX_PLANTS];
|
2020-11-06 21:00:11 +01:00
|
|
|
RTC_DATA_ATTR long gotoMode2AfterThisTimestamp = 0;
|
|
|
|
RTC_DATA_ATTR long rtcDeepSleepTime = 0; /**< Time, when the microcontroller shall be up again */
|
|
|
|
RTC_DATA_ATTR int lastPumpRunning = 0;
|
|
|
|
RTC_DATA_ATTR long lastWaterValue = 0;
|
2020-12-13 17:17:54 +01:00
|
|
|
RTC_DATA_ATTR float rtcLastLipoTemp = 0.0f;
|
|
|
|
RTC_DATA_ATTR float rtcLastWaterTemp = 0.0f;
|
2020-11-28 01:45:57 +01:00
|
|
|
RTC_DATA_ATTR float rtcLastBatteryVoltage = 0.0f;
|
|
|
|
RTC_DATA_ATTR float rtcLastSolarVoltage = 0.0f;
|
2020-11-06 21:00:11 +01:00
|
|
|
RTC_DATA_ATTR int gBootCount = 0;
|
|
|
|
RTC_DATA_ATTR int gCurrentPlant = 0; /**< Value Range: 1 ... 7 (0: no plant needs water) */
|
2020-12-13 17:17:54 +01:00
|
|
|
RTC_DATA_ATTR int rtcLipoTempIndex = -1;
|
|
|
|
RTC_DATA_ATTR int rtcWaterTempIndex = -1;
|
2020-10-19 01:39:56 +02:00
|
|
|
|
2020-12-21 16:12:46 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* LOCAL VARIABLES
|
|
|
|
******************************************************************************/
|
|
|
|
const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minutes */
|
|
|
|
|
2020-11-28 01:45:57 +01:00
|
|
|
int wakeUpReason = WAKEUP_REASON_UNDEFINED;
|
2020-11-04 21:57:40 +01:00
|
|
|
bool volatile mode3Active = false; /**< Controller must not sleep */
|
2020-10-31 03:51:35 +01:00
|
|
|
bool volatile mDeepsleep = false;
|
2020-10-16 19:26:05 +02:00
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
int readCounter = 0;
|
2020-09-21 20:42:24 +02:00
|
|
|
bool mConfigured = false;
|
2020-12-21 16:12:46 +01:00
|
|
|
long nextBlink = 0; /**< Time needed in main loop to support expected blink code */
|
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);
|
2020-12-13 17:17:54 +01:00
|
|
|
RunningMedian lipoTempSensor = RunningMedian(5);
|
|
|
|
RunningMedian waterTempSensor = RunningMedian(5);
|
2020-10-16 19:26:05 +02:00
|
|
|
|
2020-12-13 17:17:54 +01:00
|
|
|
OneWire oneWire(SENSOR_DS18B20);
|
|
|
|
DallasTemperature sensors(&oneWire);
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
Plant mPlants[MAX_PLANTS] = {
|
|
|
|
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-12-21 16:12:46 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* LOCAL FUNCTIONS
|
|
|
|
******************************************************************************/
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
float getBatteryVoltage()
|
|
|
|
{
|
2020-10-21 20:03:12 +02:00
|
|
|
return ADC_5V_TO_3V3(lipoRawSensor.getAverage());
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
float getSolarVoltage()
|
|
|
|
{
|
2020-10-21 20:03:12 +02:00
|
|
|
return SOLAR_VOLT(solarRawSensor.getAverage());
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void setMoistureTrigger(int plantId, long value)
|
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
if ((plantId >= 0) && (plantId < MAX_PLANTS))
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
rtcPlant[plantId].moistTrigger = value;
|
2020-11-04 21:57:40 +01:00
|
|
|
}
|
2020-10-31 19:25:13 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 23:28:08 +01:00
|
|
|
void setLastMoisture(int plantId, long value)
|
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
if ((plantId >= 0) && (plantId < MAX_PLANTS))
|
2020-11-04 23:28:08 +01:00
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
rtcPlant[plantId].moisture = value;
|
2020-11-04 23:28:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long getLastMoisture(int plantId)
|
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
if ((plantId >= 0) && (plantId < MAX_PLANTS))
|
2020-11-04 23:28:08 +01:00
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
return rtcPlant[plantId].moisture;
|
2020-11-06 21:00:11 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
return -1;
|
2020-11-04 23:28:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 00:01:19 +01:00
|
|
|
long getDistance()
|
|
|
|
{
|
2020-12-09 21:43:07 +01:00
|
|
|
byte startByte, h_data, l_data, sum;
|
2020-11-18 22:11:37 +01:00
|
|
|
byte buf[3];
|
2020-12-12 00:01:19 +01:00
|
|
|
|
2020-11-18 22:11:37 +01:00
|
|
|
startByte = (byte)Serial.read();
|
2020-12-12 00:01:19 +01:00
|
|
|
if (startByte == 255)
|
|
|
|
{
|
2020-12-09 21:43:07 +01:00
|
|
|
unsigned int distance;
|
2020-11-18 22:11:37 +01:00
|
|
|
Serial.readBytes(buf, 3);
|
|
|
|
h_data = buf[0];
|
|
|
|
l_data = buf[1];
|
|
|
|
sum = buf[2];
|
2020-12-12 00:01:19 +01:00
|
|
|
distance = (h_data << 8) + l_data;
|
|
|
|
if (((startByte + h_data + l_data) & 0xFF) != sum)
|
|
|
|
{
|
2020-11-18 22:11:37 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-12 00:01:19 +01:00
|
|
|
else
|
|
|
|
{
|
2020-11-18 22:11:37 +01:00
|
|
|
return distance;
|
2020-12-12 00:01:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-18 22:11:37 +01:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 16:12:46 +01:00
|
|
|
/**
|
|
|
|
* @brief Read Voltage
|
|
|
|
* Read the battery voltage and the current voltage, provided by the solar panel
|
|
|
|
*/
|
2020-11-04 21:57:40 +01:00
|
|
|
void readSystemSensors()
|
|
|
|
{
|
2020-12-12 00:01:19 +01:00
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
{
|
2020-11-07 00:34:27 +01:00
|
|
|
lipoRawSensor.add(analogRead(SENSOR_LIPO));
|
|
|
|
solarRawSensor.add(analogRead(SENSOR_SOLAR));
|
|
|
|
}
|
|
|
|
Serial << "Lipo " << lipoRawSensor.getAverage() << " -> " << getBatteryVoltage() << endl;
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
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-11-04 21:57:40 +01:00
|
|
|
void espDeepSleepFor(long seconds, bool activatePump = false)
|
|
|
|
{
|
|
|
|
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-11-04 21:57:40 +01:00
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
{
|
2020-10-31 19:25:13 +01:00
|
|
|
long cTime = getCurrentTime();
|
2020-11-04 21:57:40 +01:00
|
|
|
if (cTime < 100000)
|
|
|
|
{
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial << "Wait for ntp" << endl;
|
|
|
|
delay(100);
|
2020-11-04 21:57:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-31 19:25:13 +01:00
|
|
|
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);
|
2020-11-04 21:57:40 +01:00
|
|
|
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-11-04 21:57:40 +01: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-11-04 21:57:40 +01: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);
|
2020-11-04 23:28:08 +01:00
|
|
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
2020-11-04 21:57:40 +01:00
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
2020-10-23 19:32:12 +02:00
|
|
|
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");
|
2020-11-04 21:57:40 +01:00
|
|
|
esp_sleep_enable_timer_wakeup((seconds * 1000U * 1000U));
|
|
|
|
mDeepsleep = true;
|
2020-10-21 19:50:05 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01: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-11-04 21:57:40 +01: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-11-04 21:57:40 +01: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
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
if (lastPumpRunning != -1)
|
|
|
|
{
|
2020-11-11 21:28:03 +01:00
|
|
|
long waterDiff = waterRawSensor.getAverage() - lastWaterValue;
|
2020-10-20 18:06:37 +02:00
|
|
|
//TODO attribute used water in ml to plantid
|
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
2020-11-04 23:28:08 +01:00
|
|
|
long raw = mPlants[i].getCurrentMoisture();
|
|
|
|
long pct = 100 - map(raw, MOIST_SENSOR_MIN_ADC, MOIST_SENSOR_MAX_ADC, 0, 100);
|
|
|
|
if (raw == MISSING_SENSOR)
|
|
|
|
{
|
|
|
|
pct = 0;
|
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
if (pct < 0)
|
|
|
|
{
|
|
|
|
pct = 0;
|
|
|
|
}
|
|
|
|
if (pct > 100)
|
|
|
|
{
|
|
|
|
pct = 100;
|
|
|
|
}
|
2020-11-04 23:28:08 +01:00
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
mPlants[i].setProperty("moist").send(String(pct));
|
2020-11-04 23:28:08 +01:00
|
|
|
mPlants[i].setProperty("moistraw").send(String(raw));
|
2020-10-21 20:46:09 +02:00
|
|
|
}
|
2020-11-11 21:33:11 +01:00
|
|
|
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - waterRawSensor.getAverage()));
|
|
|
|
Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << "%)" << endl;
|
2020-11-11 21:28:03 +01:00
|
|
|
lastWaterValue = waterRawSensor.getAverage();
|
2020-10-20 18:06:37 +02:00
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
sensorLipo.setProperty("percent").send(String(100 * lipoRawSensor.getAverage() / 4095));
|
|
|
|
sensorLipo.setProperty("volt").send(String(getBatteryVoltage()));
|
|
|
|
sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage()) / 4095));
|
|
|
|
sensorSolar.setProperty("volt").send(String(getSolarVoltage()));
|
2020-11-28 01:45:57 +01:00
|
|
|
startupReason.setProperty("startupReason").send(String(wakeUpReason));
|
2020-10-20 18:06:37 +02:00
|
|
|
|
2020-12-13 17:17:54 +01:00
|
|
|
rtcLipoTempIndex = lipoSensorIndex.get();
|
|
|
|
rtcWaterTempIndex = waterSensorIndex.get();
|
|
|
|
|
|
|
|
float lipoTempCurrent = lipoTempSensor.getMedian();
|
|
|
|
if (lipoTempCurrent != NAN)
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2020-12-21 17:07:15 +01:00
|
|
|
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(lipoTempCurrent));
|
2020-11-01 14:30:26 +01:00
|
|
|
}
|
2020-12-13 17:17:54 +01:00
|
|
|
float t2 = waterTempSensor.getMedian();
|
2020-11-04 21:57:40 +01:00
|
|
|
if (t2 != NAN)
|
|
|
|
{
|
2020-12-21 17:07:15 +01:00
|
|
|
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(t2));
|
2020-11-01 14:30:26 +01:00
|
|
|
}
|
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-12-13 17:17:54 +01:00
|
|
|
bool lipoTempWarning = lipoTempCurrent != 85 && abs(lipoTempCurrent - t2) > 10;
|
2020-11-04 21:57:40 +01: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-11-04 21:57:40 +01:00
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
2020-10-31 19:25:13 +01:00
|
|
|
setMoistureTrigger(i, mPlants[i].mSetting->pSensorDry->get());
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01: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-11-04 21:57:40 +01:00
|
|
|
if (lastPumpRunning != -1 && !hasWater)
|
|
|
|
{
|
2020-10-21 20:46:09 +02:00
|
|
|
Serial.println("Want to pump but no water");
|
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
if (lastPumpRunning != -1 && hasWater)
|
|
|
|
{
|
2020-11-04 22:12:00 +01:00
|
|
|
if (mode3Active)
|
|
|
|
{
|
|
|
|
Serial.println("Mode 3 active, ignoring pump request");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
digitalWrite(OUTPUT_PUMP, HIGH);
|
|
|
|
setLastActivationForPump(lastPumpRunning, getCurrentTime());
|
|
|
|
mPlants[lastPumpRunning].activatePump();
|
|
|
|
}
|
2020-10-20 18:06:37 +02:00
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
if (lastPumpRunning == -1 || !hasWater)
|
|
|
|
{
|
|
|
|
if (getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE)
|
|
|
|
{
|
2020-11-04 23:28:08 +01:00
|
|
|
gotoMode2AfterThisTimestamp = getCurrentTime() + maxTimeBetweenMQTTUpdates.get();
|
2020-10-21 19:50:05 +02:00
|
|
|
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-11-04 21:57:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-04 23:28:08 +01:00
|
|
|
gotoMode2AfterThisTimestamp = getCurrentTime() + maxTimeBetweenMQTTUpdates.get();
|
2020-10-21 19:50:05 +02:00
|
|
|
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
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-21 19:50:05 +02:00
|
|
|
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-11-04 21:57:40 +01:00
|
|
|
long getMoistureTrigger(int plantId)
|
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
if ((plantId >= 0) && (plantId < MAX_PLANTS))
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
return rtcPlant[plantId].moistTrigger;
|
2020-11-06 21:00:11 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
return -1;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void setLastActivationForPump(int plantId, long value)
|
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
if ((plantId >= 0) && (plantId < MAX_PLANTS))
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
rtcPlant[plantId].lastActive = value;
|
2020-11-04 21:57:40 +01:00
|
|
|
}
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
long getLastActivationForPump(int plantId)
|
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
if ((plantId >= 0) && (plantId < MAX_PLANTS))
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
return rtcPlant[plantId].lastActive;
|
2020-11-06 21:00:11 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-06 19:15:43 +01:00
|
|
|
return -1;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 16:18:22 +01:00
|
|
|
/**
|
|
|
|
* @brief Read ultra sensor JSN-SR04T-2.0
|
|
|
|
* Read the distance of the water level.
|
|
|
|
*/
|
2020-12-12 00:01:19 +01:00
|
|
|
void readDistance()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
{
|
|
|
|
long start = millis();
|
|
|
|
while (!Serial.available())
|
|
|
|
{
|
2020-12-13 17:17:54 +01:00
|
|
|
if ((start + 500) < millis())
|
2020-12-12 00:01:19 +01:00
|
|
|
{
|
2020-12-13 17:17:54 +01:00
|
|
|
Serial << "Abort reading hall sensor, not measurement after 200ms" << endl;
|
|
|
|
waterRawSensor.add(0);
|
2020-12-12 00:01:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned int distance = getDistance();
|
|
|
|
if (distance > 0)
|
|
|
|
{
|
|
|
|
waterRawSensor.add(distance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-11-04 23:28:08 +01:00
|
|
|
bool readSensors()
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2020-11-06 21:00:11 +01:00
|
|
|
bool leaveMode1 = false;
|
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);
|
|
|
|
|
2020-11-04 22:12:00 +01:00
|
|
|
delay(20);
|
2020-12-13 17:17:54 +01:00
|
|
|
sensors.begin();
|
2020-10-16 19:26:05 +02:00
|
|
|
/* wait before reading something */
|
2020-11-04 21:57:40 +01:00
|
|
|
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-11-04 22:12:00 +01:00
|
|
|
delay(10);
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
2020-11-04 23:28:08 +01:00
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
|
|
|
long current = mPlants[i].getCurrentMoisture();
|
|
|
|
long delta = abs(getLastMoisture(i) - current);
|
|
|
|
bool tmp = (delta > MOIST_DELTA_TRIGGER_ADC);
|
|
|
|
setLastMoisture(i, current);
|
|
|
|
if (tmp)
|
|
|
|
{
|
2020-11-28 01:45:57 +01:00
|
|
|
wakeUpReason = WAKEUP_REASON_MOIST_CHANGE + i;
|
2020-11-06 21:00:11 +01:00
|
|
|
leaveMode1 = true;
|
2020-11-04 23:28:08 +01:00
|
|
|
Serial.printf("Mode2 start due to moist delta in plant %d with %ld \r\n", i, delta);
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 19:26:05 +02:00
|
|
|
|
2020-12-13 17:17:54 +01:00
|
|
|
if (abs(getBatteryVoltage() - rtcLastBatteryVoltage) > LIPO_DELTA_VOLT_ADC)
|
|
|
|
{
|
|
|
|
wakeUpReason = WAKEUP_REASON_BATTERY_CHANGE;
|
|
|
|
leaveMode1 = true;
|
|
|
|
}
|
|
|
|
if (abs(getSolarVoltage() - rtcLastSolarVoltage) > SOLAR_DELTA_VOLT_ADC)
|
|
|
|
{
|
|
|
|
wakeUpReason = WAKEUP_REASON_SOLAR_CHANGE;
|
|
|
|
leaveMode1 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtcLastLipoTemp = lipoTempSensor.getAverage();
|
|
|
|
rtcLastWaterTemp = waterTempSensor.getAverage();
|
|
|
|
rtcLastBatteryVoltage = getBatteryVoltage();
|
|
|
|
rtcLastSolarVoltage = getSolarVoltage();
|
|
|
|
|
|
|
|
readDistance();
|
|
|
|
Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl;
|
|
|
|
int sensorCount = 0;
|
|
|
|
int timeoutTemp = millis() + 2000;
|
|
|
|
while (sensorCount == 0 && millis() < timeoutTemp)
|
|
|
|
{
|
|
|
|
sensors.begin();
|
|
|
|
sensorCount = sensors.getDeviceCount();
|
|
|
|
Serial << "Waitloop: One wire count: " << sensorCount << endl;
|
|
|
|
delay(200);
|
|
|
|
}
|
|
|
|
Serial << "One wire count: " << sensorCount << endl;
|
|
|
|
if (sensorCount > 0)
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2020-12-12 00:01:19 +01:00
|
|
|
/* Required to read the temperature once */
|
|
|
|
int readAgain = 5;
|
|
|
|
while (readAgain > 0)
|
2020-11-06 21:00:11 +01:00
|
|
|
{
|
2020-12-13 17:17:54 +01:00
|
|
|
sensors.requestTemperatures();
|
|
|
|
if (sensorCount > 0)
|
2020-12-12 00:01:19 +01:00
|
|
|
{
|
2020-12-13 17:17:54 +01:00
|
|
|
if (rtcLipoTempIndex != -1)
|
|
|
|
{
|
|
|
|
float temp1Raw = sensors.getTempCByIndex(rtcLipoTempIndex);
|
|
|
|
Serial << "lipoTempCurrent: " << temp1Raw << endl;
|
|
|
|
lipoTempSensor.add(temp1Raw);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial << "missing lipotemp, proceed to mode2: " << endl;
|
|
|
|
leaveMode1 = 1;
|
|
|
|
readAgain = 0;
|
|
|
|
wakeUpReason = WAKEUP_REASON_RTC_MISSING;
|
|
|
|
}
|
2020-12-12 00:01:19 +01:00
|
|
|
}
|
2020-12-13 17:17:54 +01:00
|
|
|
if (sensorCount > 1 && rtcWaterTempIndex != -1)
|
2020-12-12 00:01:19 +01:00
|
|
|
{
|
2020-12-13 17:17:54 +01:00
|
|
|
float temp2Raw = sensors.getTempCByIndex(rtcWaterTempIndex);
|
|
|
|
Serial << "waterTempCurrent: " << temp2Raw << endl;
|
|
|
|
waterTempSensor.add(temp2Raw);
|
2020-12-12 00:01:19 +01:00
|
|
|
}
|
2020-12-13 17:17:54 +01:00
|
|
|
if ((lipoTempSensor.getAverage() - rtcLastLipoTemp > TEMPERATURE_DELTA_TRIGGER_IN_C) ||
|
|
|
|
(rtcLastLipoTemp - lipoTempSensor.getAverage() > TEMPERATURE_DELTA_TRIGGER_IN_C))
|
2020-12-12 00:01:19 +01:00
|
|
|
{
|
|
|
|
leaveMode1 = true;
|
|
|
|
}
|
2020-12-13 17:17:54 +01:00
|
|
|
if ((waterTempSensor.getAverage() - rtcLastWaterTemp > TEMPERATURE_DELTA_TRIGGER_IN_C) ||
|
|
|
|
(rtcLastWaterTemp - waterTempSensor.getAverage() > TEMPERATURE_DELTA_TRIGGER_IN_C))
|
2020-12-12 00:01:19 +01:00
|
|
|
{
|
|
|
|
leaveMode1 = true;
|
|
|
|
}
|
|
|
|
if (!leaveMode1)
|
|
|
|
{
|
|
|
|
readAgain = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
readAgain--;
|
|
|
|
delay(50);
|
2020-11-11 21:09:38 +01:00
|
|
|
}
|
2020-12-13 17:17:54 +01:00
|
|
|
for (int i = 0; i < sensorCount; i++)
|
|
|
|
{
|
|
|
|
Serial << "OnwWire sensor " << i << " has value " << sensors.getTempCByIndex(i) << endl;
|
|
|
|
}
|
2020-11-01 14:30:26 +01:00
|
|
|
}
|
2020-11-06 21:00:11 +01:00
|
|
|
|
2020-12-13 17:17:54 +01:00
|
|
|
if (abs(lipoTempSensor.getAverage() - rtcLastLipoTemp) > TEMPERATURE_DELTA_TRIGGER_IN_C)
|
2020-12-12 00:01:19 +01:00
|
|
|
{
|
2020-11-28 01:45:57 +01:00
|
|
|
leaveMode1 = true;
|
|
|
|
wakeUpReason = WAKEUP_REASON_TEMP1_CHANGE;
|
|
|
|
}
|
2020-12-13 17:17:54 +01:00
|
|
|
if (abs(waterTempSensor.getAverage() - rtcLastWaterTemp) > TEMPERATURE_DELTA_TRIGGER_IN_C)
|
2020-12-12 00:01:19 +01:00
|
|
|
{
|
2020-11-28 01:45:57 +01:00
|
|
|
wakeUpReason = WAKEUP_REASON_TEMP2_CHANGE;
|
|
|
|
leaveMode1 = true;
|
|
|
|
}
|
|
|
|
|
2020-10-16 19:26:05 +02:00
|
|
|
/* deactivate the sensors */
|
|
|
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
2020-11-06 21:00:11 +01:00
|
|
|
return leaveMode1;
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void onHomieEvent(const HomieEvent &event)
|
|
|
|
{
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case HomieEventType::SENDING_STATISTICS:
|
|
|
|
Homie.getLogger() << "My statistics" << endl;
|
|
|
|
break;
|
|
|
|
case HomieEventType::MQTT_READY:
|
|
|
|
Serial.printf("NTP Setup with server %s\r\n", ntpServer.get());
|
|
|
|
configTime(0, 0, ntpServer.get());
|
|
|
|
//wait for rtc sync?
|
|
|
|
rtcDeepSleepTime = deepSleepTime.get();
|
|
|
|
Serial << "Setup plants" << endl;
|
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
|
|
|
mPlants[i].postMQTTconnection();
|
|
|
|
}
|
2020-10-28 20:31:18 +01:00
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
mode2MQTT();
|
|
|
|
break;
|
|
|
|
case HomieEventType::READY_TO_SLEEP:
|
|
|
|
Homie.getLogger() << "rtsleep" << endl;
|
|
|
|
esp_deep_sleep_start();
|
|
|
|
break;
|
|
|
|
case HomieEventType::OTA_STARTED:
|
|
|
|
Homie.getLogger() << "OTA started" << endl;
|
|
|
|
digitalWrite(OUTPUT_SENSOR, HIGH);
|
|
|
|
digitalWrite(OUTPUT_PUMP, HIGH);
|
|
|
|
gpio_hold_dis(GPIO_NUM_13); //pump pwr
|
|
|
|
gpio_deep_sleep_hold_dis();
|
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
|
|
|
mPlants[i].deactivatePump();
|
|
|
|
}
|
|
|
|
mode3Active = true;
|
|
|
|
break;
|
|
|
|
case HomieEventType::OTA_SUCCESSFUL:
|
2020-11-01 14:30:26 +01:00
|
|
|
Homie.getLogger() << "OTA successfull" << endl;
|
2020-11-04 21:57:40 +01:00
|
|
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
|
|
|
digitalWrite(OUTPUT_PUMP, LOW);
|
|
|
|
ESP.restart();
|
|
|
|
break;
|
|
|
|
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-11-04 21:57:40 +01:00
|
|
|
int determineNextPump()
|
|
|
|
{
|
2020-10-21 20:03:12 +02:00
|
|
|
float solarValue = getSolarVoltage();
|
2020-11-04 21:57:40 +01: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?
|
2020-11-04 21:10:22 +01:00
|
|
|
|
|
|
|
int pumpToUse = -1;
|
2020-11-04 21:57:40 +01:00
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
2020-11-04 21:10:22 +01:00
|
|
|
Plant plant = mPlants[i];
|
2020-10-19 01:39:56 +02:00
|
|
|
long lastActivation = getLastActivationForPump(i);
|
2020-11-04 21:57:40 +01:00
|
|
|
long sinceLastActivation = getCurrentTime() - lastActivation;
|
2020-10-19 01:39:56 +02:00
|
|
|
//this pump is in cooldown skip it and disable low power mode trigger for it
|
2020-11-04 21:57:40 +01:00
|
|
|
if (plant.isInCooldown(sinceLastActivation))
|
|
|
|
{
|
2020-11-04 23:28:08 +01:00
|
|
|
Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, sinceLastActivation, plant.getCooldownInSeconds());
|
2020-10-23 16:32:05 +02:00
|
|
|
setMoistureTrigger(i, DEACTIVATED_PLANT);
|
|
|
|
continue;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
|
|
|
//skip as it is not low light
|
2020-11-04 21:57:40 +01:00
|
|
|
if (!isLowLight && plant.isAllowedOnlyAtLowLight())
|
|
|
|
{
|
2020-11-04 21:10:22 +01:00
|
|
|
Serial.printf("%d No pump required: due to light\r\n", i);
|
2020-10-19 01:39:56 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-11-06 21:00:11 +01:00
|
|
|
if (plant.getCurrentMoisture() == MISSING_SENSOR && plant.isPumpTriggerActive())
|
|
|
|
{
|
2020-11-04 23:28:08 +01:00
|
|
|
Serial.printf("%d No pump possible: missing sensor \r\n", i);
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
if (plant.isPumpRequired())
|
|
|
|
{
|
2020-10-23 16:32:05 +02:00
|
|
|
Serial.printf("%d Requested pumping\r\n", i);
|
2020-11-04 21:10:22 +01:00
|
|
|
pumpToUse = i;
|
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
else if (plant.isPumpTriggerActive())
|
|
|
|
{
|
2020-11-04 23:28:08 +01:00
|
|
|
Serial.printf("%d No pump required: moisture acceptable %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture());
|
2020-11-04 21:57:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-04 23:28:08 +01:00
|
|
|
Serial.printf("%d No pump required: disabled pump trigger \r\n", i);
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-04 21:10:22 +01:00
|
|
|
return pumpToUse;
|
2020-10-19 01:39:56 +02:00
|
|
|
}
|
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
|
|
|
|
*/
|
2020-11-04 21:57:40 +01:00
|
|
|
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"))
|
|
|
|
{
|
|
|
|
mode3Active = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mode3Active = false;
|
|
|
|
}
|
|
|
|
|
2020-10-13 20:16:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void homieLoop()
|
|
|
|
{
|
2020-10-20 20:12:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
void systemInit()
|
|
|
|
{
|
2020-10-16 20:36:07 +02:00
|
|
|
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-11-04 21:57:40 +01:00
|
|
|
|
2020-10-21 19:50:05 +02:00
|
|
|
//in seconds
|
2020-12-13 17:17:54 +01:00
|
|
|
|
2020-11-04 23:28:08 +01:00
|
|
|
maxTimeBetweenMQTTUpdates.setDefaultValue(120);
|
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-11-04 21:57:40 +01:00
|
|
|
/* waterLevelMax 1000 */ /* 100cm in mm */
|
|
|
|
waterLevelMin.setDefaultValue(50); /* 5cm in mm */
|
|
|
|
waterLevelWarn.setDefaultValue(500); /* 50cm in mm */
|
|
|
|
waterLevelVol.setDefaultValue(5000); /* 5l in ml */
|
2020-12-13 17:17:54 +01:00
|
|
|
lipoSensorIndex.setDefaultValue(0);
|
|
|
|
waterSensorIndex.setDefaultValue(-1);
|
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-11-04 21:57:40 +01:00
|
|
|
if (mConfigured)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
2020-10-23 16:20:34 +02:00
|
|
|
mPlants[i].advertise();
|
|
|
|
}
|
2020-12-21 17:07:15 +01:00
|
|
|
sensorTemp.advertise(TEMPERATUR_SENSOR_LIPO)
|
|
|
|
.setName(TEMPERATURE_NAME)
|
|
|
|
.setDatatype(NUMBER_TYPE)
|
|
|
|
.setUnit(TEMPERATURE_UNIT);
|
|
|
|
sensorTemp.advertise(TEMPERATUR_SENSOR_WATER)
|
|
|
|
.setName(TEMPERATURE_NAME)
|
|
|
|
.setDatatype(NUMBER_TYPE)
|
|
|
|
.setUnit(TEMPERATURE_UNIT);
|
2020-09-21 20:42:24 +02:00
|
|
|
|
|
|
|
sensorLipo.advertise("percent")
|
2020-11-04 21:57:40 +01:00
|
|
|
.setName("Percent")
|
2020-12-21 17:07:15 +01:00
|
|
|
.setDatatype(NUMBER_TYPE)
|
2020-11-04 21:57:40 +01:00
|
|
|
.setUnit("%");
|
2020-09-21 20:42:24 +02:00
|
|
|
sensorLipo.advertise("volt")
|
2020-11-04 21:57:40 +01:00
|
|
|
.setName("Volt")
|
2020-12-21 17:07:15 +01:00
|
|
|
.setDatatype(NUMBER_TYPE)
|
2020-11-04 21:57:40 +01:00
|
|
|
.setUnit("V");
|
2020-09-21 20:42:24 +02:00
|
|
|
|
|
|
|
sensorSolar.advertise("percent")
|
2020-11-04 21:57:40 +01:00
|
|
|
.setName("Percent")
|
2020-12-21 17:07:15 +01:00
|
|
|
.setDatatype(NUMBER_TYPE)
|
2020-11-04 21:57:40 +01:00
|
|
|
.setUnit("%");
|
2020-09-21 20:42:24 +02:00
|
|
|
sensorSolar.advertise("volt")
|
2020-11-04 21:57:40 +01:00
|
|
|
.setName("Volt")
|
2020-12-21 17:07:15 +01:00
|
|
|
.setDatatype(NUMBER_TYPE)
|
2020-11-04 21:57:40 +01:00
|
|
|
.setUnit("V");
|
2020-12-21 17:07:15 +01:00
|
|
|
sensorWater.advertise("remaining").setDatatype(NUMBER_TYPE).setUnit("%");
|
|
|
|
startupReason.advertise("startupReason").setDatatype(NUMBER_TYPE).setUnit("Enum");
|
2020-09-21 20:42:24 +02:00
|
|
|
}
|
2020-12-21 17:07:15 +01:00
|
|
|
stayAlive.advertise("alive").setName("Alive").setDatatype(NUMBER_TYPE).settable(aliveHandler);
|
2020-10-16 19:26:05 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01: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-11-04 23:28:08 +01:00
|
|
|
bool deltaTrigger = readSensors();
|
2020-11-04 21:57:40 +01:00
|
|
|
//queue sensor values for
|
2020-10-16 21:12:25 +02:00
|
|
|
|
2020-11-06 21:00:11 +01:00
|
|
|
if (deltaTrigger)
|
|
|
|
{
|
2020-11-04 23:28:08 +01:00
|
|
|
Serial.println("1 delta triggered, going to mode2");
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
if (rtcDeepSleepTime == 0)
|
|
|
|
{
|
2020-11-28 01:45:57 +01:00
|
|
|
wakeUpReason = WAKEUP_REASON_RTC_MISSING;
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial.println("1 missing rtc value, going to mode2");
|
2020-10-16 21:12:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
|
|
|
long trigger = getMoistureTrigger(i);
|
|
|
|
if (trigger == 0)
|
|
|
|
{
|
2020-11-28 01:45:57 +01:00
|
|
|
wakeUpReason = WAKEUP_REASON_RTC_MISSING;
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial << "Missing rtc trigger " << i << endl;
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
if (trigger == DEACTIVATED_PLANT)
|
|
|
|
{
|
2020-10-31 19:25:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-11-04 23:28:08 +01:00
|
|
|
long raw = mPlants[i].getCurrentMoisture();
|
|
|
|
if (raw == MISSING_SENSOR)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (raw > trigger)
|
2020-11-04 21:57:40 +01:00
|
|
|
{
|
2020-11-04 23:28:08 +01:00
|
|
|
Serial << "plant " << i << " dry " << raw << " / " << trigger << " starting mode 2" << endl;
|
2020-11-28 01:45:57 +01:00
|
|
|
wakeUpReason = WAKEUP_REASON_PLANT_DRY + i;
|
2020-10-31 19:25:13 +01:00
|
|
|
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();
|
2020-11-04 21:57:40 +01:00
|
|
|
if (cTime < 100000)
|
|
|
|
{
|
2020-10-21 19:50:05 +02:00
|
|
|
Serial.println("Starting mode 2 due to missing ntp");
|
|
|
|
//missing ntp time boot to mode3
|
2020-11-28 01:45:57 +01:00
|
|
|
wakeUpReason = WAKEUP_REASON_TIME_UNSET;
|
2020-10-21 19:50:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-04 21:57:40 +01:00
|
|
|
if (gotoMode2AfterThisTimestamp < cTime)
|
|
|
|
{
|
2020-11-28 01:45:57 +01:00
|
|
|
wakeUpReason = WAKEUP_REASON_MODE2_WAKEUP_TIMER;
|
2020-10-21 19:50:05 +02:00
|
|
|
Serial.println("Starting mode 2 after specified mode1 time");
|
|
|
|
return true;
|
2020-11-04 21:57:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-31 19:25:13 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-04 21:57:40 +01: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 */
|
2020-11-04 21:57:40 +01:00
|
|
|
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
|
|
|
|
*/
|
2020-11-04 21:57:40 +01:00
|
|
|
void setup()
|
|
|
|
{
|
2020-11-18 22:11:37 +01:00
|
|
|
Serial.begin(9600);
|
2020-10-16 19:26:05 +02:00
|
|
|
Serial.setTimeout(1000); // Set timeout of 1 second
|
2020-11-04 21:57:40 +01:00
|
|
|
Serial << endl
|
|
|
|
<< endl;
|
2020-10-16 19:26:05 +02:00
|
|
|
/* Intialize Plant */
|
2020-11-04 21:57:40 +01:00
|
|
|
for (int i = 0; i < MAX_PLANTS; i++)
|
|
|
|
{
|
2020-10-16 19:26:05 +02:00
|
|
|
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-11-04 21:57:40 +01: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-11-04 21:57:40 +01: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-11-04 21:57:40 +01:00
|
|
|
if ((getBatteryVoltage() < MINIMUM_LIPO_VOLT) &&
|
|
|
|
(getBatteryVoltage() > NO_LIPO_VOLT))
|
|
|
|
{
|
2020-10-21 20:03:12 +02:00
|
|
|
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);
|
2020-11-04 21:57:40 +01:00
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_ON);
|
2020-10-28 20:00:24 +01:00
|
|
|
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
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
if (mode1())
|
|
|
|
{
|
2020-10-20 18:41:41 +02:00
|
|
|
mode2();
|
2020-11-04 21:57:40 +01:00
|
|
|
}
|
|
|
|
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-11-04 21:57:40 +01:00
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
if (!mDeepsleep || mode3Active)
|
|
|
|
{
|
2020-10-23 19:32:12 +02:00
|
|
|
Homie.loop();
|
2020-11-04 21:57:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial << "Bye" << endl;
|
2020-10-31 19:25:13 +01:00
|
|
|
Serial.flush();
|
2020-10-23 19:32:12 +02:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
2020-10-16 20:36:07 +02:00
|
|
|
|
2020-11-04 21:57:40 +01:00
|
|
|
if (millis() > 30000 && !mode3Active)
|
|
|
|
{
|
|
|
|
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 */
|
2020-11-04 21:57:40 +01:00
|
|
|
if (mode3Active)
|
|
|
|
{
|
|
|
|
if (nextBlink < millis())
|
|
|
|
{
|
2020-11-04 21:10:22 +01:00
|
|
|
nextBlink = millis() + 500;
|
2020-11-04 21:57:40 +01:00
|
|
|
digitalWrite(OUTPUT_SENSOR, !digitalRead(OUTPUT_SENSOR));
|
2020-11-04 21:10:22 +01:00
|
|
|
}
|
2020-11-01 20:42:45 +01:00
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
2020-11-06 22:19:16 +01:00
|
|
|
|
2020-11-29 05:04:46 +01:00
|
|
|
/** @}*/
|