Pump timer

This commit is contained in:
Stefan Ostermann 2021-06-14 21:36:08 +02:00
parent d381748046
commit cc51b50e50
3 changed files with 111 additions and 5 deletions

View File

@ -35,13 +35,35 @@ const char index_html[] = R"rawliteral(
</head>
<body>
<h1>Greenhousino Irrigation System</h1>
<button class="button" onmousedown="toggleCheckbox('on');" ontouchstart="toggleCheckbox('on');" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">PUMP!</button>
Pump state: <span id="state"></span><br/><br/>
<button class="button" onmousedown="toggleCheckbox('on');" ontouchstart="toggleCheckbox('on');" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">PUMP!</button><br/><br/>
<button class="button" onmouseup="toggleCheckbox('on');" ontouchend="toggleCheckbox('on');">On!</button><br/><br/>
<button class="button" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">Off!</button><br/><br/>
<script>
setInterval(getState, 500);
function toggleCheckbox(x) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/" + x, true);
xhr.send();
}
function getState() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
displayState(xhr.response);
}
}
xhr.open("GET","/state", true);
xhr.send();
}
function displayState(state) {
document.getElementById("state").innerHTML = state;
}
</script>
</body>
</html>)rawliteral";

49
irrigation/src/index.html Normal file
View File

@ -0,0 +1,49 @@
<!DOCTYPE HTML><html>
<head>
<title>Greenhousino Irrigation System</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial; text-align: center; margin:0px auto; padding-top: 30px;}
.button {
padding: 10px 20px;
font-size: 24px;
text-align: center;
outline: none;
color: #fff;
background-color: #2f4468;
border: none;
border-radius: 5px;
box-shadow: 0 6px #999;
cursor: pointer;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
.button:hover {background-color: #1f2e45}
.button:active {
background-color: #1f2e45;
box-shadow: 0 4px #666;
transform: translateY(2px);
}
</style>
</head>
<body>
<h1>Greenhousino Irrigation System</h1>
<h2>State: <div id="state"></div></h2>
<button class="button" onmousedown="toggleCheckbox('on');" ontouchstart="toggleCheckbox('on');" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">PUMP!</button>
<button class="button" onmouseup="toggleCheckbox('on');" ontouchend="toggleCheckbox('on');">On!</button>
<button class="button" onmouseup="toggleCheckbox('off');" ontouchend="toggleCheckbox('off');">Off!</button>
<script>
function toggleCheckbox(x) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/" + x, true);
xhr.send();
}
</script>
</body>
</html>

View File

@ -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