simon-jouet / ESP32Controller

ESP32-based 3D printer controller
120 stars 29 forks source link

PoC Octoprint compatibility #11

Closed simon-jouet closed 3 years ago

simon-jouet commented 5 years ago

(That's Marlin related but there are more people here than on my dead Marlin branch :))

Just spent some time tonight to write the minimum required to have octoprint upload and print compatibility. Slic3r-PE looks to be quite happy with it and I can see the file being uploaded properly and the M23/M24 queued properly to start printing. That's really just a proof of concept, just wrote the software with my ESP32 board not connected to any printer.

I will clean that up and probably test it more later, just wanted to post it here if anybody is interested.

Note: If you try, a big limitation right now is that you need to specify a filename in Slic3r that follows the 8.3 format (for instance test.gco) otherwise it will fail.

diff --git a/Marlin/src/HAL/HAL_ESP32/web.cpp b/Marlin/src/HAL/HAL_ESP32/web.cpp
index b2bb03d71..a3c1cadc2 100644
--- a/Marlin/src/HAL/HAL_ESP32/web.cpp
+++ b/Marlin/src/HAL/HAL_ESP32/web.cpp
@@ -26,6 +26,7 @@

 #if ENABLED(WEBSUPPORT)

+#include "../../sd/cardreader.h" // O_* conflict with posix in idf
 #include <SPIFFS.h>
 #include "wifi.h"

@@ -35,7 +36,55 @@ void onNotFound(AsyncWebServerRequest *request){
   request->send(404);
 }

+void onUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
+  if (index == 0) {
+    // TODO need to deal with 8.3 filename
+    card.openFile(&filename[0u], false);
+  }
+
+  // Write the chunk
+  card.write(data, len);
+
+  // On the final chunk
+  if (final) {
+    card.closefile();
+
+    // If requested to print, enqueue the file
+    if (request->hasParam("print", true)) {
+      AsyncWebParameter* p = request->getParam("print", true);
+
+      if (p->value().compareTo("true") == 0) {
+        card.openAndPrintFile(&filename[0u]);
+      }
+    }
+
+    // TODO send the response, not required for Slic3r
+}
+
 void web_init() {
+  server.on("/api/version", HTTP_GET, [](AsyncWebServerRequest *request) {
+    request->send(200, "application/json", "{\"api\": \"0.1\", \"server\": \"1.3.10\"}");
+  });
+
+  server.on("/api/files/local", HTTP_POST, [](AsyncWebServerRequest *request) {
+    request->send(200);
+  }, onUpload);
+
   server.addHandler(&events);       // attach AsyncEventSource
   server.serveStatic("/", SPIFFS, "/www").setDefaultFile("index.html");
   server.onNotFound(onNotFound);
simon-jouet commented 3 years ago

closing this, I'm rewriting the web server to use esp-idf httpd instead of AsyncWebServer, the perf should be significantly better