Correct remote GPIO recipes

This commit is contained in:
Dave Jones
2017-07-14 14:01:29 +01:00
parent eafae5c31a
commit b2e4237a57
13 changed files with 62 additions and 44 deletions

View File

@@ -1,9 +1,11 @@
from gpiozero import LED from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOPin from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause from signal import pause
factory = PiGPIOFactory(host='192.168.1.3')
button = Button(2) button = Button(2)
led = LED(PiGPIOPin(17, host='192.168.1.3')) led = LED(17, pin_factory=factory)
led.source = button.values led.source = button.values

View File

@@ -1,11 +1,14 @@
from gpiozero import LED from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOPin from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero.tools import all_values from gpiozero.tools import all_values
from signal import pause from signal import pause
factory3 = PiGPIOFactory(host='192.168.1.3')
factory4 = PiGPIOFactory(host='192.168.1.3')
led = LED(17) led = LED(17)
button_1 = Button(PiGPIOPin(17, host='192.168.1.3')) button_1 = Button(17, pin_factory=factory3)
button_2 = Button(PiGPIOPin(17, host='192.168.1.4')) button_2 = Button(17, pin_factory=factory4)
led.source = all_values(button_1.values, button_2.values) led.source = all_values(button_1.values, button_2.values)

View File

@@ -1,8 +1,9 @@
from gpiozero import LED from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOPin from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep from time import sleep
led = LED(PiGPIOPin(17, host='192.168.1.3')) factory = PiGPIOFactory(host='192.168.1.3')
led = LED(17, pin_factory=factory)
while True: while True:
led.on() led.on()

View File

@@ -1,9 +1,11 @@
from gpiozero import LED from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOPin from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep from time import sleep
led_1 = LED(PiGPIOPin(17, host='192.168.1.3')) factory3 = PiGPIOFactory(host='192.168.1.3')
led_2 = LED(PiGPIOPin(17, host='192.168.1.4')) factory4 = PiGPIOFactory(host='192.168.1.4')
led_1 = LED(17, pin_factory=factory3)
led_2 = LED(17, pin_factory=factory4)
while True: while True:
led_1.on() led_1.on()

View File

@@ -1,9 +1,10 @@
from gpiozero import LED from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOPin from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep from time import sleep
remote_factory = PiGPIOFactory(host='192.168.1.3')
led_1 = LED(17) # local pin led_1 = LED(17) # local pin
led_2 = LED(PiGPIOPin(17, host='192.168.1.3')) # remote pin led_2 = LED(17, pin_factory=remote_factory) # remote pin
while True: while True:
led_1.on() led_1.on()

View File

@@ -1,8 +1,9 @@
from gpiozero import LED from gpiozero import LED
from gpiozero.pins.rpigpio import RPiGPIOPin from gpiozero.pins.rpigpio import RPiGPIOFactory
from time import sleep from time import sleep
led_1 = LED(RPiGPIOPin(17)) # local pin local_factory = RPiGPIOFactory()
led_1 = LED(17, pin_factory=local_factory) # local pin
led_2 = LED(17) # remote pin led_2 = LED(17) # remote pin
while True: while True:

View File

@@ -1,10 +1,13 @@
from gpiozero import LED from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOPin from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep from time import sleep
factory3 = PiGPIOFactory(host='192.168.1.3')
factory4 = PiGPIOFactory(host='192.168.1.4')
led_1 = LED(17) # local pin led_1 = LED(17) # local pin
led_2 = LED(PiGPIOPin(17, host='192.168.1.3')) # remote pin on one pi led_2 = LED(17, pin_factory=factory3) # remote pin on one pi
led_3 = LED(PiGPIOPin(17, host='192.168.1.4')) # remote pin on another pi led_3 = LED(17, pin_factory=factory4) # remote pin on another pi
while True: while True:
led_1.on() led_1.on()

View File

@@ -1,8 +1,9 @@
from gpiozero import LED from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOPin from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause from signal import pause
led = LED(PiGPIOPin(17, host='raspberrypi.local')) factory = PiGPIOFactory(host='raspberrypi.local')
led = LED(17, pin_factory=factory)
led.blink() led.blink()

View File

@@ -1,8 +1,9 @@
from gpiozero import MotionSensor from gpiozero import MotionSensor
from gpiozero.pins.pigpio import PiGPIOPin from gpiozero.pins.pigpio import PiGPIOFactory
from sense_hat import SenseHat from sense_hat import SenseHat
pir = MotionSensor(PiGPIOPin(4, host='192.168.1.4')) # remote motion sensor remote_factory = PiGPIOFactory(host='192.198.1.4')
pir = MotionSensor(4, pin_factory=remote_factory) # remote motion sensor
sense = SenseHat() # local sense hat sense = SenseHat() # local sense hat
while True: while True:

View File

@@ -1,8 +1,9 @@
from gpiozero import LightSensor from gpiozero import LightSensor
from gpiozero.pins.pigpio import PiGPIOPin from gpiozero.pins.pigpio import PiGPIOFactory
from sense_hat import SenseHat from sense_hat import SenseHat
light = LightSensor(PiGPIOPin(4, host='192.168.1.4')) # remote motion sensor remote_factory = PiGPIOFactory(host='192.168.1.4')
light = LightSensor(4, pin_factory=remote_factory) # remote motion sensor
sense = SenseHat() # local sense hat sense = SenseHat() # local sense hat
blue = (0, 0, 255) blue = (0, 0, 255)

View File

@@ -3,5 +3,5 @@ from gpiozero import TrafficHat
from gpiozero.pins.pigpio import PiGPIOFactory from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep from time import sleep
gpiozero.Device._set_pin_factory(PiGPIOFactory(host='192.168.1.3')) gpiozero.Device.pin_factory = PiGPIOFactory(host='192.168.1.3')
th = TrafficHat() # traffic hat on 192.168.1.3 using remote pins th = TrafficHat() # traffic hat on 192.168.1.3 using remote pins

View File

@@ -3,6 +3,7 @@ from gpiozero import TrafficHat
from gpiozero.pins.pigpio import PiGPIOFactory from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep from time import sleep
remote_factory = PiGPIOFactory(host='192.168.1.3')
th_1 = TrafficHat() # traffic hat using local pins th_1 = TrafficHat() # traffic hat using local pins
gpiozero.Device._set_pin_factory(PiGPIOFactory(host='192.168.1.3')) th_2 = TrafficHat(pin_factory=remote_factory) # traffic hat on 192.168.1.3 using remote pins
th_2 = TrafficHat() # traffic hat on 192.168.1.3 using remote pins

View File

@@ -12,8 +12,8 @@ documentation page.
One of the pin libraries supported, `pigpio`_, provides the ability to control One of the pin libraries supported, `pigpio`_, provides the ability to control
GPIO pins remotely over the network, which means you can use GPIO Zero to GPIO pins remotely over the network, which means you can use GPIO Zero to
control devices connected to a Raspberry Pi on the network. You can do this from control devices connected to a Raspberry Pi on the network. You can do this
another Raspberry Pi, or even from a PC. from another Raspberry Pi, or even from a PC.
See the :doc:`recipes_remote_gpio` page for examples on how remote pins can be See the :doc:`recipes_remote_gpio` page for examples on how remote pins can be
used. used.
@@ -22,8 +22,8 @@ Preparing the Raspberry Pi
========================== ==========================
If you're using Raspbian Jessie (desktop - not Jessie Lite) then you have If you're using Raspbian Jessie (desktop - not Jessie Lite) then you have
everything you need to use the remote GPIO feature. If you're using Jessie Lite, everything you need to use the remote GPIO feature. If you're using Jessie
or another distribution, you'll need to install pigpio: Lite, or another distribution, you'll need to install pigpio::
.. code-block:: console .. code-block:: console
@@ -182,16 +182,16 @@ If you are running this from a PC (not a Raspberry Pi) with gpiozero and the
pigpio Python library installed, this will work with no further configuration. pigpio Python library installed, this will work with no further configuration.
However, if you are running this from a Raspberry Pi, you will also need to However, if you are running this from a Raspberry Pi, you will also need to
ensure the default pin factory is set to ``PiGPIOPin``. If ``RPi.GPIO`` is ensure the default pin factory is set to ``PiGPIOPin``. If ``RPi.GPIO`` is
installed, this will be selected as the default pin factory, so either uninstall installed, this will be selected as the default pin factory, so either
it, or use another environment variable to set it to ``PiGPIOPin``: uninstall it, or use another environment variable to set it to ``PiGPIOPin``:
.. code-block:: console .. code-block:: console
$ GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.3 python3 hello.py $ GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.3 python3 hello.py
This usage will set the pin factory to :class:`PiGPIOPin` with a default host of This usage will set the pin factory to :class:`PiGPIOFactory` with a default
``192.168.1.3``. The pin factory can be changed inline in the code, as seen in host of ``192.168.1.3``. The pin factory can be changed inline in the code, as
the following sections. seen in the following sections.
With this usage, you can write gpiozero code like you would on a Raspberry Pi, With this usage, you can write gpiozero code like you would on a Raspberry Pi,
with no modifications needed. For example: with no modifications needed. For example:
@@ -218,9 +218,9 @@ Pin objects
=========== ===========
An alternative (or additional) method of configuring gpiozero objects to use An alternative (or additional) method of configuring gpiozero objects to use
remote pins is to create instances of :class:PiGPIOPin objects, and remote pins is to create instances of :class:`PiGPIOFactory` objects, and use
instantiating device objects with those pin objects, rather than just numbers. them when instantiating device objects. For example, with no environment
For example, with no environment variables set: variables set:
.. literalinclude:: examples/led_remote_1.py .. literalinclude:: examples/led_remote_1.py
@@ -230,8 +230,8 @@ This allows devices on multiple Raspberry Pis to be used in the same script:
You can, of course, continue to create gpiozero device objects as normal, and You can, of course, continue to create gpiozero device objects as normal, and
create others using remote pins. For example, if run on a Raspberry Pi, the create others using remote pins. For example, if run on a Raspberry Pi, the
following script will flash an LED on the host Pi, and also on another Pi on the following script will flash an LED on the host Pi, and also on another Pi on
network: the network:
.. literalinclude:: examples/led_remote_3.py .. literalinclude:: examples/led_remote_3.py
@@ -249,7 +249,8 @@ Note that these examples use the :class:`LED` class, which takes a ``pin``
argument to initialise. Some classes, particularly those representing HATs and argument to initialise. Some classes, particularly those representing HATs and
other add-on boards, do not require their pin numbers to be specified. However, other add-on boards, do not require their pin numbers to be specified. However,
it is still possible to use remote pins with these devices, either using it is still possible to use remote pins with these devices, either using
environment variables, or by using :meth:`~Device._set_pin_factory`: environment variables, :attr:`Device.pin_factory`, or the ``pin_factory``
keyword argument:
.. literalinclude:: examples/traffichat_remote_1.py .. literalinclude:: examples/traffichat_remote_1.py
@@ -299,10 +300,10 @@ from the computer, referencing the host by its hostname, like so:
.. note:: .. note::
When running code directly on a Raspberry Pi, any pin type can be used When running code directly on a Raspberry Pi, any pin factory can be used
(assuming the relevant library is installed), but when a device is used (assuming the relevant library is installed), but when a device is used
remotely, only :class:`PiGPIOPin` can be used, as pigpio is the only pin remotely, only :class:`PiGPIOFactory` can be used, as pigpio is the only
library which supports remote GPIO. pin library which supports remote GPIO.
.. _RPi.GPIO: https://pypi.python.org/pypi/RPi.GPIO .. _RPi.GPIO: https://pypi.python.org/pypi/RPi.GPIO