Pi Switch is a Python library for the Raspberry Pi to control 315/433MHz remote controlled power sockets.
Besides that Pi Switch can be used to send data from one Raspberry Pi
to another using the
This library is basically a Python wrapper around the Arduino rc-switch C++ library.
We need to install the following dependencies:
- python boost
Install wiring pi:
git clone git://git.drogon.net/wiringPi
cd wiringPi ./build
In a next step please update your Raspberry Pi:
sudo apt-get update
Install python boost, python header files and python-pip:
sudo apt-get install python-dev libboost-python-dev python-pip
Finally, we install
pi_switch using pip:
sudo pip install pi_switch
Building from source
Please check that all dependencies are installed. See section
git clone https://github.com/lexruee/pi-switch-python.git
sudo python setup.py install
Known devices that seem to work are listed on the rc-switch wiki.
Pi Switch uses the wiringPi library and the wiringPi pin mapping.
WiringPi Pin 0 <=> BCM GPIO17 <=> Header Pin 11
Programs that use pi_switch must be run with sudo.
RCSwitchSender class provides low level methods to send binary
or tri-state strings.
For more details see the rc-switch wiki.
Send binary string
# Type B example: address group = 1, channel = 1 import pi_switch sender = pi_switch.RCSwitchSender() sender.enableTransmit(0) # use WiringPi pin 0 sender.send("000101010001010101010101") # switch on sender.send("000101010001010101010100") # switch off
Send tri-state string
# Type B example: address group = 1, channel = 1 import pi_switch sender = pi_switch.RCSwitchSender() sender.enableTransmit(0) # use WiringPi pin 0 sender.sendTriState("0FFF0FFFFFF1") # switch on sender.sendTriState("0FFF0FFFFFF0") # switch off
# Type B example: address group = 1, channel = 1 import pi_switch sender = pi_switch.RCSwitchSender() sender.enableTransmit(0) # use WiringPi pin 0 sender.sendDecimal(1381717, 24) # switch on sender.sendDecimal(1381716, 24) # switch off
RCSwitchSender setter methods
The default settings of the
RCSwitchSender can be changed by means of the following setter methods:
RCSwitchReceiver class provides a low level method to receive data.
The following example shows how the
RCSwitchReceiver class is used:
from pi_switch import RCSwitchReceiver receiver = RCSwitchReceiver() receiver.enableReceive(2) num = 0 while True: if receiver.available(): received_value = receiver.getReceivedValue() if received_value: num += 1 print("Received[%s]:" % num) print(received_value) print("%s / %s bit" % (received_value, receiver.getReceivedBitlength())) print("Protocol: %s" % receiver.getReceivedProtocol()) print("") receiver.resetAvailable()
RCSwitchReceiver class provides the following methods:
There are 4 kind of switch types according to the rc-switch wiki page.
- Type A - 10 pole DIP switches
- Type B - Two rotary/sliding switches
- Type C - Intertechno
- Type D
Switch Type A
NOTE: Tested and works!
import pi_switch # DIP switches 1..5 where "1" = on and "0" = off, # if all DIP switches are on it's "11111" first = "11111" # DIP switches 6..10 (A..E) where "1" = on and "0" = off, # if all DIP switches are on it's "11111" second = "11111" switch = pi_switch.RCSwitchA(first, second) switch.enableTransmit(0) # use WiringPi pin 0 <=> GPIO17 switch.switchOn() switch.switchOff()
Switch Type B
NOTE: Tested and works!
import pi_switch address_group = 1 # Address group (1..4) channel = 2 # Channel (1..4) switch = pi_switch.RCSwitchB(address_group, channel) switch.enableTransmit(0) # use WiringPi pin 0 <=> GPIO17 switch.switchOn() switch.switchOff()
Switch Type C
NOTE: Tested and works! Please make sure to send a 'on' sequence within the first five seconds the switch is plugged. This enables the switch to recognize the family code.
import pi_switch family_code = "a" # Familycode (a..f) group = 1 # Number of group (1..4) device = 1 # Number of device (1..4) switch = pi_switch.RCSwitchC(family_code, group, device) #address group 1, channel 2 switch.enableTransmit(0) # use WiringPi pin 0 <=> GPIO17 switch.switchOn() switch.switchOff()
Switch Type D
NOTE: Not tested!
import pi_switch group = "A" # Code of the switch group (A,B,C,D) device = 1 # Number of the switch itself (1..3) switch = pi_switch.RCSwitchD(group, device) #address group 1, channel 2 switch.enableTransmit(0) # use WiringPi pin 0 <=> GPIO17 switch.switchOn() switch.switchOff()
send.py is a python program to switch power sockets on or off.
It takes the following arguments:
- command: -c
- type: -t
- settings: -s
The command argument must be either on or off. Type must be A, B, C or D. Settings must be a comma separated list which specifies the family code, group code, address code, channel number or device number.
For more details about the switch types see the rc-switch wiki page.
#switch type A: sudo python send.py -c off -t A -s 11001,01000 -p 0 #switch type B: sudo python send.py -c off -t B -s 1,3 -p 0 #switch type C: sudo python send.py -c off -t C -s a,1,1 -p 0 #switch type D: sudo python send.py -c off -t D -s A,1 -p 0
""" A program that toggles three light switches """ import time import pi_switch def create_switch(addr, channel): """creates a switch of type B""" switch = pi_switch.RCSwitchB(addr, channel) switch.enableTransmit(0) # use WiringPi pin 0 <=> GPIO17 return switch def toggle(switch): """toggles a switch on and off""" switch.switchOn() time.sleep(1) switch.switchOff() time.sleep(1) switches = [[1,1],[1,2],[1,3]] switches = [ create_switch(p,q) for (p,q) in switches ] while True: for switch in switches: toggle(switch)
Please feel free to contribute or to improve this library (Codebase, Documentation etc.). Every contribution counts.
I'm not an expert in this field. I'm just the guy who wrote the glue to port the library for the Python Programming Language :-).
- Fork it ( https://github.com/[my-github-username]/pi-switch-python/fork )
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create a new Pull Request
Contact and Bugs
Please use github's issue tracking system.
I'm not responsible for any hardware damages or other accidents.
You use this library at your own risk.