Read ultrasonnic sensor 5 times
This commit is contained in:
parent
e583122a10
commit
8adf4d6f0d
@ -48,7 +48,7 @@ RTC_DATA_ATTR long rtcMoistureTrigger6 = 0; /**<Level for the moisture sensor
|
|||||||
RTC_DATA_ATTR int lastPumpRunning = 0;
|
RTC_DATA_ATTR int lastPumpRunning = 0;
|
||||||
RTC_DATA_ATTR long lastWaterValue = 0;
|
RTC_DATA_ATTR long lastWaterValue = 0;
|
||||||
|
|
||||||
const char* ntpServer = "pool.ntp.org";
|
const char *ntpServer = "pool.ntp.org";
|
||||||
|
|
||||||
bool warmBoot = true;
|
bool warmBoot = true;
|
||||||
bool mode3Active = false; /**< Controller must not sleep */
|
bool mode3Active = false; /**< Controller must not sleep */
|
||||||
@ -59,7 +59,6 @@ int plantSensor1 = 0;
|
|||||||
int readCounter = 0;
|
int readCounter = 0;
|
||||||
bool mConfigured = false;
|
bool mConfigured = false;
|
||||||
|
|
||||||
|
|
||||||
auto wait4sleep = timer_create_default(); // create a timer with default settings
|
auto wait4sleep = timer_create_default(); // create a timer with default settings
|
||||||
|
|
||||||
RTC_DATA_ATTR int gBootCount = 0;
|
RTC_DATA_ATTR int gBootCount = 0;
|
||||||
@ -71,7 +70,6 @@ RunningMedian waterRawSensor = RunningMedian(5);
|
|||||||
RunningMedian temp1 = RunningMedian(5);
|
RunningMedian temp1 = RunningMedian(5);
|
||||||
RunningMedian temp2 = RunningMedian(5);
|
RunningMedian temp2 = RunningMedian(5);
|
||||||
|
|
||||||
|
|
||||||
Ds18B20 dallas(SENSOR_DS18B20);
|
Ds18B20 dallas(SENSOR_DS18B20);
|
||||||
|
|
||||||
Plant mPlants[MAX_PLANTS] = {
|
Plant mPlants[MAX_PLANTS] = {
|
||||||
@ -81,18 +79,20 @@ Plant mPlants[MAX_PLANTS] = {
|
|||||||
Plant(SENSOR_PLANT3, OUTPUT_PUMP3, 3, &plant3, &mSetting3),
|
Plant(SENSOR_PLANT3, OUTPUT_PUMP3, 3, &plant3, &mSetting3),
|
||||||
Plant(SENSOR_PLANT4, OUTPUT_PUMP4, 4, &plant4, &mSetting4),
|
Plant(SENSOR_PLANT4, OUTPUT_PUMP4, 4, &plant4, &mSetting4),
|
||||||
Plant(SENSOR_PLANT5, OUTPUT_PUMP5, 5, &plant5, &mSetting5),
|
Plant(SENSOR_PLANT5, OUTPUT_PUMP5, 5, &plant5, &mSetting5),
|
||||||
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6, &plant6, &mSetting6)
|
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6, &plant6, &mSetting6)};
|
||||||
};
|
|
||||||
|
|
||||||
float getBatteryVoltage(){
|
float getBatteryVoltage()
|
||||||
|
{
|
||||||
return ADC_5V_TO_3V3(lipoRawSensor.getAverage());
|
return ADC_5V_TO_3V3(lipoRawSensor.getAverage());
|
||||||
}
|
}
|
||||||
|
|
||||||
float getSolarVoltage(){
|
float getSolarVoltage()
|
||||||
|
{
|
||||||
return SOLAR_VOLT(solarRawSensor.getAverage());
|
return SOLAR_VOLT(solarRawSensor.getAverage());
|
||||||
}
|
}
|
||||||
|
|
||||||
void readSystemSensors() {
|
void readSystemSensors()
|
||||||
|
{
|
||||||
lipoRawSensor.add(analogRead(SENSOR_LIPO));
|
lipoRawSensor.add(analogRead(SENSOR_LIPO));
|
||||||
solarRawSensor.add(analogRead(SENSOR_SOLAR));
|
solarRawSensor.add(analogRead(SENSOR_SOLAR));
|
||||||
}
|
}
|
||||||
@ -100,209 +100,255 @@ void readSystemSensors() {
|
|||||||
int determineNextPump();
|
int determineNextPump();
|
||||||
void setLastActivationForPump(int pumpId, long time);
|
void setLastActivationForPump(int pumpId, long time);
|
||||||
|
|
||||||
|
long getCurrentTime()
|
||||||
long getCurrentTime(){
|
{
|
||||||
struct timeval tv_now;
|
struct timeval tv_now;
|
||||||
gettimeofday(&tv_now, NULL);
|
gettimeofday(&tv_now, NULL);
|
||||||
return tv_now.tv_sec;
|
return tv_now.tv_sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
//wait till homie flushed mqtt ect.
|
//wait till homie flushed mqtt ect.
|
||||||
bool prepareSleep(void *) {
|
bool prepareSleep(void *)
|
||||||
|
{
|
||||||
//FIXME wait till pending mqtt is done, then start sleep via event or whatever
|
//FIXME wait till pending mqtt is done, then start sleep via event or whatever
|
||||||
//Homie.disableResetTrigger();
|
//Homie.disableResetTrigger();
|
||||||
|
|
||||||
bool queueIsEmpty = true;
|
bool queueIsEmpty = true;
|
||||||
if(queueIsEmpty){
|
if (queueIsEmpty)
|
||||||
|
{
|
||||||
mDeepsleep = true;
|
mDeepsleep = true;
|
||||||
}
|
}
|
||||||
return false; // repeat? true there is something in the queue to be done
|
return false; // repeat? true there is something in the queue to be done
|
||||||
}
|
}
|
||||||
|
|
||||||
void espDeepSleepFor(long seconds, bool activatePump = false){
|
void espDeepSleepFor(long seconds, bool activatePump = false)
|
||||||
|
{
|
||||||
delay(1500);
|
delay(1500);
|
||||||
|
|
||||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
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_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_XTAL, ESP_PD_OPTION_ON);
|
||||||
if (activatePump) {
|
if (activatePump)
|
||||||
|
{
|
||||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
|
||||||
gpio_deep_sleep_hold_en();
|
gpio_deep_sleep_hold_en();
|
||||||
gpio_hold_en(GPIO_NUM_13); //pump pwr
|
gpio_hold_en(GPIO_NUM_13); //pump pwr
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
|
||||||
gpio_hold_dis(GPIO_NUM_13); //pump pwr
|
gpio_hold_dis(GPIO_NUM_13); //pump pwr
|
||||||
gpio_deep_sleep_hold_dis();
|
gpio_deep_sleep_hold_dis();
|
||||||
digitalWrite(OUTPUT_PUMP, LOW);
|
digitalWrite(OUTPUT_PUMP, LOW);
|
||||||
for (int i=0; i < MAX_PLANTS; i++) {
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
|
{
|
||||||
mPlants[i].deactivatePump();
|
mPlants[i].deactivatePump();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//gpio_hold_en(GPIO_NUM_23); //p0
|
//gpio_hold_en(GPIO_NUM_23); //p0
|
||||||
//FIXME fix for outher outputs
|
//FIXME fix for outher outputs
|
||||||
|
|
||||||
|
|
||||||
Serial.print("Going to sleep for ");
|
Serial.print("Going to sleep for ");
|
||||||
Serial.print(seconds);
|
Serial.print(seconds);
|
||||||
Serial.println(" seconds");
|
Serial.println(" seconds");
|
||||||
esp_sleep_enable_timer_wakeup( (seconds * 1000U * 1000U) );
|
esp_sleep_enable_timer_wakeup((seconds * 1000U * 1000U));
|
||||||
wait4sleep.in(500, prepareSleep);
|
wait4sleep.in(500, prepareSleep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mode2MQTT()
|
||||||
|
{
|
||||||
void mode2MQTT(){
|
|
||||||
readSystemSensors();
|
readSystemSensors();
|
||||||
|
|
||||||
configTime(0, 0, ntpServer);
|
configTime(0, 0, ntpServer);
|
||||||
|
|
||||||
digitalWrite(OUTPUT_PUMP, LOW);
|
digitalWrite(OUTPUT_PUMP, LOW);
|
||||||
for (int i=0; i < MAX_PLANTS; i++) {
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
|
{
|
||||||
mPlants[i].deactivatePump();
|
mPlants[i].deactivatePump();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (deepSleepTime.get()) {
|
if (deepSleepTime.get())
|
||||||
|
{
|
||||||
Serial << "sleeping for " << deepSleepTime.get() << endl;
|
Serial << "sleeping for " << deepSleepTime.get() << endl;
|
||||||
}
|
}
|
||||||
/* Publish default values */
|
/* Publish default values */
|
||||||
|
|
||||||
if(lastPumpRunning != -1){
|
if (lastPumpRunning != -1)
|
||||||
|
{
|
||||||
long waterDiff = waterRawSensor.getAverage() - lastWaterValue;
|
long waterDiff = waterRawSensor.getAverage() - lastWaterValue;
|
||||||
//TODO attribute used water in ml to plantid
|
//TODO attribute used water in ml to plantid
|
||||||
}
|
}
|
||||||
for(int i=0; i < MAX_PLANTS; i++) {
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 ));
|
{
|
||||||
|
mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095));
|
||||||
}
|
}
|
||||||
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - waterRawSensor.getAverage() ));
|
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - waterRawSensor.getAverage()));
|
||||||
Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage() ) << "%)" << endl;
|
Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << "%)" << endl;
|
||||||
lastWaterValue = waterRawSensor.getAverage();
|
lastWaterValue = waterRawSensor.getAverage();
|
||||||
|
|
||||||
sensorLipo.setProperty("percent").send( String(100 * lipoRawSensor.getAverage() / 4095) );
|
sensorLipo.setProperty("percent").send(String(100 * lipoRawSensor.getAverage() / 4095));
|
||||||
sensorLipo.setProperty("volt").send( String(getBatteryVoltage()) );
|
sensorLipo.setProperty("volt").send(String(getBatteryVoltage()));
|
||||||
sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage() ) / 4095));
|
sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage()) / 4095));
|
||||||
sensorSolar.setProperty("volt").send( String(getSolarVoltage()) );
|
sensorSolar.setProperty("volt").send(String(getSolarVoltage()));
|
||||||
|
|
||||||
float temp[2] = { TEMP_INIT_VALUE, TEMP_INIT_VALUE };
|
float temp[2] = {TEMP_INIT_VALUE, TEMP_INIT_VALUE};
|
||||||
float* pFloat = temp;
|
float *pFloat = temp;
|
||||||
int devices = dallas.readAllTemperatures(pFloat, 2);
|
int devices = dallas.readAllTemperatures(pFloat, 2);
|
||||||
if (devices < 2) {
|
if (devices < 2)
|
||||||
if ((pFloat[0] > TEMP_INIT_VALUE) && (pFloat[0] < TEMP_MAX_VALUE) ) {
|
{
|
||||||
sensorTemp.setProperty("control").send( String(pFloat[0]));
|
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) ) {
|
else if (devices >= 2)
|
||||||
sensorTemp.setProperty("control").send( String(pFloat[1]));
|
{
|
||||||
|
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]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool lipoTempWarning = abs(temp[0] - temp[1]) > 5;
|
bool lipoTempWarning = abs(temp[0] - temp[1]) > 5;
|
||||||
if(lipoTempWarning){
|
if (lipoTempWarning)
|
||||||
|
{
|
||||||
Serial.println("Lipo temp incorrect, panic mode deepsleep");
|
Serial.println("Lipo temp incorrect, panic mode deepsleep");
|
||||||
espDeepSleepFor(PANIK_MODE_DEEPSLEEP);
|
espDeepSleepFor(PANIK_MODE_DEEPSLEEP);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasWater = true;//FIXMEmWaterGone > waterLevelMin.get();
|
bool hasWater = true; //FIXMEmWaterGone > waterLevelMin.get();
|
||||||
//FIXME no water warning message
|
//FIXME no water warning message
|
||||||
lastPumpRunning = determineNextPump();
|
lastPumpRunning = determineNextPump();
|
||||||
if(lastPumpRunning != -1 && !hasWater){
|
if (lastPumpRunning != -1 && !hasWater)
|
||||||
|
{
|
||||||
Serial.println("Want to pump but no water");
|
Serial.println("Want to pump but no water");
|
||||||
}
|
}
|
||||||
if(lastPumpRunning != -1 && hasWater){
|
if (lastPumpRunning != -1 && hasWater)
|
||||||
|
{
|
||||||
digitalWrite(OUTPUT_PUMP, HIGH);
|
digitalWrite(OUTPUT_PUMP, HIGH);
|
||||||
setLastActivationForPump(lastPumpRunning, getCurrentTime());
|
setLastActivationForPump(lastPumpRunning, getCurrentTime());
|
||||||
mPlants[lastPumpRunning].activatePump();
|
mPlants[lastPumpRunning].activatePump();
|
||||||
}
|
}
|
||||||
if(lastPumpRunning == -1 || !hasWater){
|
if (lastPumpRunning == -1 || !hasWater)
|
||||||
if(getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE){
|
{
|
||||||
gotoMode2AfterThisTimestamp = getCurrentTime()+deepSleepNightTime.get();
|
if (getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE)
|
||||||
|
{
|
||||||
|
gotoMode2AfterThisTimestamp = getCurrentTime() + deepSleepNightTime.get();
|
||||||
Serial.println("No pumps to activate and low light, deepSleepNight");
|
Serial.println("No pumps to activate and low light, deepSleepNight");
|
||||||
espDeepSleepFor(deepSleepNightTime.get());
|
espDeepSleepFor(deepSleepNightTime.get());
|
||||||
}else {
|
}
|
||||||
gotoMode2AfterThisTimestamp = getCurrentTime()+deepSleepTime.get();
|
else
|
||||||
|
{
|
||||||
|
gotoMode2AfterThisTimestamp = getCurrentTime() + deepSleepTime.get();
|
||||||
Serial.println("No pumps to activate, deepSleep");
|
Serial.println("No pumps to activate, deepSleep");
|
||||||
espDeepSleepFor(deepSleepTime.get());
|
espDeepSleepFor(deepSleepTime.get());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}else {
|
else
|
||||||
|
{
|
||||||
gotoMode2AfterThisTimestamp = 0;
|
gotoMode2AfterThisTimestamp = 0;
|
||||||
Serial.println("Running pump, watering deepsleep");
|
Serial.println("Running pump, watering deepsleep");
|
||||||
espDeepSleepFor(wateringDeepSleep.get(), true);
|
espDeepSleepFor(wateringDeepSleep.get(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMoistureTrigger(int plantId, long value){
|
void setMoistureTrigger(int plantId, long value)
|
||||||
if(plantId == 0){
|
{
|
||||||
|
if (plantId == 0)
|
||||||
|
{
|
||||||
rtcMoistureTrigger0 = value;
|
rtcMoistureTrigger0 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 1){
|
if (plantId == 1)
|
||||||
|
{
|
||||||
rtcMoistureTrigger1 = value;
|
rtcMoistureTrigger1 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 2){
|
if (plantId == 2)
|
||||||
|
{
|
||||||
rtcMoistureTrigger2 = value;
|
rtcMoistureTrigger2 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 3){
|
if (plantId == 3)
|
||||||
|
{
|
||||||
rtcMoistureTrigger3 = value;
|
rtcMoistureTrigger3 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 4){
|
if (plantId == 4)
|
||||||
|
{
|
||||||
rtcMoistureTrigger4 = value;
|
rtcMoistureTrigger4 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 5){
|
if (plantId == 5)
|
||||||
|
{
|
||||||
rtcMoistureTrigger5 = value;
|
rtcMoistureTrigger5 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 6){
|
if (plantId == 6)
|
||||||
|
{
|
||||||
rtcMoistureTrigger6 = value;
|
rtcMoistureTrigger6 = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLastActivationForPump(int plantId, long value){
|
void setLastActivationForPump(int plantId, long value)
|
||||||
if(plantId == 0){
|
{
|
||||||
|
if (plantId == 0)
|
||||||
|
{
|
||||||
rtcLastActive0 = value;
|
rtcLastActive0 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 1){
|
if (plantId == 1)
|
||||||
|
{
|
||||||
rtcLastActive1 = value;
|
rtcLastActive1 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 2){
|
if (plantId == 2)
|
||||||
|
{
|
||||||
rtcLastActive2 = value;
|
rtcLastActive2 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 3){
|
if (plantId == 3)
|
||||||
|
{
|
||||||
rtcLastActive3 = value;
|
rtcLastActive3 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 4){
|
if (plantId == 4)
|
||||||
|
{
|
||||||
rtcLastActive4 = value;
|
rtcLastActive4 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 5){
|
if (plantId == 5)
|
||||||
|
{
|
||||||
rtcLastActive5 = value;
|
rtcLastActive5 = value;
|
||||||
}
|
}
|
||||||
if(plantId == 6){
|
if (plantId == 6)
|
||||||
|
{
|
||||||
rtcLastActive6 = value;
|
rtcLastActive6 = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long getLastActivationForPump(int plantId){
|
long getLastActivationForPump(int plantId)
|
||||||
if(plantId == 0){
|
{
|
||||||
|
if (plantId == 0)
|
||||||
|
{
|
||||||
return rtcLastActive0;
|
return rtcLastActive0;
|
||||||
}
|
}
|
||||||
if(plantId == 1){
|
if (plantId == 1)
|
||||||
|
{
|
||||||
return rtcLastActive1;
|
return rtcLastActive1;
|
||||||
}
|
}
|
||||||
if(plantId == 2){
|
if (plantId == 2)
|
||||||
|
{
|
||||||
return rtcLastActive2;
|
return rtcLastActive2;
|
||||||
}
|
}
|
||||||
if(plantId == 3){
|
if (plantId == 3)
|
||||||
|
{
|
||||||
return rtcLastActive3;
|
return rtcLastActive3;
|
||||||
}
|
}
|
||||||
if(plantId == 4){
|
if (plantId == 4)
|
||||||
|
{
|
||||||
return rtcLastActive4;
|
return rtcLastActive4;
|
||||||
}
|
}
|
||||||
if(plantId == 5){
|
if (plantId == 5)
|
||||||
|
{
|
||||||
return rtcLastActive5;
|
return rtcLastActive5;
|
||||||
}
|
}
|
||||||
if(plantId == 6){
|
if (plantId == 6)
|
||||||
|
{
|
||||||
return rtcLastActive6;
|
return rtcLastActive6;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
@ -312,7 +358,8 @@ long getLastActivationForPump(int plantId){
|
|||||||
* @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
|
* @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
|
||||||
* These sensors (ADC2) can only be read when no Wifi is used.
|
* These sensors (ADC2) can only be read when no Wifi is used.
|
||||||
*/
|
*/
|
||||||
void readSensors() {
|
void readSensors()
|
||||||
|
{
|
||||||
Serial << "Read Sensors" << endl;
|
Serial << "Read Sensors" << endl;
|
||||||
|
|
||||||
readSystemSensors();
|
readSystemSensors();
|
||||||
@ -323,8 +370,10 @@ void readSensors() {
|
|||||||
|
|
||||||
delay(100);
|
delay(100);
|
||||||
/* wait before reading something */
|
/* wait before reading something */
|
||||||
for (int readCnt=0;readCnt < AMOUNT_SENOR_QUERYS; readCnt++) {
|
for (int readCnt = 0; readCnt < AMOUNT_SENOR_QUERYS; readCnt++)
|
||||||
for(int i=0; i < MAX_PLANTS; i++) {
|
{
|
||||||
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
|
{
|
||||||
mPlants[i].addSenseValue();
|
mPlants[i].addSenseValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -334,16 +383,18 @@ void readSensors() {
|
|||||||
Serial << "DS18B20" << String(dallas.readDevices()) << endl;
|
Serial << "DS18B20" << String(dallas.readDevices()) << endl;
|
||||||
delay(200);
|
delay(200);
|
||||||
|
|
||||||
|
|
||||||
/* Required to read the temperature once */
|
/* Required to read the temperature once */
|
||||||
float temp[2] = {0, 0};
|
float temp[2] = {0, 0};
|
||||||
float* pFloat = temp;
|
float *pFloat = temp;
|
||||||
for(int i=0; i < 10; i++) {
|
for (int i = 0; i < 10; i++)
|
||||||
if (dallas.readAllTemperatures(pFloat, 2) > 0) {
|
{
|
||||||
|
if (dallas.readAllTemperatures(pFloat, 2) > 0)
|
||||||
|
{
|
||||||
Serial << "t1: " << String(temp[0]) << endl;
|
Serial << "t1: " << String(temp[0]) << endl;
|
||||||
Serial << "t2: " << String(temp[1]) << endl;
|
Serial << "t2: " << String(temp[1]) << endl;
|
||||||
// first read returns crap, ignore result and read again
|
// first read returns crap, ignore result and read again
|
||||||
if (i <= 2) {
|
if (i <= 2)
|
||||||
|
{
|
||||||
temp1.add(temp[0]);
|
temp1.add(temp[0]);
|
||||||
temp2.add(temp[1]);
|
temp2.add(temp[1]);
|
||||||
}
|
}
|
||||||
@ -352,23 +403,28 @@ void readSensors() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Use the Ultrasonic sensor to measure waterLevel */
|
/* Use the Ultrasonic sensor to measure waterLevel */
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
digitalWrite(SENSOR_SR04_TRIG, LOW);
|
digitalWrite(SENSOR_SR04_TRIG, LOW);
|
||||||
delayMicroseconds(2);
|
delayMicroseconds(2);
|
||||||
digitalWrite(SENSOR_SR04_TRIG, HIGH);
|
digitalWrite(SENSOR_SR04_TRIG, HIGH);
|
||||||
delayMicroseconds(10);
|
delayMicroseconds(10);
|
||||||
digitalWrite(SENSOR_SR04_TRIG, LOW);
|
digitalWrite(SENSOR_SR04_TRIG, LOW);
|
||||||
float duration = pulseIn(SENSOR_SR04_ECHO, HIGH);
|
float duration = pulseIn(SENSOR_SR04_ECHO, HIGH);
|
||||||
waterRawSensor.add((duration*.343)/2);
|
waterRawSensor.add((duration * .343) / 2);
|
||||||
Serial << "Distance sensor " << duration << " ms : " << waterRawSensor.getAverage() << " cm" << endl;
|
Serial << "Distance sensor " << duration << " ms : " << waterRawSensor.getAverage() << " cm" << endl;
|
||||||
|
delay(20);
|
||||||
|
}
|
||||||
/* deactivate the sensors */
|
/* deactivate the sensors */
|
||||||
digitalWrite(OUTPUT_SENSOR, LOW);
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Homie.getMqttClient().disconnect();
|
//Homie.getMqttClient().disconnect();
|
||||||
|
|
||||||
void onHomieEvent(const HomieEvent& event) {
|
void onHomieEvent(const HomieEvent &event)
|
||||||
switch(event.type) {
|
{
|
||||||
|
switch (event.type)
|
||||||
|
{
|
||||||
case HomieEventType::SENDING_STATISTICS:
|
case HomieEventType::SENDING_STATISTICS:
|
||||||
Homie.getLogger() << "My statistics" << endl;
|
Homie.getLogger() << "My statistics" << endl;
|
||||||
break;
|
break;
|
||||||
@ -376,7 +432,8 @@ void onHomieEvent(const HomieEvent& event) {
|
|||||||
//wait for rtc sync?
|
//wait for rtc sync?
|
||||||
rtcDeepSleepTime = deepSleepTime.get();
|
rtcDeepSleepTime = deepSleepTime.get();
|
||||||
Serial << "MQTT ready " << rtcDeepSleepTime << " ms ds" << endl;
|
Serial << "MQTT ready " << rtcDeepSleepTime << " ms ds" << endl;
|
||||||
for(int i=0; i < MAX_PLANTS; i++) {
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
|
{
|
||||||
mPlants[i].postMQTTconnection();
|
mPlants[i].postMQTTconnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -391,10 +448,11 @@ void onHomieEvent(const HomieEvent& event) {
|
|||||||
digitalWrite(OUTPUT_PUMP, LOW);
|
digitalWrite(OUTPUT_PUMP, LOW);
|
||||||
gpio_hold_dis(GPIO_NUM_13); //pump pwr
|
gpio_hold_dis(GPIO_NUM_13); //pump pwr
|
||||||
gpio_deep_sleep_hold_dis();
|
gpio_deep_sleep_hold_dis();
|
||||||
for (int i=0; i < MAX_PLANTS; i++) {
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
|
{
|
||||||
mPlants[i].deactivatePump();
|
mPlants[i].deactivatePump();
|
||||||
}
|
}
|
||||||
mode3Active=true;
|
mode3Active = true;
|
||||||
break;
|
break;
|
||||||
case HomieEventType::OTA_SUCCESSFUL:
|
case HomieEventType::OTA_SUCCESSFUL:
|
||||||
digitalWrite(OUTPUT_SENSOR, LOW);
|
digitalWrite(OUTPUT_SENSOR, LOW);
|
||||||
@ -406,27 +464,32 @@ void onHomieEvent(const HomieEvent& event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int determineNextPump(){
|
int determineNextPump()
|
||||||
|
{
|
||||||
float solarValue = getSolarVoltage();
|
float solarValue = getSolarVoltage();
|
||||||
bool isLowLight =(solarValue > SOLAR_CHARGE_MIN_VOLTAGE || solarValue < SOLAR_CHARGE_MAX_VOLTAGE);
|
bool isLowLight = (solarValue > SOLAR_CHARGE_MIN_VOLTAGE || solarValue < SOLAR_CHARGE_MAX_VOLTAGE);
|
||||||
|
|
||||||
//FIXME instead of for, use sorted by last activation index to ensure equal runtime?
|
//FIXME instead of for, use sorted by last activation index to ensure equal runtime?
|
||||||
for(int i=0; i < MAX_PLANTS; i++) {
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
|
{
|
||||||
long lastActivation = getLastActivationForPump(i);
|
long lastActivation = getLastActivationForPump(i);
|
||||||
long sinceLastActivation = getCurrentTime()-lastActivation;
|
long sinceLastActivation = getCurrentTime() - lastActivation;
|
||||||
//this pump is in cooldown skip it and disable low power mode trigger for it
|
//this pump is in cooldown skip it and disable low power mode trigger for it
|
||||||
if(mPlants[i].isInCooldown(sinceLastActivation) ){
|
if (mPlants[i].isInCooldown(sinceLastActivation))
|
||||||
|
{
|
||||||
Serial.printf("%d Skipping due to cooldown\r\n", i);
|
Serial.printf("%d Skipping due to cooldown\r\n", i);
|
||||||
setMoistureTrigger(i, DEACTIVATED_PLANT);
|
setMoistureTrigger(i, DEACTIVATED_PLANT);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//skip as it is not low light
|
//skip as it is not low light
|
||||||
if(!isLowLight && mPlants[i].isAllowedOnlyAtLowLight()){
|
if (!isLowLight && mPlants[i].isAllowedOnlyAtLowLight())
|
||||||
|
{
|
||||||
Serial.println("Skipping due to light");
|
Serial.println("Skipping due to light");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mPlants->isPumpRequired()){
|
if (mPlants->isPumpRequired())
|
||||||
|
{
|
||||||
Serial.printf("%d Requested pumping\r\n", i);
|
Serial.printf("%d Requested pumping\r\n", i);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@ -443,23 +506,29 @@ int determineNextPump(){
|
|||||||
* @return true when the command was parsed and executed succuessfully
|
* @return true when the command was parsed and executed succuessfully
|
||||||
* @return false on errors when parsing the request
|
* @return false on errors when parsing the request
|
||||||
*/
|
*/
|
||||||
bool aliveHandler(const HomieRange& range, const String& value) {
|
bool aliveHandler(const HomieRange &range, const String &value)
|
||||||
if (range.isRange) return false; // only one controller is present
|
{
|
||||||
|
if (range.isRange)
|
||||||
|
return false; // only one controller is present
|
||||||
Serial << value << endl;
|
Serial << value << endl;
|
||||||
if (value.equals("ON") || value.equals("On") || value.equals("1")) {
|
if (value.equals("ON") || value.equals("On") || value.equals("1"))
|
||||||
mode3Active=true;
|
{
|
||||||
} else {
|
mode3Active = true;
|
||||||
mode3Active=false;
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mode3Active = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void homieLoop(){
|
void homieLoop()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void systemInit(){
|
void systemInit()
|
||||||
|
{
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
Homie_setFirmware("PlantControl", FIRMWARE_VERSION);
|
Homie_setFirmware("PlantControl", FIRMWARE_VERSION);
|
||||||
@ -481,8 +550,10 @@ void systemInit(){
|
|||||||
Homie.setup();
|
Homie.setup();
|
||||||
|
|
||||||
mConfigured = Homie.isConfigured();
|
mConfigured = Homie.isConfigured();
|
||||||
if (mConfigured) {
|
if (mConfigured)
|
||||||
for(int i=0; i < MAX_PLANTS; i++) {
|
{
|
||||||
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
|
{
|
||||||
mPlants[i].advertise();
|
mPlants[i].advertise();
|
||||||
}
|
}
|
||||||
sensorTemp.advertise("control")
|
sensorTemp.advertise("control")
|
||||||
@ -516,8 +587,8 @@ void systemInit(){
|
|||||||
stayAlive.advertise("alive").setName("Alive").setDatatype("number").settable(aliveHandler);
|
stayAlive.advertise("alive").setName("Alive").setDatatype("number").settable(aliveHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mode1()
|
||||||
bool mode1(){
|
{
|
||||||
Serial.println("m1");
|
Serial.println("m1");
|
||||||
Serial << getCurrentTime() << " curtime" << endl;
|
Serial << getCurrentTime() << " curtime" << endl;
|
||||||
|
|
||||||
@ -534,62 +605,72 @@ bool mode1(){
|
|||||||
(rtcMoistureTrigger3 == 0) ||
|
(rtcMoistureTrigger3 == 0) ||
|
||||||
(rtcMoistureTrigger4 == 0) ||
|
(rtcMoistureTrigger4 == 0) ||
|
||||||
(rtcMoistureTrigger5 == 0) ||
|
(rtcMoistureTrigger5 == 0) ||
|
||||||
(rtcMoistureTrigger6 == 0)
|
(rtcMoistureTrigger6 == 0))
|
||||||
)
|
|
||||||
{
|
{
|
||||||
Serial.println("RTCm2");
|
Serial.println("RTCm2");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((rtcMoistureTrigger0 != DEACTIVATED_PLANT) && (mPlants[0].getSensorValue() < rtcMoistureTrigger0) ) {
|
if ((rtcMoistureTrigger0 != DEACTIVATED_PLANT) && (mPlants[0].getSensorValue() < rtcMoistureTrigger0))
|
||||||
|
{
|
||||||
Serial.println("mt0");
|
Serial.println("mt0");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((rtcMoistureTrigger1 != DEACTIVATED_PLANT) && (mPlants[1].getSensorValue() < rtcMoistureTrigger1) ) {
|
if ((rtcMoistureTrigger1 != DEACTIVATED_PLANT) && (mPlants[1].getSensorValue() < rtcMoistureTrigger1))
|
||||||
|
{
|
||||||
Serial.println("mt1");
|
Serial.println("mt1");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((rtcMoistureTrigger2 != DEACTIVATED_PLANT) && (mPlants[2].getSensorValue() < rtcMoistureTrigger2) ) {
|
if ((rtcMoistureTrigger2 != DEACTIVATED_PLANT) && (mPlants[2].getSensorValue() < rtcMoistureTrigger2))
|
||||||
|
{
|
||||||
Serial.println("mt2");
|
Serial.println("mt2");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((rtcMoistureTrigger3 != DEACTIVATED_PLANT) && (mPlants[3].getSensorValue() < rtcMoistureTrigger3) ) {
|
if ((rtcMoistureTrigger3 != DEACTIVATED_PLANT) && (mPlants[3].getSensorValue() < rtcMoistureTrigger3))
|
||||||
|
{
|
||||||
Serial.println("mt3");
|
Serial.println("mt3");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((rtcMoistureTrigger4 != DEACTIVATED_PLANT) && (mPlants[4].getSensorValue() < rtcMoistureTrigger4) ) {
|
if ((rtcMoistureTrigger4 != DEACTIVATED_PLANT) && (mPlants[4].getSensorValue() < rtcMoistureTrigger4))
|
||||||
|
{
|
||||||
Serial.println("mt4");
|
Serial.println("mt4");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((rtcMoistureTrigger5 != DEACTIVATED_PLANT) && (mPlants[5].getSensorValue() < rtcMoistureTrigger5) ) {
|
if ((rtcMoistureTrigger5 != DEACTIVATED_PLANT) && (mPlants[5].getSensorValue() < rtcMoistureTrigger5))
|
||||||
|
{
|
||||||
Serial.println("mt5");
|
Serial.println("mt5");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((rtcMoistureTrigger6 != DEACTIVATED_PLANT) && (mPlants[6].getSensorValue() < rtcMoistureTrigger6) ) {
|
if ((rtcMoistureTrigger6 != DEACTIVATED_PLANT) && (mPlants[6].getSensorValue() < rtcMoistureTrigger6))
|
||||||
|
{
|
||||||
Serial.println("mt6");
|
Serial.println("mt6");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//check how long it was already in mode1 if to long goto mode2
|
//check how long it was already in mode1 if to long goto mode2
|
||||||
|
|
||||||
long cTime = getCurrentTime();
|
long cTime = getCurrentTime();
|
||||||
if(cTime < 100000){
|
if (cTime < 100000)
|
||||||
|
{
|
||||||
Serial.println("Starting mode 2 due to missing ntp");
|
Serial.println("Starting mode 2 due to missing ntp");
|
||||||
//missing ntp time boot to mode3
|
//missing ntp time boot to mode3
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if(gotoMode2AfterThisTimestamp < cTime){
|
if (gotoMode2AfterThisTimestamp < cTime)
|
||||||
|
{
|
||||||
Serial.println("Starting mode 2 after specified mode1 time");
|
Serial.println("Starting mode 2 after specified mode1 time");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mode2(){
|
void mode2()
|
||||||
|
{
|
||||||
Serial.println("m2");
|
Serial.println("m2");
|
||||||
systemInit();
|
systemInit();
|
||||||
|
|
||||||
/* Jump into Mode 3, if not configured */
|
/* Jump into Mode 3, if not configured */
|
||||||
if (!mConfigured) {
|
if (!mConfigured)
|
||||||
|
{
|
||||||
Serial.println("m3");
|
Serial.println("m3");
|
||||||
mode3Active = true;
|
mode3Active = true;
|
||||||
}
|
}
|
||||||
@ -599,12 +680,15 @@ void mode2(){
|
|||||||
* @brief Startup function
|
* @brief Startup function
|
||||||
* Is called once, the controller is started
|
* Is called once, the controller is started
|
||||||
*/
|
*/
|
||||||
void setup() {
|
void setup()
|
||||||
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.setTimeout(1000); // Set timeout of 1 second
|
Serial.setTimeout(1000); // Set timeout of 1 second
|
||||||
Serial << endl << endl;
|
Serial << endl
|
||||||
|
<< endl;
|
||||||
/* Intialize Plant */
|
/* Intialize Plant */
|
||||||
for(int i=0; i < MAX_PLANTS; i++) {
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
|
{
|
||||||
mPlants[i].init();
|
mPlants[i].init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -620,7 +704,8 @@ void setup() {
|
|||||||
/* Disable Wifi and bluetooth */
|
/* Disable Wifi and bluetooth */
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
|
|
||||||
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS) {
|
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS)
|
||||||
|
{
|
||||||
//increase the config settings to 50 and the json to 3000
|
//increase the config settings to 50 and the json to 3000
|
||||||
Serial << "Limits.hpp" << endl;
|
Serial << "Limits.hpp" << endl;
|
||||||
}
|
}
|
||||||
@ -629,19 +714,23 @@ void setup() {
|
|||||||
|
|
||||||
//Panik mode, the Lipo is empty, sleep a long long time:
|
//Panik mode, the Lipo is empty, sleep a long long time:
|
||||||
if ((getBatteryVoltage() < MINIMUM_LIPO_VOLT) &&
|
if ((getBatteryVoltage() < MINIMUM_LIPO_VOLT) &&
|
||||||
(getBatteryVoltage() > NO_LIPO_VOLT)) {
|
(getBatteryVoltage() > NO_LIPO_VOLT))
|
||||||
|
{
|
||||||
Serial << PANIK_MODE_DEEPSLEEP << " s lipo " << getBatteryVoltage() << "V" << endl;
|
Serial << PANIK_MODE_DEEPSLEEP << " s lipo " << getBatteryVoltage() << "V" << endl;
|
||||||
esp_sleep_enable_timer_wakeup(PANIK_MODE_DEEPSLEEP_US);
|
esp_sleep_enable_timer_wakeup(PANIK_MODE_DEEPSLEEP_US);
|
||||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
|
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_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_XTAL, ESP_PD_OPTION_ON);
|
||||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mode1()){
|
if (mode1())
|
||||||
|
{
|
||||||
mode2();
|
mode2();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial.println("nop");
|
Serial.println("nop");
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
@ -653,15 +742,20 @@ void setup() {
|
|||||||
* Executs the Homie base functionallity or triggers sleeping, if requested.
|
* Executs the Homie base functionallity or triggers sleeping, if requested.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void loop() {
|
void loop()
|
||||||
if (!mDeepsleep) {
|
{
|
||||||
|
if (!mDeepsleep)
|
||||||
|
{
|
||||||
Homie.loop();
|
Homie.loop();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(millis() > 30000 && !mode3Active){
|
if (millis() > 30000 && !mode3Active)
|
||||||
Serial << (millis()/ 1000) << "s alive" << endl;
|
{
|
||||||
|
Serial << (millis() / 1000) << "s alive" << endl;
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user