我用由 LED 组成的 Raspberry Pi 构建了一个小型设置。我已成功地将我的 Pi 与 Bluemix 连接起来。我编写了一个小代码,用于打开和关闭来自 mybluemix.net 网页的 LED(函数 myCommandcallback)。
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):
if cmd.event == "light":
payload = json.loads(cmd.payload)
command = payload["command"]
print command
if command == "on":
GPIO.output(17, GPIO.HIGH)
elif command == "off":
GPIO.output(17, GPIO.LOW)
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
当我点击网页上的按钮打开 LED 时,IBM IoT 平台上的传感器信息显示“灯亮”,但 LED 并没有真正打开。如果收到信息,是否可以检查 Pi?