DesignSpark Electrical Logolinkedin
  • Brought to you by
  • RS Components
  • Allied Electronics

April 16, 2015 10:45

Follow

Temperature to Speech with Raspberry Pi 2 and resin.io

It’s A Great Time To Be Alive

cover.jpg

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.

Here’s the components we’ll need before we start:-

Raspberry Pi 2 (Although it will work with the B+, A+)
W1 thermal sensor (DS18S20, DS1822, DS18B20)
Button
Wires
Breadboard (optional)
Speaker
Resistor (~4.7K-10K Ohm)

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.

Screen Shot 2015-03-23 at 9.40.16 AM.png

Next, download the OS image and select your network configuration of choice.

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

If you are on windows you can use windisk2, more details here.

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.

button_bb.png

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.

button+temp_bb.png

Plug your speaker into the 35mm jack

title

Now our device is setup and ready to receive some code.

Clone our temp-to-speech project to your local machine.

$ git clone https://github.com/resin-projects/temp2speech.git

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.

RUN apt-get update && apt-get install -y python python-dev python-pip mplayer

RUN pip install RPi.Gpio w1thermsensor

The RUN command is executed on resin.io’s build server. We are installing a couple dependencies we’ll need for this project.

ADD . /app

This adds our current code base(dockerfile + temp_to_speech.py + speech.sh) into a folder called app. It’s good practice to not dump your project in the root our your container.

CMD ["python", "/app/temp_to_speech.py"]

CMD is only run on the device once the container has started running, it simply tells the container to run temp_to_speech.py.

Now that you understand how resin.io builds your container let’s push!

Firstly you’ll need to associate our local repository with our resin.io app’s remote repository.

Make sure you are in the project directory.

$ cd resin-temp-to-speech

Now add your apps resin.io git remote endpoint, you can find this in the top corner of your app’s dashboard.

$ git remote add resin git@git.resin.io:username/temptospeech.git

Now it’s simply a matter of pushing your code onto your Pi.

$ git push resin master

successfulpush.gif

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.

temp-to-speech-envars.png

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.

Bonus! If you prefer javascript we’ve made a temp-to-speech node.js app as well. Also note it doesn’t need a dockerfile, resin.io uses node’s package.json to install your dependencies and build your image, pretty cool!

 

ResinIO has not written a bio yet…

April 16, 2015 10:45

Follow