0

I'm struggling to get some reliability out of the aforementioned packages. Here are my problems:

  1. When I want to toggle a single output, all outputs reset and change into their new state
  2. Outputs don't always follow the expected outcome (e.g. outputs dont always turn on or off when instructed).

Heres the code sending to the pi running pigpiod:

import argparse
from gpiozero import OutputDevice
from gpiozero.pins.pigpio import PiGPIOFactory

parser = argparse.ArgumentParser(description='Start, stop, or start aux equipment')
parser.add_argument('func', type=str, help='Function to perform [startup, shutdown, auxstart, laser(toggle), airassist(toggle), exhaust(toggle)]')
args = parser.parse_args()

#RD1P Relays
factory = PiGPIOFactory(host='192.168.9.170')
las = OutputDevice(22, pin_factory=factory)
rex = OutputDevice(27, pin_factory=factory)
ras = OutputDevice(17, pin_factory=factory)

def startup():
    print('Starting up the machine.')
    las.on()

def shutdown():
    print('Shutting down all equipment.')
    las.off()
    rex.off()
    ras.off()

def auxstart():
    print('Starting up the auxilliary equipment.')
    rex.on()
    ras.on()

def auxstop():
    print('Starting up the auxilliary equipment.')
    rex.off()
    ras.off()

def laser():
    state = las.value
    print (f"Toggling Laser from {str(state)}.")
    if state == True:
        las.off()
    else:
        las.on()

def airassist():
    state = ras.value
    print (f"Toggling airassist from {str(state)}.")
    if state == True:
        ras.off()
    else:
        ras.on()

def exhaust():
    state = rex.value
    print (f"Toggling airassist from {str(state)}.")
    if state == True:
        ras.off()
    else:
        rex.on()


if __name__ == "__main__":
    func = args.func
    if func == 'startup': startup()
    elif func == 'auxstart': auxstart()
    elif func == 'auxstop': auxstop()
    elif func == 'laser': laser()
    elif func == 'airassist': airassist()
    elif func == 'exhaust': exhaust()
    else: shutdown()

So as an example, if I run "startup" then as expected las turns on, by itself. If I then run "auxstart", rax and ras turn on but las turns off. If I then re-run "startup", las turns on, but rax and ras turns off. Do these outputs have to be controlled indivually instead of in function groups like I have in the code? Is there something specific I need to do to prevent the "flicker" that occurs on the outputs when switching any of their states?

Edit: Added some code to try and run individual toggles based on current state but it only seems to want to switch to the "on" state and doesn't seem to read the status correctly (see picture). When trying to toggle the output, it just flicks from on to off back to on. I'm fully aware that there is a toggle function but it behaves identically as described.

enter image description here

Running some code locally to simply turn a single output on or of and check the status nets some more strange results. If I manually set the GPIO to OUTPUT raspi-gpio set 17,22,27,5,6,13 op I get the following:

addohm@d1p-woctorelay:~/scripts $ raspi-gpio get 17,22,27,5,6,13
GPIO 5: level=0 fsel=1 func=OUTPUT
GPIO 6: level=0 fsel=1 func=OUTPUT
GPIO 13: level=0 fsel=1 func=OUTPUT
GPIO 17: level=0 fsel=1 func=OUTPUT
GPIO 22: level=0 fsel=1 func=OUTPUT
GPIO 27: level=0 fsel=1 func=OUTPUT

if I then run the following:

import gpiozero

RELAY_PIN = 17

relay = gpiozero.OutputDevice(RELAY_PIN, active_high=True, initial_value=False)

relay.on() # switch off

Nothing appears to happen. If I check the status manually with raspi-gpio get 17,22,27,5,6,13 I get:

GPIO 5: level=0 fsel=1 func=OUTPUT
GPIO 6: level=0 fsel=1 func=OUTPUT
GPIO 13: level=0 fsel=1 func=OUTPUT
GPIO 17: level=0 fsel=0 func=INPUT
GPIO 22: level=0 fsel=1 func=OUTPUT
GPIO 27: level=0 fsel=1 func=OUTPUT

Why would the GPIO get set to input from the previous script?

7
  • Surely the script will reinitialise its state every time you run it. Are you assuming that it remembers what you previously did?
    – joan
    Jun 13, 2023 at 16:29
  • @joan yes, that's why I added the toggle scripts to test it - and it still performs unexpectedly.
    – addohm
    Jun 13, 2023 at 17:43
  • Sorry I didn't update the OP with the additional toggle scripts.
    – addohm
    Jun 13, 2023 at 17:58
  • gpiozero likely resets the GPIO to a known state (say off) when it initialises the GPIO. If possible I would use pigpio via the pigpio Python module and read the state of the GPIO before setting them as outputs.
    – joan
    Jun 13, 2023 at 19:20
  • @joan Im not actually setting the state, I'm guessing pigpiod is. I'll do some more testing and update the OP.
    – addohm
    Jun 13, 2023 at 21:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.