Skip to content
Advertisement

Hardcode RF Codes in rpi-rf_send script + python raspberry pi

I’d like to hardcode the RF Code in the rpi-rf_send.py send script so I can repeatedly activate the same device automatically.

I’ve reviewed all of the documentation and I cannot find any clear method to do that.

Docs:

https://pypi.org/project/rpi-rf/

https://github.com/milaq/rpi-rf

It works well when passing the arguments directly from the terminal in this format:

python3 send.py -p 174 -t 1 123456

But what I need to do is hard code those arg variables into the script and I cannot find any direction on how to do that.

Here is the code:

import argparse
import logging

from rpi_rf import RFDevice

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

parser = argparse.ArgumentParser(description='Sends a decimal code via a 433/315MHz GPIO device')
parser.add_argument('code', metavar='CODE', type=int,
                    help="Decimal code to send")
parser.add_argument('-g', dest='gpio', type=int, default=17,
                    help="GPIO pin (Default: 17)")
parser.add_argument('-p', dest='pulselength', type=int, default=None,
                    help="Pulselength (Default: 350)")
parser.add_argument('-t', dest='protocol', type=int, default=None,
                    help="Protocol (Default: 1)")
parser.add_argument('-l', dest='length', type=int, default=None,
                    help="Codelength (Default: 24)")
parser.add_argument('-r', dest='repeat', type=int, default=10,
                    help="Repeat cycles (Default: 10)")
args = parser.parse_args()

rfdevice = RFDevice(args.gpio)
rfdevice.enable_tx()
rfdevice.tx_repeat = args.repeat

if args.protocol:
    protocol = args.protocol
else:
    protocol = "default"
if args.pulselength:
    pulselength = args.pulselength
else:
    pulselength = "default"
if args.length:
    length = args.length
else:
    length = "default"

logging.info(str(args.code) +
             " [protocol: " + str(protocol) +
             ", pulselength: " + str(pulselength) +
             ", length: " + str(length) +
             ", repeat: " + str(rfdevice.tx_repeat) + "]")

rfdevice.tx_code(args.code, args.protocol, args.pulselength, args.length)
rfdevice.cleanup()

I’ve tried experimenting with the variables in rfdevice.tx_code by passing the arguments directly but still get errors. I’ve tried:

rfdevice.tx_codes(123456, 1, 174)

The trace returns this:

usage:send.py [-h] [-g GPIO] [-p PULSELENGTH] [-t PROTOCOL] C
send.py: error: the following arguments the following are required: C 

Secondly:

rfdevice.tx_code(123456.code, 1.protocol, 174.pulselength)

Which returns an “invalid syntax” error…

Do any of you kind souls have any idea which variable I need to edit to hard code the RF Code to replicate “python3 send.py -p 174 -t 1 123456”?

Thanks in advance.

Advertisement

Answer

Ok, figured it out… well at least a work around. Instead of trying to change the variables within the send.py script, you can use the subprocess library to execute the script when required and pass the arguments to it.

Simply by adding:

import subprocess 

subprocess.Popen(['python','send.py','-p','174','-t','1','123456'])
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement