Qrome - updated to include the compile options for Repetier

pull/99/head
David Payne 2019-04-11 16:23:58 -07:00
parent 30da7285a3
commit c7453a2883
8 changed files with 2332 additions and 2324 deletions

View File

@ -1,396 +1,396 @@
/** The MIT License (MIT) /** The MIT License (MIT)
Copyright (c) 2018 David Payne Copyright (c) 2018 David Payne
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// Additional Contributions: // Additional Contributions:
/* 15 Jan 2019 : Owen Carter : Add psucontrol query via POST api call */ /* 15 Jan 2019 : Owen Carter : Add psucontrol query via POST api call */
#include "OctoPrintClient.h" #include "OctoPrintClient.h"
OctoPrintClient::OctoPrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu) { OctoPrintClient::OctoPrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu) {
updateOctoPrintClient(ApiKey, server, port, user, pass, psu); updatePrintClient(ApiKey, server, port, user, pass, psu);
} }
void OctoPrintClient::updateOctoPrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu) { void OctoPrintClient::updatePrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu) {
server.toCharArray(myServer, 100); server.toCharArray(myServer, 100);
myApiKey = ApiKey; myApiKey = ApiKey;
myPort = port; myPort = port;
encodedAuth = ""; encodedAuth = "";
if (user != "") { if (user != "") {
String userpass = user + ":" + pass; String userpass = user + ":" + pass;
base64 b64; base64 b64;
encodedAuth = b64.encode(userpass, true); encodedAuth = b64.encode(userpass, true);
} }
pollPsu = psu; pollPsu = psu;
} }
boolean OctoPrintClient::validate() { boolean OctoPrintClient::validate() {
boolean rtnValue = false; boolean rtnValue = false;
printerData.error = ""; printerData.error = "";
if (String(myServer) == "") { if (String(myServer) == "") {
printerData.error += "Server address is required; "; printerData.error += "Server address is required; ";
} }
if (myApiKey == "") { if (myApiKey == "") {
printerData.error += "ApiKey is required; "; printerData.error += "ApiKey is required; ";
} }
if (printerData.error == "") { if (printerData.error == "") {
rtnValue = true; rtnValue = true;
} }
return rtnValue; return rtnValue;
} }
WiFiClient OctoPrintClient::getSubmitRequest(String apiGetData) { WiFiClient OctoPrintClient::getSubmitRequest(String apiGetData) {
WiFiClient printClient; WiFiClient printClient;
printClient.setTimeout(5000); printClient.setTimeout(5000);
Serial.println("Getting Octoprint Data via GET"); Serial.println("Getting Octoprint Data via GET");
Serial.println(apiGetData); Serial.println(apiGetData);
result = ""; result = "";
if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection
printClient.println(apiGetData); printClient.println(apiGetData);
printClient.println("Host: " + String(myServer) + ":" + String(myPort)); printClient.println("Host: " + String(myServer) + ":" + String(myPort));
printClient.println("X-Api-Key: " + myApiKey); printClient.println("X-Api-Key: " + myApiKey);
if (encodedAuth != "") { if (encodedAuth != "") {
printClient.print("Authorization: "); printClient.print("Authorization: ");
printClient.println("Basic " + encodedAuth); printClient.println("Basic " + encodedAuth);
} }
printClient.println("User-Agent: ArduinoWiFi/1.1"); printClient.println("User-Agent: ArduinoWiFi/1.1");
printClient.println("Connection: close"); printClient.println("Connection: close");
if (printClient.println() == 0) { if (printClient.println() == 0) {
Serial.println("Connection to " + String(myServer) + ":" + String(myPort) + " failed."); Serial.println("Connection to " + String(myServer) + ":" + String(myPort) + " failed.");
Serial.println(); Serial.println();
resetPrintData(); resetPrintData();
printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed."; printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed.";
return printClient; return printClient;
} }
} }
else { else {
Serial.println("Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect Serial.println("Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect
Serial.println(); Serial.println();
resetPrintData(); resetPrintData();
printerData.error = "Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort); printerData.error = "Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort);
return printClient; return printClient;
} }
// Check HTTP status // Check HTTP status
char status[32] = {0}; char status[32] = {0};
printClient.readBytesUntil('\r', status, sizeof(status)); printClient.readBytesUntil('\r', status, sizeof(status));
if (strcmp(status, "HTTP/1.1 200 OK") != 0 && strcmp(status, "HTTP/1.1 409 CONFLICT") != 0) { if (strcmp(status, "HTTP/1.1 200 OK") != 0 && strcmp(status, "HTTP/1.1 409 CONFLICT") != 0) {
Serial.print(F("Unexpected response: ")); Serial.print(F("Unexpected response: "));
Serial.println(status); Serial.println(status);
printerData.state = ""; printerData.state = "";
printerData.error = "Response: " + String(status); printerData.error = "Response: " + String(status);
return printClient; return printClient;
} }
// Skip HTTP headers // Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n"; char endOfHeaders[] = "\r\n\r\n";
if (!printClient.find(endOfHeaders)) { if (!printClient.find(endOfHeaders)) {
Serial.println(F("Invalid response")); Serial.println(F("Invalid response"));
printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort); printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
} }
return printClient; return printClient;
} }
WiFiClient OctoPrintClient::getPostRequest(String apiPostData, String apiPostBody) { WiFiClient OctoPrintClient::getPostRequest(String apiPostData, String apiPostBody) {
WiFiClient printClient; WiFiClient printClient;
printClient.setTimeout(5000); printClient.setTimeout(5000);
Serial.println("Getting Octoprint Data via POST"); Serial.println("Getting Octoprint Data via POST");
Serial.println(apiPostData + " | " + apiPostBody); Serial.println(apiPostData + " | " + apiPostBody);
result = ""; result = "";
if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection
printClient.println(apiPostData); printClient.println(apiPostData);
printClient.println("Host: " + String(myServer) + ":" + String(myPort)); printClient.println("Host: " + String(myServer) + ":" + String(myPort));
printClient.println("Connection: close"); printClient.println("Connection: close");
printClient.println("X-Api-Key: " + myApiKey); printClient.println("X-Api-Key: " + myApiKey);
if (encodedAuth != "") { if (encodedAuth != "") {
printClient.print("Authorization: "); printClient.print("Authorization: ");
printClient.println("Basic " + encodedAuth); printClient.println("Basic " + encodedAuth);
} }
printClient.println("User-Agent: ArduinoWiFi/1.1"); printClient.println("User-Agent: ArduinoWiFi/1.1");
printClient.println("Content-Type: application/json"); printClient.println("Content-Type: application/json");
printClient.print("Content-Length: "); printClient.print("Content-Length: ");
printClient.println(apiPostBody.length()); printClient.println(apiPostBody.length());
printClient.println(); printClient.println();
printClient.println(apiPostBody); printClient.println(apiPostBody);
if (printClient.println() == 0) { if (printClient.println() == 0) {
Serial.println("Connection to " + String(myServer) + ":" + String(myPort) + " failed."); Serial.println("Connection to " + String(myServer) + ":" + String(myPort) + " failed.");
Serial.println(); Serial.println();
resetPrintData(); resetPrintData();
printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed."; printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed.";
return printClient; return printClient;
} }
} }
else { else {
Serial.println("Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect Serial.println("Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect
Serial.println(); Serial.println();
resetPrintData(); resetPrintData();
printerData.error = "Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort); printerData.error = "Connection to OctoPrint failed: " + String(myServer) + ":" + String(myPort);
return printClient; return printClient;
} }
// Check HTTP status // Check HTTP status
char status[32] = {0}; char status[32] = {0};
printClient.readBytesUntil('\r', status, sizeof(status)); printClient.readBytesUntil('\r', status, sizeof(status));
if (strcmp(status, "HTTP/1.1 200 OK") != 0 && strcmp(status, "HTTP/1.1 409 CONFLICT") != 0) { if (strcmp(status, "HTTP/1.1 200 OK") != 0 && strcmp(status, "HTTP/1.1 409 CONFLICT") != 0) {
Serial.print(F("Unexpected response: ")); Serial.print(F("Unexpected response: "));
Serial.println(status); Serial.println(status);
printerData.state = ""; printerData.state = "";
printerData.error = "Response: " + String(status); printerData.error = "Response: " + String(status);
return printClient; return printClient;
} }
// Skip HTTP headers // Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n"; char endOfHeaders[] = "\r\n\r\n";
if (!printClient.find(endOfHeaders)) { if (!printClient.find(endOfHeaders)) {
Serial.println(F("Invalid response")); Serial.println(F("Invalid response"));
printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort); printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
} }
return printClient; return printClient;
} }
void OctoPrintClient::getPrinterJobResults() { void OctoPrintClient::getPrinterJobResults() {
if (!validate()) { if (!validate()) {
return; return;
} }
//**** get the Printer Job status //**** get the Printer Job status
String apiGetData = "GET /api/job HTTP/1.1"; String apiGetData = "GET /api/job HTTP/1.1";
WiFiClient printClient = getSubmitRequest(apiGetData); WiFiClient printClient = getSubmitRequest(apiGetData);
if (printerData.error != "") { if (printerData.error != "") {
return; return;
} }
const size_t bufferSize = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 2*JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 710; const size_t bufferSize = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 2*JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 710;
DynamicJsonBuffer jsonBuffer(bufferSize); DynamicJsonBuffer jsonBuffer(bufferSize);
// Parse JSON object // Parse JSON object
JsonObject& root = jsonBuffer.parseObject(printClient); JsonObject& root = jsonBuffer.parseObject(printClient);
if (!root.success()) { if (!root.success()) {
Serial.println("OctoPrint Data Parsing failed: " + String(myServer) + ":" + String(myPort)); Serial.println("OctoPrint Data Parsing failed: " + String(myServer) + ":" + String(myPort));
printerData.error = "OctoPrint Data Parsing failed: " + String(myServer) + ":" + String(myPort); printerData.error = "OctoPrint Data Parsing failed: " + String(myServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
return; return;
} }
printerData.averagePrintTime = (const char*)root["job"]["averagePrintTime"]; printerData.averagePrintTime = (const char*)root["job"]["averagePrintTime"];
printerData.estimatedPrintTime = (const char*)root["job"]["estimatedPrintTime"]; printerData.estimatedPrintTime = (const char*)root["job"]["estimatedPrintTime"];
printerData.fileName = (const char*)root["job"]["file"]["name"]; printerData.fileName = (const char*)root["job"]["file"]["name"];
printerData.fileSize = (const char*)root["job"]["file"]["size"]; printerData.fileSize = (const char*)root["job"]["file"]["size"];
printerData.lastPrintTime = (const char*)root["job"]["lastPrintTime"]; printerData.lastPrintTime = (const char*)root["job"]["lastPrintTime"];
printerData.progressCompletion = (const char*)root["progress"]["completion"]; printerData.progressCompletion = (const char*)root["progress"]["completion"];
printerData.progressFilepos = (const char*)root["progress"]["filepos"]; printerData.progressFilepos = (const char*)root["progress"]["filepos"];
printerData.progressPrintTime = (const char*)root["progress"]["printTime"]; printerData.progressPrintTime = (const char*)root["progress"]["printTime"];
printerData.progressPrintTimeLeft = (const char*)root["progress"]["printTimeLeft"]; printerData.progressPrintTimeLeft = (const char*)root["progress"]["printTimeLeft"];
printerData.filamentLength = (const char*)root["job"]["filament"]["tool0"]["length"]; printerData.filamentLength = (const char*)root["job"]["filament"]["tool0"]["length"];
printerData.state = (const char*)root["state"]; printerData.state = (const char*)root["state"];
if (isOperational()) { if (isOperational()) {
Serial.println("Status: " + printerData.state); Serial.println("Status: " + printerData.state);
} else { } else {
Serial.println("Printer Not Operational"); Serial.println("Printer Not Operational");
} }
//**** get the Printer Temps and Stat //**** get the Printer Temps and Stat
apiGetData = "GET /api/printer?exclude=sd,history HTTP/1.1"; apiGetData = "GET /api/printer?exclude=sd,history HTTP/1.1";
printClient = getSubmitRequest(apiGetData); printClient = getSubmitRequest(apiGetData);
if (printerData.error != "") { if (printerData.error != "") {
return; return;
} }
const size_t bufferSize2 = 3*JSON_OBJECT_SIZE(2) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(9) + 300; const size_t bufferSize2 = 3*JSON_OBJECT_SIZE(2) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(9) + 300;
DynamicJsonBuffer jsonBuffer2(bufferSize2); DynamicJsonBuffer jsonBuffer2(bufferSize2);
// Parse JSON object // Parse JSON object
JsonObject& root2 = jsonBuffer2.parseObject(printClient); JsonObject& root2 = jsonBuffer2.parseObject(printClient);
if (!root2.success()) { if (!root2.success()) {
printerData.isPrinting = false; printerData.isPrinting = false;
printerData.toolTemp = ""; printerData.toolTemp = "";
printerData.toolTargetTemp = ""; printerData.toolTargetTemp = "";
printerData.bedTemp = ""; printerData.bedTemp = "";
printerData.bedTargetTemp = (const char*)root2["temperature"]["bed"]["target"]; printerData.bedTargetTemp = (const char*)root2["temperature"]["bed"]["target"];
return; return;
} }
String printing = (const char*)root2["state"]["flags"]["printing"]; String printing = (const char*)root2["state"]["flags"]["printing"];
if (printing == "true") { if (printing == "true") {
printerData.isPrinting = true; printerData.isPrinting = true;
} }
printerData.toolTemp = (const char*)root2["temperature"]["tool0"]["actual"]; printerData.toolTemp = (const char*)root2["temperature"]["tool0"]["actual"];
printerData.toolTargetTemp = (const char*)root2["temperature"]["tool0"]["target"]; printerData.toolTargetTemp = (const char*)root2["temperature"]["tool0"]["target"];
printerData.bedTemp = (const char*)root2["temperature"]["bed"]["actual"]; printerData.bedTemp = (const char*)root2["temperature"]["bed"]["actual"];
printerData.bedTargetTemp = (const char*)root2["temperature"]["bed"]["target"]; printerData.bedTargetTemp = (const char*)root2["temperature"]["bed"]["target"];
if (isPrinting()) { if (isPrinting()) {
Serial.println("Status: " + printerData.state + " " + printerData.fileName + "(" + printerData.progressCompletion + "%)"); Serial.println("Status: " + printerData.state + " " + printerData.fileName + "(" + printerData.progressCompletion + "%)");
} }
} }
void OctoPrintClient::getPrinterPsuState() { void OctoPrintClient::getPrinterPsuState() {
//**** get the PSU state (if enabled and printer operational) //**** get the PSU state (if enabled and printer operational)
if (pollPsu && isOperational()) { if (pollPsu && isOperational()) {
if (!validate()) { if (!validate()) {
printerData.isPSUoff = false; // we do not know PSU state, so assume on. printerData.isPSUoff = false; // we do not know PSU state, so assume on.
return; return;
} }
String apiPostData = "POST /api/plugin/psucontrol HTTP/1.1"; String apiPostData = "POST /api/plugin/psucontrol HTTP/1.1";
String apiPostBody = "{\"command\":\"getPSUState\"}"; String apiPostBody = "{\"command\":\"getPSUState\"}";
WiFiClient printClient = getPostRequest(apiPostData,apiPostBody); WiFiClient printClient = getPostRequest(apiPostData,apiPostBody);
if (printerData.error != "") { if (printerData.error != "") {
printerData.isPSUoff = false; // we do not know PSU state, so assume on. printerData.isPSUoff = false; // we do not know PSU state, so assume on.
return; return;
} }
const size_t bufferSize3 = JSON_OBJECT_SIZE(2) + 300; const size_t bufferSize3 = JSON_OBJECT_SIZE(2) + 300;
DynamicJsonBuffer jsonBuffer3(bufferSize3); DynamicJsonBuffer jsonBuffer3(bufferSize3);
// Parse JSON object // Parse JSON object
JsonObject& root3 = jsonBuffer3.parseObject(printClient); JsonObject& root3 = jsonBuffer3.parseObject(printClient);
if (!root3.success()) { if (!root3.success()) {
printerData.isPSUoff = false; // we do not know PSU state, so assume on printerData.isPSUoff = false; // we do not know PSU state, so assume on
return; return;
} }
String psu = (const char*)root3["isPSUOn"]; String psu = (const char*)root3["isPSUOn"];
if (psu == "true") { if (psu == "true") {
printerData.isPSUoff = false; // PSU checked and is on printerData.isPSUoff = false; // PSU checked and is on
} else { } else {
printerData.isPSUoff = true; // PSU checked and is off, set flag printerData.isPSUoff = true; // PSU checked and is off, set flag
} }
printClient.stop(); //stop client printClient.stop(); //stop client
} else { } else {
printerData.isPSUoff = false; // we are not checking PSU state, so assume on printerData.isPSUoff = false; // we are not checking PSU state, so assume on
} }
} }
// Reset all PrinterData // Reset all PrinterData
void OctoPrintClient::resetPrintData() { void OctoPrintClient::resetPrintData() {
printerData.averagePrintTime = ""; printerData.averagePrintTime = "";
printerData.estimatedPrintTime = ""; printerData.estimatedPrintTime = "";
printerData.fileName = ""; printerData.fileName = "";
printerData.fileSize = ""; printerData.fileSize = "";
printerData.lastPrintTime = ""; printerData.lastPrintTime = "";
printerData.progressCompletion = ""; printerData.progressCompletion = "";
printerData.progressFilepos = ""; printerData.progressFilepos = "";
printerData.progressPrintTime = ""; printerData.progressPrintTime = "";
printerData.progressPrintTimeLeft = ""; printerData.progressPrintTimeLeft = "";
printerData.state = ""; printerData.state = "";
printerData.toolTemp = ""; printerData.toolTemp = "";
printerData.toolTargetTemp = ""; printerData.toolTargetTemp = "";
printerData.filamentLength = ""; printerData.filamentLength = "";
printerData.bedTemp = ""; printerData.bedTemp = "";
printerData.bedTargetTemp = ""; printerData.bedTargetTemp = "";
printerData.isPrinting = false; printerData.isPrinting = false;
printerData.isPSUoff = false; printerData.isPSUoff = false;
printerData.error = ""; printerData.error = "";
} }
String OctoPrintClient::getAveragePrintTime(){ String OctoPrintClient::getAveragePrintTime(){
return printerData.averagePrintTime; return printerData.averagePrintTime;
} }
String OctoPrintClient::getEstimatedPrintTime() { String OctoPrintClient::getEstimatedPrintTime() {
return printerData.estimatedPrintTime; return printerData.estimatedPrintTime;
} }
String OctoPrintClient::getFileName() { String OctoPrintClient::getFileName() {
return printerData.fileName; return printerData.fileName;
} }
String OctoPrintClient::getFileSize() { String OctoPrintClient::getFileSize() {
return printerData.fileSize; return printerData.fileSize;
} }
String OctoPrintClient::getLastPrintTime(){ String OctoPrintClient::getLastPrintTime(){
return printerData.lastPrintTime; return printerData.lastPrintTime;
} }
String OctoPrintClient::getProgressCompletion() { String OctoPrintClient::getProgressCompletion() {
return String(printerData.progressCompletion.toInt()); return String(printerData.progressCompletion.toInt());
} }
String OctoPrintClient::getProgressFilepos() { String OctoPrintClient::getProgressFilepos() {
return printerData.progressFilepos; return printerData.progressFilepos;
} }
String OctoPrintClient::getProgressPrintTime() { String OctoPrintClient::getProgressPrintTime() {
return printerData.progressPrintTime; return printerData.progressPrintTime;
} }
String OctoPrintClient::getProgressPrintTimeLeft() { String OctoPrintClient::getProgressPrintTimeLeft() {
String rtnValue = printerData.progressPrintTimeLeft; String rtnValue = printerData.progressPrintTimeLeft;
if (getProgressCompletion() == "100") { if (getProgressCompletion() == "100") {
rtnValue = "0"; // Print is done so this should be 0 this is a fix for OctoPrint rtnValue = "0"; // Print is done so this should be 0 this is a fix for OctoPrint
} }
return rtnValue; return rtnValue;
} }
String OctoPrintClient::getState() { String OctoPrintClient::getState() {
return printerData.state; return printerData.state;
} }
boolean OctoPrintClient::isPrinting() { boolean OctoPrintClient::isPrinting() {
return printerData.isPrinting; return printerData.isPrinting;
} }
boolean OctoPrintClient::isPSUoff() { boolean OctoPrintClient::isPSUoff() {
return printerData.isPSUoff; return printerData.isPSUoff;
} }
boolean OctoPrintClient::isOperational() { boolean OctoPrintClient::isOperational() {
boolean operational = false; boolean operational = false;
if (printerData.state == "Operational" || isPrinting()) { if (printerData.state == "Operational" || isPrinting()) {
operational = true; operational = true;
} }
return operational; return operational;
} }
String OctoPrintClient::getTempBedActual() { String OctoPrintClient::getTempBedActual() {
return printerData.bedTemp; return printerData.bedTemp;
} }
String OctoPrintClient::getTempBedTarget() { String OctoPrintClient::getTempBedTarget() {
return printerData.bedTargetTemp; return printerData.bedTargetTemp;
} }
String OctoPrintClient::getTempToolActual() { String OctoPrintClient::getTempToolActual() {
return printerData.toolTemp; return printerData.toolTemp;
} }
String OctoPrintClient::getTempToolTarget() { String OctoPrintClient::getTempToolTarget() {
return printerData.toolTargetTemp; return printerData.toolTargetTemp;
} }
String OctoPrintClient::getFilamentLength() { String OctoPrintClient::getFilamentLength() {
return printerData.filamentLength; return printerData.filamentLength;
} }
String OctoPrintClient::getError() { String OctoPrintClient::getError() {
return printerData.error; return printerData.error;
} }
String OctoPrintClient::getValueRounded(String value) { String OctoPrintClient::getValueRounded(String value) {
float f = value.toFloat(); float f = value.toFloat();
int rounded = (int)(f+0.5f); int rounded = (int)(f+0.5f);
return String(rounded); return String(rounded);
} }

View File

@ -1,98 +1,98 @@
/** The MIT License (MIT) /** The MIT License (MIT)
Copyright (c) 2018 David Payne Copyright (c) 2018 David Payne
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// Additional Contributions: // Additional Contributions:
/* 15 Jan 2019 : Owen Carter : Add psucontrol query via POST api call */ /* 15 Jan 2019 : Owen Carter : Add psucontrol query via POST api call */
#pragma once #pragma once
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include "libs/ArduinoJson/ArduinoJson.h" #include "libs/ArduinoJson/ArduinoJson.h"
#include <base64.h> #include <base64.h>
class OctoPrintClient { class OctoPrintClient {
private: private:
char myServer[100]; char myServer[100];
int myPort = 80; int myPort = 80;
String myApiKey = ""; String myApiKey = "";
String encodedAuth = ""; String encodedAuth = "";
boolean pollPsu; boolean pollPsu;
void resetPrintData(); void resetPrintData();
boolean validate(); boolean validate();
WiFiClient getSubmitRequest(String apiGetData); WiFiClient getSubmitRequest(String apiGetData);
WiFiClient getPostRequest(String apiPostData, String apiPostBody); WiFiClient getPostRequest(String apiPostData, String apiPostBody);
String result; String result;
typedef struct { typedef struct {
String averagePrintTime; String averagePrintTime;
String estimatedPrintTime; String estimatedPrintTime;
String fileName; String fileName;
String fileSize; String fileSize;
String lastPrintTime; String lastPrintTime;
String progressCompletion; String progressCompletion;
String progressFilepos; String progressFilepos;
String progressPrintTime; String progressPrintTime;
String progressPrintTimeLeft; String progressPrintTimeLeft;
String state; String state;
String toolTemp; String toolTemp;
String toolTargetTemp; String toolTargetTemp;
String filamentLength; String filamentLength;
String bedTemp; String bedTemp;
String bedTargetTemp; String bedTargetTemp;
boolean isPrinting; boolean isPrinting;
boolean isPSUoff; boolean isPSUoff;
String error; String error;
} PrinterStruct; } PrinterStruct;
PrinterStruct printerData; PrinterStruct printerData;
public: public:
OctoPrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu); OctoPrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu);
void getPrinterJobResults(); void getPrinterJobResults();
void getPrinterPsuState(); void getPrinterPsuState();
void updateOctoPrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu); void updatePrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu);
String getAveragePrintTime(); String getAveragePrintTime();
String getEstimatedPrintTime(); String getEstimatedPrintTime();
String getFileName(); String getFileName();
String getFileSize(); String getFileSize();
String getLastPrintTime(); String getLastPrintTime();
String getProgressCompletion(); String getProgressCompletion();
String getProgressFilepos(); String getProgressFilepos();
String getProgressPrintTime(); String getProgressPrintTime();
String getProgressPrintTimeLeft(); String getProgressPrintTimeLeft();
String getState(); String getState();
boolean isPrinting(); boolean isPrinting();
boolean isOperational(); boolean isOperational();
boolean isPSUoff(); boolean isPSUoff();
String getTempBedActual(); String getTempBedActual();
String getTempBedTarget(); String getTempBedTarget();
String getTempToolActual(); String getTempToolActual();
String getTempToolTarget(); String getTempToolTarget();
String getFilamentLength(); String getFilamentLength();
String getValueRounded(String value); String getValueRounded(String value);
String getError(); String getError();
}; };

View File

@ -1,339 +1,339 @@
/** The MIT License (MIT) /** The MIT License (MIT)
Copyright (c) 2018 David Payne Copyright (c) 2018 David Payne
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// Additional Contributions: // Additional Contributions:
/* 15 Jan 2019 : Owen Carter : Add psucontrol query via POST api call */ /* 15 Jan 2019 : Owen Carter : Add psucontrol query via POST api call */
/* 07 April 2019 : Jon Smith : Redesigned this class for Repetier Server */ /* 07 April 2019 : Jon Smith : Redesigned this class for Repetier Server */
#include "RepetierClient.h" #include "RepetierClient.h"
RepetierClient::RepetierClient(String ApiKey, String server, int port, String user, String pass, boolean psu) { RepetierClient::RepetierClient(String ApiKey, String server, int port, String user, String pass, boolean psu) {
updateOctoPrintClient(ApiKey, server, port, user, pass, psu); updatePrintClient(ApiKey, server, port, user, pass, psu);
} }
void RepetierClient::updateOctoPrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu) { void RepetierClient::updatePrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu) {
server.toCharArray(myServer, 100); server.toCharArray(myServer, 100);
myApiKey = ApiKey; myApiKey = ApiKey;
myPort = port; myPort = port;
encodedAuth = ""; encodedAuth = "";
if (user != "") { if (user != "") {
String userpass = user + ":" + pass; String userpass = user + ":" + pass;
base64 b64; base64 b64;
encodedAuth = b64.encode(userpass, true); encodedAuth = b64.encode(userpass, true);
} }
pollPsu = psu; pollPsu = psu;
} }
boolean RepetierClient::validate() { boolean RepetierClient::validate() {
boolean rtnValue = false; boolean rtnValue = false;
printerData.error = ""; printerData.error = "";
if (String(myServer) == "") { if (String(myServer) == "") {
printerData.error += "Server address is required; "; printerData.error += "Server address is required; ";
} }
if (myApiKey == "") { if (myApiKey == "") {
printerData.error += "ApiKey is required; "; printerData.error += "ApiKey is required; ";
} }
if (printerData.error == "") { if (printerData.error == "") {
rtnValue = true; rtnValue = true;
} }
return rtnValue; return rtnValue;
} }
WiFiClient RepetierClient::getSubmitRequest(String apiGetData) { WiFiClient RepetierClient::getSubmitRequest(String apiGetData) {
WiFiClient printClient; WiFiClient printClient;
printClient.setTimeout(5000); printClient.setTimeout(5000);
Serial.println("Getting Repetier Data via GET"); Serial.println("Getting Repetier Data via GET");
Serial.println(apiGetData); Serial.println(apiGetData);
result = ""; result = "";
if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection if (printClient.connect(myServer, myPort)) { //starts client connection, checks for connection
printClient.println(apiGetData); printClient.println(apiGetData);
printClient.println("Host: " + String(myServer) + ":" + String(myPort)); printClient.println("Host: " + String(myServer) + ":" + String(myPort));
printClient.println("X-Api-Key: " + myApiKey); printClient.println("X-Api-Key: " + myApiKey);
if (encodedAuth != "") { if (encodedAuth != "") {
printClient.print("Authorization: "); printClient.print("Authorization: ");
printClient.println("Basic " + encodedAuth); printClient.println("Basic " + encodedAuth);
} }
printClient.println("User-Agent: ArduinoWiFi/1.1"); printClient.println("User-Agent: ArduinoWiFi/1.1");
printClient.println("Connection: close"); printClient.println("Connection: close");
if (printClient.println() == 0) { if (printClient.println() == 0) {
Serial.println("Connection to " + String(myServer) + ":" + String(myPort) + " failed."); Serial.println("Connection to " + String(myServer) + ":" + String(myPort) + " failed.");
Serial.println(); Serial.println();
resetPrintData(); resetPrintData();
printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed."; printerData.error = "Connection to " + String(myServer) + ":" + String(myPort) + " failed.";
return printClient; return printClient;
} }
} }
else { else {
Serial.println("Connection to Repeteir failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect Serial.println("Connection to Repeteir failed: " + String(myServer) + ":" + String(myPort)); //error message if no client connect
Serial.println(); Serial.println();
resetPrintData(); resetPrintData();
printerData.error = "Connection to Repetier failed: " + String(myServer) + ":" + String(myPort); printerData.error = "Connection to Repetier failed: " + String(myServer) + ":" + String(myPort);
return printClient; return printClient;
} }
// Check HTTP status // Check HTTP status
char status[32] = {0}; char status[32] = {0};
printClient.readBytesUntil('\r', status, sizeof(status)); printClient.readBytesUntil('\r', status, sizeof(status));
if (strcmp(status, "Host: 200 OK") != 0) { if (strcmp(status, "Host: 200 OK") != 0) {
Serial.print(F("Unexpected response: ")); Serial.print(F("Unexpected response: "));
Serial.println(status); Serial.println(status);
printerData.state = ""; printerData.state = "";
printerData.error = "Response: " + String(status); printerData.error = "Response: " + String(status);
return printClient; return printClient;
} }
// Skip HTTP headers // Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n"; char endOfHeaders[] = "\r\n\r\n";
if (!printClient.find(endOfHeaders)) { if (!printClient.find(endOfHeaders)) {
Serial.println(F("Invalid response")); Serial.println(F("Invalid response"));
printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort); printerData.error = "Invalid response from " + String(myServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
} }
return printClient; return printClient;
} }
void RepetierClient::getPrinterJobResults() { void RepetierClient::getPrinterJobResults() {
if (!validate()) { if (!validate()) {
return; return;
} }
//**** get the Printer Job status //**** get the Printer Job status
String apiGetData = "GET /printer/api/?a=listPrinter"; String apiGetData = "GET /printer/api/?a=listPrinter";
WiFiClient printClient = getSubmitRequest(apiGetData); WiFiClient printClient = getSubmitRequest(apiGetData);
if (printerData.error != "") { if (printerData.error != "") {
return; return;
} }
const size_t bufferSize = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 2*JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 710; const size_t bufferSize = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 2*JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + 710;
DynamicJsonBuffer jsonBuffer(bufferSize); DynamicJsonBuffer jsonBuffer(bufferSize);
// Parse JSON object // Parse JSON object
JsonArray& root = jsonBuffer.parseArray(printClient); JsonArray& root = jsonBuffer.parseArray(printClient);
if (!root.success()) { if (!root.success()) {
Serial.println("Repetier Data Parsing failed: " + String(myServer) + ":" + String(myPort)); Serial.println("Repetier Data Parsing failed: " + String(myServer) + ":" + String(myPort));
printerData.error = "Repetier Data Parsing failed: " + String(myServer) + ":" + String(myPort); printerData.error = "Repetier Data Parsing failed: " + String(myServer) + ":" + String(myPort);
printerData.state = ""; printerData.state = "";
return; return;
} }
///Selecting First printer ///Selecting First printer
JsonObject& pr = root[0]; JsonObject& pr = root[0];
//printerData.averagePrintTime = (const char*)pr[""]; //printerData.averagePrintTime = (const char*)pr[""];
printerData.estimatedPrintTime = (const char*)pr["printTime"]; printerData.estimatedPrintTime = (const char*)pr["printTime"];
printerData.fileName = (const char*) pr["job"]; printerData.fileName = (const char*) pr["job"];
printerData.fileSize = (const char*) pr["totalLines"]; printerData.fileSize = (const char*) pr["totalLines"];
//printerData.filamentLength = (const char*) pr[""]; //printerData.filamentLength = (const char*) pr[""];
printerData.state = (const char*) pr["online"]; printerData.state = (const char*) pr["online"];
//printerData.lastPrintTime = (const char*) pr[""]; //printerData.lastPrintTime = (const char*) pr[""];
printerData.progressCompletion = (const char*) pr["done"]; printerData.progressCompletion = (const char*) pr["done"];
printerData.progressFilepos = (const char*) pr["linesSend"]; printerData.progressFilepos = (const char*) pr["linesSend"];
printerData.progressPrintTime = (const char*) pr["printedTimeComp"]; printerData.progressPrintTime = (const char*) pr["printedTimeComp"];
//Figure out Time Left //Figure out Time Left
long timeTot=0; long timeTot=0;
long timeElap=0; long timeElap=0;
long timeLeft=0; long timeLeft=0;
if (printerData.estimatedPrintTime != "" ) if (printerData.estimatedPrintTime != "" )
{timeTot = atol(pr["printTime"]);} {timeTot = atol(pr["printTime"]);}
if (printerData.progressPrintTime != "") if (printerData.progressPrintTime != "")
{timeElap= atol(pr["printedTimeComp"]);} {timeElap= atol(pr["printedTimeComp"]);}
timeLeft = timeTot-timeElap; timeLeft = timeTot-timeElap;
printerData.progressPrintTimeLeft =String(timeLeft); printerData.progressPrintTimeLeft =String(timeLeft);
String printing = (const char*) pr["job"]; String printing = (const char*) pr["job"];
if (printing != "none") { if (printing != "none") {
printerData.isPrinting = true; printerData.isPrinting = true;
}else {printerData.isPrinting=false;} }else {printerData.isPrinting=false;}
Serial.println("PT:"+printerData.progressPrintTime); Serial.println("PT:"+printerData.progressPrintTime);
Serial.println("PTC:"+ printerData.estimatedPrintTime); Serial.println("PTC:"+ printerData.estimatedPrintTime);
Serial.println("ST:"+ printerData.lastPrintTime); Serial.println("ST:"+ printerData.lastPrintTime);
Serial.println("TimeLeft: "+printerData.progressPrintTimeLeft); Serial.println("TimeLeft: "+printerData.progressPrintTimeLeft);
if (printerData.isPrinting) if (printerData.isPrinting)
{ Serial.println("I think I am printing");} { Serial.println("I think I am printing");}
if (isOperational()) { if (isOperational()) {
Serial.println("Status: " + printerData.state); Serial.println("Status: " + printerData.state);
} else { } else {
Serial.println("Printer Not Operational"); Serial.println("Printer Not Operational");
} }
//**** get the Printer Temps and Stat //**** get the Printer Temps and Stat
apiGetData = "GET /printer/api/?a=stateList"; apiGetData = "GET /printer/api/?a=stateList";
printClient = getSubmitRequest(apiGetData); printClient = getSubmitRequest(apiGetData);
if (printerData.error != "") { if (printerData.error != "") {
return; return;
} }
const size_t bufferSize2 = 3*JSON_OBJECT_SIZE(2) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(9) + 300; const size_t bufferSize2 = 3*JSON_OBJECT_SIZE(2) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(9) + 300;
DynamicJsonBuffer jsonBuffer2(bufferSize2); DynamicJsonBuffer jsonBuffer2(bufferSize2);
//Parse JSON object //Parse JSON object
JsonObject& root2 = jsonBuffer2.parseObject(printClient); JsonObject& root2 = jsonBuffer2.parseObject(printClient);
//Select printer //Select printer
String slug=(const char*) pr["slug"]; String slug=(const char*) pr["slug"];
JsonObject& pr2 = root2[slug]; JsonObject& pr2 = root2[slug];
if (!root2.success()) { if (!root2.success()) {
printerData.isPrinting = false; printerData.isPrinting = false;
printerData.toolTemp = ""; printerData.toolTemp = "";
printerData.toolTargetTemp = ""; printerData.toolTargetTemp = "";
printerData.bedTemp = ""; printerData.bedTemp = "";
printerData.bedTargetTemp = (const char*) pr2["heatBeds"][0]["tempSet"]; printerData.bedTargetTemp = (const char*) pr2["heatBeds"][0]["tempSet"];
return; return;
} }
printerData.toolTemp = (const char*) pr2["extruder"][0]["tempRead"]; printerData.toolTemp = (const char*) pr2["extruder"][0]["tempRead"];
printerData.toolTargetTemp = (const char*) pr2["extruder"][0]["tempSet"]; printerData.toolTargetTemp = (const char*) pr2["extruder"][0]["tempSet"];
printerData.bedTemp = (const char*) pr2["heatedBeds"][0]["tempRead"]; printerData.bedTemp = (const char*) pr2["heatedBeds"][0]["tempRead"];
printerData.bedTargetTemp = (const char*) pr2["heatedBeds"][0]["tempSet"]; printerData.bedTargetTemp = (const char*) pr2["heatedBeds"][0]["tempSet"];
if (printerData.isPrinting) { if (printerData.isPrinting) {
Serial.println("Status: " + printerData.state + " " + printerData.fileName + "(" + printerData.progressCompletion + "%)"); Serial.println("Status: " + printerData.state + " " + printerData.fileName + "(" + printerData.progressCompletion + "%)");
} }
} }
void RepetierClient::getPrinterPsuState() { void RepetierClient::getPrinterPsuState() {
//**** get the PSU state (if enabled and printer operational) //**** get the PSU state (if enabled and printer operational)
//Not implemented in Repetier Server AFAIK //Not implemented in Repetier Server AFAIK
} }
// Reset all PrinterData // Reset all PrinterData
void RepetierClient::resetPrintData() { void RepetierClient::resetPrintData() {
printerData.averagePrintTime = ""; printerData.averagePrintTime = "";
printerData.estimatedPrintTime = ""; printerData.estimatedPrintTime = "";
printerData.fileName = ""; printerData.fileName = "";
printerData.fileSize = ""; printerData.fileSize = "";
printerData.lastPrintTime = ""; printerData.lastPrintTime = "";
printerData.progressCompletion = ""; printerData.progressCompletion = "";
printerData.progressFilepos = ""; printerData.progressFilepos = "";
printerData.progressPrintTime = ""; printerData.progressPrintTime = "";
printerData.progressPrintTimeLeft = ""; printerData.progressPrintTimeLeft = "";
printerData.state = ""; printerData.state = "";
printerData.toolTemp = ""; printerData.toolTemp = "";
printerData.toolTargetTemp = ""; printerData.toolTargetTemp = "";
printerData.filamentLength = ""; printerData.filamentLength = "";
printerData.bedTemp = ""; printerData.bedTemp = "";
printerData.bedTargetTemp = ""; printerData.bedTargetTemp = "";
printerData.isPrinting = false; printerData.isPrinting = false;
printerData.isPSUoff = false; printerData.isPSUoff = false;
printerData.error = ""; printerData.error = "";
} }
String RepetierClient::getAveragePrintTime(){ String RepetierClient::getAveragePrintTime(){
return printerData.averagePrintTime; return printerData.averagePrintTime;
} }
String RepetierClient::getEstimatedPrintTime() { String RepetierClient::getEstimatedPrintTime() {
return printerData.estimatedPrintTime; return printerData.estimatedPrintTime;
} }
String RepetierClient::getFileName() { String RepetierClient::getFileName() {
return printerData.fileName; return printerData.fileName;
} }
String RepetierClient::getFileSize() { String RepetierClient::getFileSize() {
return printerData.fileSize; return printerData.fileSize;
} }
String RepetierClient::getLastPrintTime(){ String RepetierClient::getLastPrintTime(){
return printerData.lastPrintTime; return printerData.lastPrintTime;
} }
String RepetierClient::getProgressCompletion() { String RepetierClient::getProgressCompletion() {
return String(printerData.progressCompletion.toInt()); return String(printerData.progressCompletion.toInt());
} }
String RepetierClient::getProgressFilepos() { String RepetierClient::getProgressFilepos() {
return printerData.progressFilepos; return printerData.progressFilepos;
} }
String RepetierClient::getProgressPrintTime() { String RepetierClient::getProgressPrintTime() {
return printerData.progressPrintTime; return printerData.progressPrintTime;
} }
String RepetierClient::getProgressPrintTimeLeft() { String RepetierClient::getProgressPrintTimeLeft() {
String rtnValue = printerData.progressPrintTimeLeft; String rtnValue = printerData.progressPrintTimeLeft;
if (getProgressCompletion() == "100") { if (getProgressCompletion() == "100") {
rtnValue = "0"; // Print is done so this should be 0 this is a fix for OctoPrint rtnValue = "0"; // Print is done so this should be 0 this is a fix for OctoPrint
} }
return rtnValue; return rtnValue;
} }
String RepetierClient::getState() { String RepetierClient::getState() {
return printerData.state; return printerData.state;
} }
boolean RepetierClient::isPrinting() { boolean RepetierClient::isPrinting() {
return printerData.isPrinting; return printerData.isPrinting;
} }
boolean RepetierClient::isPSUoff() { boolean RepetierClient::isPSUoff() {
return printerData.isPSUoff; return printerData.isPSUoff;
} }
boolean RepetierClient::isOperational() { boolean RepetierClient::isOperational() {
boolean operational = false; boolean operational = false;
if (printerData.state == "Operational" || isPrinting()) { if (printerData.state == "Operational" || isPrinting()) {
operational = true; operational = true;
} }
return operational; return operational;
} }
String RepetierClient::getTempBedActual() { String RepetierClient::getTempBedActual() {
return printerData.bedTemp; return printerData.bedTemp;
} }
String RepetierClient::getTempBedTarget() { String RepetierClient::getTempBedTarget() {
return printerData.bedTargetTemp; return printerData.bedTargetTemp;
} }
String RepetierClient::getTempToolActual() { String RepetierClient::getTempToolActual() {
return printerData.toolTemp; return printerData.toolTemp;
} }
String RepetierClient::getTempToolTarget() { String RepetierClient::getTempToolTarget() {
return printerData.toolTargetTemp; return printerData.toolTargetTemp;
} }
String RepetierClient::getFilamentLength() { String RepetierClient::getFilamentLength() {
return printerData.filamentLength; return printerData.filamentLength;
} }
String RepetierClient::getError() { String RepetierClient::getError() {
return printerData.error; return printerData.error;
} }
String RepetierClient::getValueRounded(String value) { String RepetierClient::getValueRounded(String value) {
float f = value.toFloat(); float f = value.toFloat();
int rounded = (int)(f+0.5f); int rounded = (int)(f+0.5f);
return String(rounded); return String(rounded);
} }

View File

@ -1,98 +1,98 @@
/** The MIT License (MIT) /** The MIT License (MIT)
Copyright (c) 2018 David Payne Copyright (c) 2018 David Payne
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// Additional Contributions: /* 07 April 2019 : Jon Smith : added class for Repetier Server (kg4iae@github)*/
/* 15 Jan 2019 : Owen Carter : Add psucontrol query via POST api call */
#pragma once #pragma once
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include "libs/ArduinoJson/ArduinoJson.h" #include "libs/ArduinoJson/ArduinoJson.h"
#include <base64.h> #include <base64.h>
class RepetierClient { class RepetierClient {
private: private:
char myServer[100]; char myServer[100];
int myPort = 3344; int myPort = 3344;
String myApiKey = ""; String myApiKey = "";
String encodedAuth = ""; String encodedAuth = "";
boolean pollPsu; boolean pollPsu;
void resetPrintData(); void resetPrintData();
boolean validate(); boolean validate();
WiFiClient getSubmitRequest(String apiGetData); WiFiClient getSubmitRequest(String apiGetData);
WiFiClient getPostRequest(String apiPostData, String apiPostBody); WiFiClient getPostRequest(String apiPostData, String apiPostBody);
String result; String result;
typedef struct { typedef struct {
String averagePrintTime; String averagePrintTime;
String estimatedPrintTime; String estimatedPrintTime;
String fileName; String fileName;
String fileSize; String fileSize;
String lastPrintTime; String lastPrintTime;
String progressCompletion; String progressCompletion;
String progressFilepos; String progressFilepos;
String progressPrintTime; String progressPrintTime;
String progressPrintTimeLeft; String progressPrintTimeLeft;
String state; String state;
String toolTemp; String toolTemp;
String toolTargetTemp; String toolTargetTemp;
String filamentLength; String filamentLength;
String bedTemp; String bedTemp;
String bedTargetTemp; String bedTargetTemp;
boolean isPrinting; boolean isPrinting;
boolean isPSUoff; boolean isPSUoff;
String error; String error;
} PrinterStruct; } PrinterStruct;
PrinterStruct printerData; PrinterStruct printerData;
public: public:
RepetierClient(String ApiKey, String server, int port, String user, String pass, boolean psu); RepetierClient(String ApiKey, String server, int port, String user, String pass, boolean psu);
void getPrinterJobResults(); void getPrinterJobResults();
void getPrinterPsuState(); void getPrinterPsuState();
void updateOctoPrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu); void updatePrintClient(String ApiKey, String server, int port, String user, String pass, boolean psu);
String getAveragePrintTime(); String getAveragePrintTime();
String getEstimatedPrintTime(); String getEstimatedPrintTime();
String getFileName(); String getFileName();
String getFileSize(); String getFileSize();
String getLastPrintTime(); String getLastPrintTime();
String getProgressCompletion(); String getProgressCompletion();
String getProgressFilepos(); String getProgressFilepos();
String getProgressPrintTime(); String getProgressPrintTime();
String getProgressPrintTimeLeft(); String getProgressPrintTimeLeft();
String getState(); String getState();
boolean isPrinting(); boolean isPrinting();
boolean isOperational(); boolean isOperational();
boolean isPSUoff(); boolean isPSUoff();
String getTempBedActual(); String getTempBedActual();
String getTempBedTarget(); String getTempBedTarget();
String getTempToolActual(); String getTempToolActual();
String getTempToolTarget(); String getTempToolTarget();
String getFilamentLength(); String getFilamentLength();
String getValueRounded(String value); String getValueRounded(String value);
String getError(); String getError();
}; };

View File

@ -1,112 +1,114 @@
/** The MIT License (MIT) /** The MIT License (MIT)
Copyright (c) 2018 David Payne Copyright (c) 2018 David Payne
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// Additional Contributions: // Additional Contributions:
/* 15 Jan 2019 : Owen Carter : Add psucontrol setting */ /* 15 Jan 2019 : Owen Carter : Add psucontrol setting */
/****************************************************************************** /******************************************************************************
* Printer Monitor is designed for the Wemos D1 ESP8266 * Printer Monitor is designed for the Wemos D1 ESP8266
* Wemos D1 Mini: https://amzn.to/2qLyKJd * Wemos D1 Mini: https://amzn.to/2qLyKJd
* 0.96" OLED I2C 128x64 Display (12864) SSD1306 * 0.96" OLED I2C 128x64 Display (12864) SSD1306
* OLED Display: https://amzn.to/2JDEAUF * OLED Display: https://amzn.to/2JDEAUF
******************************************************************************/ ******************************************************************************/
/****************************************************************************** /******************************************************************************
* NOTE: The settings here are the default settings for the first loading. * NOTE: The settings here are the default settings for the first loading.
* After loading you will manage changes to the settings via the Web Interface. * After loading you will manage changes to the settings via the Web Interface.
* If you want to change settings again in the settings.h, you will need to * If you want to change settings again in the settings.h, you will need to
* erase the file system on the Wemos or use the Reset Settings option in * erase the file system on the Wemos or use the Reset Settings option in
* the Web Interface. * the Web Interface.
******************************************************************************/ ******************************************************************************/
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <WiFiManager.h> #include <WiFiManager.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <ESP8266HTTPUpdateServer.h> #include <ESP8266HTTPUpdateServer.h>
#include "TimeClient.h" #include "TimeClient.h"
#include "OctoPrintClient.h" #include "OctoPrintClient.h"
#include "OpenWeatherMapClient.h" #include "RepetierClient.h"
#include "WeatherStationFonts.h" #include "OpenWeatherMapClient.h"
#include "FS.h" #include "WeatherStationFonts.h"
#include "SH1106Wire.h" #include "FS.h"
#include "SSD1306Wire.h" #include "SH1106Wire.h"
#include "OLEDDisplayUi.h" #include "SSD1306Wire.h"
#include "OLEDDisplayUi.h"
//******************************
// Start Settings //******************************
//****************************** // Start Settings
//******************************
// OctoPrint Monitoring -- Monitor your 3D printer OctoPrint Server
String OctoPrintApiKey = ""; // ApiKey from your User Account on OctoPrint // OctoPrint / Repetier Monitoring -- Monitor your 3D OctoPrint or Repetier Server
String OctoPrintHostName = "octopi";// Default 'octopi' -- or hostname if different (optional if your IP changes) #define USE_REPETIER_CLIENT // Uncomment this line to use the Repetier Printer Server -- OctoPrint is used by default and is most common
String OctoPrintServer = ""; // IP or Address of your OctoPrint Server (DO NOT include http://) String PrinterApiKey = ""; // ApiKey from your User Account on OctoPrint / Repetier
int OctoPrintPort = 80; // the port you are running your OctoPrint server on (usually 80); String PrinterHostName = "octopi";// Default 'octopi' -- or hostname if different (optional if your IP changes)
String OctoAuthUser = ""; // only used if you have haproxy or basic athentintication turned on (not default) String PrinterServer = ""; // IP or Address of your OctoPrint / Repetier Server (DO NOT include http://)
String OctoAuthPass = ""; // only used with haproxy or basic auth (only needed if you must authenticate) int PrinterPort = 80; // the port you are running your OctoPrint / Repetier server on (usually 80);
String PrinterAuthUser = ""; // only used if you have haproxy or basic athentintication turned on (not default)
// Weather Configuration String PrinterAuthPass = ""; // only used with haproxy or basic auth (only needed if you must authenticate)
boolean DISPLAYWEATHER = true; // true = show weather when not printing / false = no weather
String WeatherApiKey = ""; // Your API Key from http://openweathermap.org/ // Weather Configuration
// Default City Location (use http://openweathermap.org/find to find city ID) boolean DISPLAYWEATHER = true; // true = show weather when not printing / false = no weather
int CityIDs[] = { 5304391 }; //Only USE ONE for weather marquee String WeatherApiKey = ""; // Your API Key from http://openweathermap.org/
boolean IS_METRIC = false; // false = Imperial and true = Metric // Default City Location (use http://openweathermap.org/find to find city ID)
// Languages: ar, bg, ca, cz, de, el, en, fa, fi, fr, gl, hr, hu, it, ja, kr, la, lt, mk, nl, pl, pt, ro, ru, se, sk, sl, es, tr, ua, vi, zh_cn, zh_tw int CityIDs[] = { 5304391 }; //Only USE ONE for weather marquee
String WeatherLanguage = "en"; //Default (en) English boolean IS_METRIC = false; // false = Imperial and true = Metric
// Languages: ar, bg, ca, cz, de, el, en, fa, fi, fr, gl, hr, hu, it, ja, kr, la, lt, mk, nl, pl, pt, ro, ru, se, sk, sl, es, tr, ua, vi, zh_cn, zh_tw
// Webserver String WeatherLanguage = "en"; //Default (en) English
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]/ // Webserver
boolean IS_BASIC_AUTH = true; // true = require athentication to change configuration settings / false = no auth const int WEBSERVER_PORT = 80; // The port you can access this device on over HTTP
char* www_username = "admin"; // User account for the Web Interface const boolean WEBSERVER_ENABLED = true; // Device will provide a web interface via http://[ip]:[port]/
char* www_password = "password"; // Password for the Web Interface boolean IS_BASIC_AUTH = true; // true = require athentication to change configuration settings / false = no auth
char* www_username = "admin"; // User account for the Web Interface
// Date and Time char* www_password = "password"; // Password for the Web Interface
float UtcOffset = -7; // Hour offset from GMT for your timezone
boolean IS_24HOUR = false; // 23:00 millitary 24 hour clock // Date and Time
int minutesBetweenDataRefresh = 15; float UtcOffset = -7; // Hour offset from GMT for your timezone
boolean DISPLAYCLOCK = true; // true = Show Clock when not printing / false = turn off display when not printing boolean IS_24HOUR = false; // 23:00 millitary 24 hour clock
int minutesBetweenDataRefresh = 15;
// Display Settings boolean DISPLAYCLOCK = true; // true = Show Clock when not printing / false = turn off display when not printing
const int I2C_DISPLAY_ADDRESS = 0x3c; // I2C Address of your Display (usually 0x3c or 0x3d)
const int SDA_PIN = D2; // Display Settings
const int SCL_PIN = D5; const int I2C_DISPLAY_ADDRESS = 0x3c; // I2C Address of your Display (usually 0x3c or 0x3d)
boolean INVERT_DISPLAY = false; // true = pins at top | false = pins at the bottom const int SDA_PIN = D2;
//#define DISPLAY_SH1106 // Uncomment this line to use the SH1106 display -- SSD1306 is used by default and is most common const int SCL_PIN = D5;
boolean INVERT_DISPLAY = false; // true = pins at top | false = pins at the bottom
// LED Settings //#define DISPLAY_SH1106 // Uncomment this line to use the SH1106 display -- SSD1306 is used by default and is most common
const int externalLight = LED_BUILTIN; // LED will always flash on bootup or Wifi Errors
boolean USE_FLASH = true; // true = System LED will Flash on Service Calls; false = disabled LED flashing // LED Settings
const int externalLight = LED_BUILTIN; // LED will always flash on bootup or Wifi Errors
// PSU Control boolean USE_FLASH = true; // true = System LED will Flash on Service Calls; false = disabled LED flashing
boolean HAS_PSU = false; // Set to true if https://github.com/kantlivelong/OctoPrint-PSUControl/ in use
// PSU Control
// OTA Updates boolean HAS_PSU = false; // Set to true if https://github.com/kantlivelong/OctoPrint-PSUControl/ in use
boolean ENABLE_OTA = true; // this will allow you to load firmware to the device over WiFi (see OTA for ESP8266)
String OTA_Password = ""; // Set an OTA password here -- leave blank if you don't want to be prompted for password // OTA Updates
boolean ENABLE_OTA = true; // this will allow you to load firmware to the device over WiFi (see OTA for ESP8266)
//****************************** String OTA_Password = ""; // Set an OTA password here -- leave blank if you don't want to be prompted for password
// End Settings
//****************************** //******************************
// End Settings
String themeColor = "light-green"; // this can be changed later in the web interface. //******************************
String themeColor = "light-green"; // this can be changed later in the web interface.

File diff suppressed because it is too large Load Diff