From 220da280ba35223f1ee23bab88f242de58c5a2ac Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Mon, 17 Jul 2017 02:03:29 +0100 Subject: [PATCH] Add more advanced and remote recipes --- docs/examples/bluedot_led.py | 11 ++++ docs/examples/bluedot_robot_1.py | 22 ++++++++ docs/examples/bluedot_robot_2.py | 26 +++++++++ docs/examples/cpu_temperature_bar_graph.py | 9 ++++ docs/examples/internet_status_indicator.py | 14 +++++ docs/examples/multi_room_doorbell.py | 8 +-- docs/examples/multi_room_motion_alert.py | 6 +-- docs/examples/remote_button_robot.py | 26 +++++++++ docs/examples/robot_pots_1.py | 11 ++++ docs/examples/robot_pots_2.py | 12 +++++ docs/examples/timed_heat_lamp.py | 11 ++++ docs/examples/whos_home_leds.py | 22 ++++++++ docs/examples/whos_home_status.py | 18 +++++++ docs/recipes.rst | 35 ++++++++++++- docs/recipes_advanced.rst | 61 +++++++++++++++++++++- docs/recipes_remote_gpio.rst | 9 ++++ 16 files changed, 290 insertions(+), 11 deletions(-) create mode 100644 docs/examples/bluedot_led.py create mode 100644 docs/examples/bluedot_robot_1.py create mode 100644 docs/examples/bluedot_robot_2.py create mode 100644 docs/examples/cpu_temperature_bar_graph.py create mode 100644 docs/examples/internet_status_indicator.py create mode 100644 docs/examples/remote_button_robot.py create mode 100644 docs/examples/robot_pots_1.py create mode 100644 docs/examples/robot_pots_2.py create mode 100644 docs/examples/timed_heat_lamp.py create mode 100644 docs/examples/whos_home_leds.py create mode 100644 docs/examples/whos_home_status.py diff --git a/docs/examples/bluedot_led.py b/docs/examples/bluedot_led.py new file mode 100644 index 0000000..305e4de --- /dev/null +++ b/docs/examples/bluedot_led.py @@ -0,0 +1,11 @@ +from bluedot import BlueDot +from gpiozero import LED + +bd = BlueDot() +led = LED(17) + +while True: + bd.wait_for_press() + led.on() + bd.wait_for_release() + led.off() diff --git a/docs/examples/bluedot_robot_1.py b/docs/examples/bluedot_robot_1.py new file mode 100644 index 0000000..1eaea57 --- /dev/null +++ b/docs/examples/bluedot_robot_1.py @@ -0,0 +1,22 @@ +from bluedot import BlueDot +from gpiozero import Robot +from signal import pause + +bd = BlueDot() +robot = Robot(left=(4, 14), right=(17, 18)) + +def move(pos): + if pos.top: + robot.forward(pos.distance) + elif pos.bottom: + robot.backward(pos.distance) + elif pos.left: + robot.left(pos.distance) + elif pos.right: + robot.right(pos.distance) + +bd.when_pressed = move +bd.when_moved = move +bd.when_released = robot.stop + +pause() diff --git a/docs/examples/bluedot_robot_2.py b/docs/examples/bluedot_robot_2.py new file mode 100644 index 0000000..9052b1f --- /dev/null +++ b/docs/examples/bluedot_robot_2.py @@ -0,0 +1,26 @@ +from gpiozero import Robot +from bluedot import BlueDot +from signal import pause + +def pos_to_values(x, y): + left = y if x > 0 else y + x + right = y if x < 0 else y - x + return (clamped(left), clamped(right)) + +def clamped(v): + return max(-1, min(1, v)) + +def drive(): + while True: + if bd.is_pressed: + x, y = bd.position.x, bd.position.y + yield pos_to_values(x, y) + else: + yield (0, 0) + +robot = Robot(left=(4, 14), right=(17, 18)) +bd = BlueDot() + +robot.source = drive() + +pause() diff --git a/docs/examples/cpu_temperature_bar_graph.py b/docs/examples/cpu_temperature_bar_graph.py new file mode 100644 index 0000000..00f17a1 --- /dev/null +++ b/docs/examples/cpu_temperature_bar_graph.py @@ -0,0 +1,9 @@ +from gpiozero import LEDBarGraph, CPUTemperature +from signal import pause + +cpu = CPUTemperature(min_temp=50, max_temp=90) +leds = LEDBarGraph(2, 3, 4, 5, 6, 7, 8, pwm=True) + +leds.source = cpu.values + +pause() diff --git a/docs/examples/internet_status_indicator.py b/docs/examples/internet_status_indicator.py new file mode 100644 index 0000000..0fbbd63 --- /dev/null +++ b/docs/examples/internet_status_indicator.py @@ -0,0 +1,14 @@ +from gpiozero import LED, PingServer +from gpiozero.tools import negated +from signal import pause + +green = LED(17) +red = LED(18) + +google = PingServer('google.com') + +green.source = google.values +green.source_delay = 60 +red.source = negated(green.values) + +pause() diff --git a/docs/examples/multi_room_doorbell.py b/docs/examples/multi_room_doorbell.py index da8a070..33229d2 100644 --- a/docs/examples/multi_room_doorbell.py +++ b/docs/examples/multi_room_doorbell.py @@ -1,12 +1,12 @@ -from gpiozero import Buzzer, Button -from gpiozero.pins.pigpio import PiGPIOPin +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'] -remote_pins = [PiGPIOPin(17, host=ip) for ip in ips] +remotes = [PiGPIOFactory(host=ip) for ip in ips] button = Button(17) # button on this pi -buzzers = [Buzzer(pin) for pin in remote_pins] # buzzers on remote pins +buzzers = [Buzzer(pin, pin_factory=r) for r in remotes] # buzzers on remote pins for buzzer in buzzers: buzzer.source = button.values diff --git a/docs/examples/multi_room_motion_alert.py b/docs/examples/multi_room_motion_alert.py index 8f3e5d1..b3bd962 100644 --- a/docs/examples/multi_room_motion_alert.py +++ b/docs/examples/multi_room_motion_alert.py @@ -1,12 +1,12 @@ from gpiozero import LEDBoard, MotionSensor -from gpiozero.pins.pigpio import PiGPIOPin +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'] -remote_pins = [PiGPIOPin(17, host=ip) for ip in ips] +remotes = [PiGPIOFactory(host=ip) for ip in ips] leds = LEDBoard(2, 3, 4, 5) # leds on this pi -sensors = [MotionSensor(pin) for pin in remote_pins] # motion sensors on other pis +sensors = [MotionSensor(17, pin_factory=r) for r in remotes] # remote sensors for led, sensor in zip(leds, sensors): led.source = sensor.values diff --git a/docs/examples/remote_button_robot.py b/docs/examples/remote_button_robot.py new file mode 100644 index 0000000..e16e726 --- /dev/null +++ b/docs/examples/remote_button_robot.py @@ -0,0 +1,26 @@ +from gpiozero import Button, Robot +from gpiozero.pins.pigpio import PiGPIOFactory +from signal import pause + +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 + +pause() diff --git a/docs/examples/robot_pots_1.py b/docs/examples/robot_pots_1.py new file mode 100644 index 0000000..3cd9065 --- /dev/null +++ b/docs/examples/robot_pots_1.py @@ -0,0 +1,11 @@ +from gpiozero import Robot, MCP3008 +from signal import pause + +robot = Robot(left=(4, 14), right=(17, 18)) + +left = MCP3008(0) +right = MCP3008(1) + +robot.source = zip(left.values, right.values) + +pause() diff --git a/docs/examples/robot_pots_2.py b/docs/examples/robot_pots_2.py new file mode 100644 index 0000000..d2c3e64 --- /dev/null +++ b/docs/examples/robot_pots_2.py @@ -0,0 +1,12 @@ +from gpiozero import Robot, MCP3008 +from gpiozero.tools import scaled +from signal import pause + +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)) + +pause() diff --git a/docs/examples/timed_heat_lamp.py b/docs/examples/timed_heat_lamp.py new file mode 100644 index 0000000..4e9c147 --- /dev/null +++ b/docs/examples/timed_heat_lamp.py @@ -0,0 +1,11 @@ +from gpiozero import Energenie, TimeOfDay +from datetime import time +from signal import pause + +lamp = Energenie(1) +daytime = TimeOfDay(time(8), time(20)) + +lamp.source = daytime.values +lamp.source_delay = 60 + +pause() diff --git a/docs/examples/whos_home_leds.py b/docs/examples/whos_home_leds.py new file mode 100644 index 0000000..275560d --- /dev/null +++ b/docs/examples/whos_home_leds.py @@ -0,0 +1,22 @@ +from gpiozero import PingServer, LEDBoard +from gpiozero.tools import negated +from signal import pause + +status = LEDBoard( + mum=LEDBoard(red=14, green=15), + dad=LEDBoard(red=17, green=18), + alice=LEDBoard(red=21, green=22) +) + +statuses = { + PingServer('192.168.1.5'): status.mum, + PingServer('192.168.1.6'): status.dad, + PingServer('192.168.1.7'): status.alice, +} + +for server, leds in statuses.items(): + leds.green.source = server.values + leds.green.source_delay = 60 + leds.red.source = negated(leds.green.values) + +pause() diff --git a/docs/examples/whos_home_status.py b/docs/examples/whos_home_status.py new file mode 100644 index 0000000..27b0e0e --- /dev/null +++ b/docs/examples/whos_home_status.py @@ -0,0 +1,18 @@ +from gpiozero import PingServer, StatusZero +from gpiozero.tools import negated +from signal import pause + +status = StatusZero('mum', 'dad', 'alice') + +statuses = { + PingServer('192.168.1.5'): status.mum, + PingServer('192.168.1.6'): status.dad, + PingServer('192.168.1.7'): status.alice, +} + +for server, leds in statuses.items(): + leds.green.source = server.values + leds.green.source_delay = 60 + leds.red.source = negated(leds.green.values) + +pause() diff --git a/docs/recipes.rst b/docs/recipes.rst index b04fffe..dc7fdba 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -401,8 +401,38 @@ Alternatively, the following example is identical, but uses the .. literalinclude:: examples/rgbled_pot_2.py :emphasize-lines: 8 -Please note the example above requires Python 3. In Python 2, :func:`zip` -doesn't support lazy evaluation so the script will simply hang. +.. note:: + + Please note the example above requires Python 3. In Python 2, :func:`zip` + doesn't support lazy evaluation so the script will simply hang. + +Timed heat lamp +=============== + +If you have a pet (e.g. a tortoise) which requires a heat lamp to be switched +on for a certain amount of time each day, you can use an `Energenie Pi-mote`_ +to remotely control the lamp, and the :class:`TimeOfDay` class to control the +timing: + +.. literalinclude:: examples/timed_heat_lamp.py + +Internet connection status indicator +==================================== + +You can use a pair of green and red LEDs to indicate whether or not your +internet connection is working. Simply use the :class:`PingServer` class to +identify whether a ping to `google.com` is successful. If successful, the green +LED is lit, and if not, the red LED is lit: + +.. literalinclude:: examples/internet_status_indicator.py + +CPU Temperature Bar Graph +========================= + +You can read the Raspberry Pi's own CPU temperature using the built-in +:class:`CPUTemperature` class, and display this on a "bar graph" of LEDs: + +.. literalinclude:: examples/cpu_temperature_bar_graph.py More recipes ============ @@ -416,3 +446,4 @@ Continue to: .. _Push Button Stop Motion: https://www.raspberrypi.org/learning/quick-reaction-game/ .. _Quick Reaction Game: https://www.raspberrypi.org/learning/quick-reaction-game/ .. _GPIO Music Box: https://www.raspberrypi.org/learning/gpio-music-box/ +.. _Energenie Pi-mote: https://energenie4u.co.uk/catalogue/product/ENER002-2PI diff --git a/docs/recipes_advanced.rst b/docs/recipes_advanced.rst index d7146ea..5feecfd 100644 --- a/docs/recipes_advanced.rst +++ b/docs/recipes_advanced.rst @@ -22,11 +22,66 @@ travispy``. Button controlled robot ======================= -Alternatively, use four buttons to program the directions and add a fifth -button to process them in turn, like a Bee-Bot or Turtle robot. +Alternatively to the examples in the simple recipes, you can use four buttons +to program the directions and add a fifth button to process them in turn, like +a Bee-Bot or Turtle robot. .. literalinclude:: examples/robot_buttons_2.py +Who's home indicator +==================== + +Using a number of green-red LED pairs, you can show the status of who's home, +according to which IP addresses you can ping successfully. Note that this +assumes each person's mobile phone has a reserved IP address on the home router. + +.. literalinclude:: examples/whos_home_leds.py + +Alternatively, using the `STATUS Zero`_ board: + +.. literalinclude:: examples/whos_home_status.py + +Robot controlled by 2 potentiometers +==================================== + +Use two potentiometers to control the left and right motor speed of a robot: + +.. literalinclude:: examples/robot_pots_1.py + +.. note:: + + Please note the example above requires Python 3. In Python 2, :func:`zip` + doesn't support lazy evaluation so the script will simply hang. + +To include reverse direction, scale the potentiometer values from 0-1 to -1-1: + +.. literalinclude:: examples/robot_pots_2.py + +BlueDot +======= + +BlueDot is a Python library an Android app which allows you to easily add +Bluetooth control to your Raspberry Pi project. A simple example to control a +LED using the BlueDot app: + +.. literalinclude:: examples/bluedot_led.py + +Note this recipe requires ``bluedot`` and the associated Android app. See the +`BlueDot documentation`_ for installation instructions. + +BlueDot robot +============= + +You can create a Bluetooth controlled robot which moves forward when the dot is +pressed and stops when it is released: + +.. literalinclude:: examples/bluedot_robot_1.py + +Or a more advanced example including controlling the robot's speed and precise +direction: + +.. literalinclude:: examples/bluedot_robot_2.py + Controlling the Pi's own LEDs ============================= @@ -71,3 +126,5 @@ run the following commands: .. _travispy: https://travispy.readthedocs.io/ +.. _STATUS Zero: https://thepihut.com/status +.. _BlueDot documentation: http://bluedot.readthedocs.io/en/latest/index.html diff --git a/docs/recipes_remote_gpio.rst b/docs/recipes_remote_gpio.rst index 650732f..7eb1433 100644 --- a/docs/recipes_remote_gpio.rst +++ b/docs/recipes_remote_gpio.rst @@ -44,6 +44,15 @@ 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). +Remote button robot +=================== + +Similarly to the simple recipe for the button controlled robot, this example +uses four buttons to control the direction of a robot. However, using remote +pins for the robot means the control buttons can be separate from the robot: + +.. literalinclude:: examples/remote_button_robot.py + Light sensor + Sense HAT =========================