From fa476a75d5c42c1161821b46e9eae8d8e7802ba0 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Fri, 1 Nov 2019 23:39:21 +0000 Subject: [PATCH] Add webserver and mDNS for easier deployment/admin and starting implementation of hackerspace closing notification --- main.ino | 133 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 120 insertions(+), 13 deletions(-) diff --git a/main.ino b/main.ino index ec868d1..11f68a5 100644 --- a/main.ino +++ b/main.ino @@ -7,15 +7,18 @@ #include //https://github.com/tzapu/WiFiManager #define ARDUINOJSON_DECODE_UNICODE 1 #include //https://github.com/bblanchon/ArduinoJson +#include // https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html //for LED status #include Ticker ticker; const byte RELAY_PIN = 12; -const byte LED_PIN = 13; // 13 for Sonoff S20, 2 for NodeMCU/ESP12 internal LED +const byte LED_PIN = 2; // 13 for Sonoff S20, 2 for NodeMCU/ESP12 internal LED const byte BUTTON_PIN = 0; +bool fuzIsOpen = false; + void tick() { //toggle state bool state = digitalRead(LED_PIN); // get the current state of LED_PIN pin @@ -161,7 +164,7 @@ bool getMessages(String roomId) { if (content.containsKey("formatted_body")) { String formatted_body = content["formatted_body"]; Serial.println(formatted_body); - if (formatted_body.indexOf("presence") >= 0) { + if (formatted_body.indexOf("" + matrixUsername.substring(0, matrixUsername.indexOf(":")) + "") >= 0) { mentionedOnMatrix = true; } } @@ -180,7 +183,7 @@ bool getMessages(String roomId) { http.addHeader("Content-Type", "application/json"); http.POST(""); String receiptBody = http.getString(); - Serial.println("Receipt "+receiptBody); + Serial.println("Receipt " + receiptBody); } } String myLastMessageToken = jsonBuffer["end"]; @@ -196,6 +199,99 @@ bool getMessages(String roomId) { } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +ESP8266WebServer httpServer(80); // webserver on port 80 https://github.com/esp8266/Arduino/blob/14262af0d19a9a3b992d5aa310a684d47b6fb876/libraries/ESP8266WebServer/examples/AdvancedWebServer/AdvancedWebServer.ino +void handleRoot() { + String html = + String("") + + "presence" + + "Admin
" + + "fuzisopen API

" + + "Rotating light: " + String(digitalRead(RELAY_PIN) == HIGH) + "
" + + "Fuz is open: " + String(fuzIsOpen) + "
" + + ""; + httpServer.send(200, "text/html", html); +} +void handleAdmin() { + if (!httpServer.authenticate(matrixUsername.c_str(), matrixPassword.c_str())) { + return httpServer.requestAuthentication(); + } + for (int i = 0; i < httpServer.args(); i++) { + if (httpServer.argName(i) == "disablerotatinglight") { + digitalWrite(RELAY_PIN, LOW); + continue; + } + if (httpServer.argName(i) == "enablerotatinglight") { + digitalWrite(RELAY_PIN, HIGH); + continue; + } + if (httpServer.argName(i) == "setfuzisopen") { + fuzIsOpen = true; + continue; + } + } + + if (httpServer.method() == HTTP_POST) { + for (int i = 0; i < httpServer.args(); i++) { + if (httpServer.argName(i) == "matrixUsername") { + matrixUsername = httpServer.arg(i); + continue; + } + if (httpServer.argName(i) == "matrixPassword") { + matrixPassword = httpServer.arg(i); + continue; + } + if (httpServer.argName(i) == "matrixRoom") { + matrixRoom = httpServer.arg(i); + continue; + } + if (httpServer.argName(i) == "matrixMessage") { + matrixMessage = httpServer.arg(i); + continue; + } + } + Serial.println(F("saving config")); + StaticJsonDocument<800> jsonBuffer; + jsonBuffer["matrixUsername"] = matrixUsername; + jsonBuffer["matrixPassword"] = matrixPassword; + jsonBuffer["matrixRoom"] = matrixRoom; + jsonBuffer["matrixMessage"] = matrixMessage; + + File configFile = SPIFFS.open("/config.json", "w"); + if (!configFile) { + Serial.println(F("failed to open config file for writing")); + } + + serializeJson(jsonBuffer, Serial); + Serial.println(); + serializeJson(jsonBuffer, configFile); + 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); + return httpServer.send(302, "text/plain", ""); + } + + String html = + String("") + + "presence admin" + + (digitalRead(RELAY_PIN) == HIGH ? "Disable rotating light" : "Enable rotating light") + "
" + + (fuzIsOpen ? "" : "Set Fuz as open") + "

" + + "
" + + "
" + + "
" + + "
" + + "
" + + "
" + ""; + 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"); +} + void morseSOSLED() { // ... ___ ... for (int i = 0; i < 3; i++) { digitalWrite(LED_PIN, LOW); // lit up @@ -313,10 +409,6 @@ void setup() { //reset settings - for testing //wifiManager.resetSettings(); - //set minimu quality of signal so it ignores AP's under that quality - //defaults to 8% - //wifiManager.setMinimumSignalQuality(); - //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds @@ -369,6 +461,7 @@ void setup() { Serial.println(F("local ip:")); Serial.println(WiFi.localIP()); + Serial.println(WiFi.SSID()); http.setReuse(true); loggedInMatrix = login(matrixUsername, matrixPassword); @@ -376,14 +469,22 @@ void setup() { Serial.println("Sucessfully athenticated"); //keep LED on digitalWrite(LED_PIN, LOW); - //light up the light + //light up rotating light digitalWrite(RELAY_PIN, HIGH); - } else { - //switch LED off - digitalWrite(LED_PIN, HIGH); - //power down the light - digitalWrite(RELAY_PIN, LOW); } + + httpServer.on("/", handleRoot); + httpServer.on("/admin", handleAdmin); + httpServer.on("/fuzisopen", handleFuzIsOpen); + httpServer.onNotFound(handleNotFound); + httpServer.begin(); + Serial.println("HTTP server started"); + if (!MDNS.begin("presence")) { // https://github.com/esp8266/Arduino/blob/14262af0d19a9a3b992d5aa310a684d47b6fb876/libraries/ESP8266mDNS/examples/mDNS_Web_Server/mDNS_Web_Server.ino + Serial.println("Error setting up MDNS responder!"); + } else { + Serial.println("mDNS responder started"); + } + MDNS.addService("http", "tcp", 80); } bool buttonState = HIGH; @@ -392,8 +493,13 @@ long previousMillis = 0; const long getMatrixMessagesInterval = 5000; unsigned long pressedTime = millis(); void loop() { + MDNS.update(); + + httpServer.handleClient(); + buttonState = digitalRead(BUTTON_PIN); if (buttonState == LOW && previousButtonState == HIGH) { // long press handling, reset settings https://forum.arduino.cc/index.php?topic=276789.msg1947963#msg1947963 + Serial.println("Button pressed (longpress handling)"); pressedTime = millis(); } if (buttonState == LOW && previousButtonState == LOW && (millis() - pressedTime) > 5000) { @@ -427,6 +533,7 @@ void loop() { delay(100); Serial.println("Button pressed"); digitalWrite(RELAY_PIN, LOW); + fuzIsOpen = true; } digitalWrite(LED_PIN, LOW); // light up the LED, in case we encounter temporary failure in getMessages() previousButtonState = buttonState;