Page 1 of 1

Checking GPIO status

Posted: Thu Apr 24, 2014 4:59 pm
by steveeb
First, my name is Steve and I'm a NOOB...

[Hi Steve]

My question is: Is there a way to check the status of GPIO on a RasPi? Here's the scenario

I wrote the LED script (Python 3) that initializes the GPIO and simply turns pin 16 on and off

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)

state = False
try:
    while 1:
        GPIO.output(23, state)
        c = input("Press return to switch the LED on/off or Q to quit: ")
        if c.strip().upper().startswith('Q'):
            break
        state = not state
except:
    state = False
    print('\n'+'Program Aborted')

GPIO.cleanup()
It runs as expected very simple.

Now suppose I run the program on my Pi and at the same time I run the program on my PC via remote access. It still kinda works, but not 100%. I get the warning when launching the script on the PC

Code: Select all

led.py:5: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(23, GPIO.OUT)
Suppose the RPi had set the LED ON, the PC's "state" variable is initialized to False or OFF. Pressing return on the PC does nothing because it's just telling the RPi to turn GPIO ON which it already is. That's expected, unless I can somehow determine the state of GPIO #23 at program launch.

Furthermore, if one or the other machines exit the program, it runs the GPIO.cleanup() which breaks the other machine's ability to communicate with the GPIO. The second machine shows no errors but pressing return will not turn the LED on.

Can someone point me in the direction of a solution to these issues. I need to be able to, on each loop, check the status of the pin to synchronize the two (or more) computers as well as check if GPIO #23 is even initialized in the case of another computer calling the cleanup routine.

Thanks.

Re: Checking GPIO status

Posted: Thu Apr 24, 2014 5:16 pm
by croston
If more than one program needs to use the same channel, then it is best to not use GPIO.cleanup() at all and start your Python script with GPIO.setwarnings(False). This is the whole reason that setwarnings() was added to the library!

Remember that you can GPIO.input() the value of an output channel too - if another program needs to check the current state.
It might be worth a read of the documentation here --> http://sourceforge.net/p/raspberry-gpio ... /Examples/

Re: Checking GPIO status

Posted: Thu Apr 24, 2014 11:00 pm
by DougieLawson
steveeb wrote: My question is: Is there a way to check the status of GPIO on a RasPi?
Fastest, easiest, quickest, funkiest way is with WebIOPi.
https://code.google.com/p/webiopi/