From 0507273d8a4b0a7c528ca4584f9269be83b5ecd3 Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Sun, 12 Mar 2017 20:23:19 +0000 Subject: [PATCH] Add Pi Zero OTG, sense hat examples and more docs --- docs/examples/pi_zero_remote.py | 9 +++++ docs/examples/sense_hat_remote.py | 10 ++++++ docs/examples/sense_hat_remote_2.py | 15 +++++++++ docs/recipes_remote_gpio.rst | 16 +++++++++ docs/remote_gpio.rst | 52 ++++++++++++++++++++++++----- 5 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 docs/examples/pi_zero_remote.py create mode 100644 docs/examples/sense_hat_remote.py create mode 100644 docs/examples/sense_hat_remote_2.py diff --git a/docs/examples/pi_zero_remote.py b/docs/examples/pi_zero_remote.py new file mode 100644 index 0000000..553460d --- /dev/null +++ b/docs/examples/pi_zero_remote.py @@ -0,0 +1,9 @@ +from gpiozero import LED +from gpiozero.pins.pigpio import PiGPIOPin +from signal import pause + +led = LED(PiGPIOPin(17, host='raspberrypi.local')) + +led.blink() + +pause() diff --git a/docs/examples/sense_hat_remote.py b/docs/examples/sense_hat_remote.py new file mode 100644 index 0000000..0b93178 --- /dev/null +++ b/docs/examples/sense_hat_remote.py @@ -0,0 +1,10 @@ +from gpiozero import MotionSensor +from gpiozero.pins.pigpio import PiGPIOPin +from sense_hat import SenseHat + +pir = MotionSensor(PiGPIOPin(4, host='192.168.1.4')) # remote motion sensor +sense = SenseHat() # local sense hat + +while True: + pir.wait_for_motion() + sense.show_message(sense.temperature) diff --git a/docs/examples/sense_hat_remote_2.py b/docs/examples/sense_hat_remote_2.py new file mode 100644 index 0000000..c13eba9 --- /dev/null +++ b/docs/examples/sense_hat_remote_2.py @@ -0,0 +1,15 @@ +from gpiozero import LightSensor +from gpiozero.pins.pigpio import PiGPIOPin +from sense_hat import SenseHat + +light = LightSensor(PiGPIOPin(4, host='192.168.1.4')) # remote motion sensor +sense = SenseHat() # local sense hat + +blue = (0, 0, 255) +yellow = (255, 255, 0) + +while True: + if light.value > 0.5: + sense.clear(yellow) + else: + sense.clear(blue) diff --git a/docs/recipes_remote_gpio.rst b/docs/recipes_remote_gpio.rst index 9d681be..650732f 100644 --- a/docs/recipes_remote_gpio.rst +++ b/docs/recipes_remote_gpio.rst @@ -43,3 +43,19 @@ doorbell, and use a push button as the doorbell: This could also be used as an internal doorbell (tell people it's time for dinner from the kitchen). + +Light sensor + Sense HAT +========================= + +The `Sense HAT`_ (not supported by GPIO Zero) includes temperature, humidity and +pressure sensors, but no light sensor. Remote pins allow an external light +sensor to be used as well. The Sense HAT LED display can be used to show +different colours according to the light levels: + +.. literalinclude:: examples/sense_hat_remote_2.py + +Note that in this case, the Sense HAT code must be run locally, and the GPIO +remotely. + + +.. _Sense HAT: https://www.raspberrypi.org/products/sense-hat/ diff --git a/docs/remote_gpio.rst b/docs/remote_gpio.rst index 0c308d8..9617f3e 100644 --- a/docs/remote_gpio.rst +++ b/docs/remote_gpio.rst @@ -207,7 +207,7 @@ Note that these examples use the :class:`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 by setting ``gpiozero.Device._pin_factory``: +environment variables, or by setting :attr:~Device._pin_factory`: .. literalinclude:: examples/traffichat_remote_1.py @@ -216,21 +216,55 @@ mutliple HATs connected to different Pis: .. literalinclude:: examples/traffichat_remote_2.py -Energenie example??? -MCP3008 example??? +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: + +.. literalinclude:: examples/sense_hat_remote.py + +Note that in this case, the Sense HAT code must be run locally, and the GPIO +remotely. + +Pi Zero USB OTG +=============== + +The `Raspberry Pi Zero`_ and `Pi Zero W`_ feature a USB OTG port, allowing users +to configure the device as (amongst other things) an Ethernet device. In this +mode, it is possible to control the Pi Zero's GPIO pins over USB from another +computer using remote pins. + +First, configure the boot partition of the SD card: + +1. Edit ``config.txt`` and add ``dtoverlay=dwc2`` on a new line, then save the +file. +1. Create an empty file called ``ssh`` (no file extension) and save it in the +boot partition. +1. Edit ``cmdline.txt`` and insert ``modules-load=dwc2,g_ether`` after +``rootwait``. + +(See `blog.gbaman.info`_ for more information) + +Then connect the Pi Zero to your computer using a micro USB cable (connecting it +to the USB port, not the power port). You'll see the indicator LED flashing as +the Pi Zero boots. When it's ready, you will be able to ping and SSH into it +using the hostname ``raspberrypi.local``. SSH into the Pi Zero, ensure Remote +GPIO is enabled and the pigpio daemon is running, and you can use remote pins +from the computer, referencing the host by its hostname, like so: + +.. literalinclude:: examples/pi_zero_remote.py .. note:: When running code directly on a Raspberry Pi, any pin type can be 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 library which supports remote GPIO. + remotely, only :class:`PiGPIOPin` can be used, as pigpio is the only pin + library which supports remote GPIO. -Pi Zero -======= - -??? .. _RPi.GPIO: https://pypi.python.org/pypi/RPi.GPIO .. _pigpio: http://abyz.co.uk/rpi/pigpio/python.html .. _get-pip: https://pip.pypa.io/en/stable/installing/ +.. _Sense HAT: https://www.raspberrypi.org/products/sense-hat/ +.. _Raspberry Pi Zero: https://www.raspberrypi.org/products/pi-zero/ +.. _Pi Zero W: https://www.raspberrypi.org/products/pi-zero-w/ +.. _blog.gbaman.info: http://blog.gbaman.info/?p=791