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.

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('', files={'file': open(sys.argv[1], 'rb')}, headers={'Host': '', '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.


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:

The content of a Gcode file sent with python script


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.

BrainiHack 2015 – Blue GSD with Brain Controlled Labyrinth Game

This weekend (13-14.3) two of my friends (Gal Weinstock and Maxim Altshul) and I participated in the BrainiHack 2015 event hosted at the AutoDesk offices in Israel.
BrainiHack is a neuroscience themed Hack-A-Thon, where makers come to compete in a day-and-a-half long neuroscience-related project building marathon.

My group is called Blue GSD, we are two Communication Systems Engineering students and a Computer Science student. we came with absolutely no background in Neuroscience and tried to make something awesome. We brought an arsenal of tools, electronic components and a home-built 3D Printer.
We ended up winning the special OpenBCI prize for the best project in the open source category. The prize was an OpenBCI Starter Kit valued at 500USD.

Our project is a Labyrinth game controlled via brain waves (EEG). We are using OpenBCI, an arduino based open source bio-sensing microcontroller, It’s brilliant and with some initial assistance from Conor, one of the founders of OpenBCI, it’s really easy to work with.

The Labyrinth
The game itself was entirely 3D printed, it’s movement is provided via two micro servo motors controlled with an Arduino Uno.
The mechanism consists of three nested frames that are anchored in different places to achieve two degrees of freedom – roll and pitch.
We had no time for strong glue and I didn’t want to make any permanent connections (I’m designing on the fly so a lot can go wrong, and some did) so my design had to be zip tie friendly, the motors and motor arms are all attached with zip ties to the frames. The hinges are M3x16 screws and nuts with some washers to keep the frames in place relative to each other and press them onto the motor.
In the end we noticed we didn’t have enough clearance under the device for the frames to move all the way and we had no time to print another outer frame, so we had to make lifters for the legs which actually worked out perfectly.
Models for all 3D printed parts can be downloaded free at Thingiverse.com

As mentioned before, OpenBCI is an open source EEG device (and more, but that’s not relevant right now, I might explore more of it’s capabilities in the future) which was handed out to whoever wanted to use it.
With OpenBCI we could attach electrodes wherever we wanted (as opposed to some of the other fixed position alternatives), that way we could experiment with different methods and brain waves and choose the ones that work for us.
Conor from OpenBCI was kind enough to give us a little guidance and showed us another project done with OpenBCI, a five-person controlled shark balloon by chip, from which we learnt a lot about the system.
In addition to all of its advantages, it’s Wireless! It includes an RFduino that transmits the data to a dedicated USB dongle that plugs into the computer.

Brain Waves
We wanted to explore more than one type of brain waves. The shark balloon project was based purely on Alpha waves, a 7.5-12.5Hz wave our brain produces in the Occipital Lobe when we close our eyes and relax, that’s neat but we wanted more. Alpha waves are very easy to detect but there’s only one type of Alpha wave per person, this means that in order to control 2 axis we would need 4 people or 2 if we use direction toggling (more on that next).
After some research we found out about SSVEP (Steady State Visually Evoked Potential), a phenomenon where the brain produces a frequency (and/or harmonies of a frequency) that matches the frequency that excites the retina. This means that if we look at a light that blinks at a constant frequency, the brain will produce the same frequency. After some experimentation, we found that the range 5Hz-20Hz was easiest to detect and that 16Hz was far enough from the Alpha waves so they don’t get mixed up.
By combining Alpha and SSVEP we have 2 types of waves we can control and anticipate, which gives us the ability to control the game with just one person.

Controlling the Game
The problem with this technology is that it’s very slow, it’s not real-time by any standard. It may take a few seconds for the wave to generate and be detected while the Labyrinth game requires finesse and delicate movements to balance the ball, this is impossible with 2 or 3 second delay between reaction and actual movement of the platform.
To overcome this challenge we decided to simplify the game, instead of continuous control of the position of the platform, each axis would only be fully tilted to one side or the other, for this to work we created a super-simple maze layout, no holes, no balancing, just walls with straight angles.
As mentioned, we wanted to control the whole thing with one person and we had two different signals we could extract. Left-right position toggle was controlled via Alpha wave and up-down position toggle was controlled via SSVEP.


Data Analysis and Translation
OpenBCI provides a neat GUI that allows you to visually see the data and analyse it more easily. The interface provides time-domain and frequency-domain (FFT) graphs as well as a map of the head with electrode activity.
Once the data is captured with OpenBCI, it is transferred to the computer for analysis, the computer runs a Processing program that computes the Fourier Transform of the signal over a defined interval of time, filters the spectrum to look at relevant frequencies and finds the most powerful frequency in the range. If the peaked frequency is the one we are looking for, a command is sent to an Arduino board via serial port. The Arduino then controls the servos according to the command received.
In our project, we were looking for peaks at around 10Hz (Alpha) and 16Hz (SSVEP) and had to ignore the very high peak at 50Hz that is caused by the AC power frequency here in Israel.

Screenshot from 2015-03-13 13_27_35

The code running on the computer is a modified version of the shark baloon’s code. Since they already figured out the Alpha wave capturing and translation, we had to modify it for one person (they used 5) and add the SSVEP frequency capturing. The shark balloon code was taken and modified from another project (which modified the original code from OpenBCI) so it’s already a little messy and we probably made it messier (with a day and a half to get the whole thing to work we had no time to waste on making it pretty).
The code running on the Arduino is fairly simple, just a loop polling input and controlling the servos. We made the servo movement slow and continuous so that the ball doesn’t jump around.
The code will be uploaded to GitHub in a few days.

Special thanks to Conor over at OpenBCI for helping us and introducing us to the technology.

OctoPi – Remote Control 3D Printing

Leaving a 3D printer unattended is not very smart, especially a homemade one. If you’re lucky, you’ll only mess up a print and waste some plastic. If you’re less lucky, you can cause some damage to the printer or worse, burn down your house!

This is where OctoPrint comes in. OctoPrint is a free 3D printer web interface. It includes a password protected interface with live video stream, time-lapse capturing, G-Code viewer, Terminal interface, Temperature graphs, motor and temperature control buttons, and more.
This means you can SEE what is going on in real-time and stop the printing or send some code to the printer if necessary. This is a huge convenience, I no longer have to go and check that everything is OK, I can do it from my phone.
Moreover, I can slice on any computer and send the G-Code from anywhere in the world. If I don’t have a computer with slicing software, I can even link OctoPrint to a local installation of CURA and slice remotely! now that’s amazing.

I work with two computers, a desktop and a laptop. Although OctoPrint gives me the freedom to send and monitor prints from any computer, I didn’t like the idea that my printer has to be permanently connected to one of them. I wanted the printer to be independent, autonomous. I didn’t want to have to leave my computer on or not be able to reset it is something else needed it.
A dedicated computer is a massive overkill, It takes up a lot of space and it costs too much for what it does, I was looking for a more mobile solution so that I could move the printer and still have all features available. I wanted the printer to be an all-in-one solution.
After a short search I found OctoPi, A Raspberry Pi image with OctoPrint and everything built-in, maintained by Guy Sheffer. This means I can mount a RPi on the printer and have the interface and all of its features and settings no matter where the printer is set or on which computer I’m currently working.

OctoPi also provides a GUI for a touch screen to be mounted on the printer, I have no need for this right now, but maybe I’ll add one in the future.

Raspberry Pi mounted on the printer. Excuse the cabling - i'm still working on that.
Raspberry Pi mounted on the printer. Excuse the cabling – i’m still working on that.

If you’ll notice, the USB cable is made of two cables, there are two reasons for this. One, the Arduino’s USB port is facing downwards and a straight jack would be pressed against the desk and apply force on the Arduino – not good. I used some scrap L shaped USB-B cable I had from an old scanner to prevent this. And two, the RPi and the Arduino are powered via two different power sources and both regulate the voltage seperately. This means that their operating voltages aren’t 100% identical. If I connect the RPi to the Arduino via regular USB cable, the RPi would supply its regulated 5V to the Arduino, making the Arduino receive power from two different sources – also not good. For this reason I cut the 5V line of the USB cable, thus connecting the + and – data lines and sharing ground (important!) without supplying power.
I didn’t cut the cable to length because I’m not completely sure this is the final position of all components. there are more to be mounted.

Working with a RPi meant i can use the GPIOs to make the printer smarter and add features I need (see the relay in the picture? more on that in a future post)