Tuesday, January 3, 2017

Adding 3d Printer power control to OctoPi

AKA Hacking the TP-Link HS100

I recently got a Monoprice Select 3d Printer...  I am using Octoprint as the controlling software and loving the versatility.   Now that I can print remotely and follow the process with the webcam I quickly found that I wished I could turn the printer ON and OFF when I wasn't using it.

A quick search on amazon found the TP-Link HS100 for $30USD ($35 on .ca).

A little further digging I found George's blog and his article on decoding the HS100 protocol.


Based on this, here is my quick instructions:


  1. Aquire the HS100 and set it up on your wifi.   You do NOT need to enable remote access.
  2. Make a note of the IP address of your HS100 (i.e. Look at the DHCP table on your router)
  3. Install script on your OctoPi:
    cd /home/pi/scripts
    wget https://raw.githubusercontent.com/ggeorgovassilis/linuxscripts/master/tp-link-hs100-smartplug/hs100.sh
    chmod u+x /home/pi/scripts/hs100.sh
  4. Test your connection using the command: "/home/pi/scripts/hs100.sh IPADDRESS status" (Don't forget to enter the IP of your HS100)
  5. Create two helper scripts. Make sure to change the IPADDRESS below!:
    IP=127.0.0.1
    echo '#!/bin/sh' > /home/pi/scripts/poweron.sh
    echo '/home/pi/scripts/hs100.sh' $IP 9999 on '>/dev/null' >> /home/pi/scripts/poweron.sh
    echo '#!/bin/sh' > /home/pi/scripts/poweroff.sh
    echo '/home/pi/scripts/hs100.sh' $IP 9999 off '>/dev/null' >> /home/pi/scripts/poweroff.sh
    chmod u+x /home/pi/scripts/poweron.sh /home/pi/scripts/poweroff.sh
    
    
  6. Install the System Command plugin in OctoPi
  7. Setup two commands, one for "PowerOn" using the command "/home/pi/scripts/poweron.sh" and one for "PowerOFF" using "/home/pi/scripts/poweroff.sh
  8. Voila!

Part 2
-------

I found another article on this topic by MOCHERY BLARG who did the same thing using the Wemo plug.    He also took it a step further and integrated with the Automatic Shutdown plugin.  Here is my remix:

  1. Install the Automatic Shutdown plugin via the plugin manager
  2. SSH into your octopi (as pi)
  3. Edit /home/pi/.octoprint/config.yaml to add a new commands.  Be sure to change IP address with the address of your HS110.

  4. cd /home/pi/.octoprint
    cp config.yaml config.yaml.bkup
    sed -e'/^    systemShutdownCommand:.*$/a \    printerShutdownCommand: \/home\/pi\/scripts\/poweroff.sh' config.yaml
    
    
    
  5. Modify the automatic shutdown plugin to call your new command:
  6. cd /home/pi/oprint/lib/python2.7/site-packages/octoprint_automaticshutdown
    cp __init__.py __init__.py.orig
    sed -i "s/systemShutdownCommand/printerShutdownCommand/g" __init__.py
    
    
  7. Restart OctoPi

Adding IFTTT to your OctoPi

I recently got a 3d printer (i3) and setup OctoPi as a controller.  It has a ton of plugins but I was surprised there isn't an IFTTT plugin.

I might get around to writing a proper plugin, but in the meantime here is a shell script hack...

Note: Setup your Maker Channel and make a note of your key to update the commands below...

Location: /home/pi/scripts/ifttt-maker.sh

#!/bin/sh
KEY=INSERT_KEY_HERE
curl -s "https://maker.ifttt.com/trigger/$1/with/key/\
$KEY?value1=$2&value2=$3value3=$4" > /dev/null
Then simply call the script /home/pi/scripts/ifttt-maker.sh TRIGGER value1 value2 value3 A simple example would be to make an Applet on IFTTT with the Maker channel receive webevent as a trigger using the trigger keyword "OctoPiPrintComplete" and an action using the iOS Notify channel. /home/pi/scripts/ifttt-maker.sh OctoPiPrintComplete "Done" Next step would be to add this as an event subscription in config.yaml....   I'll add that to another post.  (I envision sending the elapsed time as the value1 field)