mqtt for irrigation
This commit is contained in:
parent
cc51b50e50
commit
9c01025058
|
|
@ -16,3 +16,5 @@ monitor_speed = 9600
|
|||
upload_port = /dev/ttyUSB0
|
||||
lib_deps =
|
||||
tzapu/WiFiManager@^0.16.0
|
||||
knolleary/PubSubClient@^2.8
|
||||
bblanchon/ArduinoJson@^6.17.3
|
||||
|
|
@ -0,0 +1 @@
|
|||
config.h
|
||||
|
|
@ -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
|
||||
|
|
@ -7,19 +7,29 @@
|
|||
|
||||
#include "WebContent.h"
|
||||
|
||||
#include <PubSubClient.h> //MQTT
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#define RelaisPin D4 // Relais
|
||||
#define SwitchPin D1 // Active Low, Switch between D1 and GND
|
||||
|
||||
#define R_ON 0// active Low = 0, otherwise 1
|
||||
#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
|
||||
|
||||
//MQTT
|
||||
WiFiClient espClient;
|
||||
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 20000
|
||||
#define PUMP_ACTIVE_MILLIS 30000
|
||||
|
||||
void handleRoot(); // function prototypes for HTTP handlers
|
||||
void handleOn();
|
||||
|
|
@ -30,6 +40,9 @@ void handleState();
|
|||
void deactivatePump();
|
||||
void activatePump();
|
||||
|
||||
void mqtt_callback(char* inTopic, byte* payload, unsigned int length);
|
||||
void reconnect();
|
||||
|
||||
void configModeCallback(WiFiManager *myWiFiManager)
|
||||
{
|
||||
Serial.println("Entered config mode");
|
||||
|
|
@ -71,6 +84,8 @@ void setup()
|
|||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
|
||||
mqttClient.setCallback(mqtt_callback);
|
||||
|
||||
server.on("/", handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
|
||||
server.on("/on", handleOn);
|
||||
server.on("/off", handleOff);
|
||||
|
|
@ -86,11 +101,63 @@ void setup()
|
|||
void loop()
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
server.send(200, "text/html", index_html);
|
||||
|
|
@ -99,17 +166,18 @@ void handleRoot()
|
|||
void activatePump() {
|
||||
pumpStarted = millis();
|
||||
digitalWrite(RelaisPin, R_ON);
|
||||
Serial.println("Pump activated");
|
||||
}
|
||||
|
||||
void deactivatePump() {
|
||||
digitalWrite(RelaisPin, R_OFF);
|
||||
Serial.println("Pump deactivated");
|
||||
}
|
||||
|
||||
void handleOn()
|
||||
{
|
||||
activatePump();
|
||||
server.send(200, "text/plain", "on");
|
||||
|
||||
}
|
||||
|
||||
void handleOff()
|
||||
|
|
|
|||
Loading…
Reference in New Issue