From 450fdfcddf5a187fb261470427bf255231f175d5 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 3 Nov 2019 17:34:39 +0100 Subject: [PATCH] Notify if the hackerspace is open or not to presence-button.glitch.me using HTTP polling --- main.ino | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/main.ino b/main.ino index 11f68a5..d67cab7 100644 --- a/main.ino +++ b/main.ino @@ -205,7 +205,6 @@ void handleRoot() { String("") + "presence" + "Admin
" + - "fuzisopen API

" + "Rotating light: " + String(digitalRead(RELAY_PIN) == HIGH) + "
" + "Fuz is open: " + String(fuzIsOpen) + "
" + ""; @@ -228,6 +227,13 @@ void handleAdmin() { fuzIsOpen = true; continue; } + if (httpServer.argName(i) == "resetesp") { + httpServer.sendHeader("Location", httpServer.uri(), true); + httpServer.send(302, "text/plain", ""); + delay(500); + ESP.reset(); + return; + } } if (httpServer.method() == HTTP_POST) { @@ -267,7 +273,7 @@ void handleAdmin() { configFile.close(); } if (httpServer.args() > 0 || httpServer.method() == HTTP_POST) { // trim GET parameters and prevent resubmiting same form on refresh - httpServer.sendHeader("Location", String("/admin"), true); + httpServer.sendHeader("Location", httpServer.uri(), true); return httpServer.send(302, "text/plain", ""); } @@ -275,7 +281,8 @@ void handleAdmin() { String("") + "presence admin" + (digitalRead(RELAY_PIN) == HIGH ? "Disable rotating light" : "Enable rotating light") + "
" + - (fuzIsOpen ? "" : "Set Fuz as open") + "

" + + (fuzIsOpen ? "" : "Set Fuz as open") + "
" + + "Reboot ESP" + "

" + "
" + "
" + "
" + @@ -285,9 +292,6 @@ void handleAdmin() { ""; httpServer.send(200, "text/html", html); } -void handleFuzIsOpen() { - httpServer.send(200, "text/plain", String(fuzIsOpen)); -} void handleNotFound() { httpServer.send(404, "text/plain", httpServer.uri() + " not found"); } @@ -316,6 +320,14 @@ void morseSOSLED() { // ... ___ ... delay(500); } +void notifyFuzIsOpen() { + HTTPClient http; + http.begin("http://presence-button.glitch.me/status?fuzisopen=" + String(fuzIsOpen)); + http.setAuthorization(matrixUsername.c_str(), matrixPassword.c_str()); + http.GET(); + Serial.println("GET status body: " + http.getString()); +} + bool loggedInMatrix = false; void setup() { Serial.begin(115200); @@ -475,7 +487,6 @@ void setup() { httpServer.on("/", handleRoot); httpServer.on("/admin", handleAdmin); - httpServer.on("/fuzisopen", handleFuzIsOpen); httpServer.onNotFound(handleNotFound); httpServer.begin(); Serial.println("HTTP server started"); @@ -525,6 +536,7 @@ void loop() { digitalWrite(RELAY_PIN, HIGH); mentionedOnMatrix = false; } + notifyFuzIsOpen(); } bool relayState = digitalRead(RELAY_PIN);