Pump length, mqtt improvements
This commit is contained in:
parent
9c01025058
commit
39adfd7cef
|
|
@ -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"
|
||||
`
|
||||
|
|
@ -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
|
||||
|
|
@ -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");
|
||||
mqttClient.subscribe(MQTT_TF_TOPIC);
|
||||
|
||||
if (pumpActive) {
|
||||
mqttClient.publish(MQTT_STATE_TOPIC, MQTT_ON);
|
||||
} else {
|
||||
mqttClient.publish(MQTT_STATE_TOPIC, MQTT_OFF);
|
||||
}
|
||||
else
|
||||
|
||||
mqttClient.subscribe(MQTT_TF_TOPIC);
|
||||
} else
|
||||
{
|
||||
Serial.print("ERROR: failed, rc=");
|
||||
Serial.print(mqttClient.state());
|
||||
|
|
@ -147,14 +158,21 @@ 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 {
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue