The wonders of twitter. The good, the bad, and the ugly. Thanks to Bluemix, getting tweets around the world into our software has been a breeze. Sort of. This post will document our ventures into MQTT and hopefully help out anyone else who is struggling to get a device plugged into node-red (in this case using python). The end goal is to display these tweets on a little LCD screen in front of the balloon’s camera so that you’ll be able to see your tweets in space.
So here’s my node-red flow (ignore the top section, that’s for receiving data from the balloon; this post is about sending to the balloon):
On the very left is a twitter and a test inject node. The twitter node is set up to catch any tweets that have the hastag ‘#bluemixballoon’. It’s pretty incredible that the API can do this, and suits our needs perfectly. It then goes straight into a ‘sentiment analysis’ node, which is rather funny. It reads your message and somehow determines whether it’s a nice message or a mean message and assigns a ‘sentiment score’ where anything above 0 is nice and anything below is mean. We’re using this to filter out offensive tweets but a bit of testing showed it’s very breakable, but the technique is only easy to find if you snoop around node-red a bit, the public shouldn’t be going around breaking it too often.
It then goes into a switch node which sends nice tweets one way and mean tweets the other. The nice tweets then go into a simple node.js function that changes the payload into a JSON string (shown below) and moves it to an IoT node to send to our balloon:
To get the device connected, we create an MQTT (or IoT – It turns out that Internet of Things is IBM’s MQTT device platform, just in case that’s a bit unclear) Broker service on Bluemix and register your device to get connected to the foundation. This gives you a load of information that you should store such as your ID and your ‘password’ for getting connected. We found that this service is only available on the US region of Bluemix for now, but launching in the US isn’t a problem.
With your device connected, to connect it using a python script follow the information at http://iotf.readthedocs.org/en/latest/libraries/python_cli_for_devices.html. A little snippet is below to show how simple it is to get authorisation. (I ended up spending a while trying to use the eclipse paho MQTT python library, but I strugged to get subscribed to the right things. Don’t be like me and not google “ibm iot python” before you jump straight into coding.)
A few more lines on the device side and you can start receiving commands from Bluemix. (Make sure the program stays running by means of a loop or something or else it’ll just exit before it gets the chance to receive anything, another silly mistake from me.) Hopefully then you’ll be able to display them onto the screen. It’s not the end of the problems though – for some reason node-red sends the JSON topics with apostrophes (‘) instead of quotes (“) so the JSON module in python can’t parse it properly. What I did to get around this was the convert all apostrophes into quotes but if you have apostrophes in the tweet it turns the apostrophes around the tweet into quotes, so the workaround breaks. I’m sure there’s a way around this but I thought writing this progress report/tutorial asap would be the way to go.