Remove matrixRoom config and replace matrixMessage config by notifiedEndpoint

Also rename various variables
Unfortunately, it's not possible to do HTTP calls in a ticker
This commit is contained in:
Lomanic 2020-06-12 21:49:14 +02:00
parent 2aadc3b5e0
commit f13018e93b
1 changed files with 30 additions and 42 deletions

View File

@ -8,11 +8,11 @@
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson #include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <ESP8266mDNS.h> // https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html #include <ESP8266mDNS.h> // https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
//for LED status //for LED status
#include <Ticker.h> #include <Ticker.h>
Ticker ticker; Ticker ledTicker;
const byte RELAY_PIN = D2; // SHOULD BE 12 for Sonoff S20 const byte RELAY_PIN = D2; // SHOULD BE 12 for Sonoff S20
const byte LED_PIN = LED_BUILTIN; // 13 for Sonoff S20, 2 for NodeMCU/ESP12 internal LED const byte LED_PIN = LED_BUILTIN; // 13 for Sonoff S20, 2 for NodeMCU/ESP12 internal LED
@ -33,15 +33,14 @@ void configModeCallback (WiFiManager *myWiFiManager) {
//if you used auto generated SSID, print it //if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID()); Serial.println(myWiFiManager->getConfigPortalSSID());
//entered config mode, make led toggle faster //entered config mode, make led toggle faster
ticker.attach(0.5, blinkLED); ledTicker.attach(0.5, blinkLED);
} }
//define your default values here, if there are different values in config.json, they are overwritten. //define your default values here, if there are different values in config.json, they are overwritten.
String matrixUsername; String matrixUsername;
String matrixPassword; String matrixPassword;
String matrixRoom = "!ppCFWxNWJeGbyoNZVw:matrix.fuz.re"; // #entropy:matrix.fuz.re String notifiedEndpoint = "https://presence-button-staging.glitch.me/status?fuzisopen="; // #entropy:matrix.fuz.re
String matrixMessage = "Test";
//flag for saving data //flag for saving data
bool shouldSaveConfig = false; bool shouldSaveConfig = false;
@ -104,22 +103,17 @@ void handleAdmin() {
matrixPassword = httpServer.arg(i); matrixPassword = httpServer.arg(i);
continue; continue;
} }
if (httpServer.argName(i) == "matrixRoom") { if (httpServer.argName(i) == "notifiedEndpoint") {
matrixRoom = httpServer.arg(i); notifiedEndpoint = httpServer.arg(i);
continue;
}
if (httpServer.argName(i) == "matrixMessage") {
matrixMessage = httpServer.arg(i);
continue; continue;
} }
} }
Serial.println(F("saving config")); Serial.println(F("saving config"));
const size_t capacity = JSON_OBJECT_SIZE(4) + 440; // https://arduinojson.org/v6/assistant/ const size_t capacity = JSON_OBJECT_SIZE(3) + 440; // https://arduinojson.org/v6/assistant/
DynamicJsonDocument jsonBuffer(capacity); DynamicJsonDocument jsonBuffer(capacity);
jsonBuffer["matrixUsername"] = matrixUsername; jsonBuffer["matrixUsername"] = matrixUsername;
jsonBuffer["matrixPassword"] = matrixPassword; jsonBuffer["matrixPassword"] = matrixPassword;
jsonBuffer["matrixRoom"] = matrixRoom; jsonBuffer["notifiedEndpoint"] = notifiedEndpoint;
jsonBuffer["matrixMessage"] = matrixMessage;
File configFile = SPIFFS.open("/config.json", "w"); File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) { if (!configFile) {
@ -145,8 +139,7 @@ void handleAdmin() {
"<form method='post'>" + "<form method='post'>" +
"<div><label for='matrixUsername'>matrixUsername </label><input name='matrixUsername' id='matrixUsername' value='" + matrixUsername + "'></div>" + "<div><label for='matrixUsername'>matrixUsername </label><input name='matrixUsername' id='matrixUsername' value='" + matrixUsername + "'></div>" +
"<div><label for='matrixPassword'>matrixPassword </label><input name='matrixPassword' id='matrixPassword' value='" + matrixPassword + "'></div>" + "<div><label for='matrixPassword'>matrixPassword </label><input name='matrixPassword' id='matrixPassword' value='" + matrixPassword + "'></div>" +
"<div><label for='matrixRoom'>matrixRoom </label><input name='matrixRoom' id='matrixRoom' value='" + matrixRoom + "'></div>" + "<div><label for='notifiedEndpoint'>notifiedEndpoint </label><input name='notifiedEndpoint' id='notifiedEndpoint' value='" + notifiedEndpoint + "'></div>" +
"<div><label for='matrixMessage'>matrixMessage </label><input name='matrixMessage' id='matrixMessage' value='" + matrixMessage + "'></div>" +
"<div><button>Submit</button></div></form>" "<div><button>Submit</button></div></form>"
"</body></html>"; "</body></html>";
httpServer.send(200, "text/html", html); httpServer.send(200, "text/html", html);
@ -158,15 +151,17 @@ void handleNotFound() {
BearSSL::WiFiClientSecure secureClient; BearSSL::WiFiClientSecure secureClient;
HTTPClient http; HTTPClient http;
void notifyFuzIsOpen(bool fuzIsOpen) { void notifyFuzIsOpen(bool fuzIsOpen) {
http.begin(secureClient, "https://presence-button-staging.glitch.me/status?fuzisopen=" + String(fuzIsOpen)); http.begin(secureClient, notifiedEndpoint + String(fuzIsOpen));
http.setAuthorization(matrixUsername.c_str(), matrixPassword.c_str()); http.setAuthorization(matrixUsername.c_str(), matrixPassword.c_str());
int httpCode = http.GET(); int httpCode = http.GET();
Serial.println("notifyFuzIsOpen body: " + http.getString());
Serial.println("notifyFuzIsOpen return code: " + String(httpCode)); Serial.println("notifyFuzIsOpen return code: " + String(httpCode));
http.end(); http.end();
if(httpCode != 200){ // something is wrong, bad network or misconfigured credentials if (httpCode != 200) { // something is wrong, bad network or misconfigured credentials
ticker.attach(0.1, blinkLED); ledTicker.attach(0.1, blinkLED);
} else { } else {
ticker.detach(); ledTicker.detach();
digitalWrite(LED_PIN, LOW); // ensure the LED is lit
} }
} }
@ -182,8 +177,8 @@ void setup() {
//set button pin as input //set button pin as input
pinMode(BUTTON_PIN, INPUT); pinMode(BUTTON_PIN, INPUT);
// start ticker with 1 because we start in AP mode and try to connect // start ledTicker with 1 because we start in AP mode and try to connect
ticker.attach(1.1, blinkLED); ledTicker.attach(1.1, blinkLED);
Serial.println(F("mounting FS...")); Serial.println(F("mounting FS..."));
@ -200,7 +195,7 @@ void setup() {
std::unique_ptr<char[]> buf(new char[size]); std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size); configFile.readBytes(buf.get(), size);
const size_t capacity = JSON_OBJECT_SIZE(4) + 440; // https://arduinojson.org/v6/assistant/ const size_t capacity = JSON_OBJECT_SIZE(3) + 440; // https://arduinojson.org/v6/assistant/
DynamicJsonDocument jsonBuffer(capacity); DynamicJsonDocument jsonBuffer(capacity);
auto error = deserializeJson(jsonBuffer, buf.get()); auto error = deserializeJson(jsonBuffer, buf.get());
if (error) { if (error) {
@ -217,10 +212,8 @@ void setup() {
matrixUsername = m0; matrixUsername = m0;
String m1 = jsonBuffer["matrixPassword"]; String m1 = jsonBuffer["matrixPassword"];
matrixPassword = m1; matrixPassword = m1;
String m2 = jsonBuffer["matrixRoom"]; String m2 = jsonBuffer["notifiedEndpoint"];
matrixRoom = m2; notifiedEndpoint = m2;
String m3 = jsonBuffer["matrixMessage"];
matrixMessage = m3;
} }
configFile.close(); configFile.close();
} }
@ -237,8 +230,7 @@ void setup() {
// id/name placeholder/prompt default length // id/name placeholder/prompt default length
WiFiManagerParameter customMatrixUsername("Matrix username", "Matrix username", matrixUsername.c_str(), 50); WiFiManagerParameter customMatrixUsername("Matrix username", "Matrix username", matrixUsername.c_str(), 50);
WiFiManagerParameter customMatrixPassword("Matrix password", "Matrix password", matrixPassword.c_str(), 50); WiFiManagerParameter customMatrixPassword("Matrix password", "Matrix password", matrixPassword.c_str(), 50);
WiFiManagerParameter customMatrixRoom("Matrix room", "Matrix room", matrixRoom.c_str(), 200); WiFiManagerParameter customNotifiedEndpoint("Notified endpoint", "Notified endpoint", notifiedEndpoint.c_str(), 200);
WiFiManagerParameter customMatrixMessage("Matrix message", "Matrix message", matrixMessage.c_str(), 500);
//WiFiManager //WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around //Local intialization. Once its business is done, there is no need to keep it around
@ -258,8 +250,7 @@ void setup() {
//add all your parameters here //add all your parameters here
wifiManager.addParameter(&customMatrixUsername); wifiManager.addParameter(&customMatrixUsername);
wifiManager.addParameter(&customMatrixPassword); wifiManager.addParameter(&customMatrixPassword);
wifiManager.addParameter(&customMatrixRoom); wifiManager.addParameter(&customNotifiedEndpoint);
wifiManager.addParameter(&customMatrixMessage);
//reset settings - for testing //reset settings - for testing
//wifiManager.resetSettings(); //wifiManager.resetSettings();
@ -283,25 +274,23 @@ void setup() {
//if you get here you have connected to the WiFi //if you get here you have connected to the WiFi
Serial.println(F("connected...yeey :)")); Serial.println(F("connected...yeey :)"));
ticker.detach(); ledTicker.detach();
//keep LED on //keep LED on
digitalWrite(LED_PIN, LOW); digitalWrite(LED_PIN, LOW);
//read updated parameters //read updated parameters
matrixUsername = customMatrixUsername.getValue(); matrixUsername = customMatrixUsername.getValue();
matrixPassword = customMatrixPassword.getValue(); matrixPassword = customMatrixPassword.getValue();
matrixRoom = customMatrixRoom.getValue(); notifiedEndpoint = customNotifiedEndpoint.getValue();
matrixMessage = customMatrixMessage.getValue();
//save the custom parameters to FS //save the custom parameters to FS
if (shouldSaveConfig) { if (shouldSaveConfig) {
Serial.println(F("saving config")); Serial.println(F("saving config"));
const size_t capacity = JSON_OBJECT_SIZE(4) + 440; // https://arduinojson.org/v6/assistant/ const size_t capacity = JSON_OBJECT_SIZE(3) + 440; // https://arduinojson.org/v6/assistant/
DynamicJsonDocument jsonBuffer(capacity); DynamicJsonDocument jsonBuffer(capacity);
jsonBuffer["matrixUsername"] = matrixUsername; jsonBuffer["matrixUsername"] = matrixUsername;
jsonBuffer["matrixPassword"] = matrixPassword; jsonBuffer["matrixPassword"] = matrixPassword;
jsonBuffer["matrixRoom"] = matrixRoom; jsonBuffer["notifiedEndpoint"] = notifiedEndpoint;
jsonBuffer["matrixMessage"] = matrixMessage;
File configFile = SPIFFS.open("/config.json", "w"); File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) { if (!configFile) {
@ -337,7 +326,7 @@ void setup() {
bool buttonState = HIGH; bool buttonState = HIGH;
bool previousButtonState = HIGH; bool previousButtonState = HIGH;
long previousMillis = 0; long previousMillis = 0;
const long getMatrixMessagesInterval = 5000; const long notifyInterval = 5000;
unsigned long pressedTime = millis(); unsigned long pressedTime = millis();
void loop() { void loop() {
MDNS.update(); MDNS.update();
@ -356,14 +345,13 @@ void loop() {
delay(500); delay(500);
ESP.reset(); ESP.reset();
} }
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
if (currentMillis - previousMillis > getMatrixMessagesInterval) { if (currentMillis - previousMillis > notifyInterval) {
previousMillis = currentMillis; previousMillis = currentMillis;
notifyFuzIsOpen(fuzIsOpen); notifyFuzIsOpen(fuzIsOpen);
} }
if(!fuzIsOpen){ if (!fuzIsOpen) {
digitalWrite(RELAY_PIN, HIGH); digitalWrite(RELAY_PIN, HIGH);
} }
@ -374,7 +362,7 @@ void loop() {
Serial.println("Button pressed"); Serial.println("Button pressed");
digitalWrite(RELAY_PIN, LOW); digitalWrite(RELAY_PIN, LOW);
fuzIsOpen = true; fuzIsOpen = true;
notifyFuzIsOpen(fuzIsOpen);
} }
digitalWrite(LED_PIN, LOW); // ensure the LED is lit
previousButtonState = buttonState; previousButtonState = buttonState;
} }