2

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!

  • inside loop you could check second button in defferent moments and if it pressed then use `exit()` to exit script. – furas Oct 27 '19 at 19:10
  • or in some file you can save `PID` (process ID) of this script and second button may run script which gets `PID` from file and kills process with this ID. It can use even system/bash command `kill` – furas Oct 27 '19 at 19:12
  • @furas - thank you very much for your suggestions - I am a beginner when it comes to Python and RPI, how would I go about doing those please? – Ian Hutchinson Oct 27 '19 at 19:38
  • to get PID - `os.getpid()`. To write in file - `fh = open(..., 'w')`, `fh.write(pid)`, `fh.close()` . To read `fh = open(...)`, `pid = int(fh.read())`. To kill process `os.kill(pid, 9)` – furas Oct 27 '19 at 20:09
  • There is different example which use module `subprocess` to run another script and it subprocess has method to kill this process.: https://stackoverflow.com/questions/31039972/python-subprocess-popen-pid-return-the-pid-of-the-parent-script – furas Oct 27 '19 at 20:15
  • @furas I'm not going to lie, this has gone a little over my head, but I will give it a go in the morning. I have a backup copy of my script so I have nothing to loose by trying. Thank you for your help so far – Ian Hutchinson Oct 28 '19 at 00:29
  • @furas please see my updated script which is working on initial button press. Now I am trying to abort it on a different button press. Had to go down this route as my script didn't like subprocess or shell for some reason. Thank you so much for your help so far, it really is sorting me out :-) – Ian Hutchinson Oct 28 '19 at 02:08
  • did you try check button inside `for`-loop in different moments ? You could also use loop instead of `sleep(value)` - `for x in range(value): sleep(1) if check_button(): break` - so it will check button every 1 second during longer sleep and it will use `break` to exit `for`-loop. – furas Oct 28 '19 at 03:32
  • @furas haven't tried it - where abouts would I put it? – Ian Hutchinson Oct 28 '19 at 17:40
  • @furas Think I have managed it; please see the thread for what I eventually used. – Ian Hutchinson Oct 28 '19 at 18:12
  • you did what I was thinking about. You can event repeate it two times with `sleep(wait_for_next/2)` so it will check button more often and you can stop it during sleep. – furas Oct 28 '19 at 18:27

0 Answers0