Files
loustique-home/composants/byPanda/alarme.py

187 lines
4.9 KiB
Python
Raw Normal View History

2026-03-27 13:33:32 +01:00
import RPi.GPIO as GPIO
2026-04-02 22:20:07 +02:00
import time
import threading
2026-03-27 13:33:32 +01:00
2026-04-02 23:09:57 +02:00
GPIO.setmode(GPIO.BCM)
2026-03-27 13:33:32 +01:00
GPIO.setwarnings(False)
2026-04-02 23:09:57 +02:00
PIN_LED_R = 17
2026-04-03 18:35:33 +02:00
PIN_LED_G = 22
PIN_LED_B = 3
PIN_PIR = 23
2026-04-02 23:09:57 +02:00
PIN_BUZZER = 18
2026-04-02 22:20:07 +02:00
2026-04-03 18:35:33 +02:00
ROWS = [5, 6, 13, 19]
COLS = [26, 12, 16, 20]
2026-04-02 22:20:07 +02:00
KEYPAD_MAP = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D'],
]
CODE_SECRET = "1234"
2026-04-03 18:35:33 +02:00
2026-04-02 22:20:07 +02:00
GPIO.setup(PIN_LED_R, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(PIN_LED_G, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(PIN_LED_B, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(PIN_BUZZER, GPIO.OUT, initial=GPIO.LOW)
2026-04-03 18:35:33 +02:00
GPIO.setup(PIN_PIR, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
2026-04-02 22:20:07 +02:00
for row in ROWS:
GPIO.setup(row, GPIO.OUT, initial=GPIO.HIGH)
for col in COLS:
GPIO.setup(col, GPIO.IN, pull_up_down=GPIO.PUD_UP)
2026-04-03 18:35:33 +02:00
etat = "Désarmée"
2026-04-02 22:20:07 +02:00
etat_lock = threading.Lock()
_stop_buzzer = threading.Event()
_thread_buzzer = None
def led(r=False, g=False, b=False):
GPIO.output(PIN_LED_R, GPIO.HIGH if r else GPIO.LOW)
GPIO.output(PIN_LED_G, GPIO.HIGH if g else GPIO.LOW)
GPIO.output(PIN_LED_B, GPIO.HIGH if b else GPIO.LOW)
def bip(nb=1, duree=0.08, pause=0.12):
for _ in range(nb):
GPIO.output(PIN_BUZZER, GPIO.HIGH)
time.sleep(duree)
GPIO.output(PIN_BUZZER, GPIO.LOW)
time.sleep(pause)
2026-04-03 18:35:33 +02:00
def _buzzer_continu(stop_event):
2026-04-02 22:20:07 +02:00
while not stop_event.is_set():
GPIO.output(PIN_BUZZER, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(PIN_BUZZER, GPIO.LOW)
time.sleep(0.5)
GPIO.output(PIN_BUZZER, GPIO.LOW)
2026-04-03 18:35:33 +02:00
def etat_alarme():
with etat_lock:
return etat
2026-04-02 22:20:07 +02:00
2026-04-02 23:09:57 +02:00
def lire_touche():
2026-04-02 22:20:07 +02:00
for i, row in enumerate(ROWS):
GPIO.output(row, GPIO.LOW)
for j, col in enumerate(COLS):
if GPIO.input(col) == GPIO.LOW:
2026-04-03 18:35:33 +02:00
time.sleep(0.05)
2026-04-02 22:20:07 +02:00
while GPIO.input(col) == GPIO.LOW:
2026-04-03 18:35:33 +02:00
pass
2026-04-02 22:20:07 +02:00
GPIO.output(row, GPIO.HIGH)
return KEYPAD_MAP[i][j]
GPIO.output(row, GPIO.HIGH)
return None
2026-04-02 23:09:57 +02:00
def lire_code(nb_chiffres=4, timeout=30):
2026-04-02 22:20:07 +02:00
saisi = ""
debut = time.time()
2026-04-02 23:09:57 +02:00
print(" Code : ", end="", flush=True)
2026-04-02 22:20:07 +02:00
while len(saisi) < nb_chiffres:
if time.time() - debut > timeout:
2026-04-03 18:35:33 +02:00
print("\n [Timeout]")
2026-04-02 22:20:07 +02:00
return ""
touche = lire_touche()
if touche and touche.isdigit():
saisi += touche
print("*", end="", flush=True)
time.sleep(0.05)
print()
return saisi
2026-04-02 23:09:57 +02:00
2026-04-02 22:20:07 +02:00
def passer_en_desarmee():
2026-04-02 23:09:57 +02:00
global etat, _thread_buzzer
2026-04-02 22:20:07 +02:00
_stop_buzzer.set()
if _thread_buzzer and _thread_buzzer.is_alive():
_thread_buzzer.join()
with etat_lock:
2026-04-03 18:35:33 +02:00
etat = "Désarmée"
led(b=True) # Bleu
print("[ÉTAT] ● DÉSARMÉE")
2026-04-02 22:20:07 +02:00
def passer_en_armee():
2026-04-02 23:09:57 +02:00
global etat
2026-04-03 18:35:33 +02:00
print("[ÉTAT] ● ARMEMENT... Stabilisation capteur (10s)")
led(r=True, g=True)
time.sleep(10)
2026-04-02 22:20:07 +02:00
with etat_lock:
2026-04-03 18:35:33 +02:00
etat = "Armée"
led(g=True)
bip(nb=2)
print("[ÉTAT] ● ARMÉE — Surveillance active !")
2026-04-02 22:20:07 +02:00
def passer_en_declenchee():
2026-04-02 23:09:57 +02:00
global etat, _thread_buzzer
2026-04-02 22:20:07 +02:00
with etat_lock:
2026-04-03 18:35:33 +02:00
etat = "Déclenchée"
led(r=True) # Rouge
print("[ÉTAT] ● DÉCLENCHÉE !")
2026-04-02 23:09:57 +02:00
_stop_buzzer.clear()
2026-04-03 18:35:33 +02:00
_thread_buzzer = threading.Thread(target=_buzzer_continu, args=(_stop_buzzer,), daemon=True)
2026-04-02 23:09:57 +02:00
_thread_buzzer.start()
2026-04-03 18:35:33 +02:00
def _surveiller_pir(stop_evt):
print("[PIR] Thread de surveillance prêt")
2026-04-02 22:20:07 +02:00
while not stop_evt.is_set():
with etat_lock:
2026-04-02 23:09:57 +02:00
etat_local = etat
2026-04-03 18:35:33 +02:00
if etat_local == "Armée":
if GPIO.input(PIN_PIR) == GPIO.HIGH:
time.sleep(0.3)
if GPIO.input(PIN_PIR) == GPIO.HIGH:
passer_en_declenchee()
2026-04-02 23:09:57 +02:00
time.sleep(0.1)
2026-04-02 22:20:07 +02:00
def boucle_principale():
passer_en_desarmee()
2026-04-02 23:09:57 +02:00
stop_pir = threading.Event()
2026-04-03 18:35:33 +02:00
thread_pir = threading.Thread(target=_surveiller_pir, args=(stop_pir,), daemon=True)
2026-04-02 22:20:07 +02:00
thread_pir.start()
2026-04-03 18:35:33 +02:00
print("\n=== Système prêt (Code: " + CODE_SECRET + ") ===")
2026-04-02 22:20:07 +02:00
try:
while True:
2026-04-03 18:35:33 +02:00
etat_actuel = etat_alarme()
if etat_actuel == "Désarmée":
print("→ Saisir code pour ARMER :")
if lire_code() == CODE_SECRET:
2026-04-02 22:20:07 +02:00
passer_en_armee()
2026-04-03 18:35:33 +02:00
elif etat_actuel == "Armée":
print("→ Système ARMÉ (Code pour DÉSARMER) :")
if lire_code() == CODE_SECRET:
2026-04-02 22:20:07 +02:00
passer_en_desarmee()
2026-04-02 23:09:57 +02:00
2026-04-03 18:35:33 +02:00
elif etat_actuel == "Déclenchée":
print("→ Saisir code pour DÉSARMER :")
if lire_code() == CODE_SECRET:
passer_en_desarmee()
2026-04-02 22:20:07 +02:00
2026-04-03 18:35:33 +02:00
time.sleep(0.2)
except KeyboardInterrupt:
print("\n[INFO] Arrêt demandé par l'utilisateur")
2026-04-02 22:20:07 +02:00
finally:
stop_pir.set()
_stop_buzzer.set()
2026-04-03 18:35:33 +02:00
led(False, False, False)
2026-04-02 23:09:57 +02:00
GPIO.cleanup()
print("[INFO] GPIO libérés. Fin du programme.")