Monday, February 09, 2015

Fun with Raspberry Pi

This post is a 101 Introduction to Raspberry Pi.  I will show you how to use a Raspberry Pi to create an Apple TV, Chromecast equivalent, and also read and push your home temperature to a server.

It is the best time to be a geek! There is 3G everywhere and 8MB Bandwidth at home and office. Geeks everywhere carry a super computer in their pocket now!

TBs of storage are now available for a dime. Everything is backed up realtime to the cloud. Even if you lose your device or hard drive crashes, it can all be downloaded back. Maps and GPS Accuracy is spot on. The stuff geeks have now makes James Bond look like a kid showing off in a School Science project.

And we need not deal with Windows or IE anymore. It is either Android or iOS or Ubuntu or OS X.

And then comes this Raspberry Pi to the party - from the open source hardware movement - a cute little computer - the size of a palm - which costs just Rs. 2,500 and can do a hell lot of things.

How to procure a Raspberry Pi

I got mine from - protocentral was the vendor. Just the raspberry pi ( motherboard, 4 USB, HDMI port, ethernet port ) will be 2,500. I also got a kit which had a SD card with the OSes, USB dongle, a breadboard, a few resistors, a capacitor and a power supply (which blew off). Also ordered a DHT11 a temperature and humidity sensor - it is 99Rs. It all came to Rs.5000.

Raspberry Pi or Arduino?

Just like the yin-yangs of vi emacs, android iOS, raspberry pi also has a yang - it is this Arduino. While Raspberry Pi is from UK, Arduino is from Italy. From what I learnt Raspberry Pi is good for beginners - understands Python and is more general purpose and can only talk digital. Arduino needs C ( segmentation fault - thought I had broken up with you) and is more rugged and can talk analog.

Some say start your prototype on Pi, then manufacture on arduino. Might know more about this as I keep digging.

Setting up Pi

Apart from Pi you will need these things

1. A TV with HDMI input
2. A keyboard
3. Mouse
4. 8GB or more SD Cards
5. Ethernet cable, and a router ( alternative is to share wifi on laptop over ethernet port )
6. A laptop/desktop - to format SD Card


If you have a spare SD Card, format it to Fat. Then download Noobs Lite - Network Install Only -, unzip and copy the entire folder contents into the root of the SD Card.

Boot the Pi with the SD Card, and it will show an option with a few OSes.  Choose Raspbmc - this is
the XBMC port and turns your Tv into a Smart Tv.

Here you will have to connect your Pi to the router through the ethernet port - so it can download the OS ( around 700MB ). The setup is entirely automatic and does not ask for any configuration. The default user account is pi and password is raspberry.

There are so many Add-ons and settings that are available - like a Youtube Add-on, or a subtitles Add-on for Movies. There is also a free XBMC Remote app on iOS and Android  with which you can connect to the Pi to control TV.


This is a regular light weight debian port. Works quite well in the puny pi. You can get another SD Card and install Raspbian ( same steps as raspbmc above ) - so you can swap the 2 sdcards.   There is also a multi boot option so you can boot into Raspbmc or Raspbian or more. I did not try this.

Say Hi to GPIO

GPIO - General Purpose Input Output. This is where things get interesting. There are 8 pins which can either send a 5V voltage, or receive a signal.

I wired the DHT 11 - Temperature and Humidity sensor to the Pi and was able to read the settings. Was quite easy.

1. The circuit layout I got here : Looks scary - but it is actually a very simple circuit. My skills are not that great, just followed the circuit to the T and it worked at the first try.

2. Get the Adafruit drivers and see if you can read the sensor.

git clone git:// .

cd Adafruit_DHT_Driver #go to the folder where DHT driver is present

Adafruit_DHT 11 14 #11 is for DHT11 sensor, 14 is the Pin I used

If you get temp and hum in output with numbers, then the circuit is working fine. Sometimes it will not give any reading, and a hex error will show up.

3. Next is to push this to a server. Again it is super easy.

First - create an account at and get a key.

I created a shell script which will call this python script with the temperature as argument. Then this script will post the readings to thingspeak.

Shell Script :
SCRIPT="/home/pi/playground/Adafruit_DHT_Driver/Adafruit_DHT 11 14"
TEMPERATURE=`$SCRIPT | grep "Temp" | awk -F " " '{print $3}'`
HITTHING="/home/pi/playground/mycode/kandivali_temp $TEMPERATURE  "

Python Script :

#! /usr/bin/python
import datetime
import logging
import time
import httplib, urllib
import sys

# The feed id and API key that is needed
thingSpeakKey = "getyourownkey"
for arg in sys.argv:
 tempvar = arg
if (tempvar != '/home/pi/playground/mycode/kandivali_temp'):
 print "Updating ThingSpeak ", time.strftime("%A, %B %d at %H:%M:%S") 

 params = urllib.urlencode({'field1': tempvar, 'key':thingSpeakKey})
 headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
 conn = httplib.HTTPConnection("")
 conn.request("POST", "/update", params, headers)
 response = conn.getresponse()
 print "Thingspeak Response:", response.status, response.reason

 if (response.reason != 'OK'):
  print "Problem, ", response.status, response.reason

Here are a few readings I got -

Next step would be to merge the shell script with Python ( still learning ), and run this as a cron job every 3 minutes or so.

The above concept is "Hello World" equivalent of Internet of Things. I can connect to any sensor, and   when things go bad - ( lets say the temperature starts spiking ), can send a notification on a phone to someone. The possibilities are infinite.

Some Gotchas

* Pi is configured to UK Locale by default. When you hit Shift 2 ( to get the @ symbol ) you will get a Pound symbol - and so on. It is annoying. Just go to /etc/default/keyboard and set it to US and reboot.

* I connected an external Hard drive, and Pi was not detecting it. The external hard drive needed a power source before Pi could detect it.

* Pi can be connected to a USB port of the laptop. Need not  hunt for a free power slot. Figured this out after the usb power plug blew up.

* Looking at TV and typing tires the eyes. Instead set a Static DHCP IP and then ssh to pi using Putty if you are on windows  ( ssh pi@, password raspberry ) and can use your laptop's keyboard.

Happy Baking!