mqtt for irrigation

This commit is contained in:
Stefan Ostermann 2021-06-17 22:46:51 +02:00
parent cc51b50e50
commit 9c01025058
4 changed files with 91 additions and 4 deletions

View File

@ -15,4 +15,6 @@ framework = arduino
monitor_speed = 9600 monitor_speed = 9600
upload_port = /dev/ttyUSB0 upload_port = /dev/ttyUSB0
lib_deps = lib_deps =
tzapu/WiFiManager@^0.16.0 tzapu/WiFiManager@^0.16.0
knolleary/PubSubClient@^2.8
bblanchon/ArduinoJson@^6.17.3

1
irrigation/src/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.h

View File

@ -0,0 +1,16 @@
#ifndef __CONFIG_H__
#define __CONFIG_H__
/**
* For project specific settings. Copy to config.h and place your settings there.
**/
const char *MQTT_BROKER = "192.168.178.74";
const char *MQTT_TF_TOPIC = "greenhousino/pump";
const char *MQTT_CLIENT_ID = "greenhousino/pump";
const char *MQTT_LAST_WILL_TOPIC = "greenhousino/pump/status";
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
#endif

View File

@ -7,19 +7,29 @@
#include "WebContent.h" #include "WebContent.h"
#include <PubSubClient.h> //MQTT
#include <ArduinoJson.h>
#define RelaisPin D4 // Relais #define RelaisPin D4 // Relais
#define SwitchPin D1 // Active Low, Switch between D1 and GND #define SwitchPin D1 // Active Low, Switch between D1 and GND
#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"
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
//MQTT
WiFiClient espClient;
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 // Max time the pump is active
#define PUMP_ACTIVE_MILLIS 20000 #define PUMP_ACTIVE_MILLIS 30000
void handleRoot(); // function prototypes for HTTP handlers void handleRoot(); // function prototypes for HTTP handlers
void handleOn(); void handleOn();
@ -30,6 +40,9 @@ void handleState();
void deactivatePump(); void deactivatePump();
void activatePump(); void activatePump();
void mqtt_callback(char* inTopic, byte* payload, unsigned int length);
void reconnect();
void configModeCallback(WiFiManager *myWiFiManager) void configModeCallback(WiFiManager *myWiFiManager)
{ {
Serial.println("Entered config mode"); Serial.println("Entered config mode");
@ -71,6 +84,8 @@ void setup()
//if you get here you have connected to the WiFi //if you get here you have connected to the WiFi
Serial.println("connected...yeey :)"); Serial.println("connected...yeey :)");
mqttClient.setCallback(mqtt_callback);
server.on("/", handleRoot); // Call the 'handleRoot' function when a client requests URI "/" server.on("/", handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
server.on("/on", handleOn); server.on("/on", handleOn);
server.on("/off", handleOff); server.on("/off", handleOff);
@ -86,11 +101,63 @@ void setup()
void loop() void loop()
{ {
server.handleClient(); // Listen for HTTP requests from clients server.handleClient(); // Listen for HTTP requests from clients
if (millis()-pumpStarted>PUMP_ACTIVE_MILLIS) {
if (!mqttClient.connected())
{
reconnect();
}
mqttClient.loop();
if (millis()-pumpStarted>PUMP_ACTIVE_MILLIS && digitalRead(RelaisPin) == R_ON) {
deactivatePump(); deactivatePump();
} }
} }
void reconnect()
{
// Loop until we're reconnected
while (!mqttClient.connected())
{
Serial.println("INFO: Attempting MQTT connection...");
// Attempt to connect
if (mqttClient.connect(MQTT_CLIENT_ID))
{
Serial.println("INFO: connected");
mqttClient.subscribe(MQTT_TF_TOPIC);
}
else
{
Serial.print("ERROR: failed, rc=");
Serial.print(mqttClient.state());
Serial.println("DEBUG: try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void mqtt_callback(char* inTopic, byte* payload, unsigned int length){
Serial.println("Got RF data");
Serial.write(payload, length);
Serial.println();
// convert the byte non terminated payload to string:
char pltext[length+1];
strncpy(pltext, (char*)payload, length);
pltext[length]='\0';
// convert payload string to int:
if (String(pltext)=="on") {
activatePump();
} else if (String(pltext)=="off") {
deactivatePump();
} else {
Serial.println("mqtt payload unknown!");
}
Serial.println("RF data sent");
}
void handleRoot() void handleRoot()
{ {
server.send(200, "text/html", index_html); server.send(200, "text/html", index_html);
@ -99,17 +166,18 @@ void handleRoot()
void activatePump() { void activatePump() {
pumpStarted = millis(); pumpStarted = millis();
digitalWrite(RelaisPin, R_ON); digitalWrite(RelaisPin, R_ON);
Serial.println("Pump activated");
} }
void deactivatePump() { void deactivatePump() {
digitalWrite(RelaisPin, R_OFF); digitalWrite(RelaisPin, R_OFF);
Serial.println("Pump deactivated");
} }
void handleOn() void handleOn()
{ {
activatePump(); activatePump();
server.send(200, "text/plain", "on"); server.send(200, "text/plain", "on");
} }
void handleOff() void handleOff()