commit 2c7a35235190064477fd3db4aa691124c17d2b4c Author: root Date: Thu Jul 24 20:08:10 2025 +0200 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/escucha.py b/escucha.py new file mode 100755 index 0000000..30cd7d7 --- /dev/null +++ b/escucha.py @@ -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) + diff --git a/escucha.service b/escucha.service new file mode 100644 index 0000000..78dc23d --- /dev/null +++ b/escucha.service @@ -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 + diff --git a/identificar.py b/identificar.py new file mode 100755 index 0000000..2aef8a1 --- /dev/null +++ b/identificar.py @@ -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 + diff --git a/led_off.service b/led_off.service new file mode 100644 index 0000000..b7a6d56 --- /dev/null +++ b/led_off.service @@ -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 diff --git a/led_on.service b/led_on.service new file mode 100644 index 0000000..773719f --- /dev/null +++ b/led_on.service @@ -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 + diff --git a/off.py b/off.py new file mode 100755 index 0000000..c30864c --- /dev/null +++ b/off.py @@ -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) + + diff --git a/on.py b/on.py new file mode 100755 index 0000000..055070f --- /dev/null +++ b/on.py @@ -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) + +