├── .gitignore ├── receive.py └── send.py /.gitignore: -------------------------------------------------------------------------------- 1 | log.txt 2 | -------------------------------------------------------------------------------- /receive.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import argparse 4 | import signal 5 | import sys 6 | import time 7 | import logging 8 | import subprocess 9 | 10 | from rpi_rf import RFDevice 11 | 12 | rfdevice = None 13 | 14 | logFile = open("log.txt", "r") 15 | 16 | # pylint: disable=unused-argument 17 | def exithandler(signal, frame): 18 | rfdevice.cleanup() 19 | logFile = open("log.txt", "a+") 20 | answer = input("Closing...\n Do you like run Send program with collected signals?\n Enter yes to run. \n") 21 | if answer == "yes" : 22 | logFile.close() 23 | subprocess.call(" ./send.py -f log.txt", shell=True) 24 | logFile.close() 25 | sys.exit(0) 26 | 27 | parser = argparse.ArgumentParser(description='Receive a decimal code from the target device') 28 | parser.add_argument('-g', dest='gpio', type=int, default=27, 29 | help="GPIO pin (Default: 27)") 30 | args = parser.parse_args() 31 | logging.basicConfig(level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S', 32 | format='%(asctime)-15s - [%(levelname)s] %(module)s: %(message)s', ) 33 | 34 | signal.signal(signal.SIGINT, exithandler) 35 | rfdevice = RFDevice(args.gpio) 36 | rfdevice.enable_rx() 37 | timestamp = None 38 | logFile = open("log.txt", "w+") 39 | logFile.write("CODE,PULSELENGTH,PROTOCOL\n") 40 | if args.gpio: 41 | gpio = args.gpio 42 | else: 43 | gpio = "default" 44 | logging.info("Listening for codes on GPIO " + str(gpio)) 45 | while True: 46 | if rfdevice.rx_code_timestamp != timestamp: 47 | timestamp = rfdevice.rx_code_timestamp 48 | logFile = open("log.txt", "a") 49 | logging.info("CODE: " + str(rfdevice.rx_code) + 50 | " [pulselength " + str(rfdevice.rx_pulselength) + 51 | ", protocol " + str(rfdevice.rx_proto) + "]") 52 | logFile.write(str(rfdevice.rx_code) +","+ str(rfdevice.rx_pulselength) +","+ str(rfdevice.rx_proto) +"\n") 53 | logFile.close() 54 | time.sleep(0.01) 55 | rfdevice.cleanup() 56 | logFile.close() 57 | -------------------------------------------------------------------------------- /send.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import csv 3 | import argparse 4 | import logging 5 | import time 6 | 7 | from rpi_rf import RFDevice 8 | 9 | logging.basicConfig(level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S', 10 | format='%(asctime)-15s - [%(levelname)s] %(module)s: %(message)s',) 11 | 12 | parser = argparse.ArgumentParser(description='Sends a decimal code to the target device') 13 | 14 | parser.add_argument('-g', dest='gpio', type=int, default=17, 15 | help="GPIO pin (Default: 17)") 16 | parser.add_argument('-p', dest='protocol', type=int, default=1, help="Protocol. Default: 1") 17 | parser.add_argument('-t', dest='pulselength', type=int, default=350, help="Pulse length, Default: 350") 18 | parser.add_argument('-c', dest='code', type=int, default=0, help="Decimal code to send") 19 | parser.add_argument('-f', dest='logFile', type=str, help="File from program sends the codes") 20 | args = parser.parse_args() 21 | 22 | 23 | def sendSignal(code, protocol, pulselength): 24 | rfdevice = RFDevice(args.gpio) 25 | rfdevice.enable_tx() 26 | logging.info(str(code) + 27 | " [protocol: " + str(protocol) + 28 | ", pulselength: " + str(pulselength) + "]") 29 | 30 | rfdevice.tx_code(int(code), int(protocol), int(pulselength)) 31 | rfdevice.cleanup() 32 | time.sleep(1) 33 | try: 34 | if args.protocol: 35 | protocol = args.protocol 36 | else: 37 | protocol = "default" 38 | if args.pulselength: 39 | pulselength = args.pulselength 40 | else: 41 | pulselength = "default" 42 | if args.logFile: 43 | logFile = args.logFile 44 | with open(logFile) as csv_file: 45 | csv_reader = csv.reader(csv_file, delimiter=',') 46 | line_count = 0 47 | try: 48 | for row in csv_reader: 49 | code = row[0] 50 | pulselength = row[1] 51 | protocol = row[2] 52 | if line_count == 0: 53 | if code == "CODE" and pulselength == "PULSELENGTH" and protocol == "PROTOCOL": 54 | line_count += 1 55 | else: 56 | print("Wrong file type.") 57 | if line_count >= 0 and code != "CODE": 58 | sendSignal(code, protocol, pulselength) 59 | line_count += 1 60 | except KeyboardInterrupt: 61 | print("CTRL + C pressed, stopping…") 62 | except IOError: 63 | print("An error occured trying to read file.") 64 | except IndexError: 65 | print("Please check file. Empty row.") 66 | else: 67 | sendSignal(args.code, args.protocol, args.pulselength) 68 | except KeyboardInterrupt: 69 | print("CTRL + C pressed, stopping…") 70 | 71 | 72 | 73 | --------------------------------------------------------------------------------