Monday 31 March 2014

Raspberry Pi LAMP Server

This is a simple guide to setting up a Raspberry Pi computer as a 'LAMP' (Linux, Apache MySQL, PHP) server.

I'll be using it at a later date to create a simple PHP-based To-Do list type system, which I'll cover in later posts.

Download and install the latest version of Raspbian from the Raspberry Pi website.
As I'm using linux, the command I needed was:

sudo dd bs=4M if=2014-01-07-wheezy-raspbian.img of=/dev/sdh

Boot up the Pi and go through initial setup:
  • Expand filesystem
  • Change user password
  • Boot to console
  • Enable SSH Server
  • Force audio through 3.5mm
  • Update tool to latest version
Finish the setup tool and from the command line run updates:

  • sudo apt-get update
  • sudo apt-get upgrade
Install the LAMP stack:

  • sudo apt-get install apache2 php5 mysql-client mysql-server
During this installation process you'll be asked to provide a root password for the MySQL server.
(Optional) Install PHPMyAdmin

  • sudo apt-get install phpmyadmin

This should boot the PHPMyAdmin installer tool:
  • In the first screen select the web server to configure (apache2).
  • Select yes to configure database for phpmyadmin with dbconfig-common.
  • Enter the root MySQL password. Create a phpmyadmin password for the sql server.
Now would be a good time to save the progress made. Shutdown the Pi with
  • sudo shutdown -h now
Once the systems shutdown, remove the memory card and make a backup of the cards data. I use the dd command again to create a gzipped image

  • dd if=/dev/sdh | gzip > /path/to/backups/backupname.img.gz

This allows backups to be restored just as easily, using the command

  • gunzip -c /path/to/backups/backupname.img.gz | dd of=/dev/sdh
If you want to run the Pi 'headless' - without screen or keyboard, now would be a good time to disconnect them.

Once the backup is done, put the SD card back in the Pi, and reboot.
Give it a little while to reboot, and then from your P, connect to it via SSH. On linux this is a command like

  • ssh HostnameOrIP -l pi
Once logged in, you can control the Pi from the command line as you were able to previously.

Monday 24 March 2014

Motion activated Pac-Man lamp

Version 2

Migrated the control over to an ATtiny25, using the ATtiny cores for Arduino.
I found a ported version of the IRRemote library on Github. It looks as though this port was for a specific purpose, and lacked the IR codes I needed, however it was easy enough to port the necessary parts from the orignal IRRemote library.

This is the revised code:

#include "IRremoteTiny.h"

IRsend irsend;

#define PIR PB2 // pin 7 on ATtiny

void setup(void) 
  DDRB &= ~(_BV(PIR)); // Set as input
  DDRB |= _BV(PB3);


void loop(void) {
  if (digitalRead(PIR) == HIGH) {
    digitalWrite(PB3, HIGH);
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0xF7C03F, 32);
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0xF740BF, 32);
  } else {
    digitalWrite(PB3, LOW);

Version 1
I was given a Pac-Man lamp for Christmas.
It's a simple RGB colour changing lamp that is remote controlled.

In my flat the hallway can be quite dark, and when leaving, there can be a few seconds between turning the flat's hall light off, and the external hall light being triggered.

I was considering getting a simple plug-in 'night light' type thing, but I didn't want something that stayed on all the time. And besides, I wanted to put Pac-Man to good use.

As it was a Christmas gift, it didn't seem right pulling it apart straight away, so the plan is to create a motion-triggered device that mimics the remote.

The first thing is to find the remote protocol being used.
To do this use a IR receiver module (I scavenged one from a VCR that was on the junk pile), and hook it up to an Arduino, using Ken Shirriff's arduino IR library. Using the 'IRrecvDump' sample sketch you can find the details of the protocol in the serial monitor - it'll look something like

Decoded NEC: F7C03F (32 bits) Raw (68): 15538 8950 -4200 700 -400 700 -400 650 -450 700 -400 700 -400 650 -450 650 -450 650 -450 650 -1500 700 -1500 700 -1500 650 -1550 650 -450 650 -1500 700 -1500 700 -1500 700 -1500 700 -1500 650 -450 650 -450 650 -400 700 -400 700 -400 700 -400 700 -400 700 -400 650 -1550 650 -1500 700 -1500 700 -1500 700 -1500 700 -1500 650 F740BF

Decoded NEC: F740BF (32 bits) Raw (68): 26860 8950 -4200 700 -400 700 -400 650 -400 700 -400 700 -400 700 -400 700 -400 650 -450 650 -1550 650 -1500 700 -1500 650 -1550 650 -450 650 -1500 650 -1550 700 -1500 650 -400 700 -1500 700 -400 700 -400 650 -450 650 -400 700 -400 700 -400 700 -1500 650 -450 650 -1550 650 -1500 700 -1500 650 -1550 650 -1500 700 -1500 650

My first thought that the pacman remote looks very similar to the remotes that tend to get bundled with rolls of RGB leds, and I had a couple of those going spare, so perhaps one of them could've been sacrificed, however performing the same test with those showed that they use a different protocol, and so wouldn't work.

The second thought was to make use of an old Sky TV remote - they have a feature that allows the protocol to be programmed to control several makes of TV, but after a while trying various codes, it appeared that the one I was after wasn't one of them.

So the final option was to use the arduino itself for sending the signal. It seemed a bit overkill, but I can always migrate it to a smaller microcontroller when I have one spare. The IR led was taken from the old Sky remote.

The next stage is to wire in the PIR sensor (motion detector)

I used one of these:

They're a simple 3 pin setup - Vcc, Ground, and Data. Data simply goes high for a few seconds when motion is detected.
It can be easily wired in, just effectively like a simple button.

Here's the code I used - when motion is detected, turn the lamp on for 10 seconds, then off again.

#include <IRremote.h>

IRsend irsend;

void setup()
  pinMode(13, OUTPUT);

void loop() {
  if (digitalRead(4) == HIGH) {
    digitalWrite(13, HIGH);
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0xF7C03F, 32);
    for (int i = 0; i < 3; i++) {
      irsend.sendNEC(0xF740BF, 32);
  } else {
    digitalWrite(13, LOW);

Wednesday 19 March 2014

Telephone audio interface

I work from home, and my job requires I spend a lot of time on the phone, particularly in conference calls.

I could easily go and just buy a new headset or hands free desk phone, but I already have a general purpose headset that I enjoy using, and frankly, I have accumulated a lot of headsets over the years from various phones, consoles, computers etc.

One of the items I had in my junk bin was an old wall-mounted telephone - the kind where the keypad is on the handset.

Taking the phone apart revealed three PCBs - one in the base unit of the phone, and 2 in the phone itself. One is obviously the keypad, and the other appears to be an amplifier - it was on this board that the microphone and speaker was attached.

Making the connections - the main part of the interface, is really very simply. Just cut away the microphone and speaker, and replace them with connectors.

The next step is the hang up button. Hang up buttons are press-to-break - pressing the button breaks the circuit, so as it stands connecting this to the phone line will result in your line being open.

To add extra complexity on the phone I was using the hang up button was a Double Pole Single throw switch (DPST) - this means that the switch effectively controls two circuits.
When closed, the switch connects the 4 contacts (seen de-soldered above) as follows: 1 & 2, 3 & 4

Fortunately I managed to find a 'normal' DPST switch on an old amplifier PCB in my junk bin, so wired that in place.

Also, in order to make the end product neater, I removed the coiled cable between the base and handset, and cut away the keypad - I can always use my existing phone to dial. Besides, the keypad may prove useful in a future project.

All that remained was to create a new case for it. I was able to recycle an old business card box, and 3D printed a basic front panel for the connectors.

The finished box, just connect an audio source to input, and some headphones/speaker to output, and away you go.

Monday 10 March 2014

3D printed cable tidy

Update 28/12/2017
I've revised the design of the cable tidy, and it's now up on Thingiverse.
The post covering the new design and why I created it can be found here.

For the purposes of testing the 3D printer, I'd just grabbed some files from Thingiverse.

This, however, is my first attempt at 3D printing something of my own design.

The next big choice is to select design software. As I come from more of a programmer background than design, I settled on OpenSCAD.This allows you to design objects with script, rather than manipulate the objects directly.

Once designed, the file is rendered, and can then be exported to STL format, which can in turn be used by Slic3r (can be done from within RepetierHost) to convert the STL to Gcode, which is then passed to the printer.

The inner piece - the cable will pass through the middle channel,
The outer piece - a bolt will run through the central hole to mount the inner piece.

The idea is the cable runs through the gap on the edge of the outer piece, through the middle channel of the inner piece, and out the other side of the out piece. So when the inner piece is rotated, the cable is wound around it.

Once printed, I attached a piece of clear plastic to the top to complete the enclosure (I could've 3D printed that, but as it's a flat plastic disc, it seemed wasteful to do so).

Mounting that was more of a pain that I'd thought. Hot glue alone had too much flexibility, so I added in a few miscellaneous screws to provide additional support (This was also worsened by trying to tidy a fairly thick USB charger cable - this tidy is best left to thin cables, such as headphones)

Some Sugru around the edges of the plastic finished the job:
The finished job - twist the blue edges to retract the cable

A few more lessons learned:
  • Remember RepetierHost has a pause button - handy if you need to switch rolls of filament mid-print.
  • Avoid needing to change rolls mid print in the first-place!
  • Make sure the bed is heated before starting to print, it's more important than you think.
  • Give thought to the integration of non 3D printed parts, make sure mounting holes are there if necessary.

Sunday 9 March 2014

K8200 3D Printer review

Years ago, back when 3D printing was pretty much limited to the RepRap, I had this grand idea of building a 3D printer.

Problem was, I was fairly new to electronics, and DIY in general, so my skillset was, well, non existent.

Despite the failure of that project, I was still keen on the technology, and have been keeping a close eye on the commercial offerings. The downside to those options being both the price, and buying an 'off-the-shelf' printer seemed like the lazy way out to me.

Thankfully, Maplin has started selling the Velleman K8200 printer as a kit. This keeps the price to a more modest level, and being self-assembly, reduced my laziness guilt of not building one from scratch.

In the post-Christmas sales, I noticed the kits were on offer, so I finally caved and bought one.

I was planning to do a series of build posts about it here, but the truth is there's nothing much to report that isn't already covered in detail - the manual is a comprehensive 619 pages!

First attempted print - a Mobius Strip from Thingiverse

A couple of pointers I will offer though:

  • The leads on the thermistor for the hot end are very fragile. The guide does warn about it, but it's worth reiterating...
  • You'll have some bits left over. At least I did. Not sure if you're meant to, but mine works OK.
  • It won't work perfectly first time. Expect to have to tinker with it from time to time. Also, be prepared to lose a fair bit of filament in failed prints to start with. 
  • When putting the X carriage inside the frame (page 96 on my version of the manual), there's a picture that shows the carriage fully in the frame. The picture appears too early - the subsequent pages mention about sliding additional nuts into the frame, and actually means slide the first supporting rod of the x carriage into the frame, add the nuts, then the second supporting rod.

Thursday 6 March 2014

Quick Update

This is some code for an Arduino Leonardo that will receive character data from the serial pins, then echo the character through USB as keyboard input to the PC.
This will form the basis of a bigger project that'll be the subject of another post, but for now, here's the code:

void setup() {

void loop() {
  if (Serial1.available() > 0) {
    byte inChar =;