diff --git a/irrigation/src/WebContent.h b/irrigation/src/WebContent.h index a5b1418..a3c7368 100644 --- a/irrigation/src/WebContent.h +++ b/irrigation/src/WebContent.h @@ -35,13 +35,35 @@ const char index_html[] = R"rawliteral(

Greenhousino Irrigation System

- + Pump state:

+

+

+

)rawliteral"; \ No newline at end of file diff --git a/irrigation/src/index.html b/irrigation/src/index.html new file mode 100644 index 0000000..ef4a8ab --- /dev/null +++ b/irrigation/src/index.html @@ -0,0 +1,49 @@ + + + Greenhousino Irrigation System + + + + +

Greenhousino Irrigation System

+

State:

+ + + + + + \ No newline at end of file diff --git a/irrigation/src/main.cpp b/irrigation/src/main.cpp index 64ae813..a1b5856 100644 --- a/irrigation/src/main.cpp +++ b/irrigation/src/main.cpp @@ -15,10 +15,20 @@ ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80 +// time in millis the pump has been started +long pumpStarted = 0; + +// Max time the pump is active +#define PUMP_ACTIVE_MILLIS 20000 + void handleRoot(); // function prototypes for HTTP handlers void handleOn(); void handleOff(); void handleNotFound(); +void handleState(); + +void deactivatePump(); +void activatePump(); void configModeCallback(WiFiManager *myWiFiManager) { @@ -62,8 +72,9 @@ void setup() Serial.println("connected...yeey :)"); server.on("/", handleRoot); // Call the 'handleRoot' function when a client requests URI "/" - server.on("/on", handleOn); // Call the 'handleRoot' function when a client requests URI "/" - server.on("/off", handleOff); // Call the 'handleRoot' function when a client requests URI "/" + server.on("/on", handleOn); + server.on("/off", handleOff); + server.on("/state", handleState); server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound" @@ -75,6 +86,9 @@ void setup() void loop() { server.handleClient(); // Listen for HTTP requests from clients + if (millis()-pumpStarted>PUMP_ACTIVE_MILLIS) { + deactivatePump(); + } } void handleRoot() @@ -82,19 +96,40 @@ void handleRoot() server.send(200, "text/html", index_html); } +void activatePump() { + pumpStarted = millis(); + digitalWrite(RelaisPin, R_ON); +} + +void deactivatePump() { + digitalWrite(RelaisPin, R_OFF); +} + void handleOn() { - digitalWrite(RelaisPin, R_ON); + activatePump(); server.send(200, "text/plain", "on"); } void handleOff() { - digitalWrite(RelaisPin, R_OFF); + deactivatePump(); server.send(200, "text/plain", "off"); } +void handleState() +{ + int state = digitalRead(RelaisPin); + String sstate = "off"; + switch (state) { + case R_ON: + sstate = "on"; + break; + } + server.send(200,"text/plain", sstate); +} + void handleNotFound() { server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request