mirror of
https://github.com/Lomanic/presence-button
synced 2024-11-22 05:17:30 +00:00
React to mentions on Matrix and send reply on button press, use ESP8266WiFiMulti
This commit is contained in:
parent
879f1d8a88
commit
bbd8eba8f1
359
main.ino
359
main.ino
@ -1,14 +1,19 @@
|
|||||||
#include <FS.h> //this needs to be first, or it all crashes and burns...
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266WiFiMulti.h>
|
||||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
#define ARDUINOJSON_DECODE_UNICODE 1
|
||||||
|
|
||||||
//needed for library
|
|
||||||
#include <DNSServer.h>
|
|
||||||
#include <ESP8266WebServer.h>
|
|
||||||
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
|
|
||||||
|
|
||||||
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
|
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
|
||||||
|
|
||||||
|
ESP8266WiFiMulti wifiMulti;
|
||||||
|
|
||||||
|
const byte RELAY_PIN = 12;
|
||||||
|
const byte LED_PIN = 13; // 13 for Sonoff S20, 2 for NodeMCU/ESP12 internal LED
|
||||||
|
const byte BUTTON_PIN = 0;
|
||||||
|
|
||||||
|
const String matrixUsername = "presence:matrix.fuz.re";
|
||||||
|
const String matrixPassword = "XXX";
|
||||||
|
const String matrixRoom = "!XXX:XXX";
|
||||||
|
const String matrixMessage = "Test from Sonoff S20";
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
#include <ESP8266HTTPClient.h> // for https://github.com/matt-williams/matrix-esp8266/blob/master/matrix-esp8266.ino
|
#include <ESP8266HTTPClient.h> // for https://github.com/matt-williams/matrix-esp8266/blob/master/matrix-esp8266.ino
|
||||||
|
|
||||||
@ -42,9 +47,9 @@ bool login(String user, String password) {
|
|||||||
bool success = false;
|
bool success = false;
|
||||||
|
|
||||||
String buffer;
|
String buffer;
|
||||||
buffer = createLoginBody(user, password);
|
buffer = createLoginBody(user.substring(0, user.indexOf(":")), password);
|
||||||
|
|
||||||
String url = "http://corsanywhere.glitch.me/https://matrix.fuz.re/_matrix/client/r0/login";
|
String url = "http://corsanywhere.glitch.me/https://" + user.substring(user.indexOf(":") + 1) + "/_matrix/client/r0/login";
|
||||||
// Serial.printf("POST %s\n", url.c_str());
|
// Serial.printf("POST %s\n", url.c_str());
|
||||||
|
|
||||||
http.begin(url);
|
http.begin(url);
|
||||||
@ -75,7 +80,7 @@ bool sendMessage(String roomId, String message) {
|
|||||||
String buffer;
|
String buffer;
|
||||||
buffer = createMessageBody(message);
|
buffer = createMessageBody(message);
|
||||||
|
|
||||||
String url = "http://corsanywhere.glitch.me/https://matrix.fuz.re/_matrix/client/r0/rooms/" + roomId + "/send/m.room.message/" + String(millis()) + "?access_token=" + accessToken + "&limit=1";
|
String url = "http://corsanywhere.glitch.me/https://" + roomId.substring(roomId.indexOf(":") + 1) + "/_matrix/client/r0/rooms/" + roomId + "/send/m.room.message/" + String(millis()) + "?access_token=" + accessToken + "&limit=1";
|
||||||
Serial.printf("PUT %s\n", url.c_str());
|
Serial.printf("PUT %s\n", url.c_str());
|
||||||
|
|
||||||
http.begin(url);
|
http.begin(url);
|
||||||
@ -90,222 +95,156 @@ bool sendMessage(String roomId, String message) {
|
|||||||
}
|
}
|
||||||
return success;
|
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 + "\">presence</a>") >= 0) {
|
||||||
|
mentionedOnMatrix = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (content.containsKey("body")) {
|
||||||
|
String body = content["body"];
|
||||||
|
Serial.println(body);
|
||||||
|
if (body.indexOf(matrixUsername.substring(0, matrixUsername.indexOf(":")) + ":") == 0) {
|
||||||
|
mentionedOnMatrix = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
//for LED status
|
void morseSOSLED() { // ... ___ ...
|
||||||
#include <Ticker.h>
|
for (int i = 0; i < 3; i++) {
|
||||||
Ticker ticker;
|
digitalWrite(LED_PIN, LOW); // lit up
|
||||||
|
delay(100);
|
||||||
const byte LED_PIN = 2; // 13 for Sonoff S20, 2 for NodeMCU/ESP12 internal LED
|
digitalWrite(LED_PIN, HIGH); // lit down
|
||||||
const byte BUTTON_PIN = 0;
|
delay(100);
|
||||||
|
}
|
||||||
void tick()
|
delay(50);
|
||||||
{
|
for (int i = 0; i < 3; i++) {
|
||||||
//toggle state
|
digitalWrite(LED_PIN, LOW); // lit up
|
||||||
int state = digitalRead(LED_PIN); // get the current state of LED_PIN pin
|
delay(300);
|
||||||
digitalWrite(LED_PIN, !state); // set pin to the opposite state
|
digitalWrite(LED_PIN, HIGH); // lit down
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
delay(50);
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
digitalWrite(LED_PIN, LOW); // lit up
|
||||||
|
delay(100);
|
||||||
|
digitalWrite(LED_PIN, HIGH); // lit down
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
delay(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
//gets called when WiFiManager enters configuration mode (for LED status)
|
bool loggedInMatrix = false;
|
||||||
void configModeCallback (WiFiManager *myWiFiManager) {
|
|
||||||
Serial.println("Entered config mode");
|
|
||||||
Serial.println(WiFi.softAPIP());
|
|
||||||
//if you used auto generated SSID, print it
|
|
||||||
Serial.println(myWiFiManager->getConfigPortalSSID());
|
|
||||||
//entered config mode, make led toggle faster
|
|
||||||
ticker.attach(0.2, tick);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//define your default values here, if there are different values in config.json, they are overwritten.
|
|
||||||
char matrixUsername[50];
|
|
||||||
char matrixPassword[50];
|
|
||||||
char matrixRoom[200] = "!ppCFWxNWJeGbyoNZVw:matrix.fuz.re"; // #entropy:matrix.fuz.re
|
|
||||||
char matrixMessage[500] = "Test from esp8266";
|
|
||||||
|
|
||||||
//flag for saving data
|
|
||||||
bool shouldSaveConfig = false;
|
|
||||||
|
|
||||||
//callback notifying us of the need to save config
|
|
||||||
void saveConfigCallback () {
|
|
||||||
Serial.println(F("Should save config"));
|
|
||||||
shouldSaveConfig = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
WiFiManager wifiManager;
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
|
//set relay pin as output
|
||||||
|
pinMode(RELAY_PIN, OUTPUT);
|
||||||
//set led pin as output
|
//set led pin as output
|
||||||
pinMode(LED_PIN, OUTPUT);
|
pinMode(LED_PIN, OUTPUT);
|
||||||
// start ticker with 0.5 because we start in AP mode and try to connect
|
//set button pin as input
|
||||||
ticker.attach(0.6, tick);
|
|
||||||
|
|
||||||
pinMode(BUTTON_PIN, INPUT);
|
pinMode(BUTTON_PIN, INPUT);
|
||||||
|
|
||||||
//clean FS, for testing
|
WiFi.mode(WIFI_STA);
|
||||||
//SPIFFS.format();
|
wifiMulti.addAP("XXX", "XXX");
|
||||||
//show SPIFFS info for debug
|
wifiMulti.addAP("XXX", "XXX");
|
||||||
//FSInfo fs_info;
|
wifiMulti.addAP("XXX", "XXX");
|
||||||
//SPIFFS.info(fs_info);
|
|
||||||
//Serial.printf("SPIFFS: totalBytes: %u\n", fs_info.totalBytes);
|
|
||||||
//Serial.printf("SPIFFS: usedBytes : %u\n", fs_info.usedBytes);
|
|
||||||
//Serial.printf("SPIFFS: blockSize : %u\n", fs_info.blockSize);
|
|
||||||
//Serial.printf("SPIFFS: pageSize : %u\n", fs_info.pageSize);
|
|
||||||
//Serial.printf("SPIFFS: maxOpenFiles: %u\n", fs_info.maxOpenFiles);
|
|
||||||
//Serial.printf("SPIFFS: maxPathLength : %u\n", fs_info.maxPathLength);
|
|
||||||
|
|
||||||
|
Serial.println("Connecting Wifi");
|
||||||
//read configuration from FS json
|
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
|
||||||
Serial.println(F("mounting FS..."));
|
delay(1000);
|
||||||
|
Serial.print('.');
|
||||||
if (SPIFFS.begin()) {
|
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
|
||||||
Serial.println(F("mounted file system"));
|
|
||||||
if (SPIFFS.exists("/config.json")) {
|
|
||||||
//file exists, reading and loading
|
|
||||||
Serial.println(F("reading config file"));
|
|
||||||
File configFile = SPIFFS.open("/config.json", "r");
|
|
||||||
if (configFile) {
|
|
||||||
Serial.println(F("opened config file"));
|
|
||||||
size_t size = configFile.size();
|
|
||||||
// Allocate a buffer to store contents of the file.
|
|
||||||
std::unique_ptr<char[]> buf(new char[size]);
|
|
||||||
|
|
||||||
configFile.readBytes(buf.get(), size);
|
|
||||||
StaticJsonDocument<1000> jsonBuffer;
|
|
||||||
auto error = deserializeJson(jsonBuffer, buf.get());
|
|
||||||
if (error) {
|
|
||||||
Serial.print(F("deserializeJson() failed with code "));
|
|
||||||
Serial.println(error.c_str());
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
Serial.println(F("deserializeJson() successful:"));
|
|
||||||
|
|
||||||
serializeJsonPretty(jsonBuffer, Serial);
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
strcpy(matrixUsername, jsonBuffer["matrixUsername"]);
|
|
||||||
strcpy(matrixPassword, jsonBuffer["matrixPassword"]);
|
|
||||||
strcpy(matrixRoom, jsonBuffer["matrixRoom"]);
|
|
||||||
strcpy(matrixMessage, jsonBuffer["matrixMessage"]);
|
|
||||||
}
|
}
|
||||||
configFile.close();
|
Serial.println("");
|
||||||
}
|
Serial.println("WiFi connected");
|
||||||
}
|
Serial.println("IP address: ");
|
||||||
} else {
|
Serial.println(WiFi.localIP());
|
||||||
Serial.println(F("failed to mount FS"));
|
http.setReuse(true);
|
||||||
}
|
loggedInMatrix = login(matrixUsername, matrixPassword);
|
||||||
//end read
|
if (loggedInMatrix) {
|
||||||
|
Serial.println("Sucessfully athenticated");
|
||||||
|
|
||||||
|
|
||||||
// The extra parameters to be configured (can be either global or just in the setup)
|
|
||||||
// After connecting, parameter.getValue() will get you the configured value
|
|
||||||
// id/name placeholder/prompt default length
|
|
||||||
WiFiManagerParameter customMatrixUsername("Matrix username", "Matrix username", matrixUsername, 50);
|
|
||||||
WiFiManagerParameter customMatrixPassword("Matrix password", "Matrix password", matrixPassword, 50);
|
|
||||||
WiFiManagerParameter customMatrixRoom("Matrix room", "Matrix room", matrixRoom, 200);
|
|
||||||
WiFiManagerParameter customMatrixMessage("Matrix message", "Matrix message", matrixMessage, 500);
|
|
||||||
|
|
||||||
//WiFiManager
|
|
||||||
//Local intialization. Once its business is done, there is no need to keep it around
|
|
||||||
//WiFiManager wifiManager;
|
|
||||||
//reset settings - for testing
|
|
||||||
//wifiManager.resetSettings();
|
|
||||||
|
|
||||||
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode (for status LED)
|
|
||||||
wifiManager.setAPCallback(configModeCallback);
|
|
||||||
|
|
||||||
//set config save notify callback
|
|
||||||
wifiManager.setSaveConfigCallback(saveConfigCallback);
|
|
||||||
|
|
||||||
//set static ip
|
|
||||||
// wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
|
|
||||||
|
|
||||||
//add all your parameters here
|
|
||||||
wifiManager.addParameter(&customMatrixUsername);
|
|
||||||
wifiManager.addParameter(&customMatrixPassword);
|
|
||||||
wifiManager.addParameter(&customMatrixRoom);
|
|
||||||
wifiManager.addParameter(&customMatrixMessage);
|
|
||||||
|
|
||||||
//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
|
|
||||||
//wifiManager.setTimeout(120);
|
|
||||||
|
|
||||||
//fetches ssid and pass and tries to connect
|
|
||||||
//if it does not connect it starts an access point with the specified name
|
|
||||||
//here "AutoConnectAP"
|
|
||||||
//and goes into a blocking loop awaiting configuration
|
|
||||||
if (!wifiManager.autoConnect()) {
|
|
||||||
Serial.println(F("failed to connect and hit timeout"));
|
|
||||||
delay(3000);
|
|
||||||
//reset and try again, or maybe put it to deep sleep
|
|
||||||
ESP.reset();
|
|
||||||
delay(5000);
|
|
||||||
}
|
|
||||||
|
|
||||||
//if you get here you have connected to the WiFi
|
|
||||||
Serial.println(F("connected...yeey :)"));
|
|
||||||
ticker.detach();
|
|
||||||
//keep LED on
|
//keep LED on
|
||||||
digitalWrite(LED_PIN, LOW);
|
digitalWrite(LED_PIN, LOW);
|
||||||
|
//light up the light
|
||||||
//read updated parameters
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
strcpy(matrixUsername, customMatrixUsername.getValue());
|
} else {
|
||||||
strcpy(matrixPassword, customMatrixPassword.getValue());
|
//switch LED off
|
||||||
strcpy(matrixRoom, customMatrixRoom.getValue());
|
digitalWrite(LED_PIN, HIGH);
|
||||||
strcpy(matrixMessage, customMatrixMessage.getValue());
|
//power down the light
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
//save the custom parameters to FS
|
}
|
||||||
if (shouldSaveConfig) {
|
|
||||||
Serial.println(F("saving config"));
|
|
||||||
StaticJsonDocument<1000> 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);
|
bool buttonState = HIGH;
|
||||||
Serial.println();
|
bool previousButtonState = HIGH;
|
||||||
serializeJson(jsonBuffer, configFile);
|
long previousMillis = 0;
|
||||||
configFile.close();
|
const long getMatrixMessagesInterval = 5000;
|
||||||
//end save
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println(F("local ip:"));
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
|
|
||||||
http.setReuse(true);
|
|
||||||
if (login(matrixUsername, matrixPassword)) {
|
|
||||||
Serial.println(F("Sucessfully athenticated"));
|
|
||||||
sendMessage(matrixRoom, matrixMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool button_state = 0;
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
if (!loggedInMatrix) { // send SOS in morse
|
||||||
button_state = digitalRead(BUTTON_PIN);
|
morseSOSLED();
|
||||||
if (button_state == 0) {
|
return;
|
||||||
wifiManager.resetSettings();
|
}
|
||||||
delay(500);
|
unsigned long currentMillis = millis();
|
||||||
ESP.reset();
|
if (currentMillis - previousMillis > getMatrixMessagesInterval) {
|
||||||
|
previousMillis = currentMillis;
|
||||||
|
if (!getMessages(matrixRoom)) {
|
||||||
|
morseSOSLED();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (mentionedOnMatrix) {
|
||||||
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
|
mentionedOnMatrix = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buttonState = digitalRead(BUTTON_PIN);
|
||||||
|
bool relayState = digitalRead(RELAY_PIN);
|
||||||
|
if (buttonState == LOW && previousButtonState == HIGH && relayState == HIGH && sendMessage(matrixRoom, matrixMessage)) { // button just pressed while light is up
|
||||||
|
delay(100);
|
||||||
|
Serial.println("Button pressed");
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
|
}
|
||||||
|
digitalWrite(LED_PIN, LOW); // light up the LED, in case we encounter temporary failure in getMessages()
|
||||||
|
previousButtonState = buttonState;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user