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();