0

I am using gpiozero to control devices on the Raspberry Pi. When I create a reference to (for example) an LED device, there is a parameter for creating the object without effecting it's current state: initial_state=None. (The default is initial_state=False, which automatically turns the value off upon reference object creation) The problem is it always seems to reset the hardware pin on script exit (though oddly enough not the internal "state"). What's worse, when I run the script again, it knows the state I left it in, and puts the physical pin back to that state!

Here's my jumpers on/off program, it now has a pausing input, during which the state stays unchanged, but when the program exits, the pins reset. (Though as I mention above, the state is "remembered")

#!/usr/bin/env python
from __future__ import print_function
import sys
import time
from gpiozero import LED
jump1=LED(17,initial_value=None)
jump2=LED(27,initial_value=None)


if len(sys.argv)>1:
    print ("Jumper were: (%s,%s)"%(str(jump1.is_active),str(jump2.is_active)))
    if sys.argv[1].lower() == 'on':
        jump1.on()
        jump2.on()
        print ('turned both on')
    elif sys.argv[1].lower() == 'off':
        jump1.off()
        jump2.off()
        print ('turned both off')

print ("Jumper Currently: (%s,%s)"%(str(jump1.is_active),str(jump2.is_active)))

raw_input("Press enter to exit.")

Does anyone know a way to tell gpiozero to leave the hardware alone after exit?

  • Have you a small script which can be run to demonstrate the problem? – joan Dec 4 '18 at 18:01
  • Added python 2.7 program being used. – RufusVS Dec 4 '18 at 18:08
  • That does seem to be odd behaviour. It may be a bug. If you don't get an answer perhaps check the issues at github.com/RPi-Distro/python-gpiozero/issues and raise a new one if needed. – joan Dec 4 '18 at 19:07
  • I ended up using RPi.GPIO instead of gpiozero. with RPi.GPIO you have to explicitly call the cleanup() method, otherwise it leaves everything as you last left it. Which is what I was looking for. I don't know if I'll research it further. – RufusVS Dec 5 '18 at 18:54
0

I rewrote using RPi.GPIO module instead of gpiozero. It feels different, but it was easier than researching a way to do exit without cleanup using gpiozero.

Here is the "equivalent" program without pin cleanup.

#!/usr/bin/env python
from __future__ import print_function
import sys
import time
import RPi.GPIO as GPIO

# these pin numbers map have to change
# try the 'pinout' command from the bash prompt

pina = 17
pinb = 27


# set pins up:

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pina, GPIO.OUT)
GPIO.setup(pinb, GPIO.OUT)


if len(sys.argv)>1:
    print ("Jumpers were: (%s,%s)"%  (str(GPIO.input(pina)),str(GPIO.input(pinb))))
    if sys.argv[1].lower() == 'on':
        GPIO.output(pina, GPIO.HIGH)
        GPIO.output(pinb, GPIO.HIGH)
        print ('turned both on')
    elif sys.argv[1].lower() == 'off':
        GPIO.output(pina, GPIO.LOW)
        GPIO.output(pinb, GPIO.LOW)
        print ('turned both off')

print ("Jumpers now: (%s,%s)"%    (str(GPIO.input(pina)),str(GPIO.input(pinb))))

#raw_input("Press enter to exit.")  # optional pause for testing

# Note:  I/O pins will remain at their last state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.