Last active
February 1, 2016 15:25
-
-
Save jsloyer/72680061d449b9ae726b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 time | |
import os, json | |
import ibmiotf.application | |
import uuid | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(17, GPIO.OUT) | |
client = None | |
def myCommandCallback(cmd): | |
print cmd.event | |
if cmd.event == "light": | |
payload = json.loads(cmd.payload) | |
command = payload["command"] | |
print "command is " + command | |
if command == "on": | |
print "inside on" | |
GPIO.output(17, True) | |
elif command == "off": | |
print "inside off" | |
GPIO.output(17, False) | |
else: | |
print "invalid command " + command | |
try: | |
options = ibmiotf.application.ParseConfigFile("/home/pi/device.cfg") | |
options["deviceId"] = options["id"] | |
options["id"] = "aaa" + options["id"] | |
client = ibmiotf.application.Client(options) | |
client.connect() | |
client.deviceEventCallback = myCommandCallback | |
client.subscribeToDeviceEvents(event="light") | |
while True: | |
GPIO.wait_for_edge(18, GPIO.FALLING) | |
print "Button Pushed" | |
myData = {'buttonPushed' : True} | |
client.publishEvent("raspberrypi", options["deviceId"], "input", "json", myData) | |
time.sleep(0.2) | |
except ibmiotf.ConnectionException as e: | |
print e | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment