Skip to content

Instantly share code, notes, and snippets.

@dayt0n
Created August 16, 2016 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dayt0n/ecc823677dd551abb302363c5e9ad8f9 to your computer and use it in GitHub Desktop.
Save dayt0n/ecc823677dd551abb302363c5e9ad8f9 to your computer and use it in GitHub Desktop.
run from /etc/rc.local on Raspberry Pi connected to Prusa i3 3D printer for homemade power button
# printer_switch.py
#
# controls power for a 3D printer that is connected to a Raspberry Pi (optionally running Octoprint)
#
# Copyright 2016 by dayt0n
import RPi.GPIO as GPIO
import time
import os
import subprocess
GPIO.setmode(GPIO.BCM)
upcmd = "sudo hub-ctrl -h 0 -P 2 -p 1\n"
downcmd = "sudo hub-ctrl -h 0 -P 2 -p 0\n"
# not used in this version since this part was still a little buggy
# powercmd = 'export PWRIF=$("sh -c checkit | grep Off")'
offcmd = "expect -f /usr/bin/offit"
oncmd = "expect -f/usr/bin/onit"
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
printpwr = "ON"
while True:
input_state = GPIO.input(18)
if input_state == False:
print('Button Presed')
if printpwr == "ON":
printpwr = "OFF"
process = subprocess.Popen(downcmd.split(), stdout=subprocess.PIPE)
process.wait()
print("Turned off USB")
processthree = subprocess.Popen(offcmd.split(),stdout=subprocess.PIPE)
process.wait()
print("Turning power off")
else:
printpwr = "ON"
process = subprocess.Popen(upcmd.split(), stdout=subprocess.PIPE)
print("Turned on USB")
processthree = subprocess.Popen(oncmd.split(), stdout=subprocess.PIPE)
print("Turned power on")
time.sleep(0.2)
# expect scripts look like this:
#
# spawn telnet 10.0.1.47
# sleep 2
# expect -re "User> "
# send "*username here*\r"
# expect -re "Password> "
# send "*password here*\r"
# expect -re "iBoot> "
# send "set outlet on (or off) \r\r"
# expect -re "iBoot> "
# send "logout\r"
# expect eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment