Skip to content

Instantly share code, notes, and snippets.

@jsloyer
Last active February 1, 2016 15:25
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 jsloyer/72680061d449b9ae726b to your computer and use it in GitHub Desktop.
Save jsloyer/72680061d449b9ae726b to your computer and use it in GitHub Desktop.
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