Remote gpio raspberry что это
Перейти к содержимому

Remote gpio raspberry что это

  • автор:

4. Configuring Remote GPIO¶

GPIO Zero supports a number of different pin implementations (low-level pin libraries which deal with the GPIO pins directly). By default, the RPi.GPIO library is used (assuming it is installed on your system), but you can optionally specify one to use. For more information, see the API — Pins documentation page.

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 control devices connected to a Raspberry Pi on the network. You can do this from another Raspberry Pi, or even from a PC.

See the Remote GPIO Recipes page for examples on how remote pins can be used.

4.1. Preparing the Raspberry Pi¶

If you’re using Raspberry Pi OS (desktop — not Lite) then you have everything you need to use the remote GPIO feature. If you’re using Raspberry Pi OS Lite, or another distribution, you’ll need to install pigpio:

$ sudo apt install pigpio

Alternatively, pigpio is available from abyz.me.uk.

You’ll need to enable remote connections, and launch the pigpio daemon on the Raspberry Pi.

4.1.1. Enable remote connections¶

On the Raspberry Pi OS desktop image, you can enable Remote GPIO in the Raspberry Pi configuration tool:

_images/raspi-config.png

Alternatively, enter sudo raspi-config on the command line, and enable Remote GPIO. This is functionally equivalent to the desktop method.

This will allow remote connections (until disabled) when the pigpio daemon is launched using systemctl (see below). It will also launch the pigpio daemon for the current session. Therefore, nothing further is required for the current session, but after a reboot, a systemctl command will be required.

4.1.2. Command-line: systemctl¶

To automate running the daemon at boot time, run:

$ sudo systemctl enable pigpiod

To run the daemon once using systemctl, run:

$ sudo systemctl start pigpiod

4.1.3. Command-line: pigpiod¶

Another option is to launch the pigpio daemon manually:

$ sudo pigpiod

This is for single-session-use and will not persist after a reboot. However, this method can be used to allow connections from a specific IP address, using the -n flag. For example:

$ sudo pigpiod -n localhost # allow localhost only $ sudo pigpiod -n 192.168.1.65 # allow 192.168.1.65 only $ sudo pigpiod -n localhost -n 192.168.1.65 # allow localhost and 192.168.1.65 only 

Note that running sudo pigpiod will not honour the Remote GPIO configuration setting (i.e. without the -n flag it will allow remote connections even if the remote setting is disabled), but sudo systemctl enable pigpiod or sudo systemctl start pigpiod will not allow remote connections unless configured accordingly.

4.2. Preparing the control computer¶

If the control computer (the computer you’re running your Python code from) is a Raspberry Pi running Raspberry Pi OS (or a PC running Raspberry Pi Desktop x86), then you have everything you need. If you’re using another Linux distribution, Mac OS or Windows then you’ll need to install the pigpio Python library on the PC.

4.2.1. Raspberry Pi¶

First, update your repositories list:

$ sudo apt update

Then install GPIO Zero and the pigpio library for Python 3:

$ sudo apt install python3-gpiozero python3-pigpio
$ sudo apt install python-gpiozero python-pigpio

Alternatively, install with pip:

$ sudo pip3 install gpiozero pigpio

or for Python 2:

$ sudo pip install gpiozero pigpio

4.2.2. Linux¶

First, update your distribution’s repositories list. For example:

$ sudo apt update

Then install pip for Python 3:

$ sudo apt install python3-pip
$ sudo apt install python-pip

(Alternatively, install pip with get-pip.)

Next, install GPIO Zero and pigpio for Python 3:

$ sudo pip3 install gpiozero pigpio
$ sudo pip install gpiozero pigpio

4.2.3. Mac OS¶

First, install pip. If you installed Python 3 using brew, you will already have pip. If not, install pip with get-pip.

Next, install GPIO Zero and pigpio with pip:

$ pip3 install gpiozero pigpio

Or for Python 2:

$ pip install gpiozero pigpio

4.2.4. Windows¶

Modern Python installers for Windows bundle pip with Python. If pip is not installed, you can follow this guide. Next, install GPIO Zero and pigpio with pip:

C:\Users\user1> pip install gpiozero pigpio

4.3. Environment variables¶

The simplest way to use devices with remote pins is to set the PIGPIO_ADDR environment variable to the IP address of the desired Raspberry Pi. You must run your Python script or launch your development environment with the environment variable set using the command line. For example, one of the following:

$ PIGPIO_ADDR=192.168.1.3 python3 hello.py $ PIGPIO_ADDR=192.168.1.3 python3 $ PIGPIO_ADDR=192.168.1.3 ipython3 $ PIGPIO_ADDR=192.168.1.3 idle3 & 

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. However, if you are running this from a Raspberry Pi, you will also need to ensure the default pin factory is set to PiGPIOFactory . If RPi.GPIO is installed, this will be selected as the default pin factory, so either uninstall it, or use the GPIOZERO_PIN_FACTORY environment variable to override it:

$ GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.3 python3 hello.py

This usage will set the pin factory to PiGPIOFactory with a default host of 192.168.1.3 . The pin factory can be changed inline in the code, as seen in the following sections.

With this usage, you can write gpiozero code like you would on a Raspberry Pi, with no modifications needed. For example:

from gpiozero import LED from time import sleep red = LED(17) while True: red.on() sleep(1) red.off() sleep(1) 
$ PIGPIO_ADDR=192.168.1.3 python3 led.py

will flash the LED connected to pin 17 of the Raspberry Pi with the IP address 192.168.1.3 . And:

$ PIGPIO_ADDR=192.168.1.4 python3 led.py

will flash the LED connected to pin 17 of the Raspberry Pi with the IP address 192.168.1.4 , without any code changes, as long as the Raspberry Pi has the pigpio daemon running.

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 remotely, only PiGPIOFactory can be used, as pigpio is the only pin library which supports remote GPIO.

4.4. Pin factories¶

An alternative (or additional) method of configuring gpiozero objects to use remote pins is to create instances of PiGPIOFactory objects, and use them when instantiating device objects. For example, with no environment variables set:

from gpiozero import LED from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep factory = PiGPIOFactory(host='192.168.1.3') led = LED(17, pin_factory=factory) while True: led.on() sleep(1) led.off() sleep(1) 

This allows devices on multiple Raspberry Pis to be used in the same script:

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

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 following script will flash an LED on the controller Pi, and also on another Pi on the network:

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

Alternatively, when run with the environment variables GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.3 set, the following script will behave exactly the same as the previous one:

from gpiozero import LED from gpiozero.pins.rpigpio import RPiGPIOFactory from time import sleep local_factory = RPiGPIOFactory() led_1 = LED(17, pin_factory=local_factory) # local pin led_2 = LED(17) # remote pin while True: led_1.on() led_2.off() sleep(1) led_1.off() led_2.on() sleep(1) 

Of course, multiple IP addresses can be used:

from gpiozero import LED from gpiozero.pins.pigpio import PiGPIOFactory 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_2 = LED(17, pin_factory=factory3) # remote pin on one pi led_3 = LED(17, pin_factory=factory4) # remote pin on another pi while True: led_1.on() led_2.off() led_3.on() sleep(1) led_1.off() led_2.on() led_3.off() sleep(1) 

Note that these examples use the LED class, which takes a pin argument to initialise. Some classes, particularly those representing HATs and 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 environment variables, or the pin_factory keyword argument:

import gpiozero from gpiozero import TrafficHat from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep gpiozero.Device.pin_factory = PiGPIOFactory(host='192.168.1.3') th = TrafficHat() # traffic hat on 192.168.1.3 using remote pins 

This also allows you to swap between two IP addresses and create instances of multiple HATs connected to different Pis:

import gpiozero from gpiozero import TrafficHat from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep remote_factory = PiGPIOFactory(host='192.168.1.3') th_1 = TrafficHat() # traffic hat using local pins th_2 = TrafficHat(pin_factory=remote_factory) # traffic hat on 192.168.1.3 using remote pins 

You could even use a HAT which is not supported by GPIO Zero (such as the Sense HAT) on one Pi, and use remote pins to control another over the network:

from gpiozero import MotionSensor from gpiozero.pins.pigpio import PiGPIOFactory from sense_hat import SenseHat remote_factory = PiGPIOFactory(host='192.198.1.4') pir = MotionSensor(4, pin_factory=remote_factory) # remote motion sensor sense = SenseHat() # local sense hat while True: pir.wait_for_motion() sense.show_message(sense.temperature) 

Note that in this case, the Sense HAT code must be run locally, and the GPIO remotely.

Remote control GPIO on a Raspberry Pi with GPIO Zero

GPIO Zero is a very powerful tool, and now you can use it when you’re not even on the Raspberry Pi! Using the new Remote control GPIO feature.

Advertisement

The MagPi issue 137 cover

The MagPi issue 137 out now

Begin your Raspberry Pi Adventure in the latest edition of The MagPi magazine. Everything an intrepid maker needs is inside! Acquire your vital equipment, assemble your kit, and discover maker quests. Start your new adventure today!

The GPIO Zero Python library not only makes programming simple electronics easier; it comes with some advanced features.

These offer seamless interfacing between different devices, while helping you progress along the Python learning curve. One useful thing about GPIO Zero is that you can choose which low-level pin library to use, allowing you to take advantage of the power of another library as required, without having to rewrite your code. By default, Ben Croston’s RPi.GPIO library is used, and that’s fine for most purposes.

One of the supported alternative libraries is Joan 2937’s pigpio library, which supports remote GPIO. This allows you to remotely control the GPIO pins of a Pi over a network. You can control the pins from a PC or Mac, or from another Pi, and even use the GPIOs of multiple Pis within the same script.

This month, GPIO Zero v1.4 was released, stabilising the remote pins syntax. This guide is written for v1.4 and will not work on earlier versions. Make sure you upgrade before you start: open a Terminal and enter:

sudo apt update && sudo apt install python3-gpiozero
  • This tutorial first appeared in The MagPi issue 60 and was written by Ben Nuttal. Ben is Raspberry Pi’s Community Manager, creator of the GPIO Zero library, Jam master, and the Foundation’s resident Python expert!

Remote control GPIO: How to use GPIO Zero

A simple GPIO Zero Python script looks like this:

from gpiozero import Button, LED from signal import pause btn = Button(2) led = LED(17) led.source = btn.values pause()

Running this script on a Pi will work as expected: a button connected to pin 2 (BCM numbering) will light an LED connected to pin 17 when pressed. However, when configured correctly, running this same script can control the pins of a Pi over the network.

Remote control GPIO: Pin factories

The way GPIO Zero wraps around low-level pin libraries is by providing a pin factory. By default, an RPi.GPIO-based factory is used, and when you ask for a pin, the factory gives you a connection to it using the chosen pin library. A pigpio pin factory can be used on its own (simply use the pigpio library instead of RPi.GPIO), but if an IP address is provided too, this can be used to remotely control a Pi’s pins.

To run the above script (unchanged) on a remote Pi, the Pi needs to be configured to accept remote connections. This can be done using the Raspberry Pi configuration tool (via GUI or sudo raspi-config), by enabling Remote GPIO under Interfaces. Otherwise, the Pi needs to have the pigpio daemon running, by entering sudo pigpiod in a Terminal. Finally, look up the Pi’s IP address with hostname -I. Now return to the Pi you’re running the script from, and instead of running the code normally (like python3 led_button.py), set two environment variables in the same command, using the remote Pi’s IP address:

GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.5 python3 led_button.py

Now, when the script runs, the GPIO commands are executed on the remote Pi over the network.

An alternative to running a script from the command line is to set the environment variables before launching your Python editor. For example:

GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.5 idle3 &

You can also export these variables in your .bashrc file. See magpi.cc/2qd2MEb for more information.

Hot-swapping pin factories

The previous example showed how to set the default pin factory. Unless otherwise specified, any GPIO devices created will be connected to pins created by this default pin factory. Alternatively, you can specify a pin factory (and with pigpio, an IP address) within the Python code. There are two options for doing this. You can create a pin factory instance, and pass that in as you create a new object, like so:

from gpiozero import LED, Button from gpiozero.pins.pigpio import PiGPIOFactory from signal import pause factory = PiGPIOFactory('192.168.1.5') btn = Button(2) # local RPi.GPIO pin led = LED(17, pin_factory=factory) # remote pin led.source = button.values pause()

Alternatively, you can change the default pin factory in the middle of your script, like so:

import gpiozero from gpiozero import LED, Button from gpiozero.pins.pigpio import PiGPIOFactory from signal import pause btn = Button(2) # local RPi.GPIO pin gpiozero.Device.pin_factory = PiGPIOFactory('192.168.1.5') led = LED(17) # remote pin led.source = button.values pause()

Press the button on your Pi and watch the LED light up on the remote Pi. With no environment variables set, RPi.GPIO is used as the default pin factory. When the button is created, it uses RPi.GPIO to address a local pin. The default pin factory is replaced with pigpio, connecting to a particular IP address, and the LED is created on pin 17, which now refers to the remote Pi.

While this can be a confusing concept, it’s quite simple once you get used to the idea, and it could be very useful in many projects. You can even run this code on a PC (not a Raspberry Pi) and use it to control a Pi on the network. Any platform (Windows, Mac or Linux) will work, as long as you have Python, pip, GPIO Zero, and pigpio installed. For full instructions, head over to the Remote GPIO documentation.

Security in Remote control GPIO Zero

It’s worth pointing out that allowing remote GPIO connections over the network can be risky. You probably shouldn’t do this in a real project on a network with other users. However, you can take precautions to make it more secure. An easy method is to only allow remote connections from a particular IP address when launching the pigpio daemon:

sudo pigpiod -n 192.168.1.4

Learn more about GPIO Zero

GPIO Zero is an amazing tool for creating simple electronics and making your projects just that little bit easier. Check out our Essentials book, Simple Electronics with GPIO Zero.

4. Configuring Remote GPIO¶

GPIO Zero supports a number of different pin implementations (low-level pin libraries which deal with the GPIO pins directly). By default, the RPi.GPIO library is used (assuming it is installed on your system), but you can optionally specify one to use. For more information, see the API — Pins documentation page.

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 control devices connected to a Raspberry Pi on the network. You can do this from another Raspberry Pi, or even from a PC.

See the Remote GPIO Recipes page for examples on how remote pins can be used.

4.1. Preparing the Raspberry Pi¶

If you’re using Raspbian (desktop — not Raspbian Lite) then you have everything you need to use the remote GPIO feature. If you’re using Raspbian Lite, or another distribution, you’ll need to install pigpio:

$ sudo apt install pigpio

Alternatively, pigpio is available from abyz.me.uk.

You’ll need to enable remote connections, and launch the pigpio daemon on the Raspberry Pi.

4.1.1. Enable remote connections¶

On the Raspbian desktop image, you can enable Remote GPIO in the Raspberry Pi configuration tool:

_images/raspi-config.png

Alternatively, enter sudo raspi-config on the command line, and enable Remote GPIO. This is functionally equivalent to the desktop method.

This will allow remote connections (until disabled) when the pigpio daemon is launched using systemctl (see below). It will also launch the pigpio daemon for the current session. Therefore, nothing further is required for the current session, but after a reboot, a systemctl command will be required.

4.1.2. Command-line: systemctl¶

To automate running the daemon at boot time, run:

$ sudo systemctl enable pigpiod

To run the daemon once using systemctl , run:

$ sudo systemctl start pigpiod

4.1.3. Command-line: pigpiod¶

Another option is to launch the pigpio daemon manually:

$ sudo pigpiod

This is for single-session-use and will not persist after a reboot. However, this method can be used to allow connections from a specific IP address, using the -n flag. For example:

$ sudo pigpiod -n localhost # allow localhost only $ sudo pigpiod -n 192.168.1.65 # allow 192.168.1.65 only $ sudo pigpiod -n localhost -n 192.168.1.65 # allow localhost and 192.168.1.65 only 

Note that running sudo pigpiod will not honour the Remote GPIO configuration setting (i.e. without the -n flag it will allow remote connections even if the remote setting is disabled), but sudo systemctl enable pigpiod or sudo systemctl start pigpiod will not allow remote connections unless configured accordingly.

4.2. Preparing the control computer¶

If the control computer (the computer you’re running your Python code from) is a Raspberry Pi running Raspbian (or a PC running Raspberry Pi Desktop x86), then you have everything you need. If you’re using another Linux distribution, Mac OS or Windows then you’ll need to install the pigpio Python library on the PC.

4.2.1. Raspberry Pi¶

First, update your repositories list:

$ sudo apt update

Then install GPIO Zero and the pigpio library for Python 3:

$ sudo apt install python3-gpiozero python3-pigpio
$ sudo apt install python-gpiozero python-pigpio

Alternatively, install with pip:

$ sudo pip3 install gpiozero pigpio

or for Python 2:

$ sudo pip install gpiozero pigpio

4.2.2. Linux¶

First, update your distribution’s repositories list. For example:

$ sudo apt update

Then install pip for Python 3:

$ sudo apt install python3-pip
$ sudo apt install python-pip

(Alternatively, install pip with get-pip.)

Next, install GPIO Zero and pigpio for Python 3:

$ sudo pip3 install gpiozero pigpio
$ sudo pip install gpiozero pigpio

4.2.3. Mac OS¶

First, install pip. If you installed Python 3 using brew, you will already have pip. If not, install pip with get-pip.

Next, install GPIO Zero and pigpio with pip:

$ pip3 install gpiozero pigpio

Or for Python 2:

$ pip install gpiozero pigpio

4.2.4. Windows¶

First, install pip by following this guide. Next, install GPIO Zero and pigpio with pip:

C:\Users\user1> pip install gpiozero pigpio

4.3. Environment variables¶

The simplest way to use devices with remote pins is to set the PIGPIO_ADDR environment variable to the IP address of the desired Raspberry Pi. You must run your Python script or launch your development environment with the environment variable set using the command line. For example, one of the following:

$ PIGPIO_ADDR=192.168.1.3 python3 hello.py $ PIGPIO_ADDR=192.168.1.3 python3 $ PIGPIO_ADDR=192.168.1.3 ipython3 $ PIGPIO_ADDR=192.168.1.3 idle3 & 

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. However, if you are running this from a Raspberry Pi, you will also need to ensure the default pin factory is set to PiGPIOFactory . If RPi.GPIO is installed, this will be selected as the default pin factory, so either uninstall it, or use another environment variable to set it to PiGPIOFactory :

$ GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.3 python3 hello.py

This usage will set the pin factory to PiGPIOFactory with a default host of 192.168.1.3 . The pin factory can be changed inline in the code, as seen in the following sections.

With this usage, you can write gpiozero code like you would on a Raspberry Pi, with no modifications needed. For example:

from gpiozero import LED from time import sleep red = LED(17) while True: red.on() sleep(1) red.off() sleep(1) 
$ PIGPIO_ADDR=192.168.1.3 python3 led.py

will flash the LED connected to pin 17 of the Raspberry Pi with the IP address 192.168.1.3 . And:

$ PIGPIO_ADDR=192.168.1.4 python3 led.py

will flash the LED connected to pin 17 of the Raspberry Pi with the IP address 192.168.1.4 , without any code changes, as long as the Raspberry Pi has the pigpio daemon running.

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 remotely, only PiGPIOFactory can be used, as pigpio is the only pin library which supports remote GPIO.

4.4. Pin factories¶

An alternative (or additional) method of configuring gpiozero objects to use remote pins is to create instances of PiGPIOFactory objects, and use them when instantiating device objects. For example, with no environment variables set:

from gpiozero import LED from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep factory = PiGPIOFactory(host='192.168.1.3') led = LED(17, pin_factory=factory) while True: led.on() sleep(1) led.off() sleep(1) 

This allows devices on multiple Raspberry Pis to be used in the same script:

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

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 following script will flash an LED on the controller Pi, and also on another Pi on the network:

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

Alternatively, when run with the environment variables GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.3 set, the following script will behave exactly the same as the previous one:

from gpiozero import LED from gpiozero.pins.rpigpio import RPiGPIOFactory from time import sleep local_factory = RPiGPIOFactory() led_1 = LED(17, pin_factory=local_factory) # local pin led_2 = LED(17) # remote pin while True: led_1.on() led_2.off() sleep(1) led_1.off() led_2.on() sleep(1) 

Of course, multiple IP addresses can be used:

from gpiozero import LED from gpiozero.pins.pigpio import PiGPIOFactory 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_2 = LED(17, pin_factory=factory3) # remote pin on one pi led_3 = LED(17, pin_factory=factory4) # remote pin on another pi while True: led_1.on() led_2.off() led_3.on() sleep(1) led_1.off() led_2.on() led_3.off() sleep(1) 

Note that these examples use the LED class, which takes a pin argument to initialise. Some classes, particularly those representing HATs and 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 environment variables, Device.pin_factory , or the pin_factory keyword argument:

import gpiozero from gpiozero import TrafficHat from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep gpiozero.Device.pin_factory = PiGPIOFactory(host='192.168.1.3') th = TrafficHat() # traffic hat on 192.168.1.3 using remote pins 

This also allows you to swap between two IP addresses and create instances of multiple HATs connected to different Pis:

import gpiozero from gpiozero import TrafficHat from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep remote_factory = PiGPIOFactory(host='192.168.1.3') th_1 = TrafficHat() # traffic hat using local pins th_2 = TrafficHat(pin_factory=remote_factory) # traffic hat on 192.168.1.3 using remote pins 

You could even use a HAT which is not supported by GPIO Zero (such as the Sense HAT) on one Pi, and use remote pins to control another over the network:

from gpiozero import MotionSensor from gpiozero.pins.pigpio import PiGPIOFactory from sense_hat import SenseHat remote_factory = PiGPIOFactory(host='192.198.1.4') pir = MotionSensor(4, pin_factory=remote_factory) # remote motion sensor sense = SenseHat() # local sense hat while True: pir.wait_for_motion() sense.show_message(sense.temperature) 

Note that in this case, the Sense HAT code must be run locally, and the GPIO remotely.

4. Configuring Remote GPIO¶

GPIO Zero supports a number of different pin implementations (low-level pin libraries which deal with the GPIO pins directly). By default, the RPi.GPIO library is used (assuming it is installed on your system), but you can optionally specify one to use. For more information, see the API — Pins documentation page.

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 control devices connected to a Raspberry Pi on the network. You can do this from another Raspberry Pi, or even from a PC.

See the Remote GPIO Recipes page for examples on how remote pins can be used.

4.1. Preparing the Raspberry Pi¶

If you’re using Raspberry Pi OS (desktop — not Lite) then you have everything you need to use the remote GPIO feature. If you’re using Raspberry Pi OS Lite, or another distribution, you’ll need to install pigpio:

$ sudo apt install pigpio

Alternatively, pigpio is available from abyz.me.uk.

You’ll need to enable remote connections, and launch the pigpio daemon on the Raspberry Pi.

4.1.1. Enable remote connections¶

On the Raspberry Pi OS desktop image, you can enable Remote GPIO in the Raspberry Pi configuration tool:

_images/raspi-config.png

Alternatively, enter sudo raspi-config on the command line, and enable Remote GPIO. This is functionally equivalent to the desktop method.

This will allow remote connections (until disabled) when the pigpio daemon is launched using systemctl (see below). It will also launch the pigpio daemon for the current session. Therefore, nothing further is required for the current session, but after a reboot, a systemctl command will be required.

4.1.2. Command-line: systemctl¶

To automate running the daemon at boot time, run:

$ sudo systemctl enable pigpiod

To run the daemon once using systemctl, run:

$ sudo systemctl start pigpiod

4.1.3. Command-line: pigpiod¶

Another option is to launch the pigpio daemon manually:

$ sudo pigpiod

This is for single-session-use and will not persist after a reboot. However, this method can be used to allow connections from a specific IP address, using the -n flag. For example:

$ sudo pigpiod -n localhost # allow localhost only $ sudo pigpiod -n 192.168.1.65 # allow 192.168.1.65 only $ sudo pigpiod -n localhost -n 192.168.1.65 # allow localhost and 192.168.1.65 only 

Note that running sudo pigpiod will not honour the Remote GPIO configuration setting (i.e. without the -n flag it will allow remote connections even if the remote setting is disabled), but sudo systemctl enable pigpiod or sudo systemctl start pigpiod will not allow remote connections unless configured accordingly.

4.2. Preparing the control computer¶

If the control computer (the computer you’re running your Python code from) is a Raspberry Pi running Raspberry Pi OS (or a PC running Raspberry Pi Desktop x86), then you have everything you need. If you’re using another Linux distribution, Mac OS or Windows then you’ll need to install the pigpio Python library on the PC.

4.2.1. Raspberry Pi¶

First, update your repositories list:

$ sudo apt update

Then install GPIO Zero and the pigpio library for Python 3:

$ sudo apt install python3-gpiozero python3-pigpio
$ sudo apt install python-gpiozero python-pigpio

Alternatively, install with pip:

$ sudo pip3 install gpiozero pigpio

or for Python 2:

$ sudo pip install gpiozero pigpio

4.2.2. Linux¶

First, update your distribution’s repositories list. For example:

$ sudo apt update

Then install pip for Python 3:

$ sudo apt install python3-pip
$ sudo apt install python-pip

(Alternatively, install pip with get-pip.)

Next, install GPIO Zero and pigpio for Python 3:

$ sudo pip3 install gpiozero pigpio
$ sudo pip install gpiozero pigpio

4.2.3. Mac OS¶

First, install pip. If you installed Python 3 using brew, you will already have pip. If not, install pip with get-pip.

Next, install GPIO Zero and pigpio with pip:

$ pip3 install gpiozero pigpio

Or for Python 2:

$ pip install gpiozero pigpio

4.2.4. Windows¶

Modern Python installers for Windows bundle pip with Python. If pip is not installed, you can follow this guide. Next, install GPIO Zero and pigpio with pip:

C:\Users\user1> pip install gpiozero pigpio

4.3. Environment variables¶

The simplest way to use devices with remote pins is to set the PIGPIO_ADDR environment variable to the IP address of the desired Raspberry Pi. You must run your Python script or launch your development environment with the environment variable set using the command line. For example, one of the following:

$ PIGPIO_ADDR=192.168.1.3 python3 hello.py $ PIGPIO_ADDR=192.168.1.3 python3 $ PIGPIO_ADDR=192.168.1.3 ipython3 $ PIGPIO_ADDR=192.168.1.3 idle3 & 

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. However, if you are running this from a Raspberry Pi, you will also need to ensure the default pin factory is set to PiGPIOFactory . If RPi.GPIO is installed, this will be selected as the default pin factory, so either uninstall it, or use the GPIOZERO_PIN_FACTORY environment variable to override it:

$ GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.3 python3 hello.py

This usage will set the pin factory to PiGPIOFactory with a default host of 192.168.1.3 . The pin factory can be changed inline in the code, as seen in the following sections.

With this usage, you can write gpiozero code like you would on a Raspberry Pi, with no modifications needed. For example:

from gpiozero import LED from time import sleep red = LED(17) while True: red.on() sleep(1) red.off() sleep(1) 
$ PIGPIO_ADDR=192.168.1.3 python3 led.py

will flash the LED connected to pin 17 of the Raspberry Pi with the IP address 192.168.1.3 . And:

$ PIGPIO_ADDR=192.168.1.4 python3 led.py

will flash the LED connected to pin 17 of the Raspberry Pi with the IP address 192.168.1.4 , without any code changes, as long as the Raspberry Pi has the pigpio daemon running.

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 remotely, only PiGPIOFactory can be used, as pigpio is the only pin library which supports remote GPIO.

4.4. Pin factories¶

An alternative (or additional) method of configuring gpiozero objects to use remote pins is to create instances of PiGPIOFactory objects, and use them when instantiating device objects. For example, with no environment variables set:

from gpiozero import LED from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep factory = PiGPIOFactory(host='192.168.1.3') led = LED(17, pin_factory=factory) while True: led.on() sleep(1) led.off() sleep(1) 

This allows devices on multiple Raspberry Pis to be used in the same script:

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

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 following script will flash an LED on the controller Pi, and also on another Pi on the network:

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

Alternatively, when run with the environment variables GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.3 set, the following script will behave exactly the same as the previous one:

from gpiozero import LED from gpiozero.pins.rpigpio import RPiGPIOFactory from time import sleep local_factory = RPiGPIOFactory() led_1 = LED(17, pin_factory=local_factory) # local pin led_2 = LED(17) # remote pin while True: led_1.on() led_2.off() sleep(1) led_1.off() led_2.on() sleep(1) 

Of course, multiple IP addresses can be used:

from gpiozero import LED from gpiozero.pins.pigpio import PiGPIOFactory 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_2 = LED(17, pin_factory=factory3) # remote pin on one pi led_3 = LED(17, pin_factory=factory4) # remote pin on another pi while True: led_1.on() led_2.off() led_3.on() sleep(1) led_1.off() led_2.on() led_3.off() sleep(1) 

Note that these examples use the LED class, which takes a pin argument to initialise. Some classes, particularly those representing HATs and 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 environment variables, or the pin_factory keyword argument:

import gpiozero from gpiozero import TrafficHat from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep gpiozero.Device.pin_factory = PiGPIOFactory(host='192.168.1.3') th = TrafficHat() # traffic hat on 192.168.1.3 using remote pins 

This also allows you to swap between two IP addresses and create instances of multiple HATs connected to different Pis:

import gpiozero from gpiozero import TrafficHat from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep remote_factory = PiGPIOFactory(host='192.168.1.3') th_1 = TrafficHat() # traffic hat using local pins th_2 = TrafficHat(pin_factory=remote_factory) # traffic hat on 192.168.1.3 using remote pins 

You could even use a HAT which is not supported by GPIO Zero (such as the Sense HAT) on one Pi, and use remote pins to control another over the network:

from gpiozero import MotionSensor from gpiozero.pins.pigpio import PiGPIOFactory from sense_hat import SenseHat remote_factory = PiGPIOFactory(host='192.198.1.4') pir = MotionSensor(4, pin_factory=remote_factory) # remote motion sensor sense = SenseHat() # local sense hat while True: pir.wait_for_motion() sense.show_message(sense.temperature) 

Note that in this case, the Sense HAT code must be run locally, and the GPIO remotely.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *