Last active
August 29, 2015 14:10
-
-
Save softarn/0bbaf263b9138f06359f to your computer and use it in GitHub Desktop.
BLOG - Christmas snowglobe - Simulating a button press on the raspberry pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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