Pump length, mqtt improvements

This commit is contained in:
Stefan Ostermann 2021-06-20 21:29:13 +02:00
parent 9c01025058
commit 39adfd7cef
3 changed files with 54 additions and 14 deletions

15
irrigation/README.md Normal file
View File

@ -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"
`

View File

@ -6,6 +6,7 @@
const char *MQTT_BROKER = "192.168.178.74"; const char *MQTT_BROKER = "192.168.178.74";
const char *MQTT_TF_TOPIC = "greenhousino/pump"; const char *MQTT_TF_TOPIC = "greenhousino/pump";
const char *MQTT_STATE_TOPIC = "greenhousino/pumpstate";
const char *MQTT_CLIENT_ID = "greenhousino/pump"; const char *MQTT_CLIENT_ID = "greenhousino/pump";
const char *MQTT_LAST_WILL_TOPIC = "greenhousino/pump/status"; 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_USER = "NULL"; // if NULL, no username or password is used
const char *MQTT_CLIENT_PW = "NULL"; // if NULL, no 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 #endif

View File

@ -16,8 +16,10 @@
#define R_ON 0// active Low = 0, otherwise 1 #define R_ON 0// active Low = 0, otherwise 1
#define R_OFF 1// active Low = 1, otherwise 0 #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 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 // time in millis the pump has been started
long pumpStarted = 0; long pumpStarted = 0;
// Max time the pump is active long waitTimeMillis = PUMP_ACTIVE_MILLIS;
#define PUMP_ACTIVE_MILLIS 30000
boolean pumpActive = false;
void handleRoot(); // function prototypes for HTTP handlers void handleRoot(); // function prototypes for HTTP handlers
void handleOn(); void handleOn();
@ -109,8 +113,9 @@ void loop()
mqttClient.loop(); mqttClient.loop();
if (millis()-pumpStarted>PUMP_ACTIVE_MILLIS && digitalRead(RelaisPin) == R_ON) { if (millis()-pumpStarted>waitTimeMillis && pumpActive) {
deactivatePump(); deactivatePump();
waitTimeMillis = PUMP_ACTIVE_MILLIS;
} }
} }
@ -121,12 +126,18 @@ void reconnect()
{ {
Serial.println("INFO: Attempting MQTT connection..."); Serial.println("INFO: Attempting MQTT connection...");
// Attempt to connect // 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"); 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); mqttClient.subscribe(MQTT_TF_TOPIC);
} } else
else
{ {
Serial.print("ERROR: failed, rc="); Serial.print("ERROR: failed, rc=");
Serial.print(mqttClient.state()); Serial.print(mqttClient.state());
@ -147,12 +158,19 @@ void mqtt_callback(char* inTopic, byte* payload, unsigned int length){
strncpy(pltext, (char*)payload, length); strncpy(pltext, (char*)payload, length);
pltext[length]='\0'; pltext[length]='\0';
// convert payload string to int: // convert payload string to int:
if (String(pltext)=="on") { if (String(pltext)==MQTT_ON) {
activatePump(); activatePump();
} else if (String(pltext)=="off") { } else if (String(pltext)==MQTT_OFF) {
deactivatePump(); deactivatePump();
} else { } 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"); Serial.println("RF data sent");
@ -167,34 +185,36 @@ void activatePump() {
pumpStarted = millis(); pumpStarted = millis();
digitalWrite(RelaisPin, R_ON); digitalWrite(RelaisPin, R_ON);
Serial.println("Pump activated"); Serial.println("Pump activated");
pumpActive = true;
} }
void deactivatePump() { void deactivatePump() {
digitalWrite(RelaisPin, R_OFF); digitalWrite(RelaisPin, R_OFF);
Serial.println("Pump deactivated"); Serial.println("Pump deactivated");
pumpActive = false;
} }
void handleOn() void handleOn()
{ {
activatePump(); activatePump();
server.send(200, "text/plain", "on"); server.send(200, "text/plain", "on");
mqttClient.publish(MQTT_STATE_TOPIC, MQTT_ON);
} }
void handleOff() void handleOff()
{ {
deactivatePump(); deactivatePump();
server.send(200, "text/plain", "off"); server.send(200, "text/plain", "off");
mqttClient.publish(MQTT_STATE_TOPIC, MQTT_OFF);
} }
void handleState() void handleState()
{ {
int state = digitalRead(RelaisPin);
String sstate = "off"; String sstate = "off";
switch (state) { if (pumpActive) {
case R_ON:
sstate = "on"; sstate = "on";
break;
} }
server.send(200,"text/plain", sstate); server.send(200,"text/plain", sstate);
} }