From 404c440ed4128d0a19e97092025c69cfb92c612e Mon Sep 17 00:00:00 2001 From: Lomanic Date: Mon, 24 Feb 2020 22:41:57 +0100 Subject: [PATCH 1/3] Remove EspSaveCrash as it wasn't saving any crash, doh! --- main.ino | 6 ------ 1 file changed, 6 deletions(-) diff --git a/main.ino b/main.ino index b30f143..9ee7907 100644 --- a/main.ino +++ b/main.ino @@ -9,10 +9,6 @@ #include //https://github.com/bblanchon/ArduinoJson #include // https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html - -#include "EspSaveCrash.h" //DEBUG https://github.com/krzychb/EspSaveCrash -//EspSaveCrash SaveCrash; - //for LED status #include Ticker ticker; @@ -339,8 +335,6 @@ void setup() { Serial.begin(115200); Serial.println(); - SaveCrash.print(); // DEBUG - //set relay pin as output pinMode(RELAY_PIN, OUTPUT); //set led pin as output From 4c74d44d02f179aed81a439ef1abf0fd14eb860c Mon Sep 17 00:00:00 2001 From: Lomanic Date: Mon, 24 Feb 2020 23:14:00 +0100 Subject: [PATCH 2/3] Remove useless @mention notification API --- main.ino | 81 +++++--------------------------------------------------- 1 file changed, 6 insertions(+), 75 deletions(-) diff --git a/main.ino b/main.ino index 9ee7907..6771a84 100644 --- a/main.ino +++ b/main.ino @@ -135,68 +135,6 @@ bool sendMessage(String roomId, String message) { } return success; } - -bool mentionedOnMatrix = false; -bool getMessages(String roomId) { - bool success = false; - - String url = "http://corsanywhere.glitch.me/https://" + roomId.substring(roomId.indexOf(":") + 1) + "/_matrix/client/r0/rooms/" + roomId + "/messages?access_token=" + accessToken + "&limit=1"; - if (lastMessageToken == "") { - url += "&dir=b"; - } else { - url += "&dir=f&from=" + lastMessageToken; - } - Serial.printf("GET %s\n", url.c_str()); - - http.begin(url); - int rc = http.GET(); - if (rc > 0) { - Serial.printf("%d\n", rc); - if (rc == HTTP_CODE_OK) { - String body = http.getString(); - StaticJsonDocument<1000> jsonBuffer; - deserializeJson(jsonBuffer, body); - if (lastMessageToken != "") { - JsonArray chunks = jsonBuffer["chunk"]; - JsonObject chunk = chunks[0]; - String format = chunk["format"]; - JsonObject content = chunk["content"]; - if (content.containsKey("formatted_body")) { - String formatted_body = content["formatted_body"]; - Serial.println(formatted_body); - if (formatted_body.indexOf("" + matrixUsername.substring(0, matrixUsername.indexOf(":")) + "") >= 0) { - mentionedOnMatrix = true; - } - } - if (content.containsKey("body")) { - String body = content["body"]; - Serial.println(body); - if (body.indexOf(matrixUsername.substring(0, matrixUsername.indexOf(":")) + ":") == 0 || body.indexOf("@" + matrixUsername) >= 0) { - mentionedOnMatrix = true; - } - } - //read receipt - if (chunk.containsKey("event_id")) { - String event_id = chunk["event_id"]; - String receiptUrl = "http://corsanywhere.glitch.me/https://" + roomId.substring(roomId.indexOf(":") + 1) + "/_matrix/client/r0/rooms/" + roomId + "/receipt/m.read/" + event_id + "?access_token=" + accessToken + "&limit=1"; - http.begin(receiptUrl); - http.addHeader("Content-Type", "application/json"); - http.POST(""); - String receiptBody = http.getString(); - Serial.println("Receipt " + receiptBody); - } - } - String myLastMessageToken = jsonBuffer["end"]; - lastMessageToken = String(myLastMessageToken.c_str()); - //Serial.println(lastMessageToken); - success = true; - } - } else { - Serial.printf("Error: %s\n", http.errorToString(rc).c_str()); - } - - return success; -} //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ESP8266WebServer httpServer(80); // webserver on port 80 https://github.com/esp8266/Arduino/blob/14262af0d19a9a3b992d5aa310a684d47b6fb876/libraries/ESP8266WebServer/examples/AdvancedWebServer/AdvancedWebServer.ino @@ -522,24 +460,17 @@ void loop() { ESP.reset(); } + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis > getMatrixMessagesInterval) { + previousMillis = currentMillis; + notifyFuzIsOpen(); + } + if (!loggedInMatrix) { // send SOS in morse morseSOSLED(); loggedInMatrix = login(matrixUsername, matrixPassword); return; } - unsigned long currentMillis = millis(); - if (currentMillis - previousMillis > getMatrixMessagesInterval) { - previousMillis = currentMillis; - if (!getMessages(matrixRoom)) { - morseSOSLED(); - return; - } - if (mentionedOnMatrix) { - digitalWrite(RELAY_PIN, HIGH); - mentionedOnMatrix = false; - } - notifyFuzIsOpen(); - } bool relayState = digitalRead(RELAY_PIN); From a0d852c1ed49de9c049d2704f0af9fc154c130fd Mon Sep 17 00:00:00 2001 From: Lomanic Date: Mon, 24 Feb 2020 23:35:01 +0100 Subject: [PATCH 3/3] Ping presence-button.glitch.me via (insecure) HTTPS --- main.ino | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/main.ino b/main.ino index 6771a84..77d9a4a 100644 --- a/main.ino +++ b/main.ino @@ -9,6 +9,8 @@ #include //https://github.com/bblanchon/ArduinoJson #include // https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html +#include + //for LED status #include Ticker ticker; @@ -258,10 +260,11 @@ void morseSOSLED() { // ... ___ ... delay(500); } +BearSSL::WiFiClientSecure secureClient; HTTPClient http2; // http2.setReuse(true); void notifyFuzIsOpen() { - http2.begin("http://presence-button.glitch.me/status?fuzisopen=" + String(fuzIsOpen)); + http2.begin(secureClient, "https://presence-button.glitch.me/status?fuzisopen=" + String(fuzIsOpen)); http2.setAuthorization(matrixUsername.c_str(), matrixPassword.c_str()); int httpCode = http2.GET(); Serial.println("GET status return code: " + String(httpCode)); @@ -436,6 +439,8 @@ void setup() { Serial.println("mDNS responder started"); } MDNS.addService("http", "tcp", 80); + + secureClient.setInsecure(); } bool buttonState = HIGH; @@ -480,6 +485,6 @@ void loop() { digitalWrite(RELAY_PIN, LOW); fuzIsOpen = true; } - digitalWrite(LED_PIN, LOW); // light up the LED, in case we encounter temporary failure in getMessages() + digitalWrite(LED_PIN, LOW); // ensure the LED is lit previousButtonState = buttonState; }