SOLVED
I am building a wardriving rig with a Raspberry Pi4 and am attempting to set up a button and LEDs. I have about 8 months of Python experience. On startup I want the blue LED to stay on the entire time, the red LED to stay on when Kismet is not active, the green LED to stay on when Kismet is active, and I want the button to switch back and forth, starting and stopping Kismet and having the LEDs reflect that decision.
I have ran test scripts and have ensured that the button, LEDs, and their associated pins are correctly working.
When I attempt to run the below script with the while loop I get a success rate of about 75%. Sometimes it works exactly how I programmed it, sometimes the red light never gets initiated and the button does not function, other times it will work normally then get hung on a button press, sometimes needing 3-4 presses to activate.
I understand that the polling is an issue with the Rpi GPIO software missing inputs sometimes, so I attempted to setup edge detection. I have not gotten this code to run successfully yet, I always get the error:
Traceback (most recent call last):
File "***", line 66, in <module>
GPIO.add_event_detect(7, GPIO.FALLING, callback=kismet, bouncetime=200)
RuntimeError: Failed to add edge detection
I am pretty stuck at this point and any help would be greatly appreciated.
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
I am building a wardriving rig with a Raspberry Pi4 and am attempting to set up a button and LEDs. I have about 8 months of Python experience. On startup I want the blue LED to stay on the entire time, the red LED to stay on when Kismet is not active, the green LED to stay on when Kismet is active, and I want the button to switch back and forth, starting and stopping Kismet and having the LEDs reflect that decision.
I have ran test scripts and have ensured that the button, LEDs, and their associated pins are correctly working.
When I attempt to run the below script with the while loop I get a success rate of about 75%. Sometimes it works exactly how I programmed it, sometimes the red light never gets initiated and the button does not function, other times it will work normally then get hung on a button press, sometimes needing 3-4 presses to activate.
I understand that the polling is an issue with the Rpi GPIO software missing inputs sometimes, so I attempted to setup edge detection. I have not gotten this code to run successfully yet, I always get the error:
Traceback (most recent call last):
File "***", line 66, in <module>
GPIO.add_event_detect(7, GPIO.FALLING, callback=kismet, bouncetime=200)
RuntimeError: Failed to add edge detection
I am pretty stuck at this point and any help would be greatly appreciated.
----------------------------------------------------------------------------------------------------------------------------------------
Code:
import RPi.GPIO as GPIOimport subprocessimport time#cancels out warnings for nowGPIO.setwarnings(False)#Setting mode for GPIO to use [Board(pin number) or BCM(gpio number)]GPIO.setmode(GPIO.BOARD)time.sleep(1)"""Setting up Button on Pin #7 on the pi, Setting to input, using pull up/down to set the inital value, looking for a down"""GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Setting up RED LED on Pin #32 on the pi, setting to output, and setting initial to offGPIO.setup(32, GPIO.OUT, initial=GPIO.LOW)#Setting up GREEN LED on Pin #16 on the pi, setting to output, and setting initial to offGPIO.setup(16, GPIO.OUT, initial=GPIO.LOW)#Setting up BLUE LED on Pin #22 on the pi, setting to output, and setting initial to onGPIO.setup(22, GPIO.OUT, initial=GPIO.HIGH)"""Runs a continuous loop, checking for the status of kismet.service. When the Buttons are up, if kismet is running = Green LED. Kismet not running = Red LED If the button is pressed and kismet is running, it stops kismet. If kismet is not running, it starts it. Blue - 22 Red - 32 Green - 16"""try: while True: time.sleep(0.001) result = subprocess.run(['sudo','systemctl is-active kismet.service'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) status = result.stdout.strip() if status == 'active': if GPIO.input(7) == GPIO.HIGH: GPIO.output(16, GPIO.HIGH) GPIO.output(32, GPIO.LOW) elif GPIO.input(7) == GPIO.LOW: print("Stopping Kismet") #time.sleep(0.2) subprocess.run(['sudo', 'systemctl', 'stop', 'kismet.service']) time.sleep(0.2) elif status == 'inactive': if GPIO.input(7) == GPIO.HIGH: GPIO.output(32, GPIO.HIGH) GPIO.output(16, GPIO.LOW) elif GPIO.input(7) == GPIO.LOW: print("Starting Kismet") #time.sleep(0.2) subprocess.run(['sudo', 'systemctl', 'start', 'kismet.service']) time.sleep(0.2)except KeyboardInterrupt: print("Exiting on Ctrl+C...")finally: GPIO.cleanup()
Code:
import RPi.GPIO as GPIOimport subprocessimport time#cancels out warnings for now and cleans up any poorly shutdown GPIO processesGPIO.setwarnings(False)#GPIO.cleanup()#Setting mode for GPIO to use [Board(pin number) or BCM(gpio number)]GPIO.setmode(GPIO.BOARD)time.sleep(1)"""Setting up Button on Pin #7 on the pi, Setting to input, using pull up/down to set the inital value, looking for a down"""GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Setting up RED LED on Pin #32 on the pi, setting to output, and setting initial to offGPIO.setup(32, GPIO.OUT, initial=GPIO.LOW)#Setting up GREEN LED on Pin #16 on the pi, setting to output, and setting initial to offGPIO.setup(16, GPIO.OUT, initial=GPIO.LOW)#Setting up BLUE LED on Pin #22 on the pi, setting to output, and setting initial to onGPIO.setup(22, GPIO.OUT, initial=GPIO.HIGH)def kismet(channel): result = subprocess.run(['sudo','systemctl is-active kismet.service'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) status = result.stdout.strip() if status == 'active': print("Stopping Kismet") subprocess.run(['sudo', 'systemctl', 'stop', 'kismet.service']) GPIO.output(32, GPIO.LOW) GPIO.output(16, GPIO.HIGH) elif status == 'inactive': print("Starting Kismet") subprocess.run(['sudo', 'systemctl', 'start', 'kismet.service']) GPIO.output(16, GPIO.LOW) GPIO.output(32, GPIO.HIGH)GPIO.add_event_detect(7, GPIO.FALLING, callback=kismet, bouncetime=200)try: # Keep the program running while True: time.sleep(0.1) # Sleep to prevent busy waitingexcept KeyboardInterrupt: print("Exiting on Ctrl+C")finally: GPIO.cleanup() # Cleanup GPIO settings on exit
Statistics: Posted by xdooziex — Sun Apr 13, 2025 4:20 pm