I am setting up a script in order to fire 433MHz devices in random order on a loop. I have to script to do this sorted however I am looking to have two buttons, one to launch the script and one to interrupt it if anything goes wrong.
I am fairly new to python and I would appreciate any help you guys can offer.
from time import sleep
import random
from rpi_rf import RFDevice
rfdevice = RFDevice(17)
for c in range(0,15):
#Randomly Choose 1st Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
sleep(wait_for_next)
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
#Randomly Choose 2nd Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
sleep(wait_for_next)
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
#Randomly Choose Interval Time for Reload
wait_for_next = random.uniform(7,9)
sleep(wait_for_next)
The script listed works to fire both devices a total of 30 times, however the final box will not have a keyboard or mouse so I am looking to use one button to start the script and one button to interrupt it should there be any problems arise.
Thank you anyone who looks at this and I look forward to any suggestions that arise.
!!!EDIT!!!
This is my updated script
import RPi.GPIO as GPIO
import time
import os
import subprocess
from time import sleep
import random
from rpi_rf import RFDevice
rfdevice = RFDevice(17)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(23)
if input_state == False:
print('Execute')
for c in range(0,1):
#Randomly Choose Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
sleep(wait_for_next)
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
#Randomly Choose 2nd Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
sleep(wait_for_next)
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
time.sleep(0.2)
This script executes on the button press, now I need it to abort on a different button (GPIO 24).
!!!EDIT 2!!!
Think I have done it, script below works if the button is pressed and held
import RPi.GPIO as GPIO
import time
import os
import subprocess
from time import sleep
import random
from rpi_rf import RFDevice
rfdevice = RFDevice(17)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(23)
if input_state == False:
print('Execute')
for c in range(0,1):
#Randomly Choose Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
input_state = GPIO.input(24)
if input_state == False:
print('Abort')
break
sleep(wait_for_next)
input_state = GPIO.input(24)
if input_state == False:
print('Abort')
break
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
#Randomly Choose 2nd Trap
trap = random.randint(1,3)
#Randomly Choose Interval Time
wait_for_next = random.uniform(2,5)
input_state = GPIO.input(24)
if input_state == False:
print('Abort')
break
sleep(wait_for_next)
input_state = GPIO.input(24)
if input_state == False:
print('Abort')
break
if trap == 1:
#Code for calling trap 1 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE1-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 2:
#Code for calling trap 2 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE2-REDACTED>, 1, 353)
rfdevice.disable_tx()
elif trap == 3:
#Code for calling trap 3 - Number xx
rfdevice.enable_tx()
rfdevice.tx_code(<CODE3-REDACTED>, 1, 353)
rfdevice.disable_tx()
print('Complete')
time.sleep(0.2)
Many thanks to @furas for his time and support, it is very appreciated!