Teaching Alexa To 3D Print

Sometimes a gadget like Alexa or Google Home is a solution looking for a problem. Then the problem you’ve been looking for hits you square in the face. I’ve confessed before that I have an oscilloscope problem. I also have a microcontroller development board habit. It appears now I have too many 3D printers. I recently finished building my latest one, an Anet A8 I picked up on Black Friday. While calibrating it, I found myself juggling a screwdriver, a pair of pliers, and trying to operate the thing all at one time. I realized I had to come up with a better way.

I don’t know if it qualifies as an addiction yet, but I also have an Alexa in every room (although I call it “Computer” because I’m a Star Trek fan) and a Google Home device almost everywhere. Why can’t I get one of these assistants to operate my printer for me? What are assistants for, after all, other than telling Dad jokes?

You’d think adding voice control to a 3D printer would a bit difficult. With the right tools, it is actually pretty easy. Luckily those tools aren’t anything special… if you want a set up like mine, where Alexa controls your 3D printer, read on.

The Plan

Obviously, the Alexa is going to be my voice control input so that part is taken care of. You could use Google Home, too. What would have been the hardest part a few years ago is now perhaps the easiest part, paradoxically.

The second part of the puzzle is Repetier Server. You could probably use Octoprint (Tom Nardi just wrote a great guide for setting up Octoprint), but I use Reptier. If you haven’t used either, these programs create a Web page that allows you to control your printer from a browser. They also provide a Web-based API. That means you can send commands to your printer remotely just by forming a valid web request.

You might be able to guess the third part of this equation: If This Then That or IFTTT. You’ve probably used this before. It is something of a Swiss Army knife for routing web events to different web services.

With these three items — a voice control assistant, a web server that controls your printers, and an IFTTT account, you are almost there.

 Details

It is easy enough to get IFTTT to take a trigger phrase from your assistant and do something about it. For Alexa, you have to say “Alexa, trigger mumble mumble” which would send “mumble mumble” to IFTTT. The problems is, what to send?

There are three things you’ll need to accomplish to get something like “Alexa, trigger 3D printer home” to work. First, your Reptier Server needs to be accessible from the public Internet. Yes, hackers might take over your printer, but I’m willing to take that chance. Since IFTTT will send web requests to your server from outside your network, you have to do this to work with IFTTT.

Second, you need an API key from your copy of Repetier Server and the “slug” name of your printer. You might think this would give you some relief from print-happy hackers, but no. It is easy enough to find the key and the printer names. The “slug” is a short internal name for each printer that the user doesn’t necessarily see.

The third thing is, you need the commands you want to send the printer. For example, homing is G28. You can send any G code you like or even multiple G codes.

Implementation

You should open your printer server to the Internet using some non-standard port. For example, Repetier Server normally is on port 3344. You might want to expose port 9999 instead. That isn’t bulletproof security, but it can’t hurt. How you do this depends on your network. Check your router for “Port Forwarding” and direct requests on your chosen port to the IP address of Repetier Server. If you need more guidance just Google your router’s model number and “port forwarding”.

Since your external IP address is probably not static, you will also want some dynamic DNS forwarding to associate a URL name with your external-facing machines. I have my own DNS servers, but Duck DNS works well. There are plenty of others, too.

Try your commands from a web browser to make sure they work. The first command you can send is just to get information. As an example let’s say your server is at the fake address dyn.hackaday.com:1430. Try this URL from your browser:

http://dyn.hackaday.com:1430/printer/info

You’ll see something like this (broken up for readability):

{"apikey":"1ca77731-aaaa-4c31-a44a-65deadbeefb8","name":"Repetier-Server Free",
"printers":[{"active":true,"name":"Monoprice Select Mini","online":0,"slug":"Monoprice_Select_Mini"},
{"active":true,"name":"Printrbot Plus","online":0,"slug":"Printrbot_Plus1"},
{"active":true,"name":"Anet A8","online":1,"slug":"Anet_A8"}],"version":"0.86.2"}

You can probably deduce that the big hex string is your API key (this one’s fake). You can see my three printers which the server knows as Monoprice_Select_mini, Printrbot_Plus1, and Anet_A8.

Next, try to do a printer home. You should jog the print head somewhere so you get a dramatic homing when it works. Try this (substituting your details for the bold text, of course):

http://dyn.hackaday.com:1430/printer/api/Anet_A8?apikey=1ca77731-aaaa-4c31-a44a-65deadbeefb8&a=send&data=%7B%22cmd%22:%22G28%22%7D

The URL encoded characters after “data=” are actually these characters: {"cmd":"G28"}. You can probably enter them that way into your browser and it will convert them, depending which browser you use. Repetier won’t send anything exciting back to the browser, but if all went well, your printer should home.

By the way, forwarding an external port to your 3D printer is a security hole. For a normal set up, you could ask Repetier to authenticate users or keep the service safe with, say, port knocking, VPN, or some other secure scheme. The problem is IFTTT — the thing we’ll use in the next step — is going to need to talk to the printer from outside and it isn’t going to know how to authenticate or otherwise open your ports.

If you were serious about using this and serious about security, you could write a small web service that would do something smarter to authenticate you and then forward the request to Repetier which would no longer be outside-facing. However, whatever authentication method you use would have to be one that IFTTT could handle without exposing your secrets (in other words, just passing a password in plain text isn’t the answer).

In my case, I didn’t feel too worried that during the times my printer is on that someone would scan all the ports I expose looking for 3D printing servers and start printing spam on my printers.

If Then

Once you have your web service working and accessible, you can open up IFTTT. Sign in and go to the My Applets page. Click New Applet. You’ll see “if + this then that” and you want to click the plus sign.

From the list of services, find Amazon Alexa (the search box works well). When you click that service, pick “Say a Specific Phrase”.  From there you will tell IFTTT what you want Alexa to listen for after the word “Trigger.” For example, you might enter “3d printer home.” For some reason, the text has to be all in lower case and sometimes you have to spell things like they sound so Alexa will understand. For example, “anet” required me to say “Ahhh-net” which was too hard to remember, so I changed that pretty fast.

Press “Create Trigger” and you’ll now see a plus sign between the word “then” and “that” — press it. Find the service called Webhooks and click it. There will be a single choice called “Make a web request.” Click that.

In the URL box, you can put the whole string you had working in your browser. You can use the non-URL encoded characters here. For the method, select GET and the content type is application/json. You can leave the body field blank. Click Create Action and then Finish. That’s it!

Profit

Now to home your printer just say “Alexa, trigger 3d print home.” IFTTT doesn’t give you much in the way of diagnostics, so if it doesn’t work, you’ll have to play around a bit. I actually changed mine to “3d printer” but I probably need to change it again since I have more than one I need to command.

You can add as many of these commands as you like. For example, the commands “M140 S50 M104 S190 T0” would heat your bed and extruder up to 50 and 190 degrees. You could turn on the fans, home the Z axis (my favorite), or anything else you can do in G code.

You can also do some things that aren’t in G code. For that, you’ll need to read the Repetier API documentation. For example, the copyModel command can start a job, and stopJob might be handy if you are across the room and notice the print has fallen off the bed. For that matter, if you are watching over a web camera and you have an Alexa nearby, that might be really handy!

This is a quick and easy way to add voice control to your printer. You might find it is one of the better uses for your virtual assistant. It would be nice if there was a more secure way to expose the service. Also, I am aware most people use Octoprint and that should be able to pull the same trick. If you’ve managed either of those, please tell us how you pulled it off in the comments below.

21 thoughts on “Teaching Alexa To 3D Print

  1. Somewhat related, if you have a SmartThings hub in addition to an Alexa device, you can bypass IFTTT and talk to Alexa with arbitrary commands (no ‘trigger’ needed). The way I did it was this:

    1. Use an HTTP Button addon in SmartThings. (See forums.)
    2. Set up ‘Routines’ in the Alexa app to turn on the buttons.

    Then you could say things like “Alexa, home the printer.” or “Alexa, warm the printer.” or even “Alexa, peanut butter sandwich.” (Whatever you want.)

    Another benefit is since the http request comes from inside your network (your SmartThings hub), you don’t have to open/forward ports on your router.

    1. I’m so done with the internet of things, and so ready for the intranet of things.

      Seems like so much doesn’t need to leave my house any more… sure alexa/google home do have to leave the house, to get the voice recognition from some one else’s servers… you COULD do it internally, but those systems are slower and generally seem to struggle to understand everything the big corporate systems do…

      I’ve got home assistant setup and Alexa will dutifully turn things on and off via Hue emulation…
      Home assistant can spin up items to turn on and off, and trigger a script or JSON or whatever, to my duet web interface… but it makes the language kludgy… it’s not “alexa warm up printer” now it’s “alexa turn on printer warmup”

      And I can’t say things like “alexa print file kiff c colon backslash program one” instead I need to create a bunch of togglable “switches” in home assistant one for each file on the printer, and “alexa turn on kiff c colon backslash program one” (I’ve not done this for obvious reasons)

      But it keeps me out of the cloud, and I’ve little desire to be in the cloud when I don’t have to be.

  2. Alexa, 3D Printers and IFTTT… this article really ticks off all the “annoying overhyped hipster tech” boxes. :-D

    Granted that it’s kinda cool to interconnect all sorts of gadgets, but doing so over convoluted web service cascades and open ports..? That’s got to be the worst practice imaginable. No good reason to put all this stuff online when all you want is to locally control a local device! A site like this might want to set better examples than to teach bad IOT practices. Enough of barely secured unnecesarily exposed devices out there already.

    1. See Mike Szczys’ comment about six above yours.

      Mike Szczys says:
      January 5, 2018 at 11:46 am

      That’s the work of our Art Director, Joe Kim. He’s amazing. A couple things to check out:

  3. I’m waiting for the first report of someone’s 3D printer causing an injury because someone hacked their public-facing web server…or, worse yet, a mis-interpreted command to Alexa. This just screams safety concerns, having your fingers in the print path while you issue instructions to a cloud-residing voice recognition computer.

  4. I really cooled to the whole idea of Alexa “hacking” once I realized everything has to go out to the Internet and then back in. Even if you’re running your own code on a computer on the network, Alexa will only talk to external addresses so you have to use ngrok or something similar to loop data around…such a hassle.

    Don’t get me wrong, I understand why they are offloading processing to the “cloud”. But I’d still like a “developer” option or something that let connect the damn thing to the Pi sitting 3 feet away from it without having to route it through the Internet.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.