From 109538b027fcb4ddab21254d11d032db113b65f7 Mon Sep 17 00:00:00 2001 From: probonopd Date: Tue, 9 Jul 2019 06:24:45 +0000 Subject: [PATCH 1/5] Add transparent serial bridge on port 2323 --- .../ESP8266WirelessPrintAsync.ino | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino index a1ca2f2..d40a5da 100644 --- a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino +++ b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino @@ -20,6 +20,11 @@ #include +const int transparent_networkport = 2323; + +WiFiServer transparentServer(transparent_networkport); +WiFiClient transparentClient; + const uint16_t PixelCount = 20; // this example assumes 4 pixels, making it smaller will cause a failure const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for ESP8266 (there it is GPIO2 = D4) #define colorSaturation 255 @@ -548,6 +553,9 @@ void setup() { telnetServer.begin(); telnetServer.setNoDelay(true); + + transparentServer.begin(); + transparentServer.setNoDelay(true); if (storageFS.activeSPIFFS()) { #if defined(ESP8266) @@ -1016,8 +1024,38 @@ void loop() { SendCommands(); ReceiveResponses(); + + //******************* + //* Transparent serial bridge handling * + //******************* + //check if there are any new clients + if (transparentServer.hasClient()){ + if (!transparentClient.connected()){ + if(transparentClient) transparentClient.stop(); + transparentClient = transparentServer.available(); + } + } + + //check a client for data + if (transparentClient && transparentClient.connected()){ + if(transparentClient.available()){ + size_t len = transparentClient.available(); + uint8_t sbuf[len]; + transparentClient.readBytes(sbuf, len); + Serial.write(sbuf, len); + } + } - + //check UART for data + if(Serial.available()){ + size_t len = Serial.available(); + uint8_t sbuf[len]; + Serial.readBytes(sbuf, len); + if (transparentClient && transparentClient.connected()){ + transparentClient.write(sbuf, len); + } + } + //******************* //* Telnet handling * //******************* From 3f643be81b1c723a72e3cd6d209d48197d66ab57 Mon Sep 17 00:00:00 2001 From: probonopd Date: Thu, 25 Jul 2019 12:22:05 +0000 Subject: [PATCH 2/5] Introduce and use transparent_client_is_connected variable --- .../ESP8266WirelessPrintAsync.ino | 146 ++++++++++-------- 1 file changed, 78 insertions(+), 68 deletions(-) diff --git a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino index d40a5da..977c27e 100644 --- a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino +++ b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino @@ -25,6 +25,8 @@ const int transparent_networkport = 2323; WiFiServer transparentServer(transparent_networkport); WiFiClient transparentClient; +bool transparent_client_is_connected = false; + const uint16_t PixelCount = 20; // this example assumes 4 pixels, making it smaller will cause a failure const uint8_t PixelPin = 2; // make sure to set this to the correct pin, ignored for ESP8266 (there it is GPIO2 = D4) #define colorSaturation 255 @@ -624,6 +626,8 @@ void setup() { "PROGRESS: " + stringify(fwProgressCap) + "\n" "BUILD_PERCENT: " + stringify(fwBuildPercentCap) + "\n"; } + message += "transparent_client_is_connected: " + stringify(transparent_client_is_connected); + message += "\n" message += ""; request->send(200, "text/html", message); }); @@ -975,55 +979,58 @@ void ReceiveResponses() { void loop() { - #ifdef OTA_UPDATES - //**************** - //* OTA handling * - //**************** - if (ESPrestartRequired) { // check the flag here to determine if a restart is required - PrinterSerial.printf("Restarting ESP\n\r"); - ESPrestartRequired = false; - ESP.restart(); - } - - ArduinoOTA.handle(); - #endif + if(transparent_client_is_connected == false){ + + #ifdef OTA_UPDATES + //**************** + //* OTA handling * + //**************** + if (ESPrestartRequired) { // check the flag here to determine if a restart is required + PrinterSerial.printf("Restarting ESP\n\r"); + ESPrestartRequired = false; + ESP.restart(); + } - //******************** - //* Printer handling * - //******************** - if (!printerConnected) - printerConnected = detectPrinter(); - else { - #ifndef OTA_UPDATES - MDNS.update(); // When OTA is active it's called by 'handle' method + ArduinoOTA.handle(); #endif - handlePrint(); + //******************** + //* Printer handling * + //******************** + if (!printerConnected) + printerConnected = detectPrinter(); + else { + #ifndef OTA_UPDATES + MDNS.update(); // When OTA is active it's called by 'handle' method + #endif - if (cancelPrint && !isPrinting) { // Only when cancelPrint has been processed by 'handlePrint' - cancelPrint = false; - commandQueue.clear(); - printerUsedBuffer = 0; - // Apparently we need to decide how to handle this - // For now using M112 - Emergency Stop - // http://marlinfw.org/docs/gcode/M112.html - telnetSend("Should cancel print! This is not working yet"); - commandQueue.push("M112"); // Send to 3D Printer immediately w/o waiting for anything - //playSound(); - //lcd("Print cancelled"); - } + handlePrint(); + + if (cancelPrint && !isPrinting) { // Only when cancelPrint has been processed by 'handlePrint' + cancelPrint = false; + commandQueue.clear(); + printerUsedBuffer = 0; + // Apparently we need to decide how to handle this + // For now using M112 - Emergency Stop + // http://marlinfw.org/docs/gcode/M112.html + telnetSend("Should cancel print! This is not working yet"); + commandQueue.push("M112"); // Send to 3D Printer immediately w/o waiting for anything + //playSound(); + //lcd("Print cancelled"); + } - if (!autoreportTempEnabled) { - unsigned long curMillis = millis(); - if ((signed)(temperatureTimer - curMillis) <= 0) { - commandQueue.push(TEMP_COMMAND); - temperatureTimer = curMillis + TEMPERATURE_REPORT_INTERVAL * 1000; + if (!autoreportTempEnabled) { + unsigned long curMillis = millis(); + if ((signed)(temperatureTimer - curMillis) <= 0) { + commandQueue.push(TEMP_COMMAND); + temperatureTimer = curMillis + TEMPERATURE_REPORT_INTERVAL * 1000; + } } } - } - SendCommands(); - ReceiveResponses(); + SendCommands(); + ReceiveResponses(); + } //******************* //* Transparent serial bridge handling * @@ -1031,6 +1038,7 @@ void loop() { //check if there are any new clients if (transparentServer.hasClient()){ if (!transparentClient.connected()){ + transparent_client_is_connected = false; if(transparentClient) transparentClient.stop(); transparentClient = transparentServer.available(); } @@ -1038,6 +1046,7 @@ void loop() { //check a client for data if (transparentClient && transparentClient.connected()){ + serial_client_is_connected = true; if(transparentClient.available()){ size_t len = transparentClient.available(); uint8_t sbuf[len]; @@ -1056,35 +1065,36 @@ void loop() { } } - //******************* - //* Telnet handling * - //******************* - // look for Client connect trial - if (telnetServer.hasClient() && (!serverClient || !serverClient.connected())) { - if (serverClient) - serverClient.stop(); - - serverClient = telnetServer.available(); - serverClient.flush(); // clear input buffer, else you get strange characters - } + if(transparent_client_is_connected == false){ + //******************* + //* Telnet handling * + //******************* + // look for Client connect trial + if (telnetServer.hasClient() && (!serverClient || !serverClient.connected())) { + if (serverClient) + serverClient.stop(); + + serverClient = telnetServer.available(); + serverClient.flush(); // clear input buffer, else you get strange characters + } - static String telnetCommand; - while (serverClient && serverClient.available()) { // get data from Client - { - char ch = serverClient.read(); - if (ch == '\r' || ch == '\n') { - if (telnetCommand.length() > 0) { - commandQueue.push(telnetCommand); - telnetCommand = ""; + static String telnetCommand; + while (serverClient && serverClient.available()) { // get data from Client + { + char ch = serverClient.read(); + if (ch == '\r' || ch == '\n') { + if (telnetCommand.length() > 0) { + commandQueue.push(telnetCommand); + telnetCommand = ""; + } + } + else + telnetCommand += ch; } } - else - telnetCommand += ch; - } - } - - #ifdef OTA_UPDATES - AsyncElegantOTA.loop(); - #endif + #ifdef OTA_UPDATES + AsyncElegantOTA.loop(); + #endif + } } From 21a84a78ba445ddb65d6e4ad4b59e27af53bec3a Mon Sep 17 00:00:00 2001 From: probonopd Date: Thu, 25 Jul 2019 12:35:11 +0000 Subject: [PATCH 3/5] fix transparent_client_is_connected --- ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino index 977c27e..90727f2 100644 --- a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino +++ b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino @@ -1046,7 +1046,7 @@ void loop() { //check a client for data if (transparentClient && transparentClient.connected()){ - serial_client_is_connected = true; + transparent_client_is_connected = true; if(transparentClient.available()){ size_t len = transparentClient.available(); uint8_t sbuf[len]; From 3d112a46eda07d169e12af38be6072d2db3c6d9f Mon Sep 17 00:00:00 2001 From: probonopd Date: Thu, 25 Jul 2019 12:46:41 +0000 Subject: [PATCH 4/5] Update ESP8266WirelessPrintAsync.ino --- ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino index 90727f2..bd86f6d 100644 --- a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino +++ b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino @@ -627,7 +627,7 @@ void setup() { "BUILD_PERCENT: " + stringify(fwBuildPercentCap) + "\n"; } message += "transparent_client_is_connected: " + stringify(transparent_client_is_connected); - message += "\n" + message += "\n"; message += ""; request->send(200, "text/html", message); }); From 23d7e6e35dca47124d6f6280ecad7bba7afa5ba8 Mon Sep 17 00:00:00 2001 From: probonopd Date: Thu, 25 Jul 2019 13:31:21 +0000 Subject: [PATCH 5/5] Use PrinterSerial --- ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino index bd86f6d..fabe81e 100644 --- a/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino +++ b/ESP8266WirelessPrintAsync/ESP8266WirelessPrintAsync.ino @@ -1051,15 +1051,15 @@ void loop() { size_t len = transparentClient.available(); uint8_t sbuf[len]; transparentClient.readBytes(sbuf, len); - Serial.write(sbuf, len); + PrinterSerial.write(sbuf, len); } } //check UART for data - if(Serial.available()){ - size_t len = Serial.available(); + if(PrinterSerial.available()){ + size_t len = PrinterSerial.available(); uint8_t sbuf[len]; - Serial.readBytes(sbuf, len); + PrinterSerial.readBytes(sbuf, len); if (transparentClient && transparentClient.connected()){ transparentClient.write(sbuf, len); }