class Transmit433MHz: """Transmit 433MHz commands""" def __init__(self, pin, protocol=1, pulse_length=189, bit_length=24): self.device = None self.pin = pin self.protocol = protocol self.pulse_length = pulse_length self.bit_length = bit_length self.num = 0 def enable_receive(self): try: self.device = RCSwitchReceiver() self.device.enableReceive(self.pin) self.num = 0 except Exception as err: logger.exception( "{cls} raised an exception when enabling receiving: " "{err}".format(cls=type(self).__name__, err=err)) def receive_available(self): try: if self.device.available(): received_value = self.device.getReceivedValue() if received_value: self.num += 1 bit_length = self.device.getReceivedBitlength() delay = self.device.getReceivedDelay() protocol = self.device.getReceivedProtocol() return self.num, received_value, bit_length, delay, protocol return 0, 0, 0, 0, 0 except Exception as err: logger.exception("{cls} raised an exception when receiving: " "{err}".format(cls=type(self).__name__, err=err)) def reset_available(self): self.device.resetAvailable() def transmit(self, cmd): try: self.device = RCSwitchSender() self.device.setProtocol(self.protocol) self.device.setPulseLength(self.pulse_length) self.device.enableTransmit(self.pin) self.device.sendDecimal(cmd, self.bit_length) # switch on except Exception as err: logger.exception("{cls} raised an exception when transmitting: " "{err}".format(cls=type(self).__name__, err=err))
#!/usr/bin/python # coding=utf-8 import time from pi_switch import RCSwitchSender on = 29955 off = 29964 sender = RCSwitchSender() sender.setProtocol(1) sender.setPulseLength(189) sender.enableTransmit(0) sender.sendDecimal(on, 24) # switch on time.sleep(3) sender.sendDecimal(off, 24) # switch off