diff --git a/main.ino b/main.ino index 77d9a4a..b30f143 100644 --- a/main.ino +++ b/main.ino @@ -9,7 +9,9 @@ #include //https://github.com/bblanchon/ArduinoJson #include // https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html -#include + +#include "EspSaveCrash.h" //DEBUG https://github.com/krzychb/EspSaveCrash +//EspSaveCrash SaveCrash; //for LED status #include @@ -137,6 +139,68 @@ 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 @@ -260,11 +324,10 @@ void morseSOSLED() { // ... ___ ... delay(500); } -BearSSL::WiFiClientSecure secureClient; HTTPClient http2; // http2.setReuse(true); void notifyFuzIsOpen() { - http2.begin(secureClient, "https://presence-button.glitch.me/status?fuzisopen=" + String(fuzIsOpen)); + http2.begin("http://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)); @@ -276,6 +339,8 @@ void setup() { Serial.begin(115200); Serial.println(); + SaveCrash.print(); // DEBUG + //set relay pin as output pinMode(RELAY_PIN, OUTPUT); //set led pin as output @@ -439,8 +504,6 @@ void setup() { Serial.println("mDNS responder started"); } MDNS.addService("http", "tcp", 80); - - secureClient.setInsecure(); } bool buttonState = HIGH; @@ -465,17 +528,24 @@ 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); @@ -485,6 +555,6 @@ void loop() { digitalWrite(RELAY_PIN, LOW); fuzIsOpen = true; } - digitalWrite(LED_PIN, LOW); // ensure the LED is lit + digitalWrite(LED_PIN, LOW); // light up the LED, in case we encounter temporary failure in getMessages() previousButtonState = buttonState; }