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


SUBMITTED BY: Guest

DATE: Nov. 2, 2014, 11:37 p.m.

FORMAT: Text only

SIZE: 3.0 kB

HITS: 800

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

comments powered by Disqus