Skip to content

Instantly share code, notes, and snippets.

@softarn
Last active August 29, 2015 14:10
Show Gist options
  • Save softarn/0bbaf263b9138f06359f to your computer and use it in GitHub Desktop.
Save softarn/0bbaf263b9138f06359f to your computer and use it in GitHub Desktop.
BLOG - Christmas snowglobe - Simulating a button press on the raspberry pi
import RPi.GPIO as GPIO
import urllib2
import simplejson
import time
import sys
GPIO.setmode(GPIO.BCM)
print "Setup Pin 10 to output"
GPIO.setup(10, GPIO.OUT)
def main ():
while True:
try:
updateStatus()
except KeyboardInterrupt:
print "Bye!"
sys.exit(0)
except:
print "Unexpected error:", sys.exc_info()[0]
def updateStatus():
baseUrl = ""
req = urllib2.Request(baseUrl + "/status")
opener = urllib2.build_opener()
f = opener.open(req)
json = simplejson.load(f)
if json["play"]:
print "Playing a song!"
simulateButton()
time.sleep(10)
simulateButton()
#Nice little sleep between songs
time.sleep(2)
def simulateButton ():
GPIO.output(10, True)
time.sleep(0.1)
GPIO.output(10, False)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment