From 39adfd7cef3871afe2dca020298b7cef829d8b15 Mon Sep 17 00:00:00 2001 From: Stefan Ostermann Date: Sun, 20 Jun 2021 21:29:13 +0200 Subject: [PATCH] Pump length, mqtt improvements --- irrigation/README.md | 15 +++++++++++ irrigation/src/config.h.example | 5 ++++ irrigation/src/main.cpp | 48 +++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 irrigation/README.md diff --git a/irrigation/README.md b/irrigation/README.md new file mode 100644 index 0000000..3dc6356 --- /dev/null +++ b/irrigation/README.md @@ -0,0 +1,15 @@ +# Irrigation with an ESP8266 +Tested with D1 mini. Connect a pump with a relais. Relais activation on D4. + +## Homeassistant integration +payload_on can also contain the duration of the pump in ms. +Example: +` +switch 5: +- platform: mqtt + command_topic: greenhousino/pump + state_topic: greenhousino/pumpstate + payload_on: "on" + payload_off: "off" + name: "Pumpe" +` \ No newline at end of file diff --git a/irrigation/src/config.h.example b/irrigation/src/config.h.example index c3df69f..016ee06 100644 --- a/irrigation/src/config.h.example +++ b/irrigation/src/config.h.example @@ -6,6 +6,7 @@ const char *MQTT_BROKER = "192.168.178.74"; const char *MQTT_TF_TOPIC = "greenhousino/pump"; +const char *MQTT_STATE_TOPIC = "greenhousino/pumpstate"; const char *MQTT_CLIENT_ID = "greenhousino/pump"; const char *MQTT_LAST_WILL_TOPIC = "greenhousino/pump/status"; @@ -13,4 +14,8 @@ const char *MQTT_LAST_WILL_MSG = "offline"; const char *MQTT_CLIENT_USER = "NULL"; // if NULL, no username or password is used const char *MQTT_CLIENT_PW = "NULL"; // if NULL, no password is used +// Max time the pump is active +#define PUMP_ACTIVE_MILLIS 30000 + + #endif \ No newline at end of file diff --git a/irrigation/src/main.cpp b/irrigation/src/main.cpp index 9585b5b..1f323c7 100644 --- a/irrigation/src/main.cpp +++ b/irrigation/src/main.cpp @@ -16,8 +16,10 @@ #define R_ON 0// active Low = 0, otherwise 1 #define R_OFF 1// active Low = 1, otherwise 0 -#include "config.h" +#define MQTT_ON "on" +#define MQTT_OFF "off" +#include "config.h" ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80 @@ -28,8 +30,10 @@ PubSubClient mqttClient(MQTT_BROKER, 1883, espClient); // time in millis the pump has been started long pumpStarted = 0; -// Max time the pump is active -#define PUMP_ACTIVE_MILLIS 30000 +long waitTimeMillis = PUMP_ACTIVE_MILLIS; + +boolean pumpActive = false; + void handleRoot(); // function prototypes for HTTP handlers void handleOn(); @@ -109,8 +113,9 @@ void loop() mqttClient.loop(); - if (millis()-pumpStarted>PUMP_ACTIVE_MILLIS && digitalRead(RelaisPin) == R_ON) { + if (millis()-pumpStarted>waitTimeMillis && pumpActive) { deactivatePump(); + waitTimeMillis = PUMP_ACTIVE_MILLIS; } } @@ -121,12 +126,18 @@ void reconnect() { Serial.println("INFO: Attempting MQTT connection..."); // Attempt to connect - if (mqttClient.connect(MQTT_CLIENT_ID)) + if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_CLIENT_USER, MQTT_CLIENT_PW)) { Serial.println("INFO: connected"); + + if (pumpActive) { + mqttClient.publish(MQTT_STATE_TOPIC, MQTT_ON); + } else { + mqttClient.publish(MQTT_STATE_TOPIC, MQTT_OFF); + } + mqttClient.subscribe(MQTT_TF_TOPIC); - } - else + } else { Serial.print("ERROR: failed, rc="); Serial.print(mqttClient.state()); @@ -147,12 +158,19 @@ void mqtt_callback(char* inTopic, byte* payload, unsigned int length){ strncpy(pltext, (char*)payload, length); pltext[length]='\0'; // convert payload string to int: - if (String(pltext)=="on") { + if (String(pltext)==MQTT_ON) { activatePump(); - } else if (String(pltext)=="off") { + } else if (String(pltext)==MQTT_OFF) { deactivatePump(); } else { - Serial.println("mqtt payload unknown!"); + long pumpLength = atol(pltext); + if (pumpLength > 0) { + waitTimeMillis = pumpLength; + activatePump(); + } else { + Serial.println("mqtt payload unknown!"); + } + } Serial.println("RF data sent"); @@ -167,34 +185,36 @@ void activatePump() { pumpStarted = millis(); digitalWrite(RelaisPin, R_ON); Serial.println("Pump activated"); + pumpActive = true; } void deactivatePump() { digitalWrite(RelaisPin, R_OFF); Serial.println("Pump deactivated"); + pumpActive = false; } void handleOn() { activatePump(); server.send(200, "text/plain", "on"); + mqttClient.publish(MQTT_STATE_TOPIC, MQTT_ON); } void handleOff() { deactivatePump(); server.send(200, "text/plain", "off"); + mqttClient.publish(MQTT_STATE_TOPIC, MQTT_OFF); } void handleState() { - int state = digitalRead(RelaisPin); String sstate = "off"; - switch (state) { - case R_ON: + if (pumpActive) { sstate = "on"; - break; } + server.send(200,"text/plain", sstate); }