heikoheiko

elropi.py - Remote switch Elro using Python on Raspberry PI

Oct 4th, 2012
12,560
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. """
  3. "elropi.py" for switching Elro devices using Python on Raspberry Pi
  4. by Heiko H. 2012
  5.  
  6. This file uses RPi.GPIO to output a bit train to a 433.92 MHz transmitter, allowing you
  7. to control light switches from the Elro brand.
  8.  
  9. Credits:
  10. This file is mostly a port from C++ and Wiring to Python and the RPi.GPIO library, based on
  11. C++ source code written by J. Lukas:
  12.     http://www.jer00n.nl/433send.cpp
  13. and Arduino source code written by Piepersnijder:
  14.     http://gathering.tweakers.net/forum/view_message/34919677
  15. Some parts have been rewritten and/or translated.
  16.  
  17. This code uses the Broadcom GPIO pin naming by default, which can be changed in the
  18. "GPIOMode" class variable below.
  19. For more on pin naming see: http://elinux.org/RPi_Low-level_peripherals
  20.  
  21. Version 1.0
  22. """
  23.  
  24. import time
  25. import RPi.GPIO as GPIO
  26.  
  27. class RemoteSwitch(object):
  28.     repeat = 10 # Number of transmissions
  29.     pulselength = 300 # microseconds
  30.     GPIOMode = GPIO.BCM
  31.    
  32.     def __init__(self, device, key=[1,1,1,1,1], pin=4):
  33.         '''
  34.         devices: A = 1, B = 2, C = 4, D = 8, E = 16  
  35.         key: according to dipswitches on your Elro receivers
  36.         pin: according to Broadcom pin naming
  37.         '''    
  38.         self.pin = pin
  39.         self.key = key
  40.         self.device = device
  41.         GPIO.setmode(self.GPIOMode)
  42.         GPIO.setup(self.pin, GPIO.OUT)
  43.        
  44.     def switchOn(self):
  45.         self._switch(GPIO.HIGH)
  46.  
  47.     def switchOff(self):
  48.         self._switch(GPIO.LOW)
  49.  
  50.     def _switch(self, switch):
  51.         self.bit = [142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 136, 128, 0, 0, 0]      
  52.  
  53.         for t in range(5):
  54.             if self.key[t]:
  55.                 self.bit[t]=136
  56.         x=1
  57.         for i in range(1,6):
  58.             if self.device & x > 0:
  59.                 self.bit[4+i] = 136
  60.             x = x<<1
  61.  
  62.         if switch == GPIO.HIGH:
  63.             self.bit[10] = 136
  64.             self.bit[11] = 142
  65.                
  66.         bangs = []
  67.         for y in range(16):
  68.             x = 128
  69.             for i in range(1,9):
  70.                 b = (self.bit[y] & x > 0) and GPIO.HIGH or GPIO.LOW
  71.                 bangs.append(b)
  72.                 x = x>>1
  73.                
  74.         GPIO.output(self.pin, GPIO.LOW)
  75.         for z in range(self.repeat):
  76.             for b in bangs:
  77.                 GPIO.output(self.pin, b)
  78.                 time.sleep(self.pulselength/1000000.)
  79.        
  80.        
  81. if __name__ == '__main__':
  82.     import sys
  83.     GPIO.setwarnings(False)
  84.    
  85.     if len(sys.argv) < 3:
  86.         print "usage:sudo python %s int_device int_state (e.g. '%s 2 1' switches device 2 on)" % \
  87.             (sys.argv[0], sys.argv[0])  
  88.         sys.exit(1)
  89.    
  90.    
  91.     # Change the key[] variable below according to the dipswitches on your Elro receivers.
  92.     default_key = [1,0,0,0,1]
  93.    
  94.     # change the pin accpording to your wiring
  95.     default_pin =17
  96.     device = RemoteSwitch(  device= int(sys.argv[1]),
  97.                             key=default_key,
  98.                             pin=default_pin)
  99.  
  100.     if int(sys.argv[2]):
  101.         device.switchOn()
  102.     else:
  103.         device.switchOff()
RAW Paste Data