Merged documents into ultrasonic sensor stuff

This commit is contained in:
Ollo
2020-11-11 21:42:29 +01:00
21 changed files with 6752 additions and 1363 deletions

View File

@@ -11,46 +11,45 @@
#include "DS18B20.h"
#define STARTCONV 0x44
#define READSCRATCH 0xBE // Read EEPROM
#define TEMP_LSB 0
#define TEMP_MSB 1
#define SCRATCHPADSIZE 9
#define OFFSET_CRC8 8 /**< 9th byte has the CRC of the complete data */
#define STARTCONV 0x44
#define READSCRATCH 0xBE // Read EEPROM
#define TEMP_LSB 0
#define TEMP_MSB 1
#define SCRATCHPADSIZE 9
#define OFFSET_CRC8 8 /**< 9th byte has the CRC of the complete data */
//Printf debugging
//#define DS_DEBUG
int Ds18B20::readDevices() {
int Ds18B20::readDevices()
{
byte addr[8];
int amount = -1;
while (this->mDs->search(addr)) {
while (this->mDs->search(addr))
{
amount++;
}
this->mDs->reset_search();
return amount;
}
int Ds18B20::readAllTemperatures(float* pTemperatures, int maxTemperatures) {
int Ds18B20::readAllTemperatures(float *pTemperatures, int maxTemperatures)
{
byte addr[8];
uint8_t scratchPad[SCRATCHPADSIZE];
int currentTemp = 0;
#ifdef DS_DEBUG
int i;
#endif
while (this->mDs->search(addr)) {
#ifdef DS_DEBUG
Serial.print(" ROM =");
for (i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
}
#endif
while (this->mDs->search(addr))
{
this->mDs->reset();
this->mDs->select(addr);
this->mDs->write(STARTCONV);
}
delay(750);
while (this->mDs->search(addr))
{
this->mDs->reset();
this->mDs->select(addr);
this->mDs->write(READSCRATCH);
@@ -68,36 +67,32 @@ int Ds18B20::readAllTemperatures(float* pTemperatures, int maxTemperatures) {
// byte 7: DS18S20: COUNT_PER_C
// DS18B20 & DS1822: store for crc
// byte 8: SCRATCHPAD_CRC
#ifdef DS_DEBUG
Serial.write("\r\nDATA:");
for (uint8_t i = 0; i < 9; i++) {
Serial.print(scratchPad[i], HEX);
}
#else
delay(50);
#endif
for (uint8_t i = 0; i < 9; i++) {
for (uint8_t i = 0; i < 9; i++)
{
scratchPad[i] = this->mDs->read();
}
uint8_t crc8 = this->mDs->crc8(scratchPad, 8);
/* Only work an valid data */
if (crc8 == scratchPad[OFFSET_CRC8]) {
int16_t fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11)
| (((int16_t) scratchPad[TEMP_LSB]) << 3);
float celsius = (float) fpTemperature * 0.0078125;
if (crc8 == scratchPad[OFFSET_CRC8])
{
int16_t fpTemperature = (((int16_t)scratchPad[TEMP_MSB]) << 11) | (((int16_t)scratchPad[TEMP_LSB]) << 3);
float celsius = (float)fpTemperature * 0.0078125;
#ifdef DS_DEBUG
Serial.printf("\r\nTemp%d %f °C (Raw: %d, %x =? %x)\r\n", (currentTemp+1), celsius, fpTemperature, crc8, scratchPad[8]);
Serial.printf("\r\nTemp%d %f °C (Raw: %d, %x =? %x)\r\n", (currentTemp + 1), celsius, fpTemperature, crc8, scratchPad[8]);
#endif
/* check, if the buffer as some space for our data */
if (currentTemp < maxTemperatures) {
if (currentTemp < maxTemperatures)
{
pTemperatures[currentTemp] = celsius;
} else {
}
else
{
return -1;
}
}
currentTemp++;
}
}
this->mDs->reset();
#ifdef DS_DEBUG
Serial.println(" No more addresses.");

View File

@@ -11,77 +11,87 @@
*/
#include "PlantCtrl.h"
#include "ControllerConfiguration.h"
Plant::Plant(int pinSensor, int pinPump,int plantId, HomieNode* plant, PlantSettings_t* setting) {
Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting)
{
this->mPinSensor = pinSensor;
this->mPinPump = pinPump;
this->mPlant = plant;
this->mSetting = setting;
this->mSetting = setting;
}
void Plant::init(void) {
void Plant::init(void)
{
/* Initialize Home Settings validator */
this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT);
this->mSetting->pSensorDry->setValidator([] (long candidate) {
return (((candidate >= 0) && (candidate <= 4095) ) || candidate == DEACTIVATED_PLANT);
this->mSetting->pSensorDry->setValidator([](long candidate) {
return (((candidate >= 0) && (candidate <= 4095)) || candidate == DEACTIVATED_PLANT);
});
this->mSetting->pPumpAllowedHourRangeStart->setDefaultValue(8); // start at 8:00
this->mSetting->pPumpAllowedHourRangeStart->setValidator([] (long candidate) {
return ((candidate >= 0) && (candidate <= 23) );
this->mSetting->pPumpAllowedHourRangeStart->setValidator([](long candidate) {
return ((candidate >= 0) && (candidate <= 23));
});
this->mSetting->pPumpAllowedHourRangeEnd->setDefaultValue(20); // stop pumps at 20:00
this->mSetting->pPumpAllowedHourRangeEnd->setValidator([] (long candidate) {
return ((candidate >= 0) && (candidate <= 23) );
this->mSetting->pPumpAllowedHourRangeEnd->setValidator([](long candidate) {
return ((candidate >= 0) && (candidate <= 23));
});
this->mSetting->pPumpOnlyWhenLowLight->setDefaultValue(true);
this->mSetting->pPumpCooldownInHours->setDefaultValue(20); // minutes
this->mSetting->pPumpCooldownInHours->setValidator([] (long candidate) {
return ((candidate >= 0) && (candidate <= 1024) );
this->mSetting->pPumpCooldownInHours->setValidator([](long candidate) {
return ((candidate >= 0) && (candidate <= 1024));
});
/* Initialize Hardware */
pinMode(this->mPinPump, OUTPUT);
pinMode(this->mPinSensor, ANALOG);
digitalWrite(this->mPinPump, LOW);
digitalWrite(this->mPinPump, LOW);
}
void Plant::addSenseValue(void) {
this->moistureRaw.add( analogRead(this->mPinSensor) );
void Plant::addSenseValue(void)
{
int raw = analogRead(this->mPinSensor);
if(raw < MOIST_SENSOR_MAX_ADC && raw > MOIST_SENSOR_MIN_ADC){
this->moistureRaw.add(raw);
}
}
void Plant::postMQTTconnection(void) {
void Plant::postMQTTconnection(void)
{
const String OFF = String("OFF");
this->mConnected=true;
this->mConnected = true;
this->mPlant->setProperty("switch").send(OFF);
}
void Plant::deactivatePump(void) {
void Plant::deactivatePump(void)
{
digitalWrite(this->mPinPump, LOW);
if (this->mConnected) {
if (this->mConnected)
{
const String OFF = String("OFF");
this->mPlant->setProperty("switch").send(OFF);
}
}
void Plant::activatePump(void) {
void Plant::activatePump(void)
{
digitalWrite(this->mPinPump, HIGH);
if (this->mConnected) {
if (this->mConnected)
{
const String OFF = String("ON");
this->mPlant->setProperty("switch").send(OFF);
}
}
void Plant::advertise(void) {
void Plant::advertise(void)
{
// Advertise topics
this->mPlant->advertise("switch").setName("Pump 1")
.setDatatype("boolean");
this->mPlant->advertise("switch").setName("Pump 1").setDatatype("boolean");
//FIXME add .settable(this->switchHandler)
this->mPlant->advertise("moist").setName("Percent")
.setDatatype("number")
.setUnit("%");
this->mPlant->advertise("moist").setName("Percent").setDatatype("number").setUnit("%");
this->mPlant->advertise("moistraw").setName("adc").setDatatype("number").setUnit("3.3/4096V");
}
/* FIXME
bool Plant::switchHandler(const HomieRange& range, const String& value) {
if (range.isRange) return false; // only one switch is present

View File

@@ -31,8 +31,8 @@ RunningMedian::RunningMedian(const uint8_t size)
_size = constrain(size, MEDIAN_MIN_SIZE, MEDIAN_MAX_SIZE);
#ifdef RUNNING_MEDIAN_USE_MALLOC
_ar = (float *) malloc(_size * sizeof(float));
_p = (uint8_t *) malloc(_size * sizeof(uint8_t));
_ar = (float *)malloc(_size * sizeof(float));
_p = (uint8_t *)malloc(_size * sizeof(uint8_t));
#endif
clear();
@@ -63,18 +63,22 @@ void RunningMedian::clear()
void RunningMedian::add(float value)
{
_ar[_idx++] = value;
if (_idx >= _size) _idx = 0; // wrap around
if (_cnt < _size) _cnt++;
if (_idx >= _size)
_idx = 0; // wrap around
if (_cnt < _size)
_cnt++;
_sorted = false;
}
float RunningMedian::getMedian()
{
if (_cnt == 0) return NAN;
if (_cnt == 0)
return NAN;
if (_sorted == false) sort();
if (_sorted == false)
sort();
if (_cnt & 0x01) // is it odd sized?
if (_cnt & 0x01) // is it odd sized?
{
return _ar[_p[_cnt / 2]];
}
@@ -83,7 +87,8 @@ float RunningMedian::getMedian()
float RunningMedian::getAverage()
{
if (_cnt == 0) return NAN;
if (_cnt == 0)
return NAN;
float sum = 0;
for (uint8_t i = 0; i < _cnt; i++)
@@ -95,13 +100,16 @@ float RunningMedian::getAverage()
float RunningMedian::getAverage(uint8_t nMedians)
{
if ((_cnt == 0) || (nMedians == 0)) return NAN;
if ((_cnt == 0) || (nMedians == 0))
return NAN;
if (_cnt < nMedians) nMedians = _cnt; // when filling the array for first time
if (_cnt < nMedians)
nMedians = _cnt; // when filling the array for first time
uint8_t start = ((_cnt - nMedians) / 2);
uint8_t stop = start + nMedians;
if (_sorted == false) sort();
if (_sorted == false)
sort();
float sum = 0;
for (uint8_t i = start; i < stop; i++)
@@ -113,7 +121,8 @@ float RunningMedian::getAverage(uint8_t nMedians)
float RunningMedian::getElement(const uint8_t n)
{
if ((_cnt == 0) || (n >= _cnt)) return NAN;
if ((_cnt == 0) || (n >= _cnt))
return NAN;
uint8_t pos = _idx + n;
if (pos >= _cnt) // faster than %
@@ -125,18 +134,21 @@ float RunningMedian::getElement(const uint8_t n)
float RunningMedian::getSortedElement(const uint8_t n)
{
if ((_cnt == 0) || (n >= _cnt)) return NAN;
if ((_cnt == 0) || (n >= _cnt))
return NAN;
if (_sorted == false) sort();
if (_sorted == false)
sort();
return _ar[_p[n]];
}
// n can be max <= half the (filled) size
float RunningMedian::predict(const uint8_t n)
{
if ((_cnt == 0) || (n >= _cnt / 2)) return NAN;
if ((_cnt == 0) || (n >= _cnt / 2))
return NAN;
float med = getMedian(); // takes care of sorting !
float med = getMedian(); // takes care of sorting !
if (_cnt & 0x01)
{
return max(med - _ar[_p[_cnt / 2 - n]], _ar[_p[_cnt / 2 + n]] - med);
@@ -162,7 +174,8 @@ void RunningMedian::sort()
flag = false;
}
}
if (flag) break;
if (flag)
break;
}
_sorted = true;
}

View File

@@ -1,4 +1,6 @@
/**
/** \addtogroup Controller
* @{
*
* @file main.cpp
* @author Ollo
* @brief PlantControl
@@ -6,7 +8,6 @@
* @date 2020-05-01
*
* @copyright Copyright (c) 2020
*
*/
#include "PlantCtrl.h"
#include "ControllerConfiguration.h"
@@ -16,8 +17,8 @@
#include "time.h"
#include "esp_sleep.h"
#include "RunningMedian.h"
#include <arduino-timer.h>
#include <stdint.h>
#include <math.h>
const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minutes */
@@ -26,44 +27,36 @@ const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minut
#define SOLAR4SENSORS 6.0f
#define TEMP_INIT_VALUE -999.0f
#define TEMP_MAX_VALUE 85.0f
#define HalfHour 60
typedef struct
{
long lastActive; /**< Timestamp, a pump was activated */
long moistTrigger; /**< Trigger value of the moist sensor */
long moisture; /**< last measured moist value */
} rtc_plant_t;
/********************* non volatile enable after deepsleep *******************************/
RTC_DATA_ATTR rtc_plant_t rtcPlant[MAX_PLANTS];
RTC_DATA_ATTR long gotoMode2AfterThisTimestamp = 0;
RTC_DATA_ATTR long rtcDeepSleepTime = 0; /**< Time, when the microcontroller shall be up again */
RTC_DATA_ATTR long rtcLastActive0 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger0 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcLastActive1 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger1 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcLastActive2 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger2 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcLastActive3 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger3 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcLastActive4 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger4 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcLastActive5 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger5 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcLastActive6 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger6 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR int lastPumpRunning = 0;
RTC_DATA_ATTR long lastWaterValue = 0;
const char *ntpServer = "pool.ntp.org";
RTC_DATA_ATTR float rtcLastTemp1 = 0.0f;
RTC_DATA_ATTR float rtcLastTemp2 = 0.0f;
RTC_DATA_ATTR int gBootCount = 0;
RTC_DATA_ATTR int gCurrentPlant = 0; /**< Value Range: 1 ... 7 (0: no plant needs water) */
bool warmBoot = true;
bool mode3Active = false; /**< Controller must not sleep */
bool mDeepsleep = false;
bool volatile mode3Active = false; /**< Controller must not sleep */
bool volatile mDeepsleep = false;
int plantSensor1 = 0;
int readCounter = 0;
bool mConfigured = false;
auto wait4sleep = timer_create_default(); // create a timer with default settings
RTC_DATA_ATTR int gBootCount = 0;
RTC_DATA_ATTR int gCurrentPlant = 0; /**< Value Range: 1 ... 7 (0: no plant needs water) */
RunningMedian lipoRawSensor = RunningMedian(5);
RunningMedian solarRawSensor = RunningMedian(5);
RunningMedian waterRawSensor = RunningMedian(5);
@@ -91,10 +84,41 @@ float getSolarVoltage()
return SOLAR_VOLT(solarRawSensor.getAverage());
}
void setMoistureTrigger(int plantId, long value)
{
if ((plantId >= 0) && (plantId < MAX_PLANTS))
{
rtcPlant[plantId].moistTrigger = value;
}
}
void setLastMoisture(int plantId, long value)
{
if ((plantId >= 0) && (plantId < MAX_PLANTS))
{
rtcPlant[plantId].moisture = value;
}
}
long getLastMoisture(int plantId)
{
if ((plantId >= 0) && (plantId < MAX_PLANTS))
{
return rtcPlant[plantId].moisture;
}
else
{
return -1;
}
}
void readSystemSensors()
{
lipoRawSensor.add(analogRead(SENSOR_LIPO));
solarRawSensor.add(analogRead(SENSOR_SOLAR));
for (int i=0; i < 5; i++) {
lipoRawSensor.add(analogRead(SENSOR_LIPO));
solarRawSensor.add(analogRead(SENSOR_SOLAR));
}
Serial << "Lipo " << lipoRawSensor.getAverage() << " -> " << getBatteryVoltage() << endl;
}
int determineNextPump();
@@ -107,39 +131,42 @@ long getCurrentTime()
return tv_now.tv_sec;
}
//wait till homie flushed mqtt ect.
bool prepareSleep(void *)
{
//FIXME wait till pending mqtt is done, then start sleep via event or whatever
//Homie.disableResetTrigger();
bool queueIsEmpty = true;
if (queueIsEmpty)
{
mDeepsleep = true;
}
return false; // repeat? true there is something in the queue to be done
}
void espDeepSleepFor(long seconds, bool activatePump = false)
{
delay(1500);
if (mode3Active)
{
Serial << "abort deepsleep, mode3Active" << endl;
return;
}
for (int i = 0; i < 10; i++)
{
long cTime = getCurrentTime();
if (cTime < 100000)
{
Serial << "Wait for ntp" << endl;
delay(100);
}
else
{
break;
}
}
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_ON);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
if (activatePump)
{
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
gpio_deep_sleep_hold_en();
gpio_hold_en(GPIO_NUM_13); //pump pwr
}
else
{
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
gpio_hold_dis(GPIO_NUM_13); //pump pwr
gpio_deep_sleep_hold_dis();
digitalWrite(OUTPUT_PUMP, LOW);
digitalWrite(OUTPUT_SENSOR, LOW);
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].deactivatePump();
@@ -148,19 +175,17 @@ void espDeepSleepFor(long seconds, bool activatePump = false)
//gpio_hold_en(GPIO_NUM_23); //p0
//FIXME fix for outher outputs
Serial.print("Going to sleep for ");
Serial.print("Trying to sleep for ");
Serial.print(seconds);
Serial.println(" seconds");
esp_sleep_enable_timer_wakeup((seconds * 1000U * 1000U));
wait4sleep.in(500, prepareSleep);
mDeepsleep = true;
}
void mode2MQTT()
{
readSystemSensors();
configTime(0, 0, ntpServer);
digitalWrite(OUTPUT_PUMP, LOW);
for (int i = 0; i < MAX_PLANTS; i++)
{
@@ -169,7 +194,7 @@ void mode2MQTT()
if (deepSleepTime.get())
{
Serial << "sleeping for " << deepSleepTime.get() << endl;
Serial << "deepsleep time is configured to " << deepSleepTime.get() << endl;
}
/* Publish default values */
@@ -180,7 +205,23 @@ void mode2MQTT()
}
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095));
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;
}
if (pct < 0)
{
pct = 0;
}
if (pct > 100)
{
pct = 100;
}
mPlants[i].setProperty("moist").send(String(pct));
mPlants[i].setProperty("moistraw").send(String(raw));
}
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - waterRawSensor.getAverage()));
Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << "%)" << endl;
@@ -191,34 +232,31 @@ void mode2MQTT()
sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage()) / 4095));
sensorSolar.setProperty("volt").send(String(getSolarVoltage()));
float temp[2] = {TEMP_INIT_VALUE, TEMP_INIT_VALUE};
float *pFloat = temp;
int devices = dallas.readAllTemperatures(pFloat, 2);
if (devices < 2)
float t1 = temp1.getMedian();
if (t1 != NAN)
{
if ((pFloat[0] > TEMP_INIT_VALUE) && (pFloat[0] < TEMP_MAX_VALUE))
{
sensorTemp.setProperty("control").send(String(pFloat[0]));
}
sensorTemp.setProperty("control").send(String(t1));
}
else if (devices >= 2)
float t2 = temp2.getMedian();
if (t2 != NAN)
{
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]));
}
sensorTemp.setProperty("temp").send(String(t2));
}
bool lipoTempWarning = abs(temp[0] - temp[1]) > 5;
//give mqtt time, use via publish callback instead?
delay(100);
bool lipoTempWarning = t1 != 85 && t2 != 85 && abs(t1 - t2) > 10;
if (lipoTempWarning)
{
Serial.println("Lipo temp incorrect, panic mode deepsleep");
espDeepSleepFor(PANIK_MODE_DEEPSLEEP);
return;
Serial.println("Lipo temp incorrect, panic mode deepsleep TODO");
//espDeepSleepFor(PANIK_MODE_DEEPSLEEP);
//return;
}
for (int i = 0; i < MAX_PLANTS; i++)
{
setMoistureTrigger(i, mPlants[i].mSetting->pSensorDry->get());
}
bool hasWater = true; //FIXMEmWaterGone > waterLevelMin.get();
@@ -230,23 +268,32 @@ void mode2MQTT()
}
if (lastPumpRunning != -1 && hasWater)
{
digitalWrite(OUTPUT_PUMP, HIGH);
setLastActivationForPump(lastPumpRunning, getCurrentTime());
mPlants[lastPumpRunning].activatePump();
if (mode3Active)
{
Serial.println("Mode 3 active, ignoring pump request");
}
else
{
digitalWrite(OUTPUT_PUMP, HIGH);
setLastActivationForPump(lastPumpRunning, getCurrentTime());
mPlants[lastPumpRunning].activatePump();
}
}
if (lastPumpRunning == -1 || !hasWater)
{
if (getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE)
{
gotoMode2AfterThisTimestamp = getCurrentTime() + deepSleepNightTime.get();
gotoMode2AfterThisTimestamp = getCurrentTime() + maxTimeBetweenMQTTUpdates.get();
Serial.println("No pumps to activate and low light, deepSleepNight");
espDeepSleepFor(deepSleepNightTime.get());
rtcDeepSleepTime = deepSleepNightTime.get();
}
else
{
gotoMode2AfterThisTimestamp = getCurrentTime() + deepSleepTime.get();
gotoMode2AfterThisTimestamp = getCurrentTime() + maxTimeBetweenMQTTUpdates.get();
Serial.println("No pumps to activate, deepSleep");
espDeepSleepFor(deepSleepTime.get());
rtcDeepSleepTime = deepSleepTime.get();
}
}
else
@@ -257,109 +304,47 @@ void mode2MQTT()
}
}
void setMoistureTrigger(int plantId, long value)
long getMoistureTrigger(int plantId)
{
if (plantId == 0)
if ((plantId >= 0) && (plantId < MAX_PLANTS))
{
rtcMoistureTrigger0 = value;
return rtcPlant[plantId].moistTrigger;
}
if (plantId == 1)
else
{
rtcMoistureTrigger1 = value;
}
if (plantId == 2)
{
rtcMoistureTrigger2 = value;
}
if (plantId == 3)
{
rtcMoistureTrigger3 = value;
}
if (plantId == 4)
{
rtcMoistureTrigger4 = value;
}
if (plantId == 5)
{
rtcMoistureTrigger5 = value;
}
if (plantId == 6)
{
rtcMoistureTrigger6 = value;
return -1;
}
}
void setLastActivationForPump(int plantId, long value)
{
if (plantId == 0)
if ((plantId >= 0) && (plantId < MAX_PLANTS))
{
rtcLastActive0 = value;
}
if (plantId == 1)
{
rtcLastActive1 = value;
}
if (plantId == 2)
{
rtcLastActive2 = value;
}
if (plantId == 3)
{
rtcLastActive3 = value;
}
if (plantId == 4)
{
rtcLastActive4 = value;
}
if (plantId == 5)
{
rtcLastActive5 = value;
}
if (plantId == 6)
{
rtcLastActive6 = value;
rtcPlant[plantId].lastActive = value;
}
}
long getLastActivationForPump(int plantId)
{
if (plantId == 0)
if ((plantId >= 0) && (plantId < MAX_PLANTS))
{
return rtcLastActive0;
return rtcPlant[plantId].lastActive;
}
if (plantId == 1)
else
{
return rtcLastActive1;
return -1;
}
if (plantId == 2)
{
return rtcLastActive2;
}
if (plantId == 3)
{
return rtcLastActive3;
}
if (plantId == 4)
{
return rtcLastActive4;
}
if (plantId == 5)
{
return rtcLastActive5;
}
if (plantId == 6)
{
return rtcLastActive6;
}
return -1;
}
/**
* @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
* These sensors (ADC2) can only be read when no Wifi is used.
*/
void readSensors()
bool readSensors()
{
float temp[2] = {TEMP_MAX_VALUE, TEMP_MAX_VALUE};
float *pFloat = temp;
bool leaveMode1 = false;
Serial << "Read Sensors" << endl;
readSystemSensors();
@@ -368,7 +353,7 @@ void readSensors()
pinMode(OUTPUT_SENSOR, OUTPUT);
digitalWrite(OUTPUT_SENSOR, HIGH);
delay(100);
delay(20);
/* wait before reading something */
for (int readCnt = 0; readCnt < AMOUNT_SENOR_QUERYS; readCnt++)
{
@@ -376,6 +361,19 @@ void readSensors()
{
mPlants[i].addSenseValue();
}
delay(10);
}
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)
{
leaveMode1 = true;
Serial.printf("Mode2 start due to moist delta in plant %d with %ld \r\n", i, delta);
}
}
Serial << "DS18B20" << endl;
@@ -384,24 +382,34 @@ void readSensors()
delay(200);
/* Required to read the temperature once */
float temp[2] = {0, 0};
float *pFloat = temp;
for (int i = 0; i < 10; i++)
for (int i = 0; i < 5; i++)
{
if (dallas.readAllTemperatures(pFloat, 2) > 0)
int sensors = dallas.readAllTemperatures(pFloat, 2);
if (sensors > 0)
{
Serial << "t1: " << String(temp[0]) << endl;
Serial << "t2: " << String(temp[1]) << endl;
// first read returns crap, ignore result and read again
if (i <= 2)
{
temp1.add(temp[0]);
temp2.add(temp[1]);
}
temp1.add(temp[0]);
}
delay(200);
if (sensors > 1)
{
Serial << "t2: " << String(temp[1]) << endl;
temp2.add(temp[1]);
}
delay(50);
}
if ((temp1.getAverage() - rtcLastTemp1 > TEMPERATURE_DELTA_TRIGGER_IN_C) ||
(rtcLastTemp1 - temp1.getAverage() > TEMPERATURE_DELTA_TRIGGER_IN_C)) {
leaveMode1 = true;
}
if ((temp2.getAverage() - rtcLastTemp2 > TEMPERATURE_DELTA_TRIGGER_IN_C) ||
(rtcLastTemp2 - temp2.getAverage() > TEMPERATURE_DELTA_TRIGGER_IN_C)) {
leaveMode1 = true;
}
rtcLastTemp1 = temp1.getAverage();
rtcLastTemp2 = temp2.getAverage();
/* Use the Ultrasonic sensor to measure waterLevel */
for (int i = 0; i < 5; i++)
{
@@ -417,6 +425,7 @@ void readSensors()
}
/* deactivate the sensors */
digitalWrite(OUTPUT_SENSOR, LOW);
return leaveMode1;
}
//Homie.getMqttClient().disconnect();
@@ -429,9 +438,11 @@ void onHomieEvent(const HomieEvent &event)
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 << "MQTT ready " << rtcDeepSleepTime << " ms ds" << endl;
Serial << "Setup plants" << endl;
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].postMQTTconnection();
@@ -444,8 +455,9 @@ void onHomieEvent(const HomieEvent &event)
esp_deep_sleep_start();
break;
case HomieEventType::OTA_STARTED:
Homie.getLogger() << "OTA started" << endl;
digitalWrite(OUTPUT_SENSOR, HIGH);
digitalWrite(OUTPUT_PUMP, LOW);
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++)
@@ -455,6 +467,7 @@ void onHomieEvent(const HomieEvent &event)
mode3Active = true;
break;
case HomieEventType::OTA_SUCCESSFUL:
Homie.getLogger() << "OTA successfull" << endl;
digitalWrite(OUTPUT_SENSOR, LOW);
digitalWrite(OUTPUT_PUMP, LOW);
ESP.restart();
@@ -470,32 +483,46 @@ int determineNextPump()
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?
int pumpToUse = -1;
for (int i = 0; i < MAX_PLANTS; i++)
{
Plant plant = mPlants[i];
long lastActivation = getLastActivationForPump(i);
long sinceLastActivation = getCurrentTime() - lastActivation;
//this pump is in cooldown skip it and disable low power mode trigger for it
if (mPlants[i].isInCooldown(sinceLastActivation))
if (plant.isInCooldown(sinceLastActivation))
{
Serial.printf("%d Skipping due to cooldown\r\n", i);
Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, sinceLastActivation, plant.getCooldownInSeconds());
setMoistureTrigger(i, DEACTIVATED_PLANT);
continue;
}
//skip as it is not low light
if (!isLowLight && mPlants[i].isAllowedOnlyAtLowLight())
if (!isLowLight && plant.isAllowedOnlyAtLowLight())
{
Serial.println("Skipping due to light");
Serial.printf("%d No pump required: due to light\r\n", i);
continue;
}
if (mPlants->isPumpRequired())
if (plant.getCurrentMoisture() == MISSING_SENSOR && plant.isPumpTriggerActive())
{
Serial.printf("%d No pump possible: missing sensor \r\n", i);
continue;
}
if (plant.isPumpRequired())
{
Serial.printf("%d Requested pumping\r\n", i);
return i;
pumpToUse = i;
}
else if (plant.isPumpTriggerActive())
{
Serial.printf("%d No pump required: moisture acceptable %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture());
}
else
{
Serial.printf("%d No pump required: disabled pump trigger \r\n", i);
}
Serial.printf("%d No pump required\r\n", i);
}
return -1;
return pumpToUse;
}
/**
@@ -510,7 +537,6 @@ bool aliveHandler(const HomieRange &range, const String &value)
{
if (range.isRange)
return false; // only one controller is present
Serial << value << endl;
if (value.equals("ON") || value.equals("On") || value.equals("1"))
{
mode3Active = true;
@@ -536,9 +562,11 @@ void systemInit()
// Set default values
//in seconds
deepSleepTime.setDefaultValue(10);
deepSleepNightTime.setDefaultValue(30);
maxTimeBetweenMQTTUpdates.setDefaultValue(120);
deepSleepTime.setDefaultValue(60);
deepSleepNightTime.setDefaultValue(600);
wateringDeepSleep.setDefaultValue(5);
ntpServer.setDefaultValue("pool.ntp.org");
/* waterLevelMax 1000 */ /* 100cm in mm */
waterLevelMin.setDefaultValue(50); /* 5cm in mm */
@@ -547,6 +575,7 @@ void systemInit()
Homie.setLoopFunction(homieLoop);
Homie.onEvent(onHomieEvent);
//Homie.disableLogging();
Homie.setup();
mConfigured = Homie.isConfigured();
@@ -589,63 +618,46 @@ void systemInit()
bool mode1()
{
Serial.println("m1");
Serial.println("==== Mode 1 ====");
Serial << getCurrentTime() << " curtime" << endl;
/* Disable all sleeping stuff before reading sensors */
gpio_deep_sleep_hold_dis();
readSensors();
bool deltaTrigger = readSensors();
//queue sensor values for
if ((rtcDeepSleepTime == 0) ||
(rtcMoistureTrigger0 == 0) ||
(rtcMoistureTrigger1 == 0) ||
(rtcMoistureTrigger2 == 0) ||
(rtcMoistureTrigger3 == 0) ||
(rtcMoistureTrigger4 == 0) ||
(rtcMoistureTrigger5 == 0) ||
(rtcMoistureTrigger6 == 0))
if (deltaTrigger)
{
Serial.println("RTCm2");
Serial.println("1 delta triggered, going to mode2");
return true;
}
if (rtcDeepSleepTime == 0)
{
Serial.println("1 missing rtc value, going to mode2");
return true;
}
for (int i = 0; i < MAX_PLANTS; i++)
{
long trigger = getMoistureTrigger(i);
if (trigger == 0)
{
Serial << "Missing rtc trigger " << i << endl;
return true;
}
if (trigger == DEACTIVATED_PLANT)
{
continue;
}
long raw = mPlants[i].getCurrentMoisture();
if (raw == MISSING_SENSOR)
{
continue;
}
if (raw > trigger)
{
Serial << "plant " << i << " dry " << raw << " / " << trigger << " starting mode 2" << endl;
return true;
}
}
if ((rtcMoistureTrigger0 != DEACTIVATED_PLANT) && (mPlants[0].getSensorValue() < rtcMoistureTrigger0))
{
Serial.println("mt0");
return true;
}
if ((rtcMoistureTrigger1 != DEACTIVATED_PLANT) && (mPlants[1].getSensorValue() < rtcMoistureTrigger1))
{
Serial.println("mt1");
return true;
}
if ((rtcMoistureTrigger2 != DEACTIVATED_PLANT) && (mPlants[2].getSensorValue() < rtcMoistureTrigger2))
{
Serial.println("mt2");
return true;
}
if ((rtcMoistureTrigger3 != DEACTIVATED_PLANT) && (mPlants[3].getSensorValue() < rtcMoistureTrigger3))
{
Serial.println("mt3");
return true;
}
if ((rtcMoistureTrigger4 != DEACTIVATED_PLANT) && (mPlants[4].getSensorValue() < rtcMoistureTrigger4))
{
Serial.println("mt4");
return true;
}
if ((rtcMoistureTrigger5 != DEACTIVATED_PLANT) && (mPlants[5].getSensorValue() < rtcMoistureTrigger5))
{
Serial.println("mt5");
return true;
}
if ((rtcMoistureTrigger6 != DEACTIVATED_PLANT) && (mPlants[6].getSensorValue() < rtcMoistureTrigger6))
{
Serial.println("mt6");
return true;
}
//check how long it was already in mode1 if to long goto mode2
long cTime = getCurrentTime();
@@ -660,18 +672,22 @@ bool mode1()
Serial.println("Starting mode 2 after specified mode1 time");
return true;
}
else
{
Serial << "Mode2 Timer " << gotoMode2AfterThisTimestamp << " curtime " << cTime << endl;
}
return false;
}
void mode2()
{
Serial.println("m2");
Serial.println("==== Mode 2 ====");
systemInit();
/* Jump into Mode 3, if not configured */
if (!mConfigured)
{
Serial.println("m3");
Serial.println("==== Mode 3 ====");
mode3Active = true;
}
}
@@ -732,8 +748,7 @@ void setup()
else
{
Serial.println("nop");
Serial.flush();
esp_deep_sleep_start();
espDeepSleepFor(rtcDeepSleepTime);
}
}
@@ -741,22 +756,36 @@ void setup()
* @brief Cyclic call
* Executs the Homie base functionallity or triggers sleeping, if requested.
*/
long nextBlink = 0;
void loop()
{
if (!mDeepsleep)
if (!mDeepsleep || mode3Active)
{
Homie.loop();
}
else
{
Serial << "Bye" << endl;
Serial.flush();
esp_deep_sleep_start();
}
if (millis() > 30000 && !mode3Active)
{
Serial << (millis() / 1000) << "s alive" << endl;
Serial << (millis() / 1000) << "not terminated watchdog putting to sleep" << endl;
Serial.flush();
esp_deep_sleep_start();
espDeepSleepFor(rtcDeepSleepTime);
}
/* Toggel Senor LED to visualize mode 3 */
if (mode3Active)
{
if (nextBlink < millis())
{
nextBlink = millis() + 500;
digitalWrite(OUTPUT_SENSOR, !digitalRead(OUTPUT_SENSOR));
}
}
}
/** @}*/