I have two python scripts to control an output pin in my RPi 3. The first is an interrupt service that scans a button, the second is triggered from the shell. I want to be able to run both simultaneously, so I need to read the pin value to check for it's state. But it's not working as intended. The "interrupt" script works fine, the load (a bulb in this case) switches fine, everything works. As soon as I execute the "shell" script, the load turns off, even if the value being read shows that the pin is HIGH. Also, setting the value with the "shell" script does not reflect on the actual load state, even though the value printed does reflect this change. After executing the "shell" script, the "interrupt" script's changes are not reflected on the load (even though they are reflected if I read the pin state with the "shell" script).
I hope my explanation is not very confusing...
The "interrupt" script is as follows:
#!/usr/bin/python
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(5, GPIO.OUT, initial=GPIO.LOW)
counter = 0
bulb_state = False
def bulb_changed(channel):
global bulb_state
bulb_state = not bulb_state
GPIO.output(5,bulb_state)
if bulb_state:
print("ON")
else:
print("OFF")
GPIO.add_event_detect(3, GPIO.FALLING, callback=bulb_changed, bouncetime=200)
while 1:
pass #Do nothing
GPIO.remove_event_detect(3)
GPIO.cleanup()
The "shell" script is as follows:
#!/usr/bin/python
import RPi.GPIO as GPIO
import sys
if len(sys.argv) > 1: state = sys.argv[1]
else: state = ''
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
#print(GPIO.RPI_INFO)
#print(GPIO.VERSION)
#print(GPIO.getmode())
GPIO.setup(5, GPIO.OUT)
counter = 0
bulb_state = False
def bulb_changed(channel,bulb_state):
GPIO.output(5,bulb_state)
bulb_state = GPIO.input(5)
if bulb_state:
print("ON")
else:
print("OFF")
if (state == '0' or state == 'off' or state == 'OFF'):
bulb_state = False
print("OFF")
bulb_changed(3,bulb_state)
elif (state == '1' or state == 'on' or state == 'ON'):
bulb_state = True
print("ON")
bulb_changed(3,bulb_state)
elif state == '': print(GPIO.input(5))
else: print("Comando incorrecto!")
GPIO.cleanup()
If I exit the "interrupt" script (Ctrl+C) and execute it again, it regains the ability to write to the pin. BTW, I'm using pin 3 as input (button) and pin 5 as output, and I'm running Raspbian Jessie Lite.
I need to have those scripts separated, cause I need to run an interrupt service on the background and still be able to change the pin state from the shell (using RPi.GPIO).
Thanks in advance!!