Web controlled plug sockets

Programming the Energenie plug sockets with a Raspberry Pi.

***DISCLAIMER :: I may be doing this wrong***

Problem: Sometimes I want to use my PC remotely but don’t want to leave it running 24/7 ’cause that’s bad news for my pocket and bees and shit.

Solution: Interwizzardry, pi(e)s and huge security holes.


Steps:

  1. Buy some remote controlled plug sockets
  2. Write a script to turn them on and off and on again
  3. Build a web page that runs that script

Easy!


OK. Probably not that easy. I’ll document what I do as I go.


Step One (a)

81h0OkQVADL._SX355_

Probably the easiest step. Bought a pair of Energenie plugs from Amazon which includes a Raspberry Pi add-on board in the box.

Here’s some more pictures.

Step One (b)

First off you need to download a python script to program the sockets. You can download it here or copy/paste it from the box below.


#import the required modules
import RPi.GPIO as GPIO
import time

# set the pins numbering mode
GPIO.setmode(GPIO.BOARD)

# Select the GPIO pins used for the encoder K0-K3 data inputs
GPIO.setup(11, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)

# Select the signal to select ASK/FSK
GPIO.setup(18, GPIO.OUT)

# Select the signal used to enable/disable the modulator
GPIO.setup(22, GPIO.OUT)

# Disable the modulator by setting CE pin lo
GPIO.output (22, False)

# Set the modulator to ASK for On Off Keying
# by setting MODSEL pin lo
GPIO.output (18, False)

# Initialise K0-K3 inputs of the encoder to 0000
GPIO.output (11, False)
GPIO.output (15, False)
GPIO.output (16, False)
GPIO.output (13, False)

# The On/Off code pairs correspond to the hand controller codes.
# True = '1', False ='0'

print "OUT OF THE BOX: Plug the Pi Transmitter board into the Raspberry Pi"
print "GPIO pin-header ensuring correct polarity and pin alignment."
print ""
print "The sockets will need to be inserted into separate mains wall sockets."
print "with a physical separation of at least 2 metres to ensure they don't"
print "interfere with each other. Do not put into a single extension lead."
print ""
print "For proper set up the sockets should be in their factory state with"
print "the red led flashing at 1 second intervals. If this is not the case for"
print "either socket, press and hold the green button on the front of the unit"
print "for 5 seconds or more until the red light flashes slowly."
print ""
print "A socket in learning mode will be listening for a control code to be"
print "sent from a transmitter. A socket can pair with up to 2 transmitters"
print "and will accept the following code pairs"
print ""
print "0011 and 1011 all sockets ON and OFF"
print "1111 and 0111 socket 1 ON and OFF"
print "1110 and 0110 socket 2 ON and OFF"
print "1101 and 0101 socket 3 ON and OFF"
print "1100 and 0100 socket 4 ON and OFF"
print ""
print "A socket in learning mode should accept the first code it receives"
print "If you wish the sockets to react to different codes, plug in and"
print "program first one socket then the other using this program."
print ""
print "When the code is accepted you will see the red lamp on the socket"
print "flash quickly then extinguish"
print ""
print "The program will now loop around sending codes as follows when you"
print "hit a key:"
print "socket 1 on"
print "socket 1 off"
print "socket 2 on"
print "socket 2 off"
print "all sockets on"
print "all sockets off"
print "Hit CTL C for a clean exit"
try:
# We will just loop round switching the units on and off
while True:
raw_input('hit return key to send socket 1 ON code')
# Set K0-K3
print "sending code 1111 socket 1 on"
GPIO.output (11, True)
GPIO.output (15, True)
GPIO.output (16, True)
GPIO.output (13, True)
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(0.25)
# Disable the modulator
GPIO.output (22, False)

raw_input('hit return key to send socket 1 OFF code')
# Set K0-K3
print "sending code 0111 Socket 1 off"
GPIO.output (11, True)
GPIO.output (15, True)
GPIO.output (16, True)
GPIO.output (13, False)
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(0.25)
# Disable the modulator
GPIO.output (22, False)

raw_input('hit return key to send socket 2 ON code')
# Set K0-K3
print "sending code 1110 socket 2 on"
GPIO.output (11, False)
GPIO.output (15, True)
GPIO.output (16, True)
GPIO.output (13, True)
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(0.25)
# Disable the modulator
GPIO.output (22, False)

raw_input('hit return key to send socket 2 OFF code')
# Set K0-K3
print "sending code 0110 socket 2 off"
GPIO.output (11, False)
GPIO.output (15, True)
GPIO.output (16, True)
GPIO.output (13, False)
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(0.25)
# Disable the modulator
GPIO.output (22, False)

raw_input('hit return key to send ALL ON code')
# Set K0-K3
print "sending code 1011 ALL on"
GPIO.output (11, True)
GPIO.output (15, True)
GPIO.output (16, False)
GPIO.output (13, True)
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(0.25)
# Disable the modulator
GPIO.output (22, False)

raw_input('hit return key to send ALL OFF code')
# Set K0-K3
print "sending code 0011 All off"
GPIO.output (11, True)
GPIO.output (15, True)
GPIO.output (16, False)
GPIO.output (13, False)
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(0.25)
# Disable the modulator
GPIO.output (22, False)

# Clean up the GPIOs for next time
except KeyboardInterrupt:
GPIO.cleanup()

The steps are:

  1. Run the script, but don’t press any keys yet
  2. Put the first socket you want to program into programming mode (see video below if you have problems)
  3. Press RETURN. The socket accepts the code and drops out of programming mode.
  4. Press RETURN again. This should switch off the socket
  5. Put the second socket into programming mode
  6. Press RETURN. The socket accepts the code and drops out of programming mode.
  7. Press RETURN again. This should switch off the socket
  8. Press RETURN and both sockets should come on
  9. Press RETURN and both sockers should go off

ARRRRRGGHHH. Right. This did my head in for about two hours. The instructions for setting the socket to program mode are rubbish. It say hold the button until it flashes. You have to hold the button until it flashes quickly.


Step Two

I’ve got four scripts in total, two to turn on the sockets and two to turn them off. I find this simplifies things and keeps my naming easy. (switch_on_pc.py, switch_off_pc.py, switch_on_hifi.py, switch_off_hifi.py).

The following is switch_on_pc.py. You can pull apart the socket programming script above to create the other scripts as needed.

#import the required modules
import RPi.GPIO as GPIO
import time

# set the pins numbering mode
GPIO.setmode(GPIO.BOARD)

# Select the GPIO pins used for the encoder K0-K3 data inputs
GPIO.setup(11, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)

# Select the signal to select ASK/FSK
GPIO.setup(18, GPIO.OUT)

# Select the signal used to enable/disable the modulator
GPIO.setup(22, GPIO.OUT)

# Disable the modulator by setting CE pin lo
GPIO.output (22, False)

# Set the modulator to ASK for On Off Keying
# by setting MODSEL pin lo
GPIO.output (18, False)

# Initialise K0-K3 inputs of the encoder to 0000
GPIO.output (11, False)
GPIO.output (15, False)
GPIO.output (16, False)
GPIO.output (13, False)

# Plug 1 On
GPIO.output (11, True)
GPIO.output (15, True)
GPIO.output (16, True)
GPIO.output (13, True)
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(0.25)
# Disable the modulator
GPIO.output (22, False)

#Clean up GPIO for next time
GPIO.cleanup()

Step Three

*** Add webby bit ***

Set up Apache/PHP on my pi. No point going into it here.

https://www.raspberrypi.org/documentation/remote-access/web-server/apache.md