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
  2. >>> 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
  3. 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!
  4. 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
  5. Connect to the remote Traffic HAT >>> from gpiozero import

    * >>> th = TrafficHat() >>> th.pin_factory <gpiozero.pins.pigpio.PiGPIOFactory object...> >>> th.lights.blink()
  6. 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)
  7. 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)
  8. 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)
  9. 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
  10. 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
  11. 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()
  12. 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()
  13. 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)
  14. 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
  15. 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)
  16. 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))
  17. 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