Skip to content

Instantly share code, notes, and snippets.

@jwheat
Created October 18, 2017 00:41
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 jwheat/10d06f85d8fb62a736725e6b08b3417b to your computer and use it in GitHub Desktop.
Save jwheat/10d06f85d8fb62a736725e6b08b3417b to your computer and use it in GitHub Desktop.
OctoPrint- Emergency Stop and Reprint Buttons
API = ""
import RPi.GPIO as GPIO
import time
import json
import requests
headers = {'X-Api-Key': API,"Content-Type":"application/json"}
GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_go_state = GPIO.input(11)
if input_go_state == True:
print('START Button Pressed: ')
time.sleep(0.2)
url = 'http://127.0.0.1/api/job'
s = requests.Session()
r = json.loads((requests.get(url, headers=headers)).content)["state"]
print r
if r != "Printing":
print ('Starting printing')
contents = json.dumps({"command":"start"})
requests.post(url, data=contents, headers=headers)
input_stop_state = GPIO.input(26)
if input_stop_state == True:
print('STOP Button Pressed: ')
time.sleep(0.2)
job_url = 'http://127.0.0.1/api/job'
head_url = 'http://127.0.0.1/api/printer/printhead'
s = requests.Session()
r = json.loads((requests.get(job_url, headers=headers)).content)["state"]
print r
if r == "Printing":
print ('Stopping printing')
print_contents = json.dumps({"command":"cancel"})
head_contents = json.dumps({"command": "home","axes": ["x", "y"]})
requests.post(job_url, data=print_contents, headers=headers)
requests.post(head_url, data=head_contents, headers=headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment