class RFSwitch: def __init__(self, gpio, invert_output=False): self.device = RFDevice(gpio) self.device.tx_repeat = 5 self.gpio = gpio #~self.low = GPIO.HIGH if invert_output else GPIO.LOW #~self.high = GPIO.LOW if invert_output else GPIO.HIGH self.low = GPIO.LOW self.high = GPIO.HIGH def __del__(self): self.device.cleanup() def usleep(self, t): time.sleep(t / 1000000.0) def send_decimal(self, value, lengthMsg=24, protocol=1, pulseWidth=185): self.device.enable_tx() self.device.tx_code(value, protocol, pulseWidth, lengthMsg) self.device.disable_tx() def send_timing(self, values): GPIO.setmode(GPIO.BCM) GPIO.setup(self.gpio, GPIO.OUT) GPIO.output(self.gpio, self.low) is_low = False for i in range(0, 5): for t in values: if is_low: GPIO.output(self.gpio, self.low) else: GPIO.output(self.gpio, self.high) is_low = not is_low time.sleep(t / 1000000.0) GPIO.output(self.gpio, self.low) GPIO.cleanup(self.gpio)
datefmt='%Y-%m-%d %H:%M:%S,%3s', format='%(asctime)-15s - [%(levelname)s] %(module)s: %(message)s', ) parser = argparse.ArgumentParser( description='Receives a decimal code via a 433/315MHz GPIO device') parser.add_argument('-g', dest='gpio', type=int, default=GPIO_PIN, help="GPIO pin (Default: 27)") args = parser.parse_args() signal.signal(signal.SIGINT, exithandler) rfdevice = RFDevice(args.gpio) rfdevice.disable_tx() rfdevice.enable_rx() timestamp = None logging.info("Listening for codes on GPIO " + str(args.gpio)) # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): print("Connected with result code " + str(rc)) #client = mqtt_client.Client() #client.on_connect = on_connect #client.connect(MQTT_HOST, 1883) count = 0 c = ""
class RfController: def __init__(self): self.rfReceiver = RFDevice(27) self.rfReceiver.enable_rx() self.rfTransmitter = RFDevice(17) self.rfTransmitter.tx_repeat = 20 self.timestamp = None def exithandler(self, signal, frame): self.rfReceiver.cleanup() self.rfTransmitter.cleanup() sys.exit(0) async def receive(self, timeout=None, expected_response=None): start = datetime.now() while True: if self.rfReceiver.rx_code_timestamp != self.timestamp: if self.valid_rx(self.rfReceiver.rx_pulselength, self.rfReceiver.rx_proto): self.timestamp = self.rfReceiver.rx_code_timestamp if self.rfReceiver.rx_code == expected_response: break elif not expected_response: break if timeout: diff = (datetime.now() - start).seconds if diff > timeout: break await asyncio.sleep(0.01) return self.rfReceiver.rx_code def valid_rx(self, pulse_length, protocol): if 349 <= pulse_length <= 354 and protocol == 1: return True else: return False async def get_response(self, timeout=None, expected_response=None): return await self.receive(timeout, expected_response) async def send(self, data): try: log_info_message('Sending ' + str(data)) self.rfReceiver.disable_rx() await asyncio.sleep(0.01) self.rfTransmitter.enable_tx() await asyncio.sleep(0.01) self.rfTransmitter.tx_code(data, 1) await asyncio.sleep(0.01) self.rfTransmitter.disable_tx() await asyncio.sleep(0.01) self.rfReceiver.enable_rx() log_info_message('Sent Data') await asyncio.sleep(1) except Exception as e: print('Exception while sending - retrying: ' + str(e)) await self.send(data) def exit(self): self.rfTransmitter.cleanup() self.rfReceiver.cleanup() return def disable_receiver(self): self.rfReceiver.disable_rx()