Temperature to Speech with Raspberry Pi 2 and resin.io

It’s A Great Time To Be Alive

If you have even a passing interest in technology you can’t help but be excited about the Internet of Things - small computers like the Raspberry Pi and Beaglebone Black are evolving into smaller, ever more powerful computing devices, while platforms like resin.io allow you to get code running on them with a single command.

The potential uses of this technology is very exciting, so to get you up and running fast we've developed a fairly basic project to show you how easy it is to develop for connected hardware these days.

Is It Getting Hot In Here?

We are going to be using the new Raspberry Pi 2, a thermosensor and Google’s text to speech engine to read the temperature out loud at the push of a button.

The first thing you need to do is connect your device to resin.io. Using resin.io allows you to push code to your Pi over the air as well as vastly simplifying the os installation process.

Another benefit of using resin.io is the ability to maintain a common code base across multiple devices - imagine if we had a collection of these temperature sensors scattered around the house or even a city - every time you want to update your code you’d have to manually ssh into each Pi. Resin makes easy work of this.

Sign up at resin.io. It’ll prompt you to for your public ssh key, which is used to enable you to securely git push your code to your devices.

Once logged in create an app called “tempToSpeech”, choosing 'Raspberry Pi 2' as the device type.

Once the image download is complete you will need to burn it to your SD card. If you are on a unix based system, you can install the image with a GUI like pifiller otherwise you can use the following commands.

Check your currently mounted disks$ mount

Look for your SD card in the list - mine was mounted on '/dev/disk2s1'. Then unmount the disk$ sudo umount /dev/disk2s1

Now burn the image replacing your image and card location respectively (again, update the output device specified by of= depending on your system - /dev/rdisk provides raw access to a device on mac, the device will be different on linux)$ sudo dd bs=1m if=~/Downloads/resin-myApp-0.1.0-0.0.4.img of=/dev/rdisk2

Insert the SD card into your Pi, boot it up and make sure it’s connected to the internet. After 5-10 minutes your device will appear on your resin.io app’s dashboard.

If it hasn’t appeared on your dashboard, check your ACT light. If it blinks in a cycle of four long flashes then the Pi cannot connect to the internet. If the ACT light doesn’t flicker at all, it is likely that your SD card is corrupted.

Next we need to connect our components to the Pi, so unplug your Pi from it’s power source.

Then hook up your button to the Pi, simply connect one side of the switch to ground and the other to a GPIO input, we will be using pin 17.

Connect the Thermo sensor to the Pi. Ensure data is connected to pin #4 and the ~4.7K-10K resistor connects across the data and 3.3V lines. Here is more detailed explanation of the set up from adafruit.

Before we push this to the device, let’s take a quick look at the code.

It’s a simple project with 3 files. temp_to_speech.py reads the temperature and parses it as a string to speech.sh. This bash script sends the parsed string to google text-to-speech server which returns an audio file and then plays it with mplayer. The Dockerfile tells resin.io how to build your image what dependencies need to be installed and what to execute when the code lands on your device.

Incase you’ve never used a Dockerfile let’s spend a moment to break it down quickly. A Dockerfile is a set of instructions used by resin.io’s container engine to build a container to run on your device.

FROM resin/rpi-raspbian:jessie-2015-01-15

This tells resin.io to build a container with Raspbian’s jessie as the os. But you can pick from a variety of Raspbian versions on resin.io’s docker registry. If you'd like a little more info on containers give this a read.

This pushes the code to resin.io’s server, where it’s builds the container, cross-compiles the code and downloads it to run on the Pi’s architecture. You can view this download progress from your dashboard after a successful push.

Once the code has downloaded to your device and the container has started give that button a push!

If you just spent time converting celsius to fahrenheit in your head, we have you covered - this is fully configurable using resin.io's environment variables feature, which allows you to inject container wide environment variables from your dashboard.

To specify your preferred unit, navigate to your app’s environment variables tab and add one with a key of UNIT then insert your unit of choice(celsius, fahrenheit or kelvin) as the value. The container will now restart with the prefered unit of measurement.

In our temp_to_speech.py we are retrieving this enviroment variable and setting it as the variable ‘unit’.

unit = os.getenv('UNIT', 'celsius')

os.getenv accepts two arguments, the first is the key of the variable you’d are getting, the second a fallback if their is no value assigned to the variable or if it doesn’t exist. In our case we set the fallback to celsius.

The Beginning of a Beautiful Friendship

That’s how resin.io and the new raspberry pi work together. I hope this get’s you even more excited about the new technology emerging in the hardware space. If you'd like to know a little more about what resin does behind the scenes have a look at this.