Compare commits

...

3 Commits

Author SHA1 Message Date
Lomanic a0d852c1ed Ping presence-button.glitch.me via (insecure) HTTPS 2020-02-24 23:35:01 +01:00
Lomanic 4c74d44d02 Remove useless @mention notification API 2020-02-24 23:14:08 +01:00
Lomanic 404c440ed4 Remove EspSaveCrash as it wasn't saving any crash, doh! 2020-02-24 22:41:57 +01:00
1 changed files with 12 additions and 82 deletions

View File

@ -9,9 +9,7 @@
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <ESP8266mDNS.h> // https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html
#include "EspSaveCrash.h" //DEBUG https://github.com/krzychb/EspSaveCrash
//EspSaveCrash SaveCrash;
#include <WiFiClientSecure.h>
//for LED status
#include <Ticker.h>
@ -139,68 +137,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("<a href=\"https://matrix.to/#/@" + matrixUsername + "\">" + matrixUsername.substring(0, matrixUsername.indexOf(":")) + "</a>") >= 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
@ -324,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));
@ -339,8 +276,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
@ -504,6 +439,8 @@ void setup() {
Serial.println("mDNS responder started");
}
MDNS.addService("http", "tcp", 80);
secureClient.setInsecure();
}
bool buttonState = HIGH;
@ -528,24 +465,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);
@ -555,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;
}