Upload gcode files automatically from Simplify3D to OctoPrint

 

Some slicers, like Slic3r, have a built-in feature that allows you to send gcode files directly to OctoPrint’s queue without saving and uploading manually. I wanted this feature in Simplify3D.

Someone must have done this already, right?

Kinda. there are a few solutions I found, but they have some disadvantages:
cURL – using cURL to send a POST request to OctoPrint’s REST API via the additional terminal commands for post processing in Simplify3D. This works, but it has a security flaw. A pretty big one – Simplify3D includes the post processing command inside the Gcode file, and the command for cURL contains the unique and secret API key for your OctoPrint server.
This means that ANYONE who has access to the interface (let’s say you opened a port in the router and made it accessible from anywhere) can download files in the queue WITHOUT LOGIN, open them, read the API key and take full control over your printer.
Not good.

curl
The content of a Gcode file sent with cURL

scp – what about copying straight to the machine? well, yes, it works. We are not using the API so no key is required and if you set up ssh keys, you wont need to enter your username and password.
The problem with this is that we are moving files behind OctoPrint without talking to the application, this means that in order to see our file in the queue and print them, we must refresh the webpage.
Not what I’m looking for.

A better solution

From the other examples we learn that we can’t have the API-key in the command and that we must talk to the server (i.e. use the API). This brings one thing to mind – a script!

After some googling and learning a bit about REST and python, I wrote this simple script :

import requests
import sys
requests.post('http://192.168.123.102/api/files/local', files={'file': open(sys.argv[1], 'rb')}, headers={'Host': '192.168.123.102', 'X-Api-Key': 'XXXXXXXXXXX'})

The “local” in the url can be changed to “sdcard” in order to save the files on the SD card.
In order to use the script for your setup, replace the IP with your server’s IP and the API-Key with the one for your server (can be found in OctoPrint’s settings under API).

After testing that the script works on its own, it was time to insert it to the post-processing box in Simplify3D.

settings

Note that because I put the script in the Simplify3D folder, the program needed to be run as administrator to run the script (took me a while to figure that one out). You can put it anywhere else and not have this problem.

We can see that there’s no key in the post processing command, let’s see if that’s the same in the file:

pyscript
The content of a Gcode file sent with python script

Success!

Additional features

Once you realize that you can run arbitrary scripts after the file saves, you can do pretty much anything. The REST API allows you to fully control the printer and local commands work fine. I haven’t found any use for other functionalities at this point, but I saw some people who wanted the file to be removed from the computer once uploaded or maybe start the print when the file uploads or run a special Gcode sequence. Not a problem, a quick look at the documentation and you’ll find commands for anything in the interface and a lot more.

6 comments

  1. Hi…
    I think your approach is wonderful, but I can’t get it to work…
    The python script seems to have errors… first, I think “localiles” in your code must be “local”.
    I corrected that, but anyway I get errors…
    When I run the script thru CMD I get:

    C:\>python “D:\_Software\Python\Scripts\upload.py” “[output_filepath]”
    File “D:\_Software\Python\Scripts\upload.py”, line 3
    requests.post(‘http://192.168.0.133/api/files/local={‘file’: open(sys.argv[1], ‘rb’)}, headers={‘Host’: ‘192.168.0.133’, ‘X-Api-Key’: ‘xxxxxxxxxxxxxxxxxxxxxxxxxx’})
    ^
    SyntaxError: invalid syntax

    Of course the API key is not “xxxxxxxxxx”… 😉

    Like

  2. Hi again… Don’t worry…!!!
    After trying for a while I found the right sintax for the code:

    import requests
    import sys
    requests.post(‘http://192.168.0.133/api/files/local’, files={‘file’: open(sys.argv[1], ‘rb’)}, headers={‘Host’: ‘192.168.0.133’, ‘X-Api-Key’: ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’})

    Works great, Thanks…!!! 😀

    Like

  3. I couldnt get this to work. Doesnt look to upload the file at all. Am I supposed to select Save toolpaths To Disk as I did with the insecure script?

    Thanks

    Like

Leave a Reply to Javier Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s