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
Code: Select all
sudo apt-get install git-core
sudo git clone git://git.drogon.net/wiringPi
cd wiringPi
sudo ./build
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
Code: Select all
sudo nano /etc/rc.local
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
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()
Code: Select all
chmod u+x elro_wiringpi.py
Code: Select all
sudo nano elro_wiringpi.py
Code: Select all
./elro_wiringpi.py <unit> <on/off>
<on/off>: on=1, off=2
Example
Code: Select all
./elro_wiringpi.py 4 1
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.