added testmode, fixed formating
This commit is contained in:
parent
2c70ff1ed3
commit
98799bd2d5
@ -75,6 +75,8 @@ void Plant::postMQTTconnection(void)
|
|||||||
|
|
||||||
void Plant::deactivatePump(void)
|
void Plant::deactivatePump(void)
|
||||||
{
|
{
|
||||||
|
int plantId = this->mPlantId;
|
||||||
|
Serial << "deactivating pump " << plantId << endl;
|
||||||
digitalWrite(this->mPinPump, LOW);
|
digitalWrite(this->mPinPump, LOW);
|
||||||
if (this->mConnected)
|
if (this->mConnected)
|
||||||
{
|
{
|
||||||
@ -85,6 +87,8 @@ void Plant::deactivatePump(void)
|
|||||||
|
|
||||||
void Plant::activatePump(void)
|
void Plant::activatePump(void)
|
||||||
{
|
{
|
||||||
|
int plantId = this->mPlantId;
|
||||||
|
Serial << "activating pump " << plantId << endl;
|
||||||
digitalWrite(this->mPinPump, HIGH);
|
digitalWrite(this->mPinPump, HIGH);
|
||||||
if (this->mConnected)
|
if (this->mConnected)
|
||||||
{
|
{
|
||||||
|
@ -36,7 +36,8 @@
|
|||||||
#define MAX_TANK_DEPTH 1000
|
#define MAX_TANK_DEPTH 1000
|
||||||
#define TEST_TOPIC "roundtrip\0"
|
#define TEST_TOPIC "roundtrip\0"
|
||||||
|
|
||||||
#define getTopic char* topic = new char[strlen(Homie.getConfiguration().mqtt.baseTopic) + strlen(Homie.getConfiguration().deviceId) + 1 + strlen(TEST_TOPIC) + 1]; \
|
#define getTopic \
|
||||||
|
char *topic = new char[strlen(Homie.getConfiguration().mqtt.baseTopic) + strlen(Homie.getConfiguration().deviceId) + 1 + strlen(TEST_TOPIC) + 1]; \
|
||||||
strcpy(topic, Homie.getConfiguration().mqtt.baseTopic); \
|
strcpy(topic, Homie.getConfiguration().mqtt.baseTopic); \
|
||||||
strcat(topic, Homie.getConfiguration().deviceId); \
|
strcat(topic, Homie.getConfiguration().deviceId); \
|
||||||
strcat(topic, "/"); \
|
strcat(topic, "/"); \
|
||||||
@ -57,7 +58,7 @@ void readPowerSwitchedSensors();
|
|||||||
RTC_SLOW_ATTR int lastPumpRunning = -1; /**< store last successfully waterd plant */
|
RTC_SLOW_ATTR int lastPumpRunning = -1; /**< store last successfully waterd plant */
|
||||||
RTC_SLOW_ATTR long lastWaterValue = 0; /**< to calculate the used water per plant */
|
RTC_SLOW_ATTR long lastWaterValue = 0; /**< to calculate the used water per plant */
|
||||||
|
|
||||||
RTC_SLOW_ATTR long rtcLastWateringPlant[MAX_PLANTS] = { 0 };
|
RTC_SLOW_ATTR long rtcLastWateringPlant[MAX_PLANTS] = {0};
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* LOCAL VARIABLES
|
* LOCAL VARIABLES
|
||||||
@ -74,7 +75,6 @@ RunningMedian waterRawSensor = RunningMedian(5);
|
|||||||
float mSolarVoltage = 0.0f; /**< Voltage from solar panels */
|
float mSolarVoltage = 0.0f; /**< Voltage from solar panels */
|
||||||
unsigned long setupFinishedTimestamp;
|
unsigned long setupFinishedTimestamp;
|
||||||
|
|
||||||
|
|
||||||
/*************************** Hardware abstraction *****************************/
|
/*************************** Hardware abstraction *****************************/
|
||||||
|
|
||||||
OneWire oneWire(SENSOR_ONEWIRE);
|
OneWire oneWire(SENSOR_ONEWIRE);
|
||||||
@ -116,7 +116,8 @@ void espDeepSleepFor(long seconds, bool activatePump, bool withHomieShutdown)
|
|||||||
Serial << "abort deepsleep, DownloadMode active" << endl;
|
Serial << "abort deepsleep, DownloadMode active" << endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(withHomieShutdown){
|
if (withHomieShutdown)
|
||||||
|
{
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
long cTime = getCurrentTime();
|
long cTime = getCurrentTime();
|
||||||
@ -166,19 +167,21 @@ void espDeepSleepFor(long seconds, bool activatePump, bool withHomieShutdown)
|
|||||||
Serial.println(" seconds");
|
Serial.println(" seconds");
|
||||||
esp_sleep_enable_timer_wakeup((seconds * 1000U * 1000U));
|
esp_sleep_enable_timer_wakeup((seconds * 1000U * 1000U));
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
if(withHomieShutdown){
|
if (withHomieShutdown)
|
||||||
|
{
|
||||||
Homie.prepareToSleep();
|
Homie.prepareToSleep();
|
||||||
}else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial << "Bye offline mode" << endl;
|
Serial << "Bye offline mode" << endl;
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//requires homie being started
|
//requires homie being started
|
||||||
void readOneWireSensors(bool withMQTT){
|
void readOneWireSensors(bool withMQTT)
|
||||||
|
{
|
||||||
int sensorCount = sensors.getDS18Count();
|
int sensorCount = sensors.getDS18Count();
|
||||||
Serial << "Read OneWire" << endl;
|
Serial << "Read OneWire" << endl;
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
@ -200,19 +203,25 @@ void readOneWireSensors(bool withMQTT){
|
|||||||
ds18b20Address[6],
|
ds18b20Address[6],
|
||||||
ds18b20Address[7]);
|
ds18b20Address[7]);
|
||||||
|
|
||||||
if (String(lipoSensorAddr.get()).compareTo(String(buf))) {
|
if (String(lipoSensorAddr.get()).compareTo(String(buf)))
|
||||||
if(withMQTT){
|
{
|
||||||
|
if (withMQTT)
|
||||||
|
{
|
||||||
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(temp));
|
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(temp));
|
||||||
}
|
}
|
||||||
Serial << "Lipo Temperatur " << temp << " °C " << endl;
|
Serial << "Lipo Temperatur " << temp << " °C " << endl;
|
||||||
} else if (String(waterSensorAddr.get()).compareTo(String(buf))) {
|
}
|
||||||
if(withMQTT){
|
else if (String(waterSensorAddr.get()).compareTo(String(buf)))
|
||||||
|
{
|
||||||
|
if (withMQTT)
|
||||||
|
{
|
||||||
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(temp));
|
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(temp));
|
||||||
}
|
}
|
||||||
Serial << "Water Temperatur " << temp << " °C " << endl;
|
Serial << "Water Temperatur " << temp << " °C " << endl;
|
||||||
}
|
}
|
||||||
/* Always send the sensor address with the temperatur value */
|
/* Always send the sensor address with the temperatur value */
|
||||||
if(withMQTT){
|
if (withMQTT)
|
||||||
|
{
|
||||||
sensorTemp.setProperty(String(buf)).send(String(temp));
|
sensorTemp.setProperty(String(buf)).send(String(temp));
|
||||||
}
|
}
|
||||||
Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl;
|
Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl;
|
||||||
@ -272,10 +281,10 @@ void readPowerSwitchedSensors()
|
|||||||
digitalWrite(OUTPUT_ENABLE_SENSOR, LOW);
|
digitalWrite(OUTPUT_ENABLE_SENSOR, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onMessage(char *incoming, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
|
||||||
void onMessage(char* incoming, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total){
|
{
|
||||||
getTopic
|
getTopic if (strcmp(incoming, topic) == 0)
|
||||||
if(strcmp(incoming,topic) == 0){
|
{
|
||||||
mAliveWasRead = true;
|
mAliveWasRead = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -292,7 +301,8 @@ void onHomieEvent(const HomieEvent &event)
|
|||||||
case HomieEventType::SENDING_STATISTICS:
|
case HomieEventType::SENDING_STATISTICS:
|
||||||
break;
|
break;
|
||||||
case HomieEventType::MQTT_READY:
|
case HomieEventType::MQTT_READY:
|
||||||
if (mSensorsRead) {
|
if (mSensorsRead)
|
||||||
|
{
|
||||||
Serial.printf("Timeout occured... too late!\r\n");
|
Serial.printf("Timeout occured... too late!\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -308,7 +318,8 @@ void onHomieEvent(const HomieEvent &event)
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
getTopic
|
getTopic
|
||||||
Homie.getMqttClient().subscribe(topic,2);
|
Homie.getMqttClient()
|
||||||
|
.subscribe(topic, 2);
|
||||||
Homie.getMqttClient().publish(topic, 2, false, "ping");
|
Homie.getMqttClient().publish(topic, 2, false, "ping");
|
||||||
Homie.getMqttClient().onMessage(onMessage);
|
Homie.getMqttClient().onMessage(onMessage);
|
||||||
}
|
}
|
||||||
@ -350,8 +361,8 @@ int determineNextPump()
|
|||||||
Serial.printf("%d Skip deactivated pump\r\n", i);
|
Serial.printf("%d Skip deactivated pump\r\n", i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ((rtcLastWateringPlant[i] > 0)
|
if ((rtcLastWateringPlant[i] > 0) && ((rtcLastWateringPlant[i] + plant.getCooldownInSeconds()) < getCurrentTime()))
|
||||||
&& ((rtcLastWateringPlant[i] + plant.getCooldownInSeconds()) < getCurrentTime())) {
|
{
|
||||||
Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, rtcLastWateringPlant[i], plant.getCooldownInSeconds());
|
Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, rtcLastWateringPlant[i], plant.getCooldownInSeconds());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -369,18 +380,18 @@ int determineNextPump()
|
|||||||
{
|
{
|
||||||
/* Handle e.g. start = 21, end = 8 */
|
/* Handle e.g. start = 21, end = 8 */
|
||||||
if (((plant.getHoursStart() > plant.getHoursEnd()) &&
|
if (((plant.getHoursStart() > plant.getHoursEnd()) &&
|
||||||
(getCurrentHour() >= plant.getHoursStart() || getCurrentHour() <= plant.getHoursEnd()))
|
(getCurrentHour() >= plant.getHoursStart() || getCurrentHour() <= plant.getHoursEnd())) ||
|
||||||
||
|
|
||||||
/* Handle e.g. start = 8, end = 21 */
|
/* Handle e.g. start = 8, end = 21 */
|
||||||
((plant.getHoursStart() < plant.getHoursEnd()) &&
|
((plant.getHoursStart() < plant.getHoursEnd()) &&
|
||||||
(getCurrentHour() >= plant.getHoursStart() && getCurrentHour() <= plant.getHoursEnd()))
|
(getCurrentHour() >= plant.getHoursStart() && getCurrentHour() <= plant.getHoursEnd())) ||
|
||||||
||
|
|
||||||
/* no time from NTP received */
|
/* no time from NTP received */
|
||||||
(getCurrentTime() < 10000) )
|
(getCurrentTime() < 10000))
|
||||||
{
|
{
|
||||||
Serial.printf("%d Requested pumping\r\n", i);
|
Serial.printf("%d Requested pumping\r\n", i);
|
||||||
pumpToUse = i;
|
pumpToUse = i;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial.printf("%d ignored due to time boundary: %d to %d (current %d)\r\n", i, plant.getHoursStart(), plant.getHoursEnd(), getCurrentHour());
|
Serial.printf("%d ignored due to time boundary: %d to %d (current %d)\r\n", i, plant.getHoursStart(), plant.getHoursEnd(), getCurrentHour());
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@ -413,7 +424,8 @@ bool aliveHandler(const HomieRange &range, const String &value)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(mDownloadMode){
|
if (mDownloadMode)
|
||||||
|
{
|
||||||
esp_restart();
|
esp_restart();
|
||||||
}
|
}
|
||||||
mDownloadMode = false;
|
mDownloadMode = false;
|
||||||
@ -425,7 +437,8 @@ bool aliveHandler(const HomieRange &range, const String &value)
|
|||||||
bool notStarted = true;
|
bool notStarted = true;
|
||||||
void homieLoop()
|
void homieLoop()
|
||||||
{
|
{
|
||||||
if(mMQTTReady && mAliveWasRead && notStarted){
|
if (mMQTTReady && mAliveWasRead && notStarted)
|
||||||
|
{
|
||||||
Serial.println("received alive & mqtt is ready");
|
Serial.println("received alive & mqtt is ready");
|
||||||
notStarted = false;
|
notStarted = false;
|
||||||
plantcontrol(true);
|
plantcontrol(true);
|
||||||
@ -443,7 +456,7 @@ void setup()
|
|||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
Serial <<"Wifi mode set to " << WIFI_OFF << " to allow analog2 useage " << endl;
|
Serial << "Wifi mode set to " << WIFI_OFF << " to allow analog2 useage " << endl;
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
|
|
||||||
@ -462,7 +475,6 @@ void setup()
|
|||||||
|
|
||||||
digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
|
digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
|
||||||
|
|
||||||
|
|
||||||
pinMode(OUTPUT_ENABLE_SENSOR, OUTPUT);
|
pinMode(OUTPUT_ENABLE_SENSOR, OUTPUT);
|
||||||
|
|
||||||
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS)
|
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS)
|
||||||
@ -472,7 +484,6 @@ void setup()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/************************* Start One-Wire bus ***************/
|
/************************* Start One-Wire bus ***************/
|
||||||
int tempInitStartTime = millis();
|
int tempInitStartTime = millis();
|
||||||
uint8_t sensorCount = 0U;
|
uint8_t sensorCount = 0U;
|
||||||
@ -527,12 +538,9 @@ void setup()
|
|||||||
mConfigured = Homie.isConfigured();
|
mConfigured = Homie.isConfigured();
|
||||||
if (mConfigured)
|
if (mConfigured)
|
||||||
{
|
{
|
||||||
Serial <<"Wifi mode set to " << WIFI_STA << endl;
|
Serial << "Wifi mode set to " << WIFI_STA << endl;
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < MAX_PLANTS; i++)
|
for (int i = 0; i < MAX_PLANTS; i++)
|
||||||
{
|
{
|
||||||
mPlants[i].advertise();
|
mPlants[i].advertise();
|
||||||
@ -568,8 +576,13 @@ void setup()
|
|||||||
.setDatatype(NUMBER_TYPE)
|
.setDatatype(NUMBER_TYPE)
|
||||||
.setUnit("V");
|
.setUnit("V");
|
||||||
sensorWater.advertise("remaining").setDatatype(NUMBER_TYPE).setUnit("%");
|
sensorWater.advertise("remaining").setDatatype(NUMBER_TYPE).setUnit("%");
|
||||||
} else {
|
}
|
||||||
Serial <<"Wifi mode set to " << WIFI_AP_STA << endl;
|
else
|
||||||
|
{
|
||||||
|
readOneWireSensors(false);
|
||||||
|
digitalWrite(OUTPUT_ENABLE_PUMP, HIGH);
|
||||||
|
delay(100);
|
||||||
|
Serial << "Wifi mode set to " << WIFI_AP_STA << endl;
|
||||||
WiFi.mode(WIFI_AP_STA);
|
WiFi.mode(WIFI_AP_STA);
|
||||||
Serial.println("Initial Setup. Start Accesspoint...");
|
Serial.println("Initial Setup. Start Accesspoint...");
|
||||||
mDownloadMode = true;
|
mDownloadMode = true;
|
||||||
@ -590,17 +603,38 @@ void loop()
|
|||||||
{
|
{
|
||||||
if (nextBlink < millis())
|
if (nextBlink < millis())
|
||||||
{
|
{
|
||||||
nextBlink = millis() + 500;
|
|
||||||
digitalWrite(OUTPUT_ENABLE_SENSOR, !digitalRead(OUTPUT_ENABLE_SENSOR));
|
digitalWrite(OUTPUT_ENABLE_SENSOR, !digitalRead(OUTPUT_ENABLE_SENSOR));
|
||||||
|
if (mConfigured)
|
||||||
|
{
|
||||||
|
nextBlink = millis() + 500;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
nextBlink = millis() + 5000;
|
||||||
|
if (lastPumpRunning >= 0 && lastPumpRunning < MAX_PLANTS)
|
||||||
|
{
|
||||||
|
mPlants[lastPumpRunning].deactivatePump();
|
||||||
|
}
|
||||||
|
if (lastPumpRunning > MAX_PLANTS)
|
||||||
|
{
|
||||||
|
digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
|
||||||
|
}
|
||||||
|
if (lastPumpRunning < MAX_PLANTS){
|
||||||
|
lastPumpRunning++;
|
||||||
|
mPlants[lastPumpRunning].activatePump();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
unsigned long timeSinceSetup = millis() - setupFinishedTimestamp;
|
unsigned long timeSinceSetup = millis() - setupFinishedTimestamp;
|
||||||
if ((timeSinceSetup > MQTT_TIMEOUT) && (!mSensorsRead)) {
|
if ((timeSinceSetup > MQTT_TIMEOUT) && (!mSensorsRead))
|
||||||
|
{
|
||||||
mSensorsRead = true;
|
mSensorsRead = true;
|
||||||
/* Disable Wifi and put modem into sleep mode */
|
/* Disable Wifi and put modem into sleep mode */
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
Serial <<"Wifi mode set to " << WIFI_OFF << " mqqt was no reached within " << timeSinceSetup << "ms , fallback to offline mode " << endl;
|
Serial << "Wifi mode set to " << WIFI_OFF << " mqqt was no reached within " << timeSinceSetup << "ms , fallback to offline mode " << endl;
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
plantcontrol(false);
|
plantcontrol(false);
|
||||||
}
|
}
|
||||||
@ -615,7 +649,6 @@ void loop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* @fn plantcontrol
|
* @fn plantcontrol
|
||||||
* Main function, doing the logic
|
* Main function, doing the logic
|
||||||
@ -660,7 +693,8 @@ void plantcontrol(bool withHomie)
|
|||||||
float chipTemp = battery.getTemperature();
|
float chipTemp = battery.getTemperature();
|
||||||
Serial << "Chip Temperatur " << chipTemp << " °C " << endl;
|
Serial << "Chip Temperatur " << chipTemp << " °C " << endl;
|
||||||
|
|
||||||
if(withHomie){
|
if (withHomie)
|
||||||
|
{
|
||||||
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - waterRawSensor.getAverage()));
|
sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - waterRawSensor.getAverage()));
|
||||||
sensorLipo.setProperty("percent").send(String(100 * batteryVoltage / VOLT_MAX_BATT));
|
sensorLipo.setProperty("percent").send(String(100 * batteryVoltage / VOLT_MAX_BATT));
|
||||||
sensorLipo.setProperty("volt").send(String(batteryVoltage));
|
sensorLipo.setProperty("volt").send(String(batteryVoltage));
|
||||||
@ -671,13 +705,13 @@ void plantcontrol(bool withHomie)
|
|||||||
sensorLipo.setProperty("CCA").send(String(battery.getCCA()));
|
sensorLipo.setProperty("CCA").send(String(battery.getCCA()));
|
||||||
sensorSolar.setProperty("volt").send(String(mSolarVoltage));
|
sensorSolar.setProperty("volt").send(String(mSolarVoltage));
|
||||||
sensorTemp.setProperty(TEMPERATUR_SENSOR_CHIP).send(String(chipTemp));
|
sensorTemp.setProperty(TEMPERATUR_SENSOR_CHIP).send(String(chipTemp));
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial.println("Skipping MQTT, offline mode");
|
Serial.println("Skipping MQTT, offline mode");
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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();
|
||||||
@ -716,7 +750,7 @@ void plantcontrol(bool withHomie)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println("No pumps to activate, deepSleep");
|
Serial.println("No pumps to activate, deepSleep");
|
||||||
espDeepSleepFor(deepSleepTime.get(), false ,withHomie);
|
espDeepSleepFor(deepSleepTime.get(), false, withHomie);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
Loading…
Reference in New Issue
Block a user