Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Remote GPIO

Remote GPIO

Ben Nuttall

July 01, 2017
Tweet

More Decks by Ben Nuttall

Other Decks in Programming

Transcript

  1. Remote GPIO
    with GPIO Zero, pigpio and the Traffic HAT
    Ben Nuttall
    Raspberry Pi Foundation
    UK Charity 1129409

    View Slide

  2. Open Thonny Python IDE

    View Slide

  3. Type into the shell

    View Slide

  4. >>> from gpiozero import *
    >>> th = TrafficHat()
    >>> th.lights.on()
    >>> th.lights.blink()
    >>> th.buzzer.on()
    >>> th.buzzer.off()
    >>> th.button.when_pressed = th.on
    >>> th.button.when_released = th.off

    View Slide

  5. Source/values
    >>> th.lights.green.value
    False
    >>> th.button.value
    False
    >>> th.button.value
    True
    >>> th.lights.green.source = th.button.values

    View Slide

  6. GPIO Zero pin factories

    GPIO Zero allows use with various pin libraries:
    – RPi.GPIO (default)
    – RPIO
    – pigpio
    – Native
    – Mock

    You can select one to use at launch

    Or you can change on the fly!

    View Slide

  7. pigpio

    Pin library implemented in C

    Runs as a daemon as root

    You can communicate to it with sockets (without root)

    Lots of advanced GPIO features built-in

    Accepts socket requests over the network, allowing remote
    GPIO capabilities

    Remote GPIO works between two Pis, or from PC to Pi

    View Slide

  8. Close Thonny and open a Terminal

    View Slide

  9. Start the pigpio daemon
    $ sudo pigpiod

    View Slide

  10. Find your IP address
    $ hostname -I

    View Slide

  11. Launch Thonny with another Pi’s IP address
    $ GPIOZERO_PIN_FACTORY=pigpio
    PIGPIO_ADDR=10.3.12.117 thonny&

    View Slide

  12. Connect to the remote Traffic HAT
    >>> from gpiozero import *
    >>> th = TrafficHat()
    >>> th.pin_factory

    >>> th.lights.blink()

    View Slide

  13. Examples!

    View Slide

  14. AND logic gate
    # launch with remote Pi’s IP address as default pin factory
    from gpiozero import Button, LED
    from gpiozero.pins.rpigpio import RPiGPIOFactory
    from gpiozero.tools import all_values
    local = RPiGPIOFactory()
    other_led = LED(22) # remote
    other_btn = Button(25) # remote
    my_btn = Button(25, pin_factory=local) # local
    other_led.source = all_values(my_btn.values, other_btn.values)

    View Slide

  15. OR logic gate
    # launch with remote Pi’s IP address as default pin factory
    from gpiozero import Button, LED
    from gpiozero.pins.rpigpio import RPiGPIOFactory
    from gpiozero.tools import any_values
    local = RPiGPIOFactory()
    other_led = LED(22) # remote
    other_btn = Button(25) # remote
    my_btn = Button(25, pin_factory=local) # local
    other_led.source = any_values(my_btn.values, other_btn.values)

    View Slide

  16. PingServer
    from gpiozero import TrafficHat, PingServer
    from gpiozero.tools import negated
    th = TrafficHat()
    ps = PingServer(‘10.3.15.15’)
    th.lights.green.source = ps.values
    th.lights.red.source = negated(ps.values)

    View Slide

  17. CPUTemperature
    from gpiozero import LEDBarGraph, CPUTemperature
    leds = LEDBarGraph(22, 23, 24, pwm=True)
    cpu = CPUTemperature(min_temp=40, max_temp=70)
    print(cpu.temperature)
    leds.source = cpu.values

    View Slide

  18. CPUTemperature buzzer
    from gpiozero import PWMOutputDevice, CPUTemperature
    buzzer = PWMOutputDevice(5)
    cpu = CPUTemperature(min_temp=40, max_temp=70)
    print(cpu.temperature)
    buzzer.source = cpu.values

    View Slide

  19. Multi-room motion alert
    from gpiozero import LEDBoard, MotionSensor
    from gpiozero.pins.pigpio import PiGPIOFactory
    from signal import pause
    ips = ['192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6']
    remotes = [PiGPIOFactory(host=ip) for ip in ips]
    leds = LEDBoard(2, 3, 4, 5) # leds on this pi
    sensors = [MotionSensor(17, pin_factory=r) for r in remotes] # remote sensors
    for led, sensor in zip(leds, sensors):
    led.source = sensor.values
    pause()

    View Slide

  20. Multi-room doorbell
    from gpiozero import LEDBoard, MotionSensor
    from gpiozero.pins.pigpio import PiGPIOFactory
    from signal import pause
    ips = ['192.168.1.3', '192.168.1.4', '192.168.1.5', '192.168.1.6']
    remotes = [PiGPIOFactory(host=ip) for ip in ips]
    button = Button(17) # button on this pi
    buzzers = [Buzzer(pin, pin_factory=r) for r in remotes] # buzzers on remote pins
    for buzzer in buzzers:
    buzzer.source = button.values
    pause()

    View Slide

  21. Sense HAT
    from gpiozero import LightSensor
    from gpiozero.pins.pigpio import PiGPIOFactory
    from sense_hat import SenseHat
    factory = PiGPIOFactory(host='192.168.1.4')
    light = LightSensor(4, pin_factory=factory)) # 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 Slide

  22. Remote button robot
    from gpiozero import Button, Robot
    from gpiozero.pins.pigpio import PiGPIOFactory
    factory = PiGPIOFactory(host='192.168.1.17')
    robot = Robot(left=(4, 14), right=(17, 18), pin_factory=factory) # remote pins
    # local buttons
    left = Button(26)
    right = Button(16)
    fw = Button(21)
    bw = Button(20)
    fw.when_pressed = robot.forward
    fw.when_released = robot.stop
    left.when_pressed = robot.left
    left.when_released = robot.stop
    right.when_pressed = robot.right
    right.when_released = robot.stop
    bw.when_pressed = robot.backward
    bw.when_released = robot.stop

    View Slide

  23. Remote pot robot
    from gpiozero import Robot, MCP3008
    robot = Robot(left=(4, 14), right=(17, 18))
    left = MCP3008(0)
    right = MCP3008(1)
    robot.source = zip(left.values, right.values)

    View Slide

  24. Remote pot robot
    from gpiozero import Robot, MCP3008
    from gpiozero.tools import scaled
    robot = Robot(left=(4, 14), right=(17, 18))
    left = MCP3008(0)
    right = MCP3008(1)
    robot.source = zip(scaled(left.values, -1, 1), scaled(right.values, -1, 1))

    View Slide

  25. What can you control remotely?

    Pi with LEDs showing who is home (ping known mobile phone
    IP addresses)

    Pi with LEDs showing sensor data from other rooms

    Pi with LEDs showing data from the internet (e.g. tube line info)

    Control robot remotely from your PC

    Use two HATs at once

    Run the same script on multiple Pis from one control PC

    View Slide