pfletch101
Posts: 667
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Odd behavior from gpiozero using remote GPIO

Thu Jan 17, 2019 8:29 pm

I am exploring GPIO programming on my Pi 3B+, and I am currently looking at remote GPIO. To (I hoped!) somewhat simplify things, I am using a regular Debian VM as the controller. The Pi has been set up to allow remote control of GPIO, and the controller has the appropriate libraries installed. My Python 3 test program is as follows:

Code: Select all

from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep

factory=PiGPIOFactory(host='[Pi IP Address]') # host specified in function call
# factory=PiGPIOFactory() # host specified on command line
ledR=LED(2,pin_factory=factory)
ledA=LED(3,pin_factory=factory)
ledG=LED(4,pin_factory=factory)
ledR.on()

try:
	while True:
		ledA.on()
		sleep(3)
		ledR.off()
		ledA.off()
		ledG.on()
		sleep(10)
		ledG.off()
		ledA.on()
		sleep(3)
		ledA.off()
		ledR.on()
		sleep(10)
except KeyboardInterrupt:
	pass
finally:
	ledR.off()
	ledA.off()
	ledG.off()
This crashes spectacularly as follows:

Code: Select all

peter@DebianVM:~/work$ python3 rtlights.py
/usr/local/lib/python3.5/dist-packages/gpiozero/devices.py:452: PinFactoryFallback: Falling back from rpigpio: No module named 'RPi'
  'Falling back from %s: %s' % (name, str(e))))
/usr/local/lib/python3.5/dist-packages/gpiozero/devices.py:452: PinFactoryFallback: Falling back from rpio: No module named 'RPIO'
  'Falling back from %s: %s' % (name, str(e))))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)

Did you start the pigpio daemon? E.g. sudo pigpiod

Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888

Did you specify the correct Pi host/port in the
pigpio.pi() function? E.g. pigpio.pi('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/usr/local/lib/python3.5/dist-packages/gpiozero/devices.py:452: PinFactoryFallback: Falling back from pigpio: failed to connect to localhost:8888
  'Falling back from %s: %s' % (name, str(e))))
/usr/local/lib/python3.5/dist-packages/gpiozero/devices.py:452: PinFactoryFallback: Falling back from native: unable to open /dev/gpiomem or /dev/mem; upgrade your kernel or run as root
  'Falling back from %s: %s' % (name, str(e))))
Traceback (most recent call last):
  File "rtlights.py", line 1, in <module>
    from gpiozero import LED
  File "/usr/local/lib/python3.5/dist-packages/gpiozero/__init__.py", line 22, in <module>
    from .devices import (
  File "/usr/local/lib/python3.5/dist-packages/gpiozero/devices.py", line 486, in <module>
    Device.pin_factory = _default_pin_factory()
  File "/usr/local/lib/python3.5/dist-packages/gpiozero/devices.py", line 453, in _default_pin_factory
    raise BadPinFactory('Unable to load any default pin factory!')
gpiozero.exc.BadPinFactory: Unable to load any default pin factory!
However, if I specify the location of the remote Pi on the command line (and whether or not I remove the host argument from the 'factory=...' line):

Code: Select all

peter@DebianVM:~/work$ GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=[Pi IP Address] python3 rtlights.py
the script runs correctly and the LEDs on the remote Pi turn on and off in the expected sequence.

According to the documentation, these two ways of specifying the 'target' of remote GPIO commands are equivalent. Clearly they are not! What am I missing?

Edit 1/17/2019 16:40 CST: I have just tried running the original script on another Pi on my network. It works fine (controlling the LEDs on the specified remote Pi as programmed). Go figure!

pfletch101
Posts: 667
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Odd behavior from gpiozero using remote GPIO

Mon Jan 21, 2019 4:44 pm

Since no-one responded here, I raised this as an 'Issue' on GitHub. It turns out that it is a known bug - the gpiozero library will not load without a default pin factory, and none of the usual sources of a default pin factory can be installed on a desktop. The workaround is apparently to set the GPIOZERO_PIN_FACTORY environment variable to 'mock' before running a script that loads the library. The maintainers hope to fix the bug in a future revision of the library. In the interim, it will be acknowledged in the documentation.

NoFate
Posts: 2
Joined: Wed Sep 25, 2019 5:09 pm

Re: Odd behavior from gpiozero using remote GPIO

Tue Oct 01, 2019 8:09 pm

is this already fixed? i am running Home Assistant ans i experience the same issue still

kush5451
Posts: 1
Joined: Fri Jun 26, 2020 9:55 am

Re: Odd behavior from gpiozero using remote GPIO

Fri Jun 26, 2020 10:08 am

Hi ,
I tried GPIo pin factory library to control pin from my remote PC, by using below code, Code works fine

from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep

factory = PiGPIOFactory(host=''Ip_address_of_my_Rpi')
led=LED(18,pin_factory=factory)

while(1):
led.off()
sleep(5)
led.on()
sleep(5)

Above code works fine ,only thing is by running this " led=LED(18,pin_factory=factory)" command my led glows on which I don't want to happen.
I only want to glow led whenever I called led.on()/led.off() function, could anyone help me to fix this bug.

Return to “Python”