Add Pi Zero OTG, sense hat examples and more docs

This commit is contained in:
Ben Nuttall
2017-03-12 20:23:19 +00:00
parent 8e4da94f8b
commit 0507273d8a
5 changed files with 93 additions and 9 deletions

View File

@@ -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()

View File

@@ -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)

View File

@@ -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)

View File

@@ -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/

View File

@@ -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