使用Raspberry Pi和Bluemix的IoT Python应用程序:无法打开/关闭灯光并使用按钮

时间:2016-01-30 15:19:28

标签: python ibm-cloud iot

我做了以下教程,使用IoT Foundation将我的Raspberry Pi连接到Bluemix上的应用程序:

https://developer.ibm.com/bluemix/2015/04/02/tutorial-using-a-raspberry-pi-python-iot-twilio-bluemix/

除了client.py之外,我已经完成了所有的工作(本教程中的步骤36)。我收到以下错误消息:

client.py:9: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(17, GPIO.OUT)
Traceback (most recent call last):
  File "client.py", line 23, in <module>
    options = ibmiotf.application.ParseConfigFile("/home/pi/device.cfg")
  File "/usr/local/lib/python2.7/dist-packages/ibmiotf/application.py", line 501, in ParseConfigFile
    appType = parms.get(sectionHeader, "type", "standalone")
  File "/usr/lib/python2.7/ConfigParser.py", line 618, in get
    raise NoOptionError(option, section)
ConfigParser.NoOptionError: No option 'type' in section: 'application'

我发现错误来自缺失的&#34;类型&#34;配置文件中的部分(device.cfg)。

本教程步骤35中列出的文件结构并不是说您需要&#34;类型&#34;。我使用了之前在IoT Foundation中创建的设备类型(raspberrypi)。

现在我成功连接了IoT Foundation。

我现在遇到的问题是,当我按下Raspberry Pi上的按钮或打开/关闭&#34;打开/关闭灯光时,没有任何事情发生。 Web应用程序的按钮。

如果我使用&#34; ./ cf logs myappname -recent&#34;检查python应用程序的日志。我得到以下输出:

2016-01-29T08:38:49.10+0100 [App/0]      OUT on
2016-01-29T08:38:49.10+0100 [App/0]      ERR 75.126.37.74 - - [29/Jan/2016 07:38:49] "POST /light/on HTTP/1.1"; 302 -
2016-01-29T08:38:49.34+0100 [App/0]      ERR 75.126.52.20 - - [29/Jan/2016 07:38:49] "GET / HTTP/1.1"; 200 -
2016-01-29T08:38:49.56+0100 [RTR/1]      OUT myappname.mybluemix.net - [29/01/2016:07:38:49 +0000] "GET / HTTP/1.1" 200 0 751 "http://myappname.mybluemix.net/&quot; ";Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36" 108.168.250.151:39460 x_forwarded_for:&quot;46.5.2.236"x_forwarded_proto:"http&quot; vcap_request_id:myrequestid response_time:0.31228492 app_id:myapp_id x_global_transaction_id:mytransactionid;
2016-01-29T08:40:08.32+0100 [App/0]      OUT off
2016-01-29T08:40:08.33+0100 [App/0]      ERR 75.126.37.74 - - [29/Jan/2016 07:40:08] "POST /light/off HTTP/1.1" 302 -
2016-01-29T08:40:08.51+0100 [App/0]      ERR 75.126.23.243 - - [29/Jan/2016 07:40:08] "GET / HTTP/1.1" 200 -`

本教程的作者杰夫说,我应该仔细检查Pi和Bluemix中的应用程序是否都在监听同一主题。在物联网服务中,我应该能够看到消息传来。

我不知道这意味着什么或我如何做到这一点。

我们已经在博客文章(https://www.youtube.com/watch?v=YHDhn5hGnn8)的youtube视频的评论部分讨论了我的问题,Jeff告诉我,我们应该使用它移动到StackOverflow(有意义)。

感谢您的帮助。

//编辑以获取更多信息// 当我启动应用程序时,这是我在Pi上获得的输出: sudo python client.py

2016-02-01 14:36:23,221   ibmiotf.application.Client  WARNING Unable to encrypt messages because TLSv1.2 is unavailable (MQTT over SSL requires at least Python v2.7.9 or 3.4 and openssl v1.0.1)
2016-02-01 14:36:23,716   ibmiotf.application.Client  INFO    Connected successfully: A:69wm3f:aaaafdsfsd45641safsda2313

如果我在客户端启动后第一次按下开/关按钮,我会在Pi上获得以下输出:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2287, in _thread_main
    self.loop_forever()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1261, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 811, in loop
    rc = self.loop_read(max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1073, in loop_read
    rc = self._packet_read()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1475, in _packet_read
    rc = self._packet_handle()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1943, in _packet_handle
    return self._handle_publish()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2118, in _handle_publish
    self._handle_on_message(message)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2268, in _handle_on_message
    t[1](self, self._userdata, message)
  File "/usr/local/lib/python2.7/dist-packages/ibmiotf/application.py", line 423, in __onDeviceEvent
    if self.deviceEventCallback: self.deviceEventCallback(event)
  File "client.py", line 17, in myCommandCallback
    command = cmd.payload["d"]["command"]
TypeError: string indices must be integers, not str

看起来来自物联网基金会的消息正在传达给客户。

我在IoT Foundation,Bluemix Python应用程序的环境变量和Raspberry Pi上设置了以下ID:

  • IoT基础ID:afdsfsd45641safsda2313
  • Bluemix Python应用程序ID:afdsfsd45641safsda2313
  • device.cfg中的Raspberry Pi ID:afdsfsd45641safsda2313

所有三个ID都相同。

//编辑2 // 从注释部分添加print cmd语句后记录输出:

<ibmiotf.application.Event instance at 0x76526738>
light
{"command": "on"}

更改后的Stacktrace

command = cmd.payload["d"]["command"]

command = cmd.payload["command"]

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2287, in _thread_main
    self.loop_forever()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1261, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 811, in loop
    rc = self.loop_read(max_packets)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1073, in loop_read
    rc = self._packet_read()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1475, in _packet_read
    rc = self._packet_handle()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1943, in _packet_handle
    return self._handle_publish()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2118, in _handle_publish
    self._handle_on_message(message)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2268, in _handle_on_message
    t[1](self, self._userdata, message)
  File "/usr/local/lib/python2.7/dist-packages/ibmiotf/application.py", line 423, in __onDeviceEvent
    if self.deviceEventCallback: self.deviceEventCallback(event)
  File "client.py", line 18, in myCommandCallback
    command = cmd.payload["command"]
TypeError: string indices must be integers, not str

2 个答案:

答案 0 :(得分:2)

结果改变了IBM IoT Foundation库,它解析了JSON ...

更改line 15

command = cmd.payload["d"]["command"]

payload = json.loads(cmd.payload)
command = payload["command"]

答案 1 :(得分:0)

我添加了一行“json.dumps”,它对我有效。

dataraw   = json.dumps(cmd.data)
payload = json.loads(dataraw)
command = payload["command"]

祝你好运