$30 off During Our Annual Pro Sale. View Details »

Physical Computing with Python and Raspberry Pi - EuroPython 2017

Physical Computing with Python and Raspberry Pi - EuroPython 2017

Talk given at EuroPython 2017

Ben Nuttall

July 14, 2017
Tweet

More Decks by Ben Nuttall

Other Decks in Programming

Transcript

  1. Physical computing
    with Python and Raspberry Pi
    Ben Nuttall
    Raspberry Pi Foundation
    UK Charity 1129409

    View Slide

  2. Ben Nuttall

    Raspberry Pi Community Manager

    Based in Cambridge, UK

    Creator of gpiozero python library

    Columnist on opensource.com

    github.com/bennuttall

    twitter.com/ben_nuttall

    [email protected]

    View Slide

  3. Over 14 million Raspberry Pis sold

    View Slide

  4. Raspberry Pi Foundation

    Educational charity founded
    in 2009

    Incorporates:
    – Raspberry Pi Trading Ltd
    – Code Club
    – CoderDojo

    Trading profits fund
    education programmes
    raspberrypi.org/about

    View Slide

  5. Our mission
    “Putting the power of digital making into the hands of people all over the world”
    So that people are:

    Capable of understanding and shaping an increasingly digital world

    Able to solve the problems that matter to them, both as makers and
    entrepreneurs

    Equipped for the jobs of the future
    raspberrypi.org/about

    View Slide

  6. We do this by providing...

    Low-cost, high-performance computers

    Outreach and education programmes

    Free resources and teacher training
    p

    View Slide

  7. Current models

    Raspberry Pi 3

    64-bit quad-core ARMv8
    @ 1.2GHz

    1GB RAM

    $35

    Raspberry Pi Zero / Zero W

    32-bit single-core ARMv6
    @ 1GHz

    512MB RAM

    $5 / $10
    raspberrypi.org/products

    View Slide

  8. Raspbian Jessie desktop
    raspberrypi.org/downloads

    View Slide

  9. Raspbian x86
    rpf.io/x86

    View Slide

  10. Moving on from IDLE...

    We have included a new IDE called
    Thonny

    We are investing in developing
    Nicholas Tollervery’s Mu editor –
    watch this space!
    ntoll.org/article/mu-pi

    View Slide

  11. GPIO Pins – General Purpose
    Input/Output

    View Slide

  12. Physical computing

    Flashy lights

    Motors & robots

    Photo & video

    Sensors

    Internet of Things

    Home automation

    View Slide

  13. GPIO

    3V3, 5V

    GPIO, SPI, I2C, UART

    GPIO = variable 3V3 pinout.xyz

    View Slide

  14. GPIO components

    View Slide

  15. Add-on boards / HATs

    View Slide

  16. Python - RPi.GPIO
    import RPi.GPIO as GPIO
    from time import sleep
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(17, GPIO.OUT)
    while True:
    GPIO.output(17, GPIO.HIGH)
    sleep(1)
    GPIO.output(17, GPIO.LOW)
    sleep(1)

    View Slide

  17. Python - GPIO Zero
    from gpiozero import LED
    from time import sleep
    led = LED(17)
    while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)

    View Slide

  18. Python - GPIO Zero
    from gpiozero import LED
    led = LED(17)
    led.blink()

    View Slide

  19. GPIO Zero supports...

    View Slide

  20. GPIO Zero Device Hierarchy!

    View Slide

  21. Multi-paradigm: procedural
    from gpiozero import LED, Button
    led = LED(17)
    button = Button(4)
    while True:
    if button.is_pressed:
    led.on()
    else:
    led.off()

    View Slide

  22. Multi-paradigm: event-driven
    from gpiozero import LED, Button
    led = LED(17)
    button = Button(4)
    button.when_pressed = led.on
    button.when_released = led.off

    View Slide

  23. Multi-paradigm: declarative
    from gpiozero import LED, Button
    led = LED(17)
    button = Button(4)
    led.source = button.values

    View Slide

  24. .value
    >>> led = PWMLED(17)
    >>> led.value
    0.0
    >>> led.on()
    >>> led.value
    1.0
    >>> led.value = 0

    View Slide

  25. .value
    >>> led = PWMLED(17)
    >>> pot = MCP3008()
    >>> led.value = pot.value

    View Slide

  26. .value
    >>> led = PWMLED(17)
    >>> pot = MCP3008()
    >>> led.value = pot.value
    >>> while True:
    ... led.value = pot.value

    View Slide

  27. Source / Values
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values

    View Slide

  28. Source / Values
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values

    View Slide

  29. Source / Values
    from gpiozero import LED, Button
    led = LED(17)
    button = Button(2)
    led.source = button.values

    View Slide

  30. Processing values
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values
    function

    View Slide

  31. Source tools
    from gpiozero import Button, LED
    from gpiozero.tools import negated
    led = LED(4)
    button = Button(17)
    led.source = negated(button.values)

    View Slide

  32. Combining values
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values
    Source tool
    Input Device
    .value
    .values

    View Slide

  33. AND logic gate
    from gpiozero import Button, LED
    from gpiozero.tools import all_values
    button_a = Button(2)
    button_b = Button(3)
    led = LED(17)
    led.source = all_values(button_a.values, button_b.values)

    View Slide

  34. CPU Temperature
    from gpiozero import LEDBarGraph, CPUTemperature
    cpu = CPUTemperature(min_temp=50, max_temp=90)
    leds = LEDBarGraph(2, 3, 4, 5, 6, 7, 8, pwm=True)
    leds.source = cpu.values

    View Slide

  35. Is the internet working?
    from gpiozero import LED, PingServer
    from gpiozero.tools import negated
    green = LED(17)
    red = LED(18)
    google = PingServer('google.com')
    green.source = google.values
    green.source_delay = 60
    red.source = negated(green.values)

    View Slide

  36. Energenie

    View Slide

  37. Energenie tortoise lamp
    from gpiozero import Energenie, TimeOfDay
    from datetime import time
    lamp = Energenie(1)
    daytime = TimeOfDay(time(8), time(20))
    lamp.source = daytime.values

    View Slide

  38. Supporting multiple pin libraries

    RPi.GPIO

    Implemented in C, current default

    Available in PyPI & apt repository (installed by default in Raspbian)

    RPIO

    Implemented in C, supports hardware PWM, supports Pi 1 only (dead project)

    Available in PyPI

    pigpio

    Python wrapper for C library, supports lots of protocols, runs as daemon, supports remote connections

    Available in PyPI & apt repository (installed by default in Raspbian)

    Native

    Pure Python, limited functionality, experimental

    Included in gpiozero source

    MockPin & MockPWMPin

    Pure Python, used in test suite

    Included in gpiozero source

    View Slide

  39. pigpio - remote GPIO from Pi or PC

    View Slide

  40. pigpio - remote GPIO from Pi or PC
    from gpiozero import LED
    from gpiozero.pins.pigpio import PiGPIOFactory
    from signal import pause
    factory = PiGPIOFactory('192.168.0.2')
    led = LED(22, pin_factory=factory)
    led.blink()
    pause()

    View Slide

  41. pigpio - remote GPIO from Pi or PC
    $ export GPIOZERO_PIN_FACTORY=pigpio
    $ export PIGPIO_ADDR=192.168.0.2
    $ python3 led.py
    from gpiozero import LED
    from signal import pause
    led = LED(22)
    led.blink()
    pause()

    View Slide

  42. MockPin
    $ GPIOZERO_PIN_FACTORY=mock python3
    >>> from gpiozero import LED
    >>> led = LED(22)
    >>> led.blink()
    >>> led.value
    True
    >>> led.value
    False

    View Slide

  43. MockPin
    >>> from gpiozero import LED
    >>> led = LED(22)
    >>> button = Button(23)
    >>> led.source = button.values
    >>> led.value
    False
    >>> button.pin.drive_low()
    >>> led.value
    True

    View Slide

  44. pinout command line tool

    View Slide

  45. Sense HAT

    Special made for Tim Peake's
    Astro Pi mission

    Sensors, LED display &
    joystick

    Great for science, games and
    creativity

    Works on any Pi model

    Emulators also available
    raspberrypi.org/products/sense-hat

    View Slide

  46. Sense HAT

    View Slide

  47. Sense HAT
    >>> from sense_hat import SenseHat
    >>> sense = SenseHat()
    >>> sense.show_message(“Hello world”)

    View Slide

  48. Sense HAT
    >>> sense.temperature
    25.0
    >>> sense.humidity
    45.0
    >>> sense.accelerometer
    {'pitch': 0.0, 'roll': 0.0, 'yaw': 0.0}
    >>> sense.gyroscope
    {'pitch': 0.0, 'roll': 0.0, 'yaw': 0.0}
    >>> sense.orientation
    {'pitch': 0.0, 'roll': 0.0, 'yaw': 0.0}

    View Slide

  49. Sense HAT
    from sense_hat import SenseHat
    sense = SenseHat()
    while True:
    r = 255 * sense.humidity / 100
    sense.clear(r, 0, 0)
    pythonhosted.org/sense-hat

    View Slide

  50. Astro Pi: Your code in space
    astro-pi.org

    View Slide

  51. Sense HAT Web Emulator
    trinket.io/sense-hat

    View Slide

  52. Sense HAT Desktop Emulator
    sense-emu.readthedocs.io

    View Slide

  53. Picamera

    8 megapixels (v2)
    – v1 was 5mpx

    Visible light & infra-red
    versions available

    1080p30, 720p60 and
    VGA90 video

    Command line interface
    and Python library
    raspberrypi.org/products/camera-module-v2

    View Slide

  54. Picamera - capture
    from picamera import PiCamera
    from time import sleep
    camera = PiCamera()
    camera.start_preview()
    sleep(3)
    camera.capture('/home/pi/Desktop/image.jpg')
    camera.stop_preview()

    View Slide

  55. Picamera – record video
    from picamera import PiCamera
    from time import sleep
    camera = PiCamera()
    camera.start_preview()
    camera.start_recording('/home/pi/video.h264')
    sleep(10)
    camera.stop_recording()
    camera.stop_preview()

    View Slide

  56. Picamera + GPIO push button
    from picamera import PiCamera
    from gpiozero import Button
    camera = PiCamera()
    button = Button(17)
    camera.start_preview()
    button.wait_for_press()
    camera.capture('/home/pi/Desktop/image.jpg')
    camera.stop_preview()

    View Slide

  57. Picamera image effects
    from picamera import PiCamera
    from time import sleep
    camera = PiCamera()
    camera.start_preview()
    for effect in camera.IMAGE_EFFECTS:
    camera.image_effect = effect
    camera.annotate_text = effect
    sleep(1)
    camera.stop_preview()

    View Slide

  58. Picamera image effects

    View Slide

  59. Web Streaming
    github.com/waveform80/pistreaming

    View Slide

  60. Picamera + OpenCV
    pyimagesearch.com

    View Slide

  61. Read the docs!

    gpiozero.readthedocs.io

    pythonhosted.org/sense-hat

    sense-emu.readthedocs.io

    picamera.readthedocs.io

    View Slide

  62. Google AIY Projects kit

    Free with The MagPi #57

    Google Voice HAT + speaker

    Google assistant

    Write code to process custom
    voice commands
    – “Lights on”
    – “Robot go forward”
    – “Take a picture”
    aiyprojects.withgoogle.com

    View Slide

  63. PyPI & ARM Wheels

    PyPI doesn’t support uploading ARM wheels :(

    I have a project called piwheels
    – I build all PyPI packages on a Raspberry Pi 3
    – I host a Python package repository on the same Pi

    Next gen PyPI (warehouse) now supports uploading ARM wheels :)

    See my lightning talk slides at speakerdeck.com/bennuttall

    See the project at github.com/bennuttall/piwheels
    github.com/bennuttall/piwheels

    View Slide

  64. How you can get involved...

    View Slide

  65. Python libraries

    Play with, and contribute to:
    – gpiozero
    – picamera
    – sense hat
    – more!

    Help maintainers upgrade modules to Python 3

    Create your own Python libraries for Raspberry Pi things

    View Slide

  66. The MagPi

    Community magazine established
    in 2012 (as free PDF download)

    Now the official Raspberry Pi
    magazine

    Paper copies on sale in UK/US
    shops and online

    Still a free PDF download

    Occasionally comes with a free
    computer

    Book series (also available for free)
    raspberrypi.org/magpi

    View Slide

  67. Raspberry Jam

    Independently organised
    community events

    Family-friendly

    Mix of meetup / conference /
    workshop styles

    Raspberry Jam Guidebook and
    more resources available

    Contact me about setting one up!
    raspberrypi.org/jam

    View Slide

  68. Raspberry Jam near you?
    raspberrypi.org/jam

    View Slide

  69. Code Club

    Free volunteer-led after
    school clubs for children

    Projects provided using
    Scratch, HTML and Python

    Training and support
    provided for volunteers

    Help translating materials
    codeclubworld.org

    View Slide

  70. Physical computing
    with Python and Raspberry Pi
    Ben Nuttall
    Raspberry Pi Foundation
    UK Charity 1129409

    View Slide