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"
|
|
|
|
#include "DS18B20.h"
|
|
|
|
#include <Homie.h>
|
2020-09-20 19:18:47 +02:00
|
|
|
#include "esp_sleep.h"
|
2020-10-16 15:03:51 +02:00
|
|
|
#include "HomieConfiguration.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
|
|
|
|
|
|
|
|
bool mLoopInited = false;
|
|
|
|
bool mDeepSleep = false;
|
2020-10-13 20:16:28 +02:00
|
|
|
bool mAlive=false; /**< Controller must not sleep */
|
2020-09-07 18:18:46 +02:00
|
|
|
|
|
|
|
int plantSensor1 = 0;
|
|
|
|
|
|
|
|
int lipoSenor = -1;
|
|
|
|
int lipoSensorValues = 0;
|
|
|
|
int solarSensor = -1;
|
|
|
|
int solarSensorValues = 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;
|
|
|
|
int mButtonClicks = 0;
|
2020-09-21 20:42:24 +02:00
|
|
|
bool mConfigured = false;
|
2020-09-07 18:18:46 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
Ds18B20 dallas(SENSOR_DS18B20);
|
|
|
|
|
|
|
|
Plant mPlants[MAX_PLANTS] = {
|
2020-10-14 20:07:56 +02:00
|
|
|
Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0),
|
|
|
|
Plant(SENSOR_PLANT1, OUTPUT_PUMP1, 1),
|
|
|
|
Plant(SENSOR_PLANT2, OUTPUT_PUMP2, 2),
|
|
|
|
Plant(SENSOR_PLANT3, OUTPUT_PUMP3, 3),
|
|
|
|
Plant(SENSOR_PLANT4, OUTPUT_PUMP4, 4),
|
|
|
|
Plant(SENSOR_PLANT5, OUTPUT_PUMP5, 5),
|
|
|
|
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6)
|
2020-09-07 18:18:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void readAnalogValues() {
|
|
|
|
if (readCounter < AMOUNT_SENOR_QUERYS) {
|
|
|
|
lipoSensorValues += analogRead(SENSOR_LIPO);
|
|
|
|
solarSensorValues += analogRead(SENSOR_SOLAR);
|
|
|
|
readCounter++;
|
|
|
|
} else {
|
|
|
|
lipoSenor = (lipoSensorValues >> SENSOR_QUERY_SHIFTS);
|
|
|
|
lipoSensorValues = 0;
|
|
|
|
solarSensor = (solarSensorValues >> SENSOR_QUERY_SHIFTS);
|
|
|
|
solarSensorValues = 0;
|
|
|
|
|
|
|
|
readCounter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief cyclic Homie callback
|
|
|
|
* All logic, to be done by the controller cyclically
|
|
|
|
*/
|
|
|
|
void loopHandler() {
|
|
|
|
|
|
|
|
/* Move from Setup to this position, because the Settings are only here available */
|
|
|
|
if (!mLoopInited) {
|
|
|
|
// Configure Deep Sleep:
|
|
|
|
if (deepSleepTime.get()) {
|
|
|
|
Serial << "HOMIE | Setup sleeping for " << deepSleepTime.get() << " ms" << endl;
|
|
|
|
}
|
|
|
|
/* Publish default values */
|
2020-09-20 19:48:56 +02:00
|
|
|
plant0.setProperty("switch").send(String("OFF"));
|
2020-09-07 18:18:46 +02:00
|
|
|
plant1.setProperty("switch").send(String("OFF"));
|
2020-09-20 19:48:56 +02:00
|
|
|
plant2.setProperty("switch").send(String("OFF"));
|
2020-09-07 18:18:46 +02:00
|
|
|
|
|
|
|
#if (MAX_PLANTS >= 4)
|
2020-09-20 19:48:56 +02:00
|
|
|
plant3.setProperty("switch").send(String("OFF"));
|
|
|
|
plant4.setProperty("switch").send(String("OFF"));
|
2020-09-07 18:18:46 +02:00
|
|
|
plant5.setProperty("switch").send(String("OFF"));
|
|
|
|
#endif
|
2020-09-20 19:18:47 +02:00
|
|
|
#if (MAX_PLANTS >= 7)
|
2020-09-20 19:48:56 +02:00
|
|
|
plant6.setProperty("switch").send(String("OFF"));
|
2020-09-20 19:18:47 +02:00
|
|
|
#endif
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-14 20:07:56 +02:00
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
2020-09-07 18:18:46 +02:00
|
|
|
mPlants[i].calculateSensorValue(AMOUNT_SENOR_QUERYS);
|
2020-09-21 19:40:01 +02:00
|
|
|
mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 ));
|
|
|
|
/* the last Plant, that was watered is stored in non volatile memory */
|
|
|
|
if (gCurrentPlant <= 0 && mPlants[i].isPumpRequired()) {
|
|
|
|
/* there was no plant activated -> we can store the first one */
|
|
|
|
gCurrentPlant = i + 1;
|
|
|
|
} else if (gCurrentPlant > 0 && gCurrentPlant < (i+1) &&
|
|
|
|
mPlants[(gCurrentPlant - 1)].isPumpRequired() == false) {
|
|
|
|
/* The last does not need any more some water -> jump to the next one */
|
|
|
|
gCurrentPlant = i + 1;
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
2020-09-21 19:40:01 +02:00
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
|
2020-10-14 20:07:56 +02:00
|
|
|
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - mWaterGone ));
|
|
|
|
Serial << "Water : " << mWaterGone << " cm (" << String(waterLevelMax.get() - mWaterGone ) << "%)" << endl;
|
2020-09-21 19:40:01 +02:00
|
|
|
|
|
|
|
/* Check if a plant needs water */
|
|
|
|
if (gCurrentPlant > 0) {
|
|
|
|
int plntIdx = (gCurrentPlant-1);
|
|
|
|
if (mPlants[plntIdx].isPumpRequired() &&
|
2020-10-14 20:07:56 +02:00
|
|
|
(mWaterGone > waterLevelMin.get()) &&
|
2020-09-21 19:40:01 +02:00
|
|
|
(digitalRead(mPlants[plntIdx].getPumpPin()) == LOW) ) {
|
|
|
|
Serial << "Plant" << plntIdx << " needs water" << endl;
|
|
|
|
mPlants[plntIdx].setProperty("switch").send(String("ON"));
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
2020-09-21 19:40:01 +02:00
|
|
|
digitalWrite(OUTPUT_PUMP, HIGH);
|
|
|
|
digitalWrite(mPlants[plntIdx].getPumpPin(), HIGH);
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mLoopInited = true;
|
|
|
|
|
|
|
|
readAnalogValues();
|
|
|
|
|
|
|
|
if ((millis() % 1500) == 0) {
|
|
|
|
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)) );
|
|
|
|
} else if ((millis() % 1000) == 0) {
|
|
|
|
float temp[2] = { TEMP_INIT_VALUE, TEMP_INIT_VALUE };
|
|
|
|
float* pFloat = temp;
|
|
|
|
int devices = dallas.readAllTemperatures(pFloat, 2);
|
|
|
|
if (devices < 2) {
|
|
|
|
if ((pFloat[0] > TEMP_INIT_VALUE) && (pFloat[0] < TEMP_MAX_VALUE) ) {
|
|
|
|
sensorTemp.setProperty("control").send( String(pFloat[0]));
|
|
|
|
}
|
|
|
|
} else if (devices >= 2) {
|
|
|
|
if ((pFloat[0] > TEMP_INIT_VALUE) && (pFloat[0] < TEMP_MAX_VALUE) ) {
|
|
|
|
sensorTemp.setProperty("temp").send( String(pFloat[0]));
|
|
|
|
}
|
|
|
|
if ((pFloat[1] > TEMP_INIT_VALUE) && (pFloat[1] < TEMP_MAX_VALUE) ) {
|
|
|
|
sensorTemp.setProperty("control").send( String(pFloat[1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main Loop functionality */
|
2020-10-14 20:07:56 +02:00
|
|
|
if (mWaterGone <= waterLevelMin.get()) {
|
2020-09-07 18:18:46 +02:00
|
|
|
/* let the ESP sleep qickly, as nothing must be done */
|
|
|
|
if ((millis() >= (MIN_TIME_RUNNING * MS_TO_S)) && (deepSleepTime.get() > 0)) {
|
2020-10-13 21:47:56 +02:00
|
|
|
mDeepSleep = true;
|
2020-09-21 19:40:01 +02:00
|
|
|
Serial << "No Water for pumps" << endl;
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always check, that after 5 minutes the device is sleeping */
|
|
|
|
/* Pump is running, go to sleep after defined time */
|
2020-09-20 19:48:56 +02:00
|
|
|
if (millis() >= ((MIN_TIME_RUNNING + 5) &&
|
|
|
|
(deepSleepTime.get() > 0))) {
|
2020-09-07 18:18:46 +02:00
|
|
|
mDeepSleep = true;
|
2020-09-20 19:48:56 +02:00
|
|
|
} else if ((millis() >= ((MIN_TIME_RUNNING * MS_TO_S) + 0)) &&
|
2020-09-07 18:18:46 +02:00
|
|
|
(deepSleepTime.get() > 0)) {
|
|
|
|
Serial << "Maximum time reached: " << endl;
|
|
|
|
mDeepSleep = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool switchGeneralPumpHandler(const int pump, const HomieRange& range, const String& value) {
|
|
|
|
if (range.isRange) return false; // only one switch is present
|
|
|
|
switch (pump)
|
|
|
|
{
|
|
|
|
#if MAX_PLANTS >= 1
|
|
|
|
case 0:
|
|
|
|
#endif
|
|
|
|
#if MAX_PLANTS >= 2
|
|
|
|
case 1:
|
|
|
|
#endif
|
|
|
|
#if MAX_PLANTS >= 3
|
|
|
|
#endif
|
|
|
|
case 2:
|
|
|
|
#if MAX_PLANTS >= 4
|
|
|
|
case 3:
|
|
|
|
#endif
|
|
|
|
#if MAX_PLANTS >= 5
|
|
|
|
case 4:
|
|
|
|
#endif
|
|
|
|
#if MAX_PLANTS >= 6
|
|
|
|
case 5:
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((value.equals("ON")) || (value.equals("On")) || (value.equals("on")) || (value.equals("true"))) {
|
|
|
|
digitalWrite(mPlants[pump].getPumpPin(), HIGH);
|
|
|
|
return true;
|
|
|
|
} else if ((value.equals("OFF")) || (value.equals("Off")) || (value.equals("off")) || (value.equals("false")) ) {
|
|
|
|
digitalWrite(mPlants[pump].getPumpPin(), LOW);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")) {
|
|
|
|
mAlive=true;
|
|
|
|
} else {
|
|
|
|
mAlive=false;
|
|
|
|
}
|
2020-10-13 21:47:56 +02:00
|
|
|
Serial << "HOMIE | Controller " << (mAlive ? " has coffee" : " is tired") << endl;
|
2020-10-13 20:16:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
/**
|
|
|
|
* @brief Handle Mqtt commands for the pumpe, responsible for the first plant
|
|
|
|
*
|
|
|
|
* @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 switch1Handler(const HomieRange& range, const String& value) {
|
|
|
|
return switchGeneralPumpHandler(0, range, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Handle Mqtt commands for the pumpe, responsible for the second plant
|
|
|
|
*
|
|
|
|
* @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 switch2Handler(const HomieRange& range, const String& value) {
|
|
|
|
return switchGeneralPumpHandler(1, range, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Handle Mqtt commands for the pumpe, responsible for the third plant
|
|
|
|
*
|
|
|
|
* @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 switch3Handler(const HomieRange& range, const String& value) {
|
|
|
|
return switchGeneralPumpHandler(2, range, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
|
|
|
|
* These sensors (ADC2) can only be read when no Wifi is used.
|
|
|
|
*/
|
|
|
|
void readSensors() {
|
|
|
|
/* 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++) {
|
|
|
|
mPlants[i].addSenseValue(analogRead(mPlants[i].getSensorPin()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 19:18:47 +02:00
|
|
|
#ifdef HC_SR04
|
2020-09-07 18:18:46 +02:00
|
|
|
/* Use the Ultrasonic sensor to measure waterLevel */
|
|
|
|
|
|
|
|
/* deactivate all sensors and measure the pulse */
|
2020-09-20 19:48:56 +02:00
|
|
|
digitalWrite(SENSOR_SR04_TRIG, LOW);
|
2020-09-07 18:18:46 +02:00
|
|
|
delayMicroseconds(2);
|
2020-09-20 19:48:56 +02:00
|
|
|
digitalWrite(SENSOR_SR04_TRIG, HIGH);
|
2020-09-07 18:18:46 +02:00
|
|
|
delayMicroseconds(10);
|
2020-09-20 19:48:56 +02:00
|
|
|
digitalWrite(SENSOR_SR04_TRIG, LOW);
|
|
|
|
float duration = pulseIn(SENSOR_SR04_ECHO, HIGH);
|
2020-09-07 18:18:46 +02:00
|
|
|
float distance = (duration*.0343)/2;
|
|
|
|
mWaterGone = (int) distance;
|
|
|
|
Serial << "HC_SR04 | Distance : " << String(distance) << " cm" << endl;
|
|
|
|
#endif
|
|
|
|
/* deactivate the sensors */
|
|
|
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Startup function
|
|
|
|
* Is called once, the controller is started
|
|
|
|
*/
|
|
|
|
void setup() {
|
|
|
|
/* Required to read the temperature once */
|
|
|
|
float temp[2] = {0, 0};
|
|
|
|
float* pFloat = temp;
|
|
|
|
|
|
|
|
/* read button */
|
|
|
|
pinMode(BUTTON, INPUT);
|
|
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
Serial.setTimeout(1000); // Set timeout of 1 second
|
|
|
|
Serial << endl << endl;
|
|
|
|
Serial << "Read analog sensors..." << endl;
|
|
|
|
/* Disable Wifi and bluetooth */
|
|
|
|
WiFi.mode(WIFI_OFF);
|
|
|
|
/* now ADC2 can be used */
|
|
|
|
readSensors();
|
|
|
|
/* activate Wifi again */
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
2020-09-21 20:42:24 +02:00
|
|
|
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS) {
|
|
|
|
Serial << "HOMIE | Settings: " << HomieInternals::MAX_CONFIG_SETTING_SIZE << "/" << MAX_CONFIG_SETTING_ITEMS << endl;
|
|
|
|
Serial << " | Update Limits.hpp : MAX_CONFIG_SETTING_SIZE to " << MAX_CONFIG_SETTING_ITEMS << endl;
|
2020-10-13 21:22:21 +02:00
|
|
|
Serial << " | Update Limits.hpp : MAX_JSON_CONFIG_FILE_SIZE to 3000" << endl;
|
2020-09-21 20:42:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
Homie_setFirmware("PlantControl", FIRMWARE_VERSION);
|
|
|
|
Homie.setLoopFunction(loopHandler);
|
|
|
|
|
2020-09-21 20:42:24 +02:00
|
|
|
mConfigured = Homie.isConfigured();
|
|
|
|
// Load the settings
|
|
|
|
deepSleepTime.setDefaultValue(0);
|
|
|
|
deepSleepNightTime.setDefaultValue(0);
|
|
|
|
|
2020-10-14 19:01:13 +02:00
|
|
|
if (mConfigured) {
|
2020-09-21 20:42:24 +02:00
|
|
|
// Advertise topics
|
|
|
|
plant1.advertise("switch").setName("Pump 1")
|
|
|
|
.setDatatype("boolean")
|
|
|
|
.settable(switch1Handler);
|
|
|
|
plant1.advertise("moist").setName("Percent")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("%");
|
|
|
|
plant2.advertise("switch").setName("Pump 2")
|
|
|
|
.setDatatype("boolean")
|
|
|
|
.settable(switch2Handler);
|
|
|
|
plant2.advertise("moist").setName("Percent")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("%");
|
|
|
|
plant3.advertise("switch").setName("Pump 3")
|
|
|
|
.setDatatype("boolean")
|
|
|
|
.settable(switch3Handler);
|
|
|
|
plant3.advertise("moist").setName("Percent")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("%");
|
|
|
|
#if (MAX_PLANTS >= 4)
|
|
|
|
plant4.advertise("moist").setName("Percent")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("%");
|
|
|
|
plant5.advertise("moist").setName("Percent")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("%");
|
|
|
|
plant6.advertise("moist").setName("Percent")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("%");
|
|
|
|
plant0.advertise("moist").setName("Percent")
|
|
|
|
.setDatatype("number")
|
|
|
|
.setUnit("%");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
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-13 20:16:28 +02:00
|
|
|
|
2020-10-14 19:01:13 +02:00
|
|
|
// Mode 3
|
2020-10-13 21:47:56 +02:00
|
|
|
stayAlive.advertise("alive").setName("Alive").setDatatype("number").settable(aliveHandler);
|
2020-09-21 20:42:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
Homie.setup();
|
|
|
|
|
|
|
|
/* Intialize inputs and outputs */
|
2020-10-14 20:07:56 +02:00
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
2020-09-07 18:18:46 +02:00
|
|
|
pinMode(mPlants[i].getPumpPin(), OUTPUT);
|
|
|
|
pinMode(mPlants[i].getSensorPin(), ANALOG);
|
|
|
|
digitalWrite(mPlants[i].getPumpPin(), LOW);
|
|
|
|
}
|
|
|
|
/* Setup Solar and Lipo measurement */
|
|
|
|
pinMode(SENSOR_LIPO, ANALOG);
|
|
|
|
pinMode(SENSOR_SOLAR, ANALOG);
|
|
|
|
/* Read analog values at the start */
|
|
|
|
do {
|
|
|
|
readAnalogValues();
|
|
|
|
} while (readCounter != 0);
|
|
|
|
|
|
|
|
|
|
|
|
// Configure Deep Sleep:
|
2020-09-21 20:42:24 +02:00
|
|
|
if (mConfigured && (deepSleepNightTime.get() > 0) &&
|
2020-09-07 18:18:46 +02:00
|
|
|
( SOLAR_VOLT(solarSensor) < MINIMUM_SOLAR_VOLT)) {
|
|
|
|
Serial << "HOMIE | Setup sleeping for " << deepSleepNightTime.get() << " ms as sun is at " << SOLAR_VOLT(solarSensor) << "V" << endl;
|
|
|
|
uint64_t usSleepTime = deepSleepNightTime.get() * 1000U;
|
|
|
|
esp_sleep_enable_timer_wakeup(usSleepTime);
|
2020-09-21 20:42:24 +02:00
|
|
|
}else if (mConfigured && deepSleepTime.get()) {
|
2020-09-07 18:18:46 +02:00
|
|
|
Serial << "HOMIE | Setup sleeping for " << deepSleepTime.get() << " ms" << endl;
|
|
|
|
uint64_t usSleepTime = deepSleepTime.get() * 1000U;
|
|
|
|
esp_sleep_enable_timer_wakeup(usSleepTime);
|
|
|
|
}
|
|
|
|
|
2020-10-14 20:07:56 +02:00
|
|
|
if (mConfigured &&
|
|
|
|
(ADC_5V_TO_3V3(lipoSenor) < MINIMUM_LIPO_VOLT) &&
|
|
|
|
(ADC_5V_TO_3V3(lipoSenor) > NO_LIPO_VOLT) &&
|
|
|
|
(deepSleepTime.get()) ) {
|
2020-09-07 18:18:46 +02:00
|
|
|
long sleepEmptyLipo = (deepSleepTime.get() * EMPTY_LIPO_MULTIPL);
|
|
|
|
Serial << "HOMIE | Change sleeping to " << sleepEmptyLipo << " ms as lipo is at " << ADC_5V_TO_3V3(lipoSenor) << "V" << endl;
|
|
|
|
esp_sleep_enable_timer_wakeup(sleepEmptyLipo * 1000U);
|
|
|
|
mDeepSleep = true;
|
|
|
|
}
|
|
|
|
|
2020-09-20 19:18:47 +02:00
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
|
|
|
|
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-09-21 20:42:24 +02:00
|
|
|
Serial << "DS18B20 | Initialization " << endl;
|
2020-09-07 18:18:46 +02:00
|
|
|
/* Read the temperature sensors once, as first time 85 degree is returned */
|
|
|
|
Serial << "DS18B20 | sensors: " << String(dallas.readDevices()) << endl;
|
|
|
|
delay(200);
|
|
|
|
if (dallas.readAllTemperatures(pFloat, 2) > 0) {
|
|
|
|
Serial << "DS18B20 | Temperature 1: " << String(temp[0]) << endl;
|
|
|
|
Serial << "DS18B20 | Temperature 2: " << String(temp[1]) << endl;
|
|
|
|
}
|
|
|
|
delay(200);
|
|
|
|
if (dallas.readAllTemperatures(pFloat, 2) > 0) {
|
|
|
|
Serial << "Temperature 1: " << String(temp[0]) << endl;
|
|
|
|
Serial << "Temperature 2: " << String(temp[1]) << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Cyclic call
|
|
|
|
* Executs the Homie base functionallity or triggers sleeping, if requested.
|
|
|
|
*/
|
|
|
|
void loop() {
|
2020-09-21 20:42:24 +02:00
|
|
|
if (!mDeepSleep || !mConfigured) {
|
2020-09-07 18:18:46 +02:00
|
|
|
if (Serial.available() > 0) {
|
|
|
|
// read the incoming byte:
|
|
|
|
int incomingByte = Serial.read();
|
|
|
|
|
|
|
|
switch ((char) incomingByte)
|
|
|
|
{
|
|
|
|
case 'P':
|
|
|
|
Serial << "Activate Sensor OUTPUT " << endl;
|
|
|
|
pinMode(OUTPUT_SENSOR, OUTPUT);
|
|
|
|
digitalWrite(OUTPUT_SENSOR, HIGH);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
Serial << "Deactivate Sensor OUTPUT " << endl;
|
|
|
|
pinMode(OUTPUT_SENSOR, OUTPUT);
|
|
|
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((digitalRead(BUTTON) == LOW) && (mButtonClicks % 2) == 0) {
|
|
|
|
float temp[2] = {0, 0};
|
|
|
|
float* pFloat = temp;
|
|
|
|
mButtonClicks++;
|
|
|
|
|
|
|
|
Serial << "SELF TEST (clicks: " << String(mButtonClicks) << ")" << endl;
|
|
|
|
|
|
|
|
Serial << "DS18B20 sensors: " << String(dallas.readDevices()) << endl;
|
|
|
|
delay(200);
|
|
|
|
if (dallas.readAllTemperatures(pFloat, 2) > 0) {
|
|
|
|
Serial << "Temperature 1: " << String(temp[0]) << endl;
|
|
|
|
Serial << "Temperature 2: " << String(temp[1]) << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(mButtonClicks) {
|
|
|
|
case 1:
|
|
|
|
case 3:
|
|
|
|
case 5:
|
|
|
|
if (mButtonClicks > 1) {
|
|
|
|
Serial << "Read analog sensors..." << endl;
|
|
|
|
/* Disable Wifi and bluetooth */
|
|
|
|
WiFi.mode(WIFI_OFF);
|
|
|
|
delay(50);
|
|
|
|
/* now ADC2 can be used */
|
|
|
|
readSensors();
|
|
|
|
}
|
2020-09-20 19:49:29 +02:00
|
|
|
|
2020-09-07 18:18:46 +02:00
|
|
|
Serial << "Water gone: " << String(mWaterGone) << " cm" << endl;
|
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
mPlants[i].calculateSensorValue(AMOUNT_SENOR_QUERYS);
|
|
|
|
|
|
|
|
Serial << "Moist Sensor " << (i+1) << ": " << String(mPlants[i].getSensorValue()) << " Volt: " << String(ADC_5V_TO_3V3(mPlants[i].getSensorValue())) << endl;
|
|
|
|
}
|
|
|
|
/* Read enough values */
|
|
|
|
do {
|
|
|
|
readAnalogValues();
|
|
|
|
Serial << "Read Analog (" << String(readCounter) << ")" << endl;;
|
|
|
|
} while (readCounter != 0);
|
|
|
|
|
|
|
|
Serial << "Lipo Sensor - Raw: " << String(lipoSenor) << " Volt: " << String(ADC_5V_TO_3V3(lipoSenor)) << endl;
|
|
|
|
Serial << "Solar Sensor - Raw: " << String(solarSensor) << " Volt: " << String(SOLAR_VOLT(solarSensor)) << endl;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
Serial << "Activate Sensor OUTPUT " << endl;
|
|
|
|
pinMode(OUTPUT_SENSOR, OUTPUT);
|
|
|
|
digitalWrite(OUTPUT_SENSOR, HIGH);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
Serial << "Activate Pump1 GPIO" << String(mPlants[0].getPumpPin()) << endl;
|
|
|
|
digitalWrite(mPlants[0].getPumpPin(), HIGH);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
Serial << "Activate Pump2 GPIO" << String(mPlants[1].getPumpPin()) << endl;
|
|
|
|
digitalWrite(mPlants[1].getPumpPin(), HIGH);
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
Serial << "Activate Pump3 GPIO" << String(mPlants[2].getPumpPin()) << endl;
|
|
|
|
digitalWrite(mPlants[2].getPumpPin(), HIGH);
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
Serial << "Activate Pump4/Sensor GPIO" << String(OUTPUT_PUMP4) << endl;
|
|
|
|
digitalWrite(OUTPUT_PUMP4, HIGH);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Serial << "No further tests! Please reboot" << endl;
|
|
|
|
}
|
|
|
|
Serial.flush();
|
|
|
|
}else if (mButtonClicks > 0 && (digitalRead(BUTTON) == HIGH) && (mButtonClicks % 2) == 1) {
|
|
|
|
Serial << "Self Test Ended" << endl;
|
|
|
|
mButtonClicks++;
|
|
|
|
/* Always reset all outputs */
|
|
|
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
|
|
|
for(int i=0; i < MAX_PLANTS; i++) {
|
|
|
|
digitalWrite(mPlants[i].getPumpPin(), LOW);
|
|
|
|
}
|
|
|
|
digitalWrite(OUTPUT_PUMP4, LOW);
|
|
|
|
} else if (mButtonClicks == 0) {
|
|
|
|
Homie.loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2020-10-13 21:47:56 +02:00
|
|
|
if (!mAlive) {
|
|
|
|
Serial << (millis()/ 1000) << "s running; sleeeping ..." << endl;
|
|
|
|
Serial.flush();
|
|
|
|
esp_deep_sleep_start();
|
|
|
|
} else {
|
|
|
|
mDeepSleep = false;
|
|
|
|
|
|
|
|
if (((millis()) % 10000) == 0) {
|
|
|
|
/* tell everybody how long we are awoken */
|
|
|
|
stayAlive.setProperty("alive").send( String(millis()/ 1000) );
|
|
|
|
}
|
|
|
|
}
|
2020-09-07 18:18:46 +02:00
|
|
|
}
|
|
|
|
}
|