diff --git a/irrigation/platformio.ini b/irrigation/platformio.ini index b27f964..1d5fa31 100644 --- a/irrigation/platformio.ini +++ b/irrigation/platformio.ini @@ -15,4 +15,6 @@ framework = arduino monitor_speed = 9600 upload_port = /dev/ttyUSB0 lib_deps = - tzapu/WiFiManager@^0.16.0 \ No newline at end of file + tzapu/WiFiManager@^0.16.0 + knolleary/PubSubClient@^2.8 + bblanchon/ArduinoJson@^6.17.3 \ No newline at end of file diff --git a/irrigation/src/.gitignore b/irrigation/src/.gitignore new file mode 100644 index 0000000..0e56cf2 --- /dev/null +++ b/irrigation/src/.gitignore @@ -0,0 +1 @@ +config.h diff --git a/irrigation/src/config.h.example b/irrigation/src/config.h.example new file mode 100644 index 0000000..c3df69f --- /dev/null +++ b/irrigation/src/config.h.example @@ -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 \ No newline at end of file diff --git a/irrigation/src/main.cpp b/irrigation/src/main.cpp index a1b5856..9585b5b 100644 --- a/irrigation/src/main.cpp +++ b/irrigation/src/main.cpp @@ -7,19 +7,29 @@ #include "WebContent.h" +#include //MQTT +#include + #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()