Plugging a Pi in the Sky into the Internet

Following up from the previous blog on sending messages to our raspberry pi on Bluemix (link), here’s a blog on how we’re going to receive messages from the raspberry pi and forward them to Bluemix. The hardware we’re using on the pi is Dave Akerman’s Pi in the Sky (link) and the hardware on the ground is the Funcube Pro+ Dongle (link) which plugs into my Asus netbook:

netbook
R is for receive, T is for transmit

To get good range on our receiver we’re using a yagi antenna (link), which is directional so it’ll have to be pointed in the direction of the balloon:

aerial

To get the pi sending packets through the Radiometrix transmitter on the Pi in the Sky you can follow the tutorial on this blog (link). This will be picked up by the dongle, where it’s driver places the signal into the sound card of your laptop. You then need some software that can understand these frequencies and convert them into something that packet decoding software can understand; in this case we’re using SDR# (link). Below is a picture of what you should see when receiving packets correctly. Make sure you’re using USB mode and have the ‘Correct IQ’ and ‘Swap I & Q’ boxes checked, then scroll to the frequency that your pi is sending on, and cover the ‘spike’ at your transmission frequency by changing the bandwidth to around the right size – it doesn’t have to be exact (these frequency and bandwidth settings will probably have to be tweaked to get the next piece of software working properly).

sdr

The input/output settings also have to be selected depending on your computer. On windows you need to go to your sound recording devices and turn on ‘Stereo Mix’; the software works by taking the radio waves received and putting it into your sound card and Stereo Mix allows SDR# to send its output to the next piece of software called ‘dl-fldigi). There’s an excellent tutorial on getting the settings in dl-fldigi for receiving Pi in the Sky telemetry here (link) so I’ll keep things short here. If you have your settings correct and sound card doing its job then, in the waterfall (the yellow/blue flowing section) there should be 2 clear and solid bands. If you click in the middle of these 2 then the software should do its job and you’ll start seeing your packets in the text log:

dlfldigi

This software only forwards your transmissions to the habhub servers however (link), so you can’t get the data into Bluemix through the software alone (there are options in the menus for choosing a couch database URL to forward to, but we couldn’t get this working with Bluemix – even if we managed to then we wouldn’t be able to use their lovely website software to visualise the data). However you can log all the data into a text file by going in settings to Misc -> Text i/o -> Enable rx text stream. What we’re doing is running a python script to go trawl through this pile of text which looks like the picture below (due to the weird symbols logged because of the image packets):

log

Anybody who’s tried text manipulation with Python before has probably run into problems due to encodings and dreaded unicode errors, which isn’t helped by the fact that different versions of python handle text in different ways so you have to be very specific in your solutions. My solution uses Python 3.4 with the ‘linecache’ module imported, and to get around the unicode errors I typed ‘# coding: iso-8859-1’ into the top of the textout.txt log produced by dl-fldigi. This makes Python ignore any character outside of the original ascii set (link). Unfortunately this limits the characters you can use – for example our log now ignores dollar signs – but the main data in our telemetry is just numbers and letters to signify data such as coordinates or altitude so it does a good enough job to find what we want to send to Bluemix. With this you can use linecache.getline(‘textout.txt’, <line number>) to get the line you want while ignoring all the characters outside of the set defined.

linecache
regular expressions are a godsend

You can also see in the code above that the python script starts off by connecting to Bluemix’s quickstart service – tutorial at (link). We use the json module in Python to parse the data and publish to the topic ‘iot-2/evt/status/fmt/json’ (you can choose other topics if you register your device on the foundation but this use is simple enough to do it through quickstart).

script2
our JSON format to send over MQTT

The flow in Bluemix is viciously simple (the left red bubble in the picture below). It’s just an IoT node connected straight into a Cloudant database. If you connect a debug node you should see something similar to our JSON packets in the log window on the right.

receiveflow

Accessing the data from this database and displaying it is something we’ll cover in a future blog, but for now I hope this is useful for anyone wanting to track a Pi in the sky or send data to Bluemix using MQTT.

Advertisements