first commit
This commit is contained in:
66
escucha.py
Executable file
66
escucha.py
Executable file
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
|
||||
# --- Configuración del pin del botón ---
|
||||
BOTON_PIN = 18
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(BOTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
|
||||
# --- Umbral en segundos para distinguir pulsación corta/larga ---
|
||||
UMBRAL_LARGO = 2.0
|
||||
|
||||
def accion_corta():
|
||||
print("Acción corta detectada: Apagando el sistema...")
|
||||
time.sleep(1)
|
||||
os.system("sudo shutdown -h now")
|
||||
|
||||
def accion_larga():
|
||||
print("Acción larga detectada: Reiniciando el sistema...")
|
||||
time.sleep(1)
|
||||
os.system("sudo reboot")
|
||||
|
||||
def esperar_liberacion(pin):
|
||||
"""
|
||||
Espera a que el botón sea liberado (deje de estar presionado).
|
||||
Incluye una pequeña pausa para evitar rebotes.
|
||||
"""
|
||||
while GPIO.input(pin) == GPIO.LOW:
|
||||
time.sleep(0.01)
|
||||
time.sleep(0.05) # Espera adicional para evitar rebote al soltar
|
||||
|
||||
def detectar_pulsacion():
|
||||
"""
|
||||
Detecta cuánto tiempo se mantiene presionado el botón.
|
||||
"""
|
||||
tiempo_inicio = time.time()
|
||||
esperar_liberacion(BOTON_PIN)
|
||||
tiempo_pulsado = time.time() - tiempo_inicio
|
||||
return tiempo_pulsado
|
||||
|
||||
# --- Bucle principal ---
|
||||
try:
|
||||
print("Esperando pulsación del botón (GPIO 18)...")
|
||||
while True:
|
||||
if GPIO.input(BOTON_PIN) == GPIO.LOW:
|
||||
print("Botón presionado")
|
||||
duracion = detectar_pulsacion()
|
||||
|
||||
if duracion < UMBRAL_LARGO:
|
||||
accion_corta()
|
||||
else:
|
||||
accion_larga()
|
||||
|
||||
time.sleep(0.1)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\nPrograma interrumpido por el usuario.")
|
||||
except Exception as e:
|
||||
print(f"Error inesperado: {e}")
|
||||
finally:
|
||||
GPIO.cleanup()
|
||||
sys.exit(0)
|
||||
|
||||
12
escucha.service
Normal file
12
escucha.service
Normal file
@@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=Script to reboot or shutdown by button in raspi
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/opt/scripts/escucha.py
|
||||
Type=simple
|
||||
Restart=on-failure
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
43
identificar.py
Executable file
43
identificar.py
Executable file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
import argparse
|
||||
|
||||
# Configuración del modo de los pines GPIO
|
||||
GPIO.setmode(GPIO.BCM) # Usamos el esquema BCM, que hace referencia al número de pin GPIO
|
||||
GPIO.setup(17, GPIO.OUT) # Configuramos el pin GPIO 14 como salida
|
||||
|
||||
# Función para hacer parpadear el LED
|
||||
def parpadear_led(duracion_on, duracion_off, repeticiones):
|
||||
for _ in range(repeticiones):
|
||||
GPIO.output(17, GPIO.HIGH) # Encender el LED
|
||||
time.sleep(duracion_on) # Mantener encendido
|
||||
GPIO.output(17, GPIO.LOW) # Apagar el LED
|
||||
time.sleep(duracion_off) # Mantener apagado
|
||||
# Mantener el LED encendido al final del ciclo
|
||||
GPIO.output(17, GPIO.HIGH)
|
||||
|
||||
# Definir los argumentos de línea de comandos
|
||||
def parse_arguments():
|
||||
parser = argparse.ArgumentParser(description="Hacer parpadear un LED en GPIO 14 de la Raspberry Pi.")
|
||||
parser.add_argument('--on', type=float, default=0.5, help='Tiempo (en segundos) que el LED permanece encendido. (Default: 0.5)')
|
||||
parser.add_argument('--off', type=float, default=0.5, help='Tiempo (en segundos) que el LED permanece apagado. (Default: 0.5)')
|
||||
parser.add_argument('--reps', type=int, default=100, help='Número de veces que el LED parpadeará. (Default: 10)')
|
||||
return parser.parse_args()
|
||||
|
||||
# Ejecución del script
|
||||
if __name__ == "__main__":
|
||||
# Parsear los argumentos desde la línea de comandos
|
||||
args = parse_arguments()
|
||||
|
||||
try:
|
||||
# Llamar a la función con los argumentos pasados desde la línea de comandos
|
||||
parpadear_led(args.on, args.off, args.reps)
|
||||
except KeyboardInterrupt:
|
||||
print("Saliendo del programa...")
|
||||
GPIO.output(17, GPIO.HIGH)
|
||||
finally:
|
||||
GPIO.output(17, GPIO.HIGH) # Restablecer la configuración de los pines GPIO al finalizar el script
|
||||
|
||||
12
led_off.service
Normal file
12
led_off.service
Normal file
@@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=Ejecutar Mi Script en el Apagado
|
||||
DefaultDependencies=no
|
||||
Before=shutdown.target reboot.target halt.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/opt/scripts/off.py
|
||||
RemainAfterExit=true
|
||||
|
||||
[Install]
|
||||
WantedBy=halt.target reboot.target shutdown.target
|
||||
12
led_on.service
Normal file
12
led_on.service
Normal file
@@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=Mi Script de Inicio
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/opt/scripts/on.py
|
||||
Type=simple
|
||||
Restart=on-failure
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
12
off.py
Executable file
12
off.py
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
|
||||
# Configuración del modo de los pines GPIO
|
||||
GPIO.setmode(GPIO.BCM) # Usamos el esquema BCM, que hace referencia al número de pin GPIO
|
||||
GPIO.setup(16, GPIO.OUT) # Configuramos el pin GPIO 14 como salida
|
||||
GPIO.output(16, GPIO.LOW)
|
||||
|
||||
|
||||
12
on.py
Executable file
12
on.py
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
|
||||
# Configuración del modo de los pines GPIO
|
||||
GPIO.setmode(GPIO.BCM) # Usamos el esquema BCM, que hace referencia al número de pin GPIO
|
||||
GPIO.setup(16, GPIO.OUT) # Configuramos el pin GPIO 14 como salida
|
||||
GPIO.output(16, GPIO.HIGH)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user