StuckPI
Posts: 15
Joined: Sat Mar 12, 2016 7:31 pm

Code for PAIRING Pi-mote Energenie 4 sockets

Wed May 04, 2016 4:55 pm

This code is so you can PAIR 4 sockets using the pi-mote. There is an error on socket 4 OFF, until i fix that you will need alternative code at end of post to correctly control them after being paired.
Pi: Raspberry PI 2B+ Also tested on Raspberry PI 3.
OS: Jessie Lite (update May 2016)
Device: ENER002-2PI.

Pre-requisites: gpi-gpio . energenie .
Pi mote wired on jumper leads to T-Cobbler.
(Credits for the first 2 belong to energenie, who wrote the original script, I simply corrected it, and added the correct codes to add sockets 3 and 4 if you want 4 sockets controlled by a pi, as its impossible to find the code online via google (at least i found it impossible)
NOTE: socket 4 OFF is commented out, this is because for some reason its code is too close to 3, other codes will work but its a pain looking for them. In the end i managed to find enough codes to pair the devices to all 4 sockets.
Here is the code:

Code: Select all

#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 1110 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 0111 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 socket 3 ON code')
		# Set K0-K3
		print "sending code 1011 socket 3 on"
		GPIO.output (11, True)
		GPIO.output (15, False)
		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 3 OFF code')
		# Set K0-K3
		print "sending code 1010 Socket 3 off"
		GPIO.output (11, True)
		GPIO.output (15, False)
		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 4 ON code')
		# Set K0-K3
		print "sending code 1100 socket 4 on"
		GPIO.output (11, False)
		GPIO.output (15, False)
		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)

#       There is an error in this 'OFF' code,         
#		raw_input('hit return key to send socket 4 OFF code')
#		# Set K0-K3
#		print "sending code 1011 socket 4 off"
#		GPIO.output (11, True)
#		GPIO.output (15, False)
#		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 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()
I then used https://pythonhosted.org/energenie/ to correctly control all 4 sockets (on and off)

Additional: I now have 8 sockets controlled by the pi, I am only learning its the best i can do for now as i couldnt find the answer i thought it should be here, because others might have the same problems with the pairing.
I discovered the pi-motes can be paired on a single pi without interfering with each other, there are just enough pin outs on the 40 to do it. (Hope it helps someone.)

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Code for PAIRING Pi-mote Energenie 4 sockets

Wed May 04, 2016 6:50 pm

StuckPI wrote: (Credits for the first 2 belong to energenie, who wrote the original script, I simply corrected it, and added the correct codes to add sockets 3 and 4 if you want 4 sockets controlled by a pi, as its impossible to find the code online via google (at least i found it impossible)
NOTE: socket 4 OFF is commented out, this is because for some reason its code is too close to 3, other codes will work but its a pain looking for them. In the end i managed to find enough codes to pair the devices to all 4 sockets.
The control codes for turning 4 separate remote sockets on and off are documented by Energenie both in the python script ENER002-2PI.py and in the PDF user guide - https://energenie4u.co.uk/res/pdfs/ENER314%20UM.pdf

According to those sources "1101 and 0101 socket 3 ON and OFF" and "1100 and 0100 socket 4 ON and OFF"

The PiMote board and the Energenie sockets are now supported in the gpiozero python module - http://gpiozero.readthedocs.io/en/v1.2. ... #energenie In my tests the gpiozero/Energenie combination was able to pair with my single switch to use any one of the 4 'channels' available.

richardluney
Posts: 3
Joined: Mon Nov 28, 2016 12:23 pm

Re: Code for PAIRING Pi-mote Energenie 4 sockets

Mon Nov 28, 2016 1:57 pm

I was unable to get the version of Python that I had to work properly using your script and the Pi-mote. I have been writing shell scripts for the past twenty years and thought that I could use your script as inspiration and develop my own shell script.

So I converted your code into a bash shell script and added some interactive control and also the ability to pass command line arguments. This script can be instructed to turn any socket on or off. In interactive mode the script will loop round asking for a socket number and then whether to turn it on or off. The loop is terminated when the user selects socket 0.

Arguments may be passed to the script and the screen output sent to /dev/null when automating the switching of the P-mote sockets.

e.g. ./energenie-a.sh 3 y > /dev/null

This will turn on socket 3 and dump the screen responses into /dev/null.

If anybody feels that this script would be useful I will upload it to this site.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Code for PAIRING Pi-mote Energenie 4 sockets

Tue Nov 29, 2016 12:18 pm

If anybody feels that this script would be useful I will upload it to this site.
As an interested party in this thread, can I say that demo/sample code often turns out to be of value to someone at sometime in the future.

So thanks for your efforts, and I would encourage you to publish what you have done via a blog post, a github gist, or a posting to these forums.

(Since this is a Python thread, adding a shell script here might not be appropriate.)

richardluney
Posts: 3
Joined: Mon Nov 28, 2016 12:23 pm

Re: Code for PAIRING Pi-mote Energenie 4 sockets

Sun Dec 04, 2016 4:13 pm

B.Goode wrote:
If anybody feels that this script would be useful I will upload it to this site.
As an interested party in this thread, can I say that demo/sample code often turns out to be of value to someone at sometime in the future.

So thanks for your efforts, and I would encourage you to publish what you have done via a blog post, a github gist, or a posting to these forums.

(Since this is a Python thread, adding a shell script here might not be appropriate.)
You are quite right about a shell script not being appropriate in a Python thread. I have created a new thread in the "Projects" forum where I have published the script and the code for the web page.

Thank you for the advice.

Return to “Python”