Ejemplo script

This commit is contained in:
Luca 2022-07-16 22:49:24 +02:00
commit 1070b53aa0
2 changed files with 64 additions and 0 deletions

31
README.md Normal file
View File

@ -0,0 +1,31 @@
# Script para usar la Rpi con un pulsador en GPIO
Una prueba para usar el puerto GPIO 8 de la Rpi con un pulsador y ejecutar un sonido.
## Descargar audio
Ejemplo:
```
$ wget http://www.soinumapa.net/wp-content/uploads/2015/10/amurrio_diana.mp3 -O /home/docker/gpio/audio.mp3
```
## Lanzar el script
```
# python3 main.py
```
o
```
# ./main.py
```
## Enlaces
* https://raspberrypihq.com/use-a-push-button-with-raspberry-pi-gpio/
* https://www.jeffgeerling.com/blog/2022/playing-sounds-python-on-raspberry-pi
* https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/
* https://realpython.com/intro-to-python-threading/

33
main.py Executable file
View File

@ -0,0 +1,33 @@
#!/bin/python3
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import pygame
import threading
import time
def play_sound():
if not pygame.mixer.music.get_busy() :
print("Start Sound")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
else :
print("Stop Sound")
pygame.mixer.music.stop()
def button_callback(channel):
print("Button was pushed!")
x = threading.Thread(target=play_sound)
x.start()
## init pygame
pygame.mixer.init()
pygame.mixer.music.load('/home/docker/gpio/audio.mp3')
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback, bouncetime=10000) # Setup event on pin 10 rising edge 10s wait
message = input("Press enter to quit\n\n") # Run until someone presses enter
GPIO.cleanup() # Clean up