From 4feb52b748210ba99cf9e4c5b2ce2a4f42a1abad Mon Sep 17 00:00:00 2001 From: Chrome Legion Date: Sun, 27 May 2018 22:11:48 -0700 Subject: [PATCH 1/5] Qrome - added the ability to flip the rotation of the display --- printermonitor/Settings.h | 1 + printermonitor/printermonitor.ino | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/printermonitor/Settings.h b/printermonitor/Settings.h index 0c973b2..78c88b5 100644 --- a/printermonitor/Settings.h +++ b/printermonitor/Settings.h @@ -69,6 +69,7 @@ boolean DISPLAYCLOCK = true; // true = Show Clock when not printing / false = const int I2C_DISPLAY_ADDRESS = 0x3c; // I2C Address of your Display (usually 0x3c or 0x3d) const int SDA_PIN = D2; const int SCL_PIN = D5; +const boolean INVERT_DISPLAY = false; // true = pins at top | false = pins at the bottom //#define DISPLAY_SH1106 // Uncomment this line to use the SH1106 display -- SSD1306 is used by default and is most common boolean ENABLE_OTA = true; // this will allow you to load firmware to the device over WiFi (see OTA for ESP8266) diff --git a/printermonitor/printermonitor.ino b/printermonitor/printermonitor.ino index cdf0628..9af964a 100644 --- a/printermonitor/printermonitor.ino +++ b/printermonitor/printermonitor.ino @@ -27,7 +27,7 @@ SOFTWARE. #include "Settings.h" -#define VERSION "1.4" +#define VERSION "1.5" #define HOSTNAME "OctMon-" #define CONFIG "/conf.txt" @@ -154,6 +154,9 @@ void setup() { // initialize dispaly display.init(); + if (INVERT_DISPLAY) { + display.flipScreenVertically(); // connections at top of OLED display + } display.clear(); display.display(); @@ -195,6 +198,9 @@ void setup() { // Inital UI takes care of initalising the display too. ui.init(); + if (INVERT_DISPLAY) { + display.flipScreenVertically(); //connections at top of OLED display + } // print the received signal strength: Serial.print("Signal Strength (RSSI): "); From efc8ee24c5332df69576e789a36c7f67f0874ca3 Mon Sep 17 00:00:00 2001 From: Chrome Legion Date: Tue, 29 May 2018 07:14:36 -0700 Subject: [PATCH 2/5] Qrome - updated PrintTimeLeft to report 0 when 100% done --- printermonitor/OctoPrintClient.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/printermonitor/OctoPrintClient.cpp b/printermonitor/OctoPrintClient.cpp index 2db4341..49577cf 100644 --- a/printermonitor/OctoPrintClient.cpp +++ b/printermonitor/OctoPrintClient.cpp @@ -210,7 +210,11 @@ String OctoPrintClient::getProgressPrintTime() { } String OctoPrintClient::getProgressPrintTimeLeft() { - return printerData.progressPrintTimeLeft; + String rtnValue = printerData.progressPrintTimeLeft; + if (getProgressCompletion() == "100") { + rtnValue = "0"; // Print is done so this should be 0 this is a fix for OctoPrint + } + return rtnValue; } String OctoPrintClient::getState() { From f02c3de3b7635a026b20e9f470f2f3ffade961fe Mon Sep 17 00:00:00 2001 From: Chrome Legion Date: Wed, 30 May 2018 07:41:24 -0700 Subject: [PATCH 3/5] Qrome - added reset to printer Data on failed connection --- printermonitor/OctoPrintClient.cpp | 25 +++++++++++++++++++++++-- printermonitor/OctoPrintClient.h | 1 + 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/printermonitor/OctoPrintClient.cpp b/printermonitor/OctoPrintClient.cpp index 49577cf..205ff2c 100644 --- a/printermonitor/OctoPrintClient.cpp +++ b/printermonitor/OctoPrintClient.cpp @@ -64,16 +64,16 @@ WiFiClient OctoPrintClient::getSubmitRequest(String apiGetData) { if (printClient.println() == 0) { Serial.println("Connection to " + String(myServer) + ":" + String(myPort) + " failed."); Serial.println(); + resetPrintData(); printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed."; - printerData.state = ""; return printClient; } } else { Serial.println("Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect Serial.println(); + resetPrintData(); printerData.error = "Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort); - printerData.state = ""; return printClient; } @@ -177,6 +177,27 @@ void OctoPrintClient::getPrinterJobResults() { printClient.stop(); //stop client } +// Reset all PrinterData +void OctoPrintClient::resetPrintData() { + printerData.averagePrintTime = ""; + printerData.estimatedPrintTime = ""; + printerData.fileName = ""; + printerData.fileSize = ""; + printerData.lastPrintTime = ""; + printerData.progressCompletion = ""; + printerData.progressFilepos = ""; + printerData.progressPrintTime = ""; + printerData.progressPrintTimeLeft = ""; + printerData.state = ""; + printerData.toolTemp = ""; + printerData.toolTargetTemp = ""; + printerData.filamentLength = ""; + printerData.bedTemp = ""; + printerData.bedTargetTemp = ""; + printerData.isPrinting = false; + printerData.error = ""; +} + String OctoPrintClient::getAveragePrintTime(){ return printerData.averagePrintTime; } diff --git a/printermonitor/OctoPrintClient.h b/printermonitor/OctoPrintClient.h index 8c694e5..b147715 100644 --- a/printermonitor/OctoPrintClient.h +++ b/printermonitor/OctoPrintClient.h @@ -32,6 +32,7 @@ private: int myPort = 80; String myApiKey = ""; + void resetPrintData(); boolean validate(); WiFiClient getSubmitRequest(String apiGetData); From 753b4b1660cb17bbd25b79acfeeb98866489e9ba Mon Sep 17 00:00:00 2001 From: Chrome Legion Date: Wed, 30 May 2018 21:50:51 -0700 Subject: [PATCH 4/5] Qrome - updated with mDNS to lookup address from hostname --- printermonitor/Settings.h | 1 + printermonitor/printermonitor.ino | 38 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/printermonitor/Settings.h b/printermonitor/Settings.h index 78c88b5..3eb8bc3 100644 --- a/printermonitor/Settings.h +++ b/printermonitor/Settings.h @@ -53,6 +53,7 @@ SOFTWARE. // OctoPrint Monitoring -- Monitor your 3D printer OctoPrint Server String OctoPrintApiKey = ""; // ApiKey from your User Account on OctoPrint +String OctoPrintHostName = "octopi";// Default 'octopi' -- or hostname if different (optional if your IP changes) String OctoPrintServer = ""; // IP or Address of your OctoPrint Server (DO NOT include http://) int OctoPrintPort = 80; // the port you are running your OctoPrint server on (usually 80); diff --git a/printermonitor/printermonitor.ino b/printermonitor/printermonitor.ino index 9af964a..f415bbb 100644 --- a/printermonitor/printermonitor.ino +++ b/printermonitor/printermonitor.ino @@ -100,6 +100,7 @@ const String WEB_ACTIONS = "OctoPrint API Key (get from your server)" + "" "" "" " Display Clock when printer is off

" @@ -262,6 +263,34 @@ void setup() { } flashLED(5, 500); + findMDNS(); //go find Octoprint Server by the hostname +} + +void findMDNS() { + if (OctoPrintHostName == "") { + return; // nothing to do here + } + // We now query our network for 'web servers' service + // over tcp, and get the number of available devices + int n = MDNS.queryService("http", "tcp"); + if (n == 0) { + Serial.println("no services found - make sure OctoPrint server is turned on"); + return; + } + Serial.println("*** Looking for " + OctoPrintHostName + " over mDNS"); + for (int i = 0; i < n; ++i) { + // Going through every available service, + // we're searching for the one whose hostname + // matches what we want, and then get its IP + Serial.println("Found: " + MDNS.hostname(i)); + if (MDNS.hostname(i) == OctoPrintHostName) { + IPAddress serverIp = MDNS.IP(i); + OctoPrintServer = serverIp.toString(); + OctoPrintPort = MDNS.port(i); // save the port + Serial.println("*** Found OctoPrint Server " + OctoPrintHostName + " http://" + OctoPrintServer + ":" + OctoPrintPort); + writeSettings(); // update the settings + } + } } //************************************************************ @@ -331,6 +360,7 @@ void handleUpdateConfig() { return server.requestAuthentication(); } OctoPrintApiKey = server.arg("octoPrintApiKey"); + OctoPrintHostName = server.arg("octoPrintHostName"); OctoPrintServer = server.arg("octoPrintAddress"); OctoPrintPort = server.arg("octoPrintPort").toInt(); DISPLAYCLOCK = server.hasArg("isClockEnabled"); @@ -343,6 +373,7 @@ void handleUpdateConfig() { temp = server.arg("stationpassword"); temp.toCharArray(www_password, sizeof(temp)); writeSettings(); + findMDNS(); printerClient.getPrinterJobResults(); checkDisplay(); lastEpoch = 0; @@ -380,6 +411,7 @@ void handleConfigure() { String form = String(CHANGE_FORM); form.replace("%OCTOKEY%", OctoPrintApiKey); + form.replace("%OCTOHOST%", OctoPrintHostName); form.replace("%OCTOADDRESS%", OctoPrintServer); form.replace("%OCTOPORT%", String(OctoPrintPort)); String isClockChecked = ""; @@ -736,6 +768,7 @@ void writeSettings() { Serial.println("Saving settings now..."); f.println("UtcOffset=" + String(UtcOffset)); f.println("octoKey=" + OctoPrintApiKey); + f.println("octoHost=" + OctoPrintHostName); f.println("octoServer=" + OctoPrintServer); f.println("octoPort=" + String(OctoPrintPort)); f.println("refreshRate=" + String(minutesBetweenDataRefresh)); @@ -770,6 +803,11 @@ void readSettings() { OctoPrintApiKey.trim(); Serial.println("OctoPrintApiKey=" + OctoPrintApiKey); } + if (line.indexOf("octoHost=") >= 0) { + OctoPrintHostName = line.substring(line.lastIndexOf("octoHost=") + 9); + OctoPrintHostName.trim(); + Serial.println("OctoPrintHostName=" + OctoPrintHostName); + } if (line.indexOf("octoServer=") >= 0) { OctoPrintServer = line.substring(line.lastIndexOf("octoServer=") + 11); OctoPrintServer.trim(); From f9b568403ab001c39e662c49a312aae2283a3bff Mon Sep 17 00:00:00 2001 From: Chrome Legion Date: Thu, 31 May 2018 20:14:22 -0700 Subject: [PATCH 5/5] Qrome - updated with Basic Auth for OctoPrint --- printermonitor/OctoPrintClient.cpp | 16 +++++++++++++--- printermonitor/OctoPrintClient.h | 6 ++++-- printermonitor/Settings.h | 2 ++ printermonitor/printermonitor.ino | 24 +++++++++++++++++++++--- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/printermonitor/OctoPrintClient.cpp b/printermonitor/OctoPrintClient.cpp index 205ff2c..204f9b9 100644 --- a/printermonitor/OctoPrintClient.cpp +++ b/printermonitor/OctoPrintClient.cpp @@ -23,14 +23,20 @@ SOFTWARE. #include "OctoPrintClient.h" -OctoPrintClient::OctoPrintClient(String ApiKey, String server, int port) { - updateOctoPrintClient(ApiKey, server, port); +OctoPrintClient::OctoPrintClient(String ApiKey, String server, int port, String user, String pass) { + updateOctoPrintClient(ApiKey, server, port, user, pass); } -void OctoPrintClient::updateOctoPrintClient(String ApiKey, String server, int port) { +void OctoPrintClient::updateOctoPrintClient(String ApiKey, String server, int port, String user, String pass) { server.toCharArray(myServer, 100); myApiKey = ApiKey; myPort = port; + encodedAuth = ""; + if (user != "") { + String userpass = user + ":" + pass; + base64 b64; + encodedAuth = b64.encode(userpass, true); + } } boolean OctoPrintClient::validate() { @@ -59,6 +65,10 @@ WiFiClient OctoPrintClient::getSubmitRequest(String apiGetData) { printClient.println(apiGetData); printClient.println("Host: " + String(myServer) + ":" + String(myPort)); printClient.println("X-Api-Key: " + myApiKey); + if (encodedAuth != "") { + printClient.print("Authorization: "); + printClient.println("Basic " + encodedAuth); + } printClient.println("User-Agent: ArduinoWiFi/1.1"); printClient.println("Connection: close"); if (printClient.println() == 0) { diff --git a/printermonitor/OctoPrintClient.h b/printermonitor/OctoPrintClient.h index b147715..0871868 100644 --- a/printermonitor/OctoPrintClient.h +++ b/printermonitor/OctoPrintClient.h @@ -24,6 +24,7 @@ SOFTWARE. #pragma once #include #include +#include class OctoPrintClient { @@ -31,6 +32,7 @@ private: char myServer[100]; int myPort = 80; String myApiKey = ""; + String encodedAuth = ""; void resetPrintData(); boolean validate(); @@ -62,9 +64,9 @@ private: public: - OctoPrintClient(String ApiKey, String server, int port); + OctoPrintClient(String ApiKey, String server, int port, String user, String pass); void getPrinterJobResults(); - void updateOctoPrintClient(String ApiKey, String server, int port); + void updateOctoPrintClient(String ApiKey, String server, int port, String user, String pass); String getAveragePrintTime(); String getEstimatedPrintTime(); diff --git a/printermonitor/Settings.h b/printermonitor/Settings.h index 3eb8bc3..bd4fdb8 100644 --- a/printermonitor/Settings.h +++ b/printermonitor/Settings.h @@ -56,6 +56,8 @@ String OctoPrintApiKey = ""; // ApiKey from your User Account on OctoPrint String OctoPrintHostName = "octopi";// Default 'octopi' -- or hostname if different (optional if your IP changes) String OctoPrintServer = ""; // IP or Address of your OctoPrint Server (DO NOT include http://) int OctoPrintPort = 80; // the port you are running your OctoPrint server on (usually 80); +String OctoAuthUser = ""; // only used if you have haproxy or basic athentintication turned on (not default) +String OctoAuthPass = ""; // only used with haproxy or basic auth (only needed if you must authenticate) const int WEBSERVER_PORT = 80; // The port you can access this device on over HTTP const boolean WEBSERVER_ENABLED = true; // Device will provide a web interface via http://[ip]:[port]/ diff --git a/printermonitor/printermonitor.ino b/printermonitor/printermonitor.ino index f415bbb..2a79341 100644 --- a/printermonitor/printermonitor.ino +++ b/printermonitor/printermonitor.ino @@ -83,7 +83,7 @@ String lastReportStatus = ""; boolean displayOn = true; // OctoPrint Client -OctoPrintClient printerClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort); +OctoPrintClient printerClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort, OctoAuthUser, OctoAuthPass); int printerCount = 0; //declairing prototypes @@ -103,11 +103,13 @@ const String CHANGE_FORM = "

OctoPrint Host Name (usually octopi)" "" "" + "" + "
" " Display Clock when printer is off

" " Use 24 Hour Clock (military time)

" "Time Refresh (minutes)

" "Theme Color

" - "" + "
" "" "" "
"; @@ -363,6 +365,8 @@ void handleUpdateConfig() { OctoPrintHostName = server.arg("octoPrintHostName"); OctoPrintServer = server.arg("octoPrintAddress"); OctoPrintPort = server.arg("octoPrintPort").toInt(); + OctoAuthUser = server.arg("octoUser"); + OctoAuthPass = server.arg("octoPass"); DISPLAYCLOCK = server.hasArg("isClockEnabled"); IS_24HOUR = server.hasArg("is24hour"); minutesBetweenDataRefresh = server.arg("refresh").toInt(); @@ -414,6 +418,8 @@ void handleConfigure() { form.replace("%OCTOHOST%", OctoPrintHostName); form.replace("%OCTOADDRESS%", OctoPrintServer); form.replace("%OCTOPORT%", String(OctoPrintPort)); + form.replace("%OCTOUSER%", OctoAuthUser); + form.replace("%OCTOPASS%", OctoAuthPass); String isClockChecked = ""; if (DISPLAYCLOCK) { isClockChecked = "checked='checked'"; @@ -771,6 +777,8 @@ void writeSettings() { f.println("octoHost=" + OctoPrintHostName); f.println("octoServer=" + OctoPrintServer); f.println("octoPort=" + String(OctoPrintPort)); + f.println("octoUser=" + OctoAuthUser); + f.println("octoPass=" + OctoAuthPass); f.println("refreshRate=" + String(minutesBetweenDataRefresh)); f.println("themeColor=" + themeColor); f.println("www_username=" + String(www_username)); @@ -817,6 +825,16 @@ void readSettings() { OctoPrintPort = line.substring(line.lastIndexOf("octoPort=") + 9).toInt(); Serial.println("OctoPrintPort=" + String(OctoPrintPort)); } + if (line.indexOf("octoUser=") >= 0) { + OctoAuthUser = line.substring(line.lastIndexOf("octoUser=") + 9); + OctoAuthUser.trim(); + Serial.println("OctoAuthUser=" + OctoAuthUser); + } + if (line.indexOf("octoPass=") >= 0) { + OctoAuthPass = line.substring(line.lastIndexOf("octoPass=") + 9); + OctoAuthPass.trim(); + Serial.println("OctoAuthPass=" + OctoAuthPass); + } if (line.indexOf("refreshRate=") >= 0) { minutesBetweenDataRefresh = line.substring(line.lastIndexOf("refreshRate=") + 12).toInt(); Serial.println("minutesBetweenDataRefresh=" + String(minutesBetweenDataRefresh)); @@ -848,7 +866,7 @@ void readSettings() { } } fr.close(); - printerClient.updateOctoPrintClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort); + printerClient.updateOctoPrintClient(OctoPrintApiKey, OctoPrintServer, OctoPrintPort, OctoAuthUser, OctoAuthPass); timeClient.setUtcOffset(UtcOffset); }