433MHz Mains Sockets via Raspberry Pi

I wanted to join the crazy work of home automation (which currently has the buzzword-compliant marketing name of "Internet of Things"). So I picked up some cheap radio-controlled sockets from Maplin and some 433MHz transmitters.

Wiring was straightforward: 3.3V to VCC, ground to ground, and a GPIO pin to data.

A bit of soundcard-oscilloscope sniffing and I soon knew all the possible codes that the remote control could send. Codes below are all to turn on (off is final bit replaced by 0). The "channel" is set on the socket and remote to be I-IV. The "device" is the button on the remote (set on the socket), and is 1-4.

  I/1 101010001010101010101
  I/2 101010100010101010101
  I/3 101010101000101010101
  I/4 101010101010001010101

 II/1 10001010001010101010101
 II/2 10001010100010101010101
 II/3 10001010101000101010101
 II/4 10001010101010001010101

III/1 10100010001010101010101
III/2 10100010100010101010101
III/3 10100010101000101010101
III/4 10100010101010001010101

 IV/1 10101000001010101010101
 IV/2 10101000100010101010101
 IV/3 10101000101000101010101
 IV/4 10101000101010001010101

Which meant all I needed was a small bit of Python code to send the appropriate signals:

#!/usr/bin/python

import RPi.GPIO as GPIO
import sys
import time

BITLENGTH = 0.00172275
ZERO = ( 0.000477, BITLENGTH - 0.000477 )
ONE = ( 0.001357, BITLENGTH - 0.001357 )
FUDGE = 0.00005
GAP = 0.01209725
channel = int( sys.argv[ 1 ] )

GPIO.setmode( GPIO.BCM )
GPIO.setup( channel, GPIO.OUT )

for i in range( 10 ):
    for ch in sys.argv[ 2 ]:
        if ch == '0':
            GPIO.output( channel, 1 )
            time.sleep( ZERO[ 0 ] - FUDGE )
            GPIO.output( channel, 0 )
            time.sleep( ZERO[ 1 ] - FUDGE )
        elif ch == '1':
            GPIO.output( channel, 1 )
            time.sleep( ONE[ 0 ] - FUDGE )
            GPIO.output( channel, 0 )
            time.sleep( ONE[ 1 ] - FUDGE )
    time.sleep( GAP - FUDGE )