Unable to turn on off lights or send message using button in IoT Python app on Raspberry Pi and Bluemix

0 votes

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

I connected my Raspberry Pi with an application on Bluemix using the IoT Foundation. I could get everything up and running except for the client.py (@step 36 in the tutorial). Here's the error message I got:

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'

I found the error to have been coming from the missing "type" section in the config file (device.cfg). But, the file structure listed (@step 35 of the tutorial) doesn't say, that you need a "type". So, I just used the device type(raspberrypi), which I created in the IoT Foundation earlier. Now, I could successfully connect to the IoT Foundation.

But, the issue, now, is that absolutely nothing happens upon pressing the button on my Raspberry Pi or even pressing the "Turn light on/off" button on the web application.

When I checked the logs of the python app using "./cf logs myappname -recent", the following is the output I got:

2016-01-29T08:38:49.10+0100 [App/0]      OUT on
2016-01-29T08:38:49.10+0100 [App/0]      ERR - - [29/Jan/2016 07:38:49] "POST /light/on HTTP/1.1"; 302 -
2016-01-29T08:38:49.34+0100 [App/0]      ERR - - [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" x_forwarded_for:&quot;"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 - - [29/Jan/2016 07:40:08] "POST /light/off HTTP/1.1" 302 -
2016-01-29T08:40:08.51+0100 [App/0]      ERR - - [29/Jan/2016 07:40:08] "GET / HTTP/1.1" 200 -`

I also took this up with the tutorial's writer, Jeff. He advised me to double check if the Pi and the Bluemix app are both listening on the same topic or not. And, I should be able to see messages coming through in the IoT service.

So, I'm clueless to what this means or how I should be doing this now. I would appreciate any help at all, so please do share any ideas on the same. 

Also, here's the output on my Pi upon starting the application with: 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

The first time I press the on/off buttons after the client starts, following is the output that I get on the Pi:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
  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
  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
  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

The way it looks, it seems to me that messages from the IoT Foundation are successfully reaching the client.

Following are the IDs I've set(same IDs for all) on the IoT Foundation, the environment variable of the Bluemix Python app and on my Raspberry Pi:

  • IoT Foundation ID: afdsfsd45641safsda2313
  • Bluemix Python app ID: afdsfsd45641safsda2313
  • Raspberry Pi ID in device.cfg: afdsfsd45641safsda2313
Nov 26, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points

1 answer to this question.

0 votes

Hey, it turns out that the IBM IoT Foundation library has changed the way it parses JSON. So, At line of 15 of the code, change: 

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


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

And, that should get the client up and running. 

answered Nov 26, 2018 by DataKing99
• 8,240 points

Related Questions In IoT (Internet of Things)

+1 vote
1 answer

Configuring Raspberry Pi with Windows 10 IoT App using Rest API

You should use HttpClient instead of WebClient. Try ...READ MORE

answered Oct 10, 2018 in IoT (Internet of Things) by anonymous2
• 4,280 points
+1 vote
1 answer

Does learning IoT need you to buy a Raspberry Pi or similar IoT devices?

Though I'd recommend having the device for ...READ MORE

answered Oct 16, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
0 votes
1 answer
0 votes
1 answer

Display time in a Windows Core IoT app with a clock!

It is possible, but you should understand ...READ MORE

answered Jul 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
0 votes
1 answer

How to trigger Python script on Raspberry Pi from Node-Red

Node-RED supplies an exec node as part ...READ MORE

answered Sep 14, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
0 votes
1 answer

Building Python Server for Secure communication with Raspberry Pi

There could be several ways to add ...READ MORE

answered Jan 11, 2019 in IoT (Internet of Things) by DataKing99
• 8,240 points
0 votes
1 answer

raspberry pi details

I think It's only possible if the ...READ MORE

answered Jan 23, 2019 in IoT (Internet of Things) by Shubham
• 13,490 points
0 votes
1 answer

Using Sleep()/Delay() in C# on Windows IoT

You'll be glad to know that C# ...READ MORE

answered Aug 28, 2018 in IoT (Internet of Things) by DataKing99
• 8,240 points
0 votes
1 answer

Firewall issue with Webserver in UWP on Raspberry Pi 3

Here's what could be happenning: You port hasn't ...READ MORE

answered Oct 12, 2018 in IoT (Internet of Things) by DataKing99
• 8,240 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP