Skip to content

Instantly share code, notes, and snippets.

@BoKKeR
Created July 20, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BoKKeR/9586ccc89544d336a5f6486cd7cb6dfc to your computer and use it in GitHub Desktop.
Save BoKKeR/9586ccc89544d336a5f6486cd7cb6dfc to your computer and use it in GitHub Desktop.
basic button
API = "enter octoprint api key"
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(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(26)
if input_state == False:
print('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 "Stopping printing"
contents = json.dumps({"command":"cancel"})
else:
print "Starting printing"
contents = json.dumps({"command":"start"})
requests.post(url, data=contents, headers=headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment