djjazz
Posts: 26
Joined: Fri Jan 18, 2013 8:12 pm

Tutorial: Elro remote power switch without root

Sun Feb 03, 2013 9:22 am

After getting a great deal of help on my issues (GPIO and root (again)) I like to share with you my insides on how to switch remote power switches without root permission. Please keep in mind: I’m Noob to Linux, Python (this was my first script) and electronics (did not touch a soldering iron in the last 20 years). I’m sure there a lot of ways doing this in a better way...

Objective:
My objective was to control a remote power switches via an Web Interface. This tutorial will guide you to the hardware and software required to create a class for switching a Elro remote controls without root permission. The enclosed file is build for command line use but could easily but adapted for the use as a CGI script on a web server. As the solution is supposed to be cheap and easy to build I avoided add-on chips (as an Arduino). Downside of this decision is the lack of reliability of the solution. Sometimes the Pi is not switching. I assume that the root cause for this behavior is the non-realtime behavior of the Pi.

Requirements:

General:
I assume you know more about permission in Linux than I do (which is easy)

Hardware:
- Elro Remote Control Switches AB440S (I assume you could use other switches but you need to modify the python code to adjust for different sending protocols)
- Transmitter for 433Mhz (I used RF Wireless Transmitter and Receiver Link Kit Module 433Mhz for Arduino DIY but there are a number of different transmitters looking very similar which might work as well. I used Hoagies instruction to build the transmitter system but basically I connect ‘VCC’ to 5V (pin #2), 'Data’ to BCM GPIO 17 (pin #11) and ‘GND’ to GND (pin #6). You could connect 'Data' to another input/output pin but for this tutorial I assume you use 17. From my understanding you should use protection circuits to protect your Pi from being fried but I ignored that.

Software:
Install WiringPi:
WiringPi is a library to access the GPIO interface of the Pi.
I installed the library in /usr/local/src:
Open a terminal and enter:

Code: Select all

cd /usr/local/src
Installation instruction:

Code: Select all

sudo apt-get install git-core
sudo git clone git://git.drogon.net/wiringPi
cd wiringPi
sudo ./build
Install WiringPi-Python
WiringPi-Python is an Python interface to the WiringPi Library.
Installation Instruction

Code: Select all

sudo apt-get install python-dev python-setuptools
sudo git clone https://github.com/WiringPi/WiringPi-Python.git
cd WiringPi-Python
sudo git submodule update --init
sudo python setup.py install
A prerequisite for accessing the GPIO pins in Python without root permission is that you do export the required pin(s) and define it as output before using it (gpio export <pin#> out). To avoid doing this manually each time the Pi is booted I added the command to rc.local.

Code: Select all

sudo nano /etc/rc.local
Finde the line "exit 0" and add the following line before:
sudo -u pi /usr/local/bin/gpio export 17 out

Close and save file afterwards. If you have not connected the transmitter to pin 17 you should change the pin number. Please be aware that the gpio export is just working for the specified user (in this case “pi”). If you use a different user or you want to run the script as CGI script you should change the user name. For the server you would need to modifiy the line to:

sudo -u www-data /usr/local/bin/gpio export 17 out

For the attached script I do assume that you run the script with a user who does have GPIO pin exported.

If you now reboot the Pi you should be ready to run the python script

Code: Select all

sudo reboot
Save this code as "elro_wiringpi.py" to a path of your choice:

Code: Select all

#!/usr/bin/env python
"""
For switching Elro wall plugs using Python on Raspberry Pi with wiringPi Library. 
from Dirk J. 2013

Requirements:
-WiringPi-Python Library
-433 Mhz Transmitter connected
-Export of GPIO port: gpio export <pin> out (pin=Broadcom GPIO number. Pin with connection 
 to 'data' of an 433Mhz Transmitter)

Example
$ gpio export 17 out      # Exports pin 17 (e.g. in /etc/rc.local)
$ ./elro_wiringpi.py 8 1  # Switch D is turned on

This file uses wiringPi to output a bit train to a 433.92 MHz transmitter, allowing you
to control light switches from the Elro brand (AB440S). You need to export the pin before starting the
script. It does not require root permission

Credits:
This file is a just slightly modified version of "elropi.py" from by Heiko H. 2012:
	http://pastebin.com/aRipYrZ6
 It is changed to run without root by using WiringPi-Python instead of the RPi.GPIO library.
C++ source code written by J. Lukas:
	http://www.jer00n.nl/433send.cpp
and Arduino source code written by Piepersnijder:
	http://gathering.tweakers.net/forum/view_message/34919677
Some parts have been rewritten and/or translated.

This code uses the Broadcom GPIO pin naming.
For more on pin naming see: http://elinux.org/RPi_Low-level_peripherals

Version 1.0
"""

import time
import wiringpi

class RemoteSwitch(object):
	repeat = 10 # Number of transmissions
	pulselength = 300 # microseconds
	
	def __init__(self, unit_code, system_code=[1,1,1,1,1], pin=17):
		''' 
		devices: A = 1, B = 2, C = 4, D = 8, E = 16  
		system_code: according to dipswitches on your Elro receivers
		pin: according to Broadcom pin naming
		'''		
		self.pin = pin 
		self.system_code = system_code
		self.unit_code = unit_code
		
	def switchOn(self):
		self._switch(wiringpi.HIGH)

	def switchOff(self):
		self._switch(wiringpi.LOW)

	def _switch(self, switch):
		self.bit = [142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 136, 128, 0, 0, 0]		

		for t in range(5):
			if self.system_code[t]:
				self.bit[t]=136	
		x=1
		for i in range(1,6):
			if self.unit_code & x > 0:
				self.bit[4+i] = 136
			x = x<<1

		if switch == wiringpi.HIGH:
			self.bit[10] = 136
			self.bit[11] = 142
				
		bangs = []
		for y in range(16):
			x = 128
			for i in range(1,9):
				b = (self.bit[y] & x > 0) and wiringpi.HIGH or wiringpi.LOW
				bangs.append(b)
				x = x>>1
				
		wiringpi.wiringPiSetupSys()
		wiringpi.pinMode(self.pin,wiringpi.OUTPUT)
		wiringpi.digitalWrite(self.pin,wiringpi.LOW)
		for z in range(self.repeat):
			for b in bangs:
				wiringpi.digitalWrite(self.pin, b)
				time.sleep(self.pulselength/1000000.)
		
		
if __name__ == '__main__':
	import sys
	
	# Change the system_code[] variable below according to the dipswitches on your Elro receivers.
	default_system_code = [1,1,1,1,1] 
	
	# change the pin accpording to your wiring
	default_pin =17

	if len(sys.argv) < 3:
		print "usage: python %s int_unit_code int_state (e.g. '%s 2 1' switches unit 2 on)" % \
			(sys.argv[0], sys.argv[0])  
		sys.exit(1)

	device = RemoteSwitch(  unit_code= int(sys.argv[1]), 
							system_code=default_system_code, 
							pin=default_pin)

	if int(sys.argv[2]):
		device.switchOn()
	else: 
		device.switchOff()
Make sure elro_wiringpi.py is set as executable:

Code: Select all

chmod u+x elro_wiringpi.py
Before running the script you need to make sure to adjust script to reflect the system code of your Elro switches. Use a screwdriver to open your remote switches to check for the system/home code (combination of five 0s and 1s) and the unit code (A, B, C, D or E). Now you need to change the hard-coded system code in the script:

Code: Select all

sudo nano elro_wiringpi.py 
Navigate to row 96 and change the value of "default_system_code" to represent your unit code. If you have changed the pin you should adjust the number of "default_pin" in row 99 to match your pin. Afterwards save and close the file. Now you are ready to switch. Run the script with:

Code: Select all

./elro_wiringpi.py <unit> <on/off>
<unit>: A=1, B=2, C=4, D=8, E=16
<on/off>: on=1, off=2
Example

Code: Select all

./elro_wiringpi.py 4 1
To switch unit C on.

If you want to switch using a web server it should be easy for you to use the RemoteSwitch class in a CGI script (works well for me). Just make sure you get permissions for the GPIO export and the script file right.

Credits:
The script is based on the work of Heiko H. on elropi.py.
Thanks for all support I got from the users in this forum.

elmicha
Posts: 28
Joined: Tue Jul 03, 2012 4:43 pm

Re: Tutorial: Elro remote power switch without root

Thu Jul 11, 2013 7:33 pm

Thank you for your great tutorial and the script! It works great with my Elro AB440 outlets.

realkibou
Posts: 1
Joined: Tue Jul 23, 2013 5:24 pm

Re: Tutorial: Elro remote power switch without root

Tue Jul 23, 2013 5:25 pm

I get only command not found, after I typed in for example elro_wiringpi.py.

I have the newest version of rasbmc and installed everything as in your manual.

User avatar
with ice cream
Posts: 204
Joined: Mon Jul 30, 2012 7:25 am

Re: Tutorial: Elro remote power switch without root

Thu Aug 08, 2013 12:02 pm

I don't get it to work. I made sure the sender works by successfully experimenting with an Arduino, but it doesn't seem to do anything from my Raspberry Pi. The pin (in my case GPIO 27 (P1-13)) is set up and properly opened. The dip switch settings are properly entered. I tried all kinds of alternative connections (jumper wires directly to the header, breadboard and cobbler). WebIOPi shows activity (sometimes when I watch in the proper moment). I am at my wits end.

What else can I do?

User avatar
with ice cream
Posts: 204
Joined: Mon Jul 30, 2012 7:25 am

Re: Tutorial: Elro remote power switch without root

Fri Aug 09, 2013 3:36 pm

OK. I solved it. Here's what I did.

I increased the repeat interval and decreased the pulse length:

Code: Select all

...
repeat = 24 # Number of transmissions
pulselength = 60 # microseconds
...
I'm not sure if theses are the ideal values, but my switches seem to react about three out of four times. I'm still looking for better ways of fine-tuning it. Also, I added an antenna which increased the range.

sentientpi
Posts: 3
Joined: Wed Aug 21, 2013 8:36 pm

Re: Tutorial: Elro remote power switch without root

Wed Aug 21, 2013 8:42 pm

Do you have any idea on how to change the house code?

I want to make this script work with Brennenstuhl sockets. Unit code is right, house code is wrong (i captured the signal with receiver)

Thanks in advance!

User avatar
with ice cream
Posts: 204
Joined: Mon Jul 30, 2012 7:25 am

Re: Tutorial: Elro remote power switch without root

Thu Aug 22, 2013 11:33 am

@sentientpi

How did you do the recording? I am looking for a flexible way to capture thermometer signals.

Anyway, perhaps the following links help you (there are two in German, though):

http://www.fhemwiki.de/wiki/Intertechno_Code_Berechnung
http://isn-systems.com/tools/it2elro/
http://forums.ninjablocks.com/index.php ... it-etc-/p1

sentientpi
Posts: 3
Joined: Wed Aug 21, 2013 8:36 pm

Re: Tutorial: Elro remote power switch without root

Thu Aug 22, 2013 6:22 pm

@with ice cream

Thanks for the links!

For signal capture, i opened two putty window (raspberry pi was accessed from ssh), so two sessions (i will call it transmitter and receiver window) from my desktop. From transmitter window, I've sent the signal from this script (on this page) and another window was running another c++ script "./receive" ( this is the link which includes a receiver script: http://www.sweetpi.de/blog/119/funkstec ... i-schalten)

Transmitter and receiver are connected to the raspberry pi. As I pressed the button from Brunnenstuhl remote, the receiver windows showed the id, house code and on/off status which were right as per my DIP setting on socket and remote. I tried the same unit code with this script but this time, receiver window have shown the right id but wrong house code..well, i have no idea, where to change what...because there is now difference if i will change the house code in script...need to do more research...I have ordered Elro switches now, will see if they'll work or not.

sentientpi
Posts: 3
Joined: Wed Aug 21, 2013 8:36 pm

Re: Tutorial: Elro remote power switch without root

Fri Aug 23, 2013 6:04 pm

Just for everyone: Finally, I got Brennenstuhl socket working. I needed to change the house code and it is working. I also brought Elro. Elro and Brennenstuhl uses the same chip and I could control Brennenstuhl sockets with Elro remote control.

If i can extend the range of my transmitter would be so nice as it seems to work for short distance only

Now need to write some android app :) Thank you to author on writing such a helpful codes!

ineffective
Posts: 2
Joined: Thu Jan 09, 2014 11:32 am

Re: Tutorial: Elro remote power switch without root

Thu Jan 09, 2014 11:49 am

Hello guys.
I too have worked on automating my home.

My goals:

- being able to control the lights, and other simple tools via my smartphone
- set up some rules to allow for timing, automated mechanisms as coming home -> lights on, leaving home -> lights off etc.


my way:
- i used the wiringPi and RCSwitch libraries. I then wrapped them via Java JNI and exposed them to a java spring application. This spring application is exposing further API's via REST webservices. This REST webservice can then be accessed via a smartphone application.

check here for my current status:
https://plus.google.com/108501013982176 ... Qw9RK2RV8s
https://plus.google.com/108501013982176 ... QRLneobKMW

All open source and for anyone to help out:
https://github.com/Ineffective/Pi-jAutomation433

I have a JDK 8 with early acces (hardware float calc) on my pi. it runs on a jetty server. for now all works fine. soon i'll add rules and process support via "camunda" (java framework)

I store all states and plugs, as well as (later) other data such as groups, triggers, timers etc in a local database (h2)

If anyone wants to help, jump on the wagon, become my co-developer, please let me know. a friend on the way is greatly appreciated. take whatever you like from me, just let me know when you did so i know if people actually care.

Return to “Python”