Category Archives: Raspberry Pi

Raspberry Pi Endeavours 10 – lighttpd web server

The web page the controls the Pi Car lives on a little webserver running on the Pi, we use lighttpd.

sudo apt-get install lighttpd

This creates a www directory in /var/www with a default index file, which i renamed to make way for our web files. I also added in the cgi-bin full of scripts to control the GPIO pins, and used the linux command ‘chown’ to change the ownership of the directory cgi-bin to www-data.

Lighttpd Config

By default, apparently cgi-bin files should be located under ‘/usr/lib/cgi-bin’, so I told lighttpd’s config file (under /etc/lighttpd/lighttpd.conf) that my cgi-bin files were located in /var/www.

lighttpd.conf

server.modules = (
“mod_access”,
“mod_alias”,
“mod_compress”,
“mod_redirect”,
“mod_rewrite”,
“mod_cgi”
)

server.document-root = “/var/www”
server.upload-dirs = ( “/var/cache/lighttpd/uploads” )
server.errorlog = “/var/log/lighttpd/error.log”
server.pid-file = “/var/run/lighttpd.pid”
server.username = “www-data”
server.groupname = “www-data”
server.port = 8083
index-file.names = ( “index.php”, “index.html”, “index.lighttpd.html” )
url.access-deny = ( “~”, “.inc” )
static-file.exclude-extensions = ( “.php”, “.pl”, “.fcgi” )

compress.cache-dir = “/var/cache/lighttpd/compress/”
compress.filetype = ( “application/javascript”, “text/css”, “text/html”, “text/plain” )

# default listening port for IPv6 falls back to the IPv4 port
include_shell “/usr/share/lighttpd/use-ipv6.pl ” + server.port
include_shell “/usr/share/lighttpd/create-mime.assign.pl”
include_shell “/usr/share/lighttpd/include-conf-enabled.pl”

$HTTP["url"] =~ “/cgi-bin/” {
cgi.assign = ( “” => “” )
}

cgi.assign = (
“.cgi” => “”
)

 

I also ran the command ‘lighty-enable-mod cgi’ that the lighttpd home page instructed me to do, restarted the Pi, and then went to the Pi’s IP address, on the port that I had specified in lighttpd (8083 in this case). The Pi now drives! After changing two wires, the Pi now drives correctly, all thats left is to add the traction-reduction tape to the wheels, and it will be rolling.

 

Raspberry Pi Endeavors 9 – Software

As I get Raspberry Pi 02 ready, here is exactly how I set it up:

  1. Got the latest Raspbian image, and imaged my 8gb Sandisk Extreme SD card with it using Win 32 Disk Imager (as recommended on the Raspberry Pi Downloads page)
  2. Plugged everything into Pi, booted it up.
  3. In Raspi-config, set the overclock to medium – 900MHz.
  4. Set my time zone to London, set the password, enabled SSH, and set it to exapnd_rootfs (on next reboot).
  5. Rebooted
  6. Started an xwindows session, configured WiFi.
  7. Installed software vital to mjpg-streamer (I am unsure if mplayer and VLC are required, I am fairly sure they aren’t – however on previous attempts at getting Pi02 working, I had no luck, however I am fairly sure this was due to a Chrome plugin, see next post about this. See my explained bash history below for everything I did (minus the ls’s etc):
bash_history explained – the commands that I ran 

ping 8.8.8.8                 #Pinged Google to ensure internet connectivity

Update and Upgrade Pi software, then installed third party software. libjpeg8-dev and imagemagick are required for mjpg-streamer. Subversion (SVN) used to get the mjpg files. 
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mplayer
sudo apt-get install vlc
sudo apt-get install subversion
sudo apt-get install libjpeg8-dev
sudo apt-get install imagemagick
sudo reboot -n

Install mjpg-streamer
cd
mkdir Documents
cd Documents/
mkdir code
cd code/

wget http://lilnetwork.com/download/raspberrypi/mjpg-streamer.tar.gz (Thanks to this post for a different source of mjpg-streamer)
tar -xvzf mjpg-streamer.tar.gz
svn co https://mjpg-streamer.svn.sourceforge.net/svnroot/mjpg-streamer mjpg-streamer
cd mjpg-streamer/
cd mjpg-streamer
sudo make
cd ../../
nano mjpg-start.sh
bash mjpg-start.sh           #See this post for mjpg-start.sh script, alter for your file directoy

chmod a+x mjph-start.sh     #forgot to do this at the time!
mkdir static
mv mjpg-start.sh static/
cd static/

Add  the cron job: ‘@reboot bash /home/pi/Documents/code/static/mjpg-start.sh’ 
crontab -e
pwd
sudo shutdown now

Removed the USB hub containing mouse, keyboard and webcam, plugged just the webcam into the Pi (as well as the WiFi adapter in the other port)

Now if you go to your IP address:port that you have set mjpg to use, you should see its homepage. This didn’t work for me this time, but going to the page where just the stream is viewed, worked fine for example: 192.168.0.18:8082/?action=stream

RasTrack

rastrack map of raspberry pi locations

www.rastrack.co.uk

On looking through the new raspi-config (after doing the update and upgrade to the Pi), I found a few new options – including the option to add your Pi’s location to RasTrack - a website where you can see where Pis live all over the world! Also saw the option to enable the Pi camera – hopefully being released soon.

Incognito Chrome

I have done this process a few times recently, with different set up steps trying to get a smooth webcam stream. I have gotten new SD cards, 1amp chargers, tried different webcams and WiFi dongles etc, all to the same laggy or non-existant webcam stream. Today I managed to get the Pi streaming, but after 10 seconds or so it would get more and more laggy until it froze completely. Looking at Chrome’s memory usage, I found it shot from 6mb to 500mb in that time, causing Chrome to lock up and the tab trying to crash. I have never seen this exact behaviour before, but opening the link in an incognito window works fine, leading me to think one of my plugins/extensions may be causing problems.

chrome memory

In an incognito window, the memory used by the page seems to increase from say 1 to 7mb, then straight back down to 1mb, every half a second, forever – which I assume is it loading the new photos, then disposing of old ones. Earlier in a normal window, it seemed to just accumulate more and more without ever throwing any away. Will try and look into why tomorrow, as well as trying to get Pi02 wired up and rolling!

Matt

 

Raspberry Pi Networking Files

More for my reference, as I keep forgetting the files – these are the files you need to edit when connecting to a network – which I have had to do lately when driving the car around work.

/etc/network/interfaces

In the interfaces file, I have set a static IP of 192.168.0.24

auto lo

iface lo inet loopback
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface wlan0 inet static
address 192.168.0.24
netmask 255.255.255.0
gateway 192.168.0.1
wpa-ssid “NETWORK-NAME”
wpa-psk “NETWORK-KEY”

 

/etc/wpa_supplicant/wpa_supplicant.conf

In the wpa_supplicant.conf file, multiple networks settings can be specified, I added my work network in here too, but had to use the field ‘scan_ssid=1′ to indicate it was a hidden network, and put the network name and key in as normal. When you fill in the form to connect to a network in the WiFi Config application, it adds an entry here with your entered data – so a good place to look if you mistyped a value. After returning home, I found that my home network had ‘disabled=1′, and it wouldn’t automatically connect. Without that line, it connects automatically.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid=”NETWORK-NAME”
psk=”NETWORK-KEY”
proto=RSN
key_mgmt=WPA-PSK
pairwise=TKIP
group=TKIP WEP104 WEP40
auth_alg=OPEN
}

 

Matt

Raspberry Pi Endeavors 8 – Headlights and New Interface

Finally got the 20,000 mcd (micro candles?) super bright white LEDs from eBay, and attached them to the solder board in two sets of 3 LEDs. I have wired these up to seperate GPIO pins for now, in case I want to control the left / right headlights separately - but might change this as I’m rapidly running out of GPIO pins.

Headlights on Pi Car

 

I have also got round to redesigning the UI, which previously was all stock HTML buttons, stock jquery slider etc. Designed a few buttons on Fireworks, got some JavaScript to make the hazard warning lights button flash when they are on using setInterval and setTimeout (similar to sleep();).

Updated UI

Updated UI, with headlights on

Matt

Raspberry Pi Endeavors 7 – Hardware Parts List

I started off this project wanting to use the Mr.Basic 4wd microcontroller chassis, but after 2 bad experiences with them having so much resistance that they couldn’t turn their wheels, moved to a slightly beefier 4 motor chassis.  I got both a clear and black acrylic set of these from eBay (China), and have found them both to be of a much higher quality.

Parts list for my project:

Raspberry Pi

Funnily enough, you will need a Raspberry Pi for this. Get one from RS electronics / Amazon / eBay etc. About £25

Raspberry Pi

Raspberry Pi case

This “Environmentally Shell Kit Box For Raspberry Pi” case from oig.buy on ebay is the same style as the clear chassis I got, and provides very easy access to all parts of the Pi whilst protecting the top and bottom. Around £3

Pi Case

“Environmentally Shell Kit Box For Raspberry Pi” – eBay

1A micro USB charging cable

Good for powering the Pi or recharging the mobile phone battery pack that powers the Pi. I picked some up on eBay for £4.50.

Micro USB Charger

 

Webcam

I picked up a couple of Logitech B910′s from Scan’s eBay store for £45 a pop, after using an old Microsoft lifecam, which worked fine, but the quality wasn’t so great, but you can pick them up for about £6.

Hello Kitty Webcam

The Hello Kitty webcam that Nick favoured

 

USB WiFi adapter

Eximax make great USB WiFi adapters that work natively on Mac and Linux (as well as Windows), and we found these to be awesome – as they just worked. It looks like Edimax recently just released a new black version, which I picked up for £15 on eBay. I also got a white, fairly new one that was a dongle plus antenna (all in one), and Nick used a white just antenna model, and all 3 models have been brilliant.

edimax USB wifi

 

12000 mAh mobile phone battery charger

I bought a couple of 12000 mAh mobile phone backup battery chargers to power my Pi when it goes mobile, and they last for somewhere near 8 hours. They are charged by a mini/micro USB cable, and output 1 and 2 amps over USB. Each cost around £17, and came with many little adapters to charge a variety of different phones, including micro USB for the Piberry Ras.

Mobile Charger

 

 

Electronic Components

4wd chassis

Around the £20 mark from China, I got 2 x 4WD, 4 motor chassis. [Clear one] [Black one]

Clear Chassis

Clear Chassis

Black Chassis

Black Chassis

Raspberry Pi Cobbler GPIO Super Start Kit from 4tronix_uk’s eBay store.

Including a huge breadboard, Adafruit cobbler cable and header, switches, resistors, LEDs and jumpers, the Cobbler Super Starter Kit was very useful starter kit full of essentials for messing around with the GPIO pins, and at £22, didn’t break the bank, and no soldering was required.

Cobbler starter kit

Raspberry Pi Cobbler

Including cable and header to extend the GPIO pins of the Pi to the breadboard, very good one made by Adafruit, although others are available such as this split one by Mallinson. I have one of each, but am yet to use the Mallinson one.

Mallinson connector

Mallinson GPIO Connector. Like Adafruit’s, but split at the end.

Breadboard

A 400 point breadboard is sufficient for the cobbler and motor chip.

 

Male to male jumper cables

I got a pack of 65 in my super starter kit, plus more from Maplin. Could make them yourself with bits of wire, but its handy to have the jumpers for quick prototyping.

 

LEDs and resistors

Handy for troubleshooting and emergency lights. I used some 20,000MCD super bright blue LEDs for my emergency lights, with 330 Ohm resistors, but they also seem fine without resistors, and a lot brighter. Waiting on some super bright whites for my headlights.

LEDs

 

L293D motor control chip

I just put the motor chip straight onto the breadboard, straddling the central divide.  They cost about £3 a pop, and one chip can control two separate (sets of) motors, at different speeds, if you have two pulse width modulation outputs on the device (for the Pi, you would have to do one in software).

L239D Chip

Red and black wire

Wire is always helpful with electronics.

8 AA battery holder

To provide power for the motors

8 AA battery holder

 

8 x rechargeable AA batteries

I already had a set of 2600 mAh ‘hama’ AA batteries (cells) and a charger that I got from 7dayshop many moons ago. I managed to find another very similar set on eBay for £13. These high capacity AA cells have kept my Pi driving on and off for a week now, and kept my Xbox 360 controller going for months of light use. Heavily recommend getting rechargeables and a charger – saves the environment and you buying tons of batteries.

Hama battery charger

Other Tools

Pretty handy to have:

  • Wire cutters
  • Wire strippers
  • Pliers
  • Screwdrivers
  • Tape/sticky velcro
  • Multimeter
  • Soldering Iron, solder, safety glasses etc

 

Raspberry Pi Endeavors 6 – Power!

Having only 6 AA batteries for the 4 motors wasn’t enough, and even with my super friction-reducing sellotape, the car struggled to turn on the spot, with 1 or 2 engines  normally stalling. A maximum of 9v was being provided for the 4 6v motors (connected in parallel), resulting in around 2 volts per motor (max) at a quick guess. I grabbed an 8 battery holder from Maplin, hooked this up and the car is much happier now – can turn on the spot well, and goes a bit faster too.

Hazard Lights on

Pi Car 01 with hazard warning lights flashing

As well as my emergency lighting system (ELS), I made a tiny script to simulate hazard warning lights, for when the car is just pulled over at the side of a room. The interface to control the vehicle is very simple – yet functional! When time allows, I will break out the CSS and get it looking a bit fancier.

Pi Car 01 Interface

Pi Car 01 Interface including view from the onboard camera looking out of its garage at the parking marks (sellotape on the floor) and a captivated audience

The two fields at the bottom show the JavaScript and the driving shell script reporting the key down (e.g LEFT), and the key up (all off). This is helpful for diagnosing issues such as when the off instruction arrives before the on one, and so the car starts driving and doesn’t stop. This may be fixable by getting the page to constantly tell the Pi that ‘up’ is held down, and then if the Pi notices an absence of these instructions, it will know to stop.

Matt

Raspberry Pi Endeavors 5 – Building The Car

Unfortunately both Mr.Basic 4wd chassis’ that I got had such high resistance in them, they could barely spin their own wheels, let alone drive along the floor. I got these sent back and went for a larger vehicle made from 2 pieces of acrylic and 4 motors connected directly to 4 wheels, that I picked up from ebay for about £20 [from China].

clear chassis photo

I then followed Adafruit’s guide: Lesson 9. Controlling a DC Motor. This is a great guide to power one motor – to power another, just connect another 2 input wires symmetrically on the other side, same with the outputs to the motor. You also must connect the PWM pin 18 to the EN2 [right hand side PWM pin on the chip] to allow the Pi to control the speed of both motors. If you wanted them to go different speeds, you would have to use software to emulate a PWM pin, and then connect that to the chip – as the Pi only has one PWM pin.

L293D Chip

Picture: Adafruit Learning System (Lesson 9 DC Motor)

I then used a Raspberry Pi Cobbler from Adafruit to gain easy access to all of the Pi’s GPIO pins on a breadboard. On the breadboard, I placed the cobbler, L293D motor chip, and connected them both up with jumper wires, adding the batteries for the motors to the positive and negative rails. [Follow Adafruit’s guide!]

Wiring diagram

Picture: Adafruit Learning System (Lesson 9 DC Motor)

So now the Pi is connected to the breadboard, on which is a motor chip that controls the motors using inputs from the GPIO pins. If your batteries are charged [like mine weren't first time - diagnosed by putting an LED on the motor wires, and it glowing very dimly], you are ready to start asking the motor chip to power the motors. Again, follow the Adafruit instructions that get you putting e.g pin 4 high, which signals the chip to drive the left motor in one direction. You must also set the PWM (value of 0-1024 I believe) that controls the speed of the motor. I use at least 500, but normally closer to 1000 – as the if it is to low, the motors ‘stall’ and wont turn.

All you need now is some software to control it. Me and my housemate use an index.html file hosted on the Pi that calls a bash script whenever a directional key is pressed, then when it is let go, it runs an off script that turns all control pins back to 0. You can also use Python. I used Python to make the script to control the flashing emergency LEDs [superbright blue LEDs].

More soon!

Matt

Raspberry Pi – Networking Script Change

Made some modifications to my script – made a function for writing to the file, which saved a lot of reused code, and instead of assuming that it could ping Google if the result wasn’t ‘unknown host www.google.co.uk’ – changed it to detect the words ‘ bytes from’ – from ’64 bytes from x’.

Find the code at: http://www.matthewrobbins.co.uk/files/connectiontest.py

Next up – try and make a script that detects if mjpg streamer is actually broadcasting video – if not, restart the service (this can fix issues that arise if the webcam is plugged in after boot)

Matt

On the BBC News! – Raspberry Pi Cambridge Event

A few weeks ago, I shot up to Cambridge to help out with a Raspberry Pi event at Chesterton Community College where Raspberry Pi founder Eben Upton took great pleasure in announcing that they would be providing 15,000 Raspberry Pi kits to school children in the UK, through a grant from Google.

Me and a Pi

Chatting to a group of primary school children about what the Pi is.

I think this is a fantastic initiative, and the RPi is the perfect, standard, versatile and powerful bit of kit to get people learning real computer modules – from Java and Python programming, use of Unix operating systems and their services, to lower level component control with the GPIO pins and SPI and I2C interfaces. I really do hope that schools adopt the Pi, and get students introduced to programming and real computer science so that they can become interested in the subject area if they wish, and start pursuing cool projects.

Matt

Raspberry Pi Endeavors 4 – Hardware

A week or so ago, I decided to my own version of Google Glass by selotaping the Pi, webcam and Nick’s battery pack to my hat. Here is the result:

Glass Hat

I also received my Mr.Basic 4wd chassis, which I discovered was faulty, after I had built it.

The kit:

Basic Kit

Partially built:

Half built

Built with motors:

Built Mr Basic

It looks like a really promising design, so I have ordered another one from Bizoner in China, they seem quite hard to get hold of around here. Also ordered a slightly larger one for Nick. Pictures to be posted when they arrive!

We also grabbed a couple of RC cars from Argos, and I proceeded to take mine apart to discover what kind of voltage the motors were using etc:

RC Car being held by third hands, being measured by a multimeter

Matt