maj alarme

This commit is contained in:
2026-04-01 16:17:21 +02:00
parent cc0365a2e1
commit 0ab04110ad

View File

@@ -1,245 +1,245 @@
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import time import time
import threading import threading
# ── Numérotation BCM ──────────────────────────────────────────────────────── # ── Numérotation BCM ────────────────────────────────────────────────────────
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) GPIO.setwarnings(False)
# ── Broches ───────────────────────────────────────────────────────────────── # ── Broches ─────────────────────────────────────────────────────────────────
PIN_LED_R = 17 PIN_LED_R = 17
PIN_LED_G = 27 PIN_LED_G = 22
PIN_LED_B = 22 PIN_LED_B = 27
PIN_PIR = 15 PIN_PIR = 15
PIN_BUZZER = 18 PIN_BUZZER = 18
# Keypad 4x4 — 4 lignes (sorties) + 4 colonnes (entrées pull-up) # Keypad 4x4 — 4 lignes (sorties) + 4 colonnes (entrées pull-up)
ROWS = [5, 6, 13, 19] # R1 R2 R3 R4 ROWS = [5, 6, 13, 19] # R1 R2 R3 R4
COLS = [26, 12, 16, 20] # C1 C2 C3 C4 COLS = [26, 12, 16, 20] # C1 C2 C3 C4
KEYPAD_MAP = [ KEYPAD_MAP = [
['1', '2', '3', 'A'], ['1', '2', '3', 'A'],
['4', '5', '6', 'B'], ['4', '5', '6', 'B'],
['7', '8', '9', 'C'], ['7', '8', '9', 'C'],
['*', '0', '#', 'D'], ['*', '0', '#', 'D'],
] ]
# ── Code secret (modifiable ici) ───────────────────────────────────────────── # ── Code secret (modifiable ici) ─────────────────────────────────────────────
CODE_SECRET = "1234" CODE_SECRET = "1234"
# ── Configuration GPIO ─────────────────────────────────────────────────────── # ── Configuration GPIO ───────────────────────────────────────────────────────
GPIO.setup(PIN_LED_R, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(PIN_LED_R, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(PIN_LED_G, 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_LED_B, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(PIN_BUZZER, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(PIN_BUZZER, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(PIN_PIR, GPIO.IN) GPIO.setup(PIN_PIR, GPIO.IN)
for row in ROWS: for row in ROWS:
GPIO.setup(row, GPIO.OUT, initial=GPIO.HIGH) GPIO.setup(row, GPIO.OUT, initial=GPIO.HIGH)
for col in COLS: for col in COLS:
GPIO.setup(col, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(col, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# ── État global ────────────────────────────────────────────────────────────── # ── État global ──────────────────────────────────────────────────────────────
etat = "desarmee" etat = "desarmee"
etat_lock = threading.Lock() etat_lock = threading.Lock()
_stop_buzzer = threading.Event() _stop_buzzer = threading.Event()
_thread_buzzer = None _thread_buzzer = None
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
# LED RGB # LED RGB
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
def led(r=False, g=False, b=False): def led(r=False, g=False, b=False):
"""Allume la LED RGB avec la couleur voulue.""" """Allume la LED RGB avec la couleur voulue."""
GPIO.output(PIN_LED_R, GPIO.HIGH if r else GPIO.LOW) 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_G, GPIO.HIGH if g else GPIO.LOW)
GPIO.output(PIN_LED_B, GPIO.HIGH if b else GPIO.LOW) GPIO.output(PIN_LED_B, GPIO.HIGH if b else GPIO.LOW)
def led_bleu(): led(b=True) def led_bleu(): led(b=True)
def led_vert(): led(g=True) def led_vert(): led(g=True)
def led_rouge(): led(r=True) def led_rouge(): led(r=True)
def led_off(): led() def led_off(): led()
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
# Buzzer # Buzzer
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
def bip(nb=1, duree=0.08, pause=0.12): def bip(nb=1, duree=0.08, pause=0.12):
"""Émet nb bip(s) courts.""" """Émet nb bip(s) courts."""
for _ in range(nb): for _ in range(nb):
GPIO.output(PIN_BUZZER, GPIO.HIGH) GPIO.output(PIN_BUZZER, GPIO.HIGH)
time.sleep(duree) time.sleep(duree)
GPIO.output(PIN_BUZZER, GPIO.LOW) GPIO.output(PIN_BUZZER, GPIO.LOW)
time.sleep(pause) time.sleep(pause)
def _buzzer_continu(stop_event: threading.Event): def _buzzer_continu(stop_event: threading.Event):
"""Boucle interne : buzzer ON/OFF jusqu'à stop_event.""" """Boucle interne : buzzer ON/OFF jusqu'à stop_event."""
while not stop_event.is_set(): while not stop_event.is_set():
GPIO.output(PIN_BUZZER, GPIO.HIGH) GPIO.output(PIN_BUZZER, GPIO.HIGH)
time.sleep(0.5) time.sleep(0.5)
GPIO.output(PIN_BUZZER, GPIO.LOW) GPIO.output(PIN_BUZZER, GPIO.LOW)
time.sleep(0.5) time.sleep(0.5)
GPIO.output(PIN_BUZZER, GPIO.LOW) GPIO.output(PIN_BUZZER, GPIO.LOW)
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
# Keypad 4x4 # Keypad 4x4
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
def lire_touche(): def lire_touche():
""" """
Scan matriciel : met chaque ligne à LOW tour à tour Scan matriciel : met chaque ligne à LOW tour à tour
et lit les colonnes. Retourne la touche ou None. et lit les colonnes. Retourne la touche ou None.
""" """
for i, row in enumerate(ROWS): for i, row in enumerate(ROWS):
GPIO.output(row, GPIO.LOW) GPIO.output(row, GPIO.LOW)
for j, col in enumerate(COLS): for j, col in enumerate(COLS):
if GPIO.input(col) == GPIO.LOW: if GPIO.input(col) == GPIO.LOW:
time.sleep(0.05) # anti-rebond time.sleep(0.05) # anti-rebond
while GPIO.input(col) == GPIO.LOW: while GPIO.input(col) == GPIO.LOW:
pass # attente relâchement pass # attente relâchement
GPIO.output(row, GPIO.HIGH) GPIO.output(row, GPIO.HIGH)
return KEYPAD_MAP[i][j] return KEYPAD_MAP[i][j]
GPIO.output(row, GPIO.HIGH) GPIO.output(row, GPIO.HIGH)
return None return None
def lire_code(nb_chiffres=4, timeout=30): def lire_code(nb_chiffres=4, timeout=30):
""" """
Attend nb_chiffres touches numériques sur le keypad. Attend nb_chiffres touches numériques sur le keypad.
Retourne la chaîne saisie ou '' si timeout. Retourne la chaîne saisie ou '' si timeout.
""" """
saisi = "" saisi = ""
debut = time.time() debut = time.time()
print(" Code : ", end="", flush=True) print(" Code : ", end="", flush=True)
while len(saisi) < nb_chiffres: while len(saisi) < nb_chiffres:
if time.time() - debut > timeout: if time.time() - debut > timeout:
print("\n [Timeout — saisie annulée]") print("\n [Timeout — saisie annulée]")
return "" return ""
touche = lire_touche() touche = lire_touche()
if touche and touche.isdigit(): if touche and touche.isdigit():
saisi += touche saisi += touche
print("*", end="", flush=True) print("*", end="", flush=True)
time.sleep(0.05) time.sleep(0.05)
print() print()
return saisi return saisi
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
# Transitions d'état # Transitions d'état
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
def passer_en_desarmee(): def passer_en_desarmee():
global etat, _thread_buzzer global etat, _thread_buzzer
_stop_buzzer.set() _stop_buzzer.set()
if _thread_buzzer and _thread_buzzer.is_alive(): if _thread_buzzer and _thread_buzzer.is_alive():
_thread_buzzer.join() _thread_buzzer.join()
with etat_lock: with etat_lock:
etat = "desarmee" etat = "desarmee"
led_bleu() led_bleu()
print("[ÉTAT] ● DÉSARMÉE — LED bleue") print("[ÉTAT] ● DÉSARMÉE — LED bleue")
def passer_en_armee(): def passer_en_armee():
global etat global etat
with etat_lock: with etat_lock:
etat = "armee" etat = "armee"
led_vert() led_vert()
bip(nb=2) # 2 petits bips = armée avec succès bip(nb=2) # 2 petits bips = armée avec succès
print("[ÉTAT] ● ARMÉE — LED verte — PIR actif") print("[ÉTAT] ● ARMÉE — LED verte — PIR actif")
def passer_en_declenchee(): def passer_en_declenchee():
global etat, _thread_buzzer global etat, _thread_buzzer
with etat_lock: with etat_lock:
etat = "declenchee" etat = "declenchee"
led_rouge() led_rouge()
print("[ÉTAT] ● DÉCLENCHÉE — LED rouge — buzzer actif") print("[ÉTAT] ● DÉCLENCHÉE — LED rouge — buzzer actif")
_stop_buzzer.clear() _stop_buzzer.clear()
_thread_buzzer = threading.Thread( _thread_buzzer = threading.Thread(
target=_buzzer_continu, args=(_stop_buzzer,), daemon=True target=_buzzer_continu, args=(_stop_buzzer,), daemon=True
) )
_thread_buzzer.start() _thread_buzzer.start()
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
# Thread : surveillance PIR # Thread : surveillance PIR
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
def _surveiller_pir(stop_evt: threading.Event): def _surveiller_pir(stop_evt: threading.Event):
"""Lit le PIR toutes les 100 ms. Déclenche si mouvement et armée.""" """Lit le PIR toutes les 100 ms. Déclenche si mouvement et armée."""
print("[PIR] Surveillance démarrée") print("[PIR] Surveillance démarrée")
while not stop_evt.is_set(): while not stop_evt.is_set():
with etat_lock: with etat_lock:
etat_local = etat etat_local = etat
if etat_local == "armee" and GPIO.input(PIN_PIR) == GPIO.HIGH: if etat_local == "armee" and GPIO.input(PIN_PIR) == GPIO.HIGH:
print("[PIR] ⚠ Mouvement détecté !") print("[PIR] ⚠ Mouvement détecté !")
passer_en_declenchee() passer_en_declenchee()
time.sleep(0.1) time.sleep(0.1)
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
# Boucle principale # Boucle principale
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
def boucle_principale(): def boucle_principale():
global etat global etat
# Démarrage : LED bleue (désarmée) # Démarrage : LED bleue (désarmée)
passer_en_desarmee() passer_en_desarmee()
# Thread PIR en arrière-plan # Thread PIR en arrière-plan
stop_pir = threading.Event() stop_pir = threading.Event()
thread_pir = threading.Thread( thread_pir = threading.Thread(
target=_surveiller_pir, args=(stop_pir,), daemon=True target=_surveiller_pir, args=(stop_pir,), daemon=True
) )
thread_pir.start() thread_pir.start()
print("\n=== Système d'alarme démarré ===") print("\n=== Système d'alarme démarré ===")
print(" Tapez le code sur le keypad pour armer / désarmer.\n") print(" Tapez le code sur le keypad pour armer / désarmer.\n")
try: try:
while True: while True:
with etat_lock: with etat_lock:
etat_local = etat etat_local = etat
# ── DÉSARMÉE : attente d'un code pour armer ────────────────────── # ── DÉSARMÉE : attente d'un code pour armer ──────────────────────
if etat_local == "desarmee": if etat_local == "desarmee":
print(" → Saisir le code pour ARMER :") print(" → Saisir le code pour ARMER :")
code = lire_code(nb_chiffres=len(CODE_SECRET)) code = lire_code(nb_chiffres=len(CODE_SECRET))
if code == CODE_SECRET: if code == CODE_SECRET:
print(" ✔ Code correct → armement") print(" ✔ Code correct → armement")
passer_en_armee() passer_en_armee()
elif code != "": elif code != "":
print(" ✘ Code incorrect") print(" ✘ Code incorrect")
bip(nb=1, duree=0.4) # 1 bip long = erreur bip(nb=1, duree=0.4) # 1 bip long = erreur
# ── ARMÉE : le thread PIR gère le déclenchement ────────────────── # ── ARMÉE : le thread PIR gère le déclenchement ──────────────────
elif etat_local == "armee": elif etat_local == "armee":
time.sleep(0.1) time.sleep(0.1)
# ── DÉCLENCHÉE : attente du code pour désarmer ─────────────────── # ── DÉCLENCHÉE : attente du code pour désarmer ───────────────────
elif etat_local == "declenchee": elif etat_local == "declenchee":
print(" → Saisir le code pour DÉSARMER :") print(" → Saisir le code pour DÉSARMER :")
code = lire_code(nb_chiffres=len(CODE_SECRET)) code = lire_code(nb_chiffres=len(CODE_SECRET))
if code == CODE_SECRET: if code == CODE_SECRET:
print(" ✔ Code correct → désarmement") print(" ✔ Code correct → désarmement")
passer_en_desarmee() passer_en_desarmee()
elif code != "": elif code != "":
print(" ✘ Code incorrect — alarme maintenue") print(" ✘ Code incorrect — alarme maintenue")
except KeyboardInterrupt: except KeyboardInterrupt:
print("\n[INFO] Arrêt demandé (Ctrl+C)") print("\n[INFO] Arrêt demandé (Ctrl+C)")
finally: finally:
stop_pir.set() stop_pir.set()
_stop_buzzer.set() _stop_buzzer.set()
led_off() led_off()
GPIO.cleanup() GPIO.cleanup()
print("[INFO] GPIO libérés. Fin du programme.") print("[INFO] GPIO libérés. Fin du programme.")
# ════════════════════════════════════════════════════════════════════════════ # ════════════════════════════════════════════════════════════════════════════
if __name__ == "__main__": if __name__ == "__main__":
boucle_principale() boucle_principale()