Keith Sterling

Its a mixed bag of stuff, but mainly tech, music and programming, oh and just a bit of social comment !!!

Keith Sterling - Its a mixed bag of stuff, but mainly tech, music and programming, oh and just a bit of social comment !!!

Simple Client/Server RESTful Services with Python

In this article we look at creating the basics of a RESTful service and associated client, that we’ll build on later to create our home automation server

First we need to make sure we have a couple of libraries installed, specifically httplib2 and Flask. Install them using pip

sudo pip install httplib2
sudo pip install Flask

We can check they are installed with

pip list

which should display a list which includes the following

$ pip list
Flask (0.10.1)
httplib2 (0.8)

RESTFul Server

First the server, which we build using Flask

from flask import Flask, request, json
from pymongo import MongoClient

app = Flask(__name__)
mongo_client = MongoClient()
mongo_client['HomeControl'].temperature.drop ()

@app.route("/homecontrol/api/v1.0/temperature", methods=['POST'])
def api_temperature():
    if request.headers['Content-Type'] == 'text/plain':
        print (
        return 'OK', 200
    elif request.headers['Content-Type'] == 'application/json':
        print (json.dumps(request.json))
        print (mongo_client['HomeControl']['temperature'].count ())
        return "OK", 200
        return "Unsupported Media Type", 415

if __name__ == '__main__': = False)

For a description of how to create a RESTFul server in Flask, check out the excellent blog by Miguel Grinberg

RESTFul Client

Then a basic client that emulates our temp server

import httplib2
import json
import time
import datetime

if __name__ == '__main__':

    httplib2.debuglevel     = 0
    http                    = httplib2.Http()
    content_type_header     = "application/json"

    url = ""

    data = {    'room':         "Living Room",
                'temp':         23.45,
                'humidity':     50.00,
                'timestamp':    str(

    headers = {'Content-Type': content_type_header}
    print ("Posting %s" % data)

    while True:
        response, content = http.request( url,
        print (response)
        print (content)

This simple client sits in a loop posting a JSON object to our server every 3 seconds
We’ll fill out this client as time goes by, hooking it up to real temp sensors on the pi

Room Temperature Sensor With Raspberry Pi

This article is taken from the following web pages, with a few modifications by me.

The intention is to create a Raspberry monitoring station for each room in the house, each broadcasting temperature and other information back to a central server in my office.

To start with I am using DS18B20 temperature sensor, which you can get from Amazon and eBay for about 50p-£1 each.

Along with the sensor you are going to need a breadboard, ribbon cable, some wires and 4.7k resistor


Once you have this wired up, you need to activate the sensor

$ sudo modprobe w1-gpio
$ sudo modprobe w1-therm
$ cd /sys/bus/w1/devices
$ ls

This will give you the id of the device starting 28- and looking something like


Now create a shell script we can query the sensor

nano ~/

And add the following lines

#! /bin/bash
sudo modprobe w1-gpio
sudo modprobe w1-therm
roomtemp=$(cat /sys/bus/w1/devices/28-xxxxxxxxxxxx/w1_slave | grep  -E -o ".{0,0}t=.{0,5}" | cut -c 3-)
echo "Temperature: $roomtemp"

To make it executable, give it +x permissions

chmod +x ~/

And it will print out the temp, device / 1000 for ‘C

$ ~./

Which is 18.69′C

Now lets create a little Python script that parses the data and loops printing out the value every 10 secs

nano ~/

And add the following lines

import time
while True:
        # Open the file that we viewed earlier so that python 
        # can see what is in it. Replace the serial number as before.
        tfile = open("/sys/bus/w1/devices/28-xxxxxxxxxx/w1_slave")
        # Read all of the text in the file.
        text =
        # Close the file now that the text has been read.
        # Split the text with new lines (\n) and select the second line.
        secondline = text.split("\n")[1]
        # Split the line into words, referring to the spaces, 
        # and select the 10th word (counting from 0).
        temperaturedata = secondline.split(" ")[9]
        # The first two characters are "t=", so get rid of those and 
        # convert the temperature from a string to a number.
        temperature = float(temperaturedata[2:])
        # Put the decimal point in the right place and display it.
        temperature = temperature / 1000
        print ("%2.2f'C" % temperature)

To run the script, use


Mine is currently showing a balmy 16-19′C in my office


Loading Drivers on Startup

The following lines are taken from

sudo nano /etc/modprobe.d/raspi-blacklist.conf and comment out the following 2 lines

blacklist spi-bcm2708
blacklist i2c-bcm2708

sudo nano /etc/modules


Next steps

Now that we have a temperature sensor schematic and working prototype, need to build 3 or 4 more, and then hook them up to the central server. So once they are built, I need a Restful service that each of the Pi’s can call with their temperature data. The easiest way in Python is using the Flask framework backed with a Mongo DB. Watch this space for….

Building a RESTful web service using Pythong & Flask


Changing the hostname on a Raspberry Pi

Changing your hostname on a Raspi is pretty straight forward,

To get a host name use

keith@fuzepi ~ $ hostname

We now need to change the host name for the local address

keith@fuzepi ~ $ sudo vi /etc/hosts
[sudo] password for keith:

Change the name associated with 127.0.01, then we need to change the actual name of the host

keith@fuzepi ~ $ sudo nano /etc/hostname

Change the name in this file and save
One more step is to update the system

keith@fuzepi ~ $ sudo /etc/init.d/

Finally, reboot the Pi

keith@fuzepi ~ $ reboot

and the new name should be available with the command ‘hostname’

keith@gertpi ~ $ hostname

Raspberry Pi MongoDB Installation

Full instructions for install mongodb on a Pi can be found here

However on my 256M Model B, I have to change the memory split with the GPU and just allocated it 16M. This allows the command

git clone

To not exit with “out of memory”. You can do this by running

sudo raspi-config

And selecting advanced options, then memory split



Pi Ram doubled to 512M

Various news boards are reporting that Farnell are now taking orders for PI’s with 512MB of RAM rather than 256MB

This is great news, can’t wait to see the performance increase for the X Windows system now we can allocate more to it

The Register also reports just how crap RS Components are, with customer who ordered in June still waiting for orders. My view is bin RS and go to Farnell who have shipped just about all my Pis ( 7 so far ) in under 48 hours.

First Boot – Configuring your Pi

Right you have a Pi, and a power supply, and and its hooked up to some sort of monitor and TV and you’ve followed the instructions to format your SD card with Debian, its time to boot the little beauty and get it configure.

Pop in the SD card, insert the power supply and watch as the screen springs to life showing all sorts of operating system code. Nothing to worry about, watch it with awe as the OS boots and goes through its basic checks.

The first time the OS boots it starts with the following screen which is a program called raspy-config and allows you to configure the basic parameters of the operating system.

Its menu drive and easy to use with just a keyboard. No shinny shinny GUI here for you windows and mac users, this is hard code command line !!

First option displays some information about the tool

Press the cursor keys until <OK> turns red and the press OK, this takes you back to the previous screen.

Move to the next option which allows you to repartition the SD card to use all of the available memory rather than the 500M of the image you downloaded

The work will not take please until you reboot ( see below ), so as soon as you press OK it returns to the main menu

Next we set the keyboard. I use an old Mac keyboard, but you should search the list for the right version, the next menu then allows you to select more specific version of the keyboard

The next few options can be skipped through by selecting OK as they are more about configuring keys that you’ll use with Windowing system, but we are building a super computer and don’t need them

Again skip through this section

And this one…

Once you return to the main menu, the next option is to change the password for the default user ‘pi’

When you select this option, you’ll be prompted to enter a new password

After you have entered it and then reconfirmed it, it will confirm everything has been changed. You’ll need this password at the end of this section to log into the Pi

Next we set the memory usage. I tend to stick with the default option of using as much for the OS and the minimum for the graphics as we are doing super computer cool stuff not fancy windows stuff on this project

Next we set the overclocking options. Take note of the warning. If you are not comfortable with this process leave this option at default.

If you have a Rev 2 board then you can use the Turbo mode which runs the Pi at 1Ghz.

I’ll write a whole article about overclocking and the options for really binning your board later

Next we enable the SSH server. This will provide us a mechanism to log into each of the machines from your PC or Mac rather than attached a mouse and keyboard and monitor to each device.

Finally we select the boot option to not boot straight into X windows instead just the command line when the Pi starts.

You can easily start the windowing system with the command starts from the command line at any time once you are logged in

Finally select to reboot the Pi to finish the configuration

Select to restart and the system will reboot and you’ll see the text scroll past as before. However this time if you elected to extend the file system across the whole of the CD card above, then it will pause while it carries out this work.

This can take anything up to 5 mins for a 16GB card, so be patient, eventually the system will continue…..

Thats it, its booted and running, to login enter username pi and the password you entered during the password stage above

Next steps are to create a new user and install all the software to create an amazing super computer platform….

Burning an SD Image on a Mac

This post is unashamedly Mac biased, I use them day in day out, they are my work machine and until the Pi came along my play machine. I have some Linux and some windows some where, but they rarely get booted, so this post is about how to create a Debian image on an SD card that can be used to boot a Raspberry Pi

Step 1. Download the latest image from

Step 2. Insert the SD card. It will appear on you desktop looking something like the following image. Make a note of the name of the card, in this instance below it is called ‘Untitled’

Step 3. Open a terminal. Everything we now dow will be from the command line

Step 4. Identify the SD Card.

Enter the following command into at the terminal and press return, df list all available devices

$ df

You’ll get something like that. Now look for the name of the card we recorded from Step 1. Here we can see something called /Volumes/Untitled. If you now look at the beginning of the line, you’ll see the name OSX has given the device, it will start with /dev and have something of the format /disk2s1 like below. Make a note of the first number, in this instance ’2′

Step 5. Unmount the device. Before we can burn the image we need to unmount the SD card from the file system, issue the command

$ sudo diskutil unmount /dev/disk2s1

Remember to change the first number to the one we recorded from the step above.

If it all works, you’ll get a message saying the device has been unmounted as can be seen below

Step 6. Burn the image. Now we can burn the image that we downloaded from The command we use is dd and it takes the format

$ sudo dd bs=1m if=<path to image to burn> of=<path to device to burn to>

The path to the image to burn, is the path to file we downloaded in step 2

The path to the device to burn the image to, is of the format /dev/rdiskN, where N is the number we recorded from the previous step, in our instance 2

When you press enter, nothing should happen. OSX goes off and starts burning the image, but provides no feedback, eventually after 5-10 mins it will finish and return to the command prompt

Step 7. Eject the device. Now that the image is burnt, we can eject the device and then extract the SD card from the SD slot and place it in one of our Raspberry Pi’s

Thats it, if all goes well you’ll have an SD card that contains an image of the latest Debian release

The next post will show the boot process and initial configuration of the Pi