mirror of
				https://github.com/KevinMidboe/python-gpiozero.git
				synced 2025-10-29 17:50:37 +00:00 
			
		
		
		
	Add more advanced and remote recipes
This commit is contained in:
		
							
								
								
									
										11
									
								
								docs/examples/bluedot_led.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								docs/examples/bluedot_led.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
							
								
								
									
										22
									
								
								docs/examples/bluedot_robot_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								docs/examples/bluedot_robot_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
							
								
								
									
										26
									
								
								docs/examples/bluedot_robot_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								docs/examples/bluedot_robot_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
							
								
								
									
										9
									
								
								docs/examples/cpu_temperature_bar_graph.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								docs/examples/cpu_temperature_bar_graph.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
							
								
								
									
										14
									
								
								docs/examples/internet_status_indicator.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								docs/examples/internet_status_indicator.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
							
								
								
									
										26
									
								
								docs/examples/remote_button_robot.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								docs/examples/remote_button_robot.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
							
								
								
									
										11
									
								
								docs/examples/robot_pots_1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								docs/examples/robot_pots_1.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
							
								
								
									
										12
									
								
								docs/examples/robot_pots_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								docs/examples/robot_pots_2.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
							
								
								
									
										11
									
								
								docs/examples/timed_heat_lamp.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								docs/examples/timed_heat_lamp.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
							
								
								
									
										22
									
								
								docs/examples/whos_home_leds.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								docs/examples/whos_home_leds.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
							
								
								
									
										18
									
								
								docs/examples/whos_home_status.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								docs/examples/whos_home_status.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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() | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
| ========================= | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user