在树莓派零上使用rpi-rf等待接收代码,然后发送另一个代码作为响应

时间:2019-05-03 12:18:23

标签: python-3.x raspberry-pi raspberry-pi-zero

我正在尝试在树莓派零上使用rpi-rf从远程接收代码,然后再发送另一个代码作为响应。

到目前为止,我已经能够成功发送和接收代码,但是,我无法在同一脚本中同时执行这两个操作。我不确定确切如何管理信号以及是否必须关闭和打开连接?我已经修改了示例脚本供我使用。为我指明正确方向的任何帮助都会很棒。

导入argparse 导入信号 导入系统 导入时间 导入日志记录

从rpi_rf导入RFDevice

rfdevice =无

def exithandler(信号,帧):     rfdevice.cleanup()     sys.exit(0)

#My code to send on then off signal
def mysend():
    print('My Code Send')
    rfdevice = RFDevice(17)
    rfdevice.enable_tx()
    rfdevice.tx_repeat = 10
    rfdevice.tx_code(2837039, 1, 350, 24)
    time.sleep (2.00)
    rfdevice.tx_code(2837039, 1, 350, 24)
    print ('Light ON then OFF')

logging.basicConfig(level = logging.INFO,datefmt ='%Y-%m-%d%H:%M:%S',                     format ='%(asctime)-15s-[%(levelname)s]%(module)s:%(message)s',)

#Code for waiting ans receiving an rfcode
signal.signal(signal.SIGINT, exithandler)
rfdevice = RFDevice(27)
rfdevice.enable_rx()
timestamp = None
logging.info("Listening for codes on GPIO " + str(27))
while True:
    if rfdevice.rx_code_timestamp != timestamp:
        timestamp = rfdevice.rx_code_timestamp
        logging.info(str(rfdevice.rx_code) +
                     " [pulselength " + str(rfdevice.rx_pulselength) +
                     ", protocol " + str(rfdevice.rx_proto) + "]")
        if rfdevice.rx_code == 7406865:
            print('My Code Received')
            mysend()
    time.sleep(0.01)

rfdevice.cleanup()

该代码不给出任何错误,但不发送射频代码。如果不尝试在同一脚本中接收,则发送代码可以正常工作。

0 个答案:

没有答案