Category Archives: Linux

Raspberry Pi Camera Module (of the Dalek)

So now I’ve bought the Camera Module for the Raspberry Pi. I got mine from Amazon, mainly as I was putting in a big order and was drunk enough to tag it on to the end.

<a href=" important source.png”>raspberry-pi-camera

Getting the damn thing set up and streaming video through a web page was a Pain In The Arse (which I know about)!

The only resource I could find that worked was Miguel Grinberg’s guide, but I still had problems with that. I suggest that you read his site, as it contains a lot more information. My version, below, is a quick run through, more for me to refer back to than anything else.

1. Build and Install the software

Run the following as root:

$ apt-get install libjpeg8-dev imagemagick libv4l-dev
$ ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h
$ wget
$ unzip
$ cd mjpg-streamer-code-182/mjpg-streamer $ make mjpg_streamer
$ sudo cp mjpg_streamer /usr/local/bin $ sudo cp /usr/local/lib/ $ sudo cp -R www /usr/local/www

2. Start the Camera and Streamer

Starting “raspistill” as suggested in Miguel’s guide dumped a lot of text to my xterm, making things really annoying, so I installed “screen” and ran the Camera software and the streamer in a different “screen”, which would also keep running after I logged off the machine (follow the oncreen prompts when you see them):

$ apt-get install screen
$ screen
$ mkdir /tmp/stream
$ raspistill --nopreview -w 640 -h 480 -q 5 -o /tmp/stream/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &
$ LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i " -f /tmp/stream -n pic.jpg"
-o " -w /usr/local/www" & 

Dropping out of “screen” ( CRTL-A CTRL-D ) and back to a command prompt, and its all done.

3. Viewing the Stream

Fire up your favourite Browser (which I hope is Firefox) and access the site generate by th Pi. As the eventual goal of this whole thing a Wifi controlled toy Dalek drone, the hostname is “dalek”. Thus, on my network the address is:


You’ll have to substitute dalek for either the name of the machine on your network, or the IP address, eg:

You should be getting a web page with instructions on how it all works.

Short Version:

To grab a single from the camera:


To grab streaming video:


There other things you can play with, but those two are the only ones relevant to my little project.

Finally, an example of a still picture from the camera:

(Once again, full credit to Miguel Grinberg and his excellent guide.)

Protocol of the Ardunio Dalek

Ok, so now I’m just making Doctor Who episode titles up…

Anyway, while eagerly awaiting the release of the Raspberry Pi, which should be available within a month, I’ve been playing about with the communication method twixt Raspberry and Arduino.

As the ‘berry will be running Debian Linux, my Ubuntu server had been used as the test host, despite it being far too massive and heavy to be carried by the Dalek. I have a very long USB cable for such eventualities.

The plan has come down to initially sending single character commands to the Arduino over the serial port, and listening for a response. The responses will be a 5 character code with optional additional data.

The single character commands, their actions and return messages are:

Command Action Response
q Move forward and left MOVSF Ok
w Move forward MOVFF Ok
e Move forward and right MOVFS Ok
a Spin left MOVBF Ok
s Stop MOVSS Ok
d Spin right MOVFB Ok
z Move back and left MOVBS Ok
x Move back MOVBB Ok
c Move back and rigt MOVSB Ok
h Return last movement order MOTOR {motor status} Ok
v Return version number VERSN {version number}
l Get distance from left sensor DISTL {distance in cm}
r Get distance from right sensor DISTR {distance in cm}
  Unknown command UNKNW

So, here is the Arduino Sketch for this protocol:

// Project: Dalek control system – Receive and process commands from USB
// Version 1.0 (2012-02-09-09-06)
// Tony Blews

int MotorDirectionR = 10;
int MotorDirectionL = 11;
int MotorPowerR = 12;
int MotorPowerL = 13;
int IRPinLeft = 2;
int IPPinRight = 3;
String motorStatus = String (“”);

void setup()
pinMode(MotorDirectionR, OUTPUT);
pinMode(MotorDirectionL, OUTPUT);
pinMode(MotorPowerR, OUTPUT);
pinMode(MotorPowerL, OUTPUT);
motorStatus= String(“MOVSS”);
Serial.println(“START Serial control Dalek system starting…”);

void check_distance(int IRpin) {
float volts = analogRead(IRpin)*0.0048828125; // value from sensor * (5/1024) – if running 3.3.volts then change 5 to 3.3
float distance = 30*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S –
Serial.println(distance); // print the distance
delay(100); // arbitary wait time.

// modes for the motor control
// convention here is modeXX – where X is F for forward, S for stationary and B for backwards
// first X is the left motor, second X is the right one
// for direction control, the LOW if forward and HIGH is backward
// for power control, LOW is off and HIGH is on

// all stop
void modeSS()
digitalWrite(MotorDirectionR, LOW);
digitalWrite(MotorDirectionL, LOW);
motorStatus= String(“MOVSS Ok”);

// move straight ahead
void modeFF()
digitalWrite(MotorDirectionR, LOW);
digitalWrite(MotorDirectionL, LOW);
motorStatus= String(“MOVFF Ok”);

// move straight backwards
void modeBB()
digitalWrite(MotorDirectionR, HIGH);
digitalWrite(MotorDirectionL, HIGH);
motorStatus= String(“MOVBB Ok”);

// spin left
void modeBF()
digitalWrite(MotorDirectionR, LOW);
digitalWrite(MotorDirectionL, HIGH);
motorStatus= String(“MOVBF Ok”);

//spin right
void modeFB()
digitalWrite(MotorDirectionR, HIGH);
digitalWrite(MotorDirectionL, LOW);
motorStatus= String(“MOVFB Ok”);

// move forward left
void modeSF()
digitalWrite(MotorDirectionR, LOW);
digitalWrite(MotorDirectionL, LOW);
motorStatus= String(“MOVSF Ok”);

// move forward right
void modeFS()
digitalWrite(MotorDirectionR, LOW);
digitalWrite(MotorDirectionL, LOW);
motorStatus= String(“MOVFS Ok”);

// move backward left
void modeSB()
digitalWrite(MotorDirectionR, HIGH);
digitalWrite(MotorDirectionL, LOW);
motorStatus= String(“MOVSB Ok”);

// move backward right
void modeBS()
digitalWrite(MotorDirectionR, LOW);
digitalWrite(MotorDirectionL, HIGH);
motorStatus= String(“MOVBS Ok”);

//report distance left
void lookLeft()
Serial.print(“DISTL “);

//report distance right
void lookRight()
Serial.print(“DISTR “);

// report Status back to host
void helloDalek()
Serial.print(“MOTOR “);

//report software version
void reportVersion()
Serial.println(“VERSN 1.0”);

//main program loop
void loop()
if (Serial.available() >0)
char inByte =;
// this version uses the QWEASDZXC “square” on the keyboard
// as my laptop doesn’t have a numeric keypad
// L and R are used to “look” left and right for distance sensing
// H is for Hello. To report the status of the device.
switch (inByte)
case ‘q’:
case ‘w’:
case ‘e’:
case ‘a’:
case ‘s’:
case ‘d’:
case ‘z’:
case ‘x’:
case ‘c’:
case ‘l’:
case ‘r’:
case ‘h’:
case ‘v’:


Again, parts of this come from Lucky Larry‘s site.

And the C part:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <getopt.h>

void usage(void);
int serialport_init(const char* serialport, int baud);
int serialport_writebyte(int fd, uint8_t b);
int serialport_write(int fd, const char* str);
int serialport_read_until(int fd, char* buf, char until);

int main(int argc, char *argv[])
int fd = 0;
char serialport[256];
char buf[256];
int rc,n;

fd = serialport_init(“/dev/ttyUSB0”, B9600);
if(fd==-1) return -1;
rc = serialport_write(fd, buf);
if(rc==-1) return -1;
usleep(100 * 1000 ); // sleep milliseconds
serialport_read_until(fd, buf, ‘n’);
printf(“read: %sn”,buf);
} // end main

int serialport_write(int fd, const char* str)
int len = strlen(str);
int n = write(fd, str, len);
if( n!=len )
return -1;
return 0;

int serialport_read_until(int fd, char* buf, char until)
char b[1];
int i=0;
do {
int n = read(fd, b, 1); // read a char at a time
if( n==-1) return -1; // couldn’t read
if( n==0 ) {
usleep( 10 * 1000 ); // wait 10 msec try again
buf[i] = b[0]; i++;
} while( b[0] != until );

buf[i] = 0; // null terminate the string
return 0;

// takes the string name of the serial port (e.g. “/dev/tty.usbserial”,”COM1″)
// and a baud rate (bps) and connects to that port at that speed and 8N1.
// opens the port in fully raw mode so you can send binary data.
// returns valid fd, or -1 on error
int serialport_init(const char* serialport, int baud)
struct termios toptions;
int fd;

//fprintf(stderr,”init_serialport: opening port %s @ %d bpsn”,
// serialport,baud);

fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror(“init_serialport: Unable to open port “);
return -1;

if (tcgetattr(fd, &toptions) < 0) {
perror(“init_serialport: Couldn’t get term attributes”);
return -1;
cfsetispeed(&toptions, B9600);
cfsetospeed(&toptions, B9600);

// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~CRTSCTS;

toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl

toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
toptions.c_oflag &= ~OPOST; // make raw

// see:
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;

if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
perror(“init_serialport: Couldn’t set term attributes”);
return -1;

return fd;

This is a canibalisation of code from Tod Kurt‘s site.

My botch of this just takes a single character as a command line option, passes it the Arduino and prints the reply. eg:

$ ./dav s

read: MOVSS Ok

$ ./dav v

read: VERSN 1.0

$ ./dav l

read: DISTL 42.15

$ ./dav k

read: UKNWN

In other news, I’ve ordered a DIY Shield Kit from Amazon, so I can tidy up the hardware a bit, and a miniscule USB webcam which I intend to fit into the head part of the Dalek. I also plan to move all the clever bits of circuitry into to the top half, of the Dalek, and Meccano-up a set of brackets to hold the various battery sets that are needed in the bottom half.

Datawind UbiSurfer first impressions

I’m trying to get to grips with my impulse purchase of the week. For reasons best left unexplored I’ve parted with about 120 quid for a Datawind UbiSurfer from Maplin. Its a ex-display model, hence the slight price reduction over the RRP of £149.99.


So, what is the UbiSurfer? In short, its a cheap netbook running an implementation of Linux. Oh, with a years free internet access thrown in.

More about that later, but first the specs:

  • 7 inch 800×400 TFT screen
  • 128MB RAM, 1GB Solid State disk
  • Full QWERTY keyboard and Touchpad mouse
  • Push-Push SD card socket and 2 USB ports (the box says 3, but I have 2 and a connector I’ve never seen before)
  • Measures 222x165x29.5mm, weighs 700g
  • Battery life provides 4 hours active use and 4 hours

Note the lack of a spec concerning the processor. Apparently it is some form of ARM 500Mhz job.


Applications wise you get the usual word processing and spreadsheet offerings, and a collections of email clients, PDF readers and the like, all GPLed programs.

For web browsing you have Mozilla IceWeasel (Firefox), which only works when connected via a Wifi hotspot, and the UbiSurfer browser itself.
And now we get to the main (only) reason that I bought this thing: free internet access. Included in the price of the device is 30 hours access per month (for a year) to Vodaphone’s GPRS network, via an onboard modem. As we all know, GPRS isn’t exactly lightening fast, but Datawind claim that any web page can be loaded in 7 seconds.

This is supposed to be achieved by Datawind pre-rendering the web pages on their servers (in Canada) and sending them on in a compressed form to the UbiSurfer browser, which then decodes and displays them.
Ok, that sounds fine in theory, but in practice its not so good.
Datawind’s own site ( takes about fifteen seconds to load, and more complex site such a Google Mail can take up to a minute to fully render.
This pre-rendering process also throws up problems when entering data onto a web page. Tabbing between fields can take up to ten seconds.
Obviously these problems don’t occur when using IceWeasel and connecting via a Wifi hotspot.


But, oddly, my biggest complaint about the UbiSurfer browser is about the hideous splash screen which also comes up when you disconnect, thus preventing you from viewing pages offline. Here it is in all its glory.

splashJust look at the smug pissweasel. Those grass stains are going to raise questions in the office after lunch. Luckily you can overwrite the this image with something less irritating.

I shall continue to persevere with this thing, in the hope that I can find a use for it.