finally the end of the road

This commit is contained in:
2026-04-02 23:09:57 +02:00
parent a14ebc3243
commit 9770617cd4
3 changed files with 122 additions and 78 deletions

View File

@@ -2,20 +2,20 @@ import RPi.GPIO as GPIO
import time import time
import threading import threading
# Configuration initiale # ── Numérotation BCM ────────────────────────────────────────────────────────
GPIO.setmode(GPIO.BOARD) # Utilisation des numéros physiques des pins GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) GPIO.setwarnings(False)
# --- CONFIGURATION PINS (BOARD) --- # ── Broches ─────────────────────────────────────────────────────────────────
PIN_LED_R = 11 PIN_LED_R = 17
PIN_LED_G = 15 PIN_LED_G = 27
PIN_LED_B = 13 PIN_LED_B = 22
PIN_PIR = 10 PIN_PIR = 15
PIN_BUZZER = 12 PIN_BUZZER = 18
# Pins Keypad (Vérifie bien tes branchements physiques sur ces numéros) # Keypad 4x4 — 4 lignes (sorties) + 4 colonnes (entrées pull-up)
ROWS = [29, 31, 33, 35] ROWS = [5, 6, 13, 19] # R1 R2 R3 R4
COLS = [37, 32, 36, 38] COLS = [26, 12, 16, 20] # C1 C2 C3 C4
KEYPAD_MAP = [ KEYPAD_MAP = [
['1', '2', '3', 'A'], ['1', '2', '3', 'A'],
@@ -24,9 +24,10 @@ KEYPAD_MAP = [
['*', '0', '#', 'D'], ['*', '0', '#', 'D'],
] ]
# ── Code secret (modifiable ici) ─────────────────────────────────────────────
CODE_SECRET = "1234" CODE_SECRET = "1234"
# --- INITIALISATION 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)
@@ -38,15 +39,20 @@ for row in ROWS:
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)
etat_alarme = "Désarmée" # ── État global ──────────────────────────────────────────────────────────────
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
# ════════════════════════════════════════════════════════════════════════════
def led(r=False, g=False, b=False): def led(r=False, g=False, b=False):
"""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)
@@ -56,7 +62,13 @@ 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
# ════════════════════════════════════════════════════════════════════════════
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."""
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)
@@ -64,6 +76,7 @@ def bip(nb=1, duree=0.08, pause=0.12):
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."""
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)
@@ -72,123 +85,156 @@ def _buzzer_continu(stop_event: threading.Event):
GPIO.output(PIN_BUZZER, GPIO.LOW) GPIO.output(PIN_BUZZER, GPIO.LOW)
def lire_touche(): # ════════════════════════════════════════════════════════════════════════════
# Keypad 4x4
# ════════════════════════════════════════════════════════════════════════════
def lire_touche():
"""
Scan matriciel : met chaque ligne à LOW tour à tour
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)
time.sleep(0.01) # Stabilisation électrique
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:
time.sleep(0.01) 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=20): def lire_code(nb_chiffres=4, timeout=30):
"""
Attend nb_chiffres touches numériques sur le keypad.
Retourne la chaîne saisie ou '' si timeout.
"""
saisi = "" saisi = ""
debut = time.time() debut = time.time()
print(" Entrez le 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]") 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
bip(1, 0.05) # Petit bip de confirmation touche
print("*", end="", flush=True) print("*", end="", flush=True)
time.sleep(0.05) time.sleep(0.05)
print() print()
return saisi return saisi
# --- GESTION DES ÉTATS ---
# ════════════════════════════════════════════════════════════════════════════
# Transitions d'état
# ════════════════════════════════════════════════════════════════════════════
def passer_en_desarmee(): def passer_en_desarmee():
global etat_alarme, _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_alarme = "sarmée" etat = "desarmee"
led_bleu() led_bleu()
print("[ÉTAT] ● DÉSARMÉE") print("[ÉTAT] ● DÉSARMÉE — LED bleue")
def passer_en_armee(): def passer_en_armee():
global etat_alarme global etat
with etat_lock: with etat_lock:
etat_alarme = "Armée" etat = "armee"
led_vert() led_vert()
bip(nb=2) bip(nb=2) # 2 petits bips = armée avec succès
print("[ÉTAT] ● ARMÉE") print("[ÉTAT] ● ARMÉE — LED verte — PIR actif")
def passer_en_declenchee(): def passer_en_declenchee():
global etat_alarme, _thread_buzzer global etat, _thread_buzzer
with etat_lock: with etat_lock:
# On ne déclenche que si on était armé etat = "declenchee"
if etat_alarme == "Armée": led_rouge()
etat_alarme = "Déclenchée" print("[ÉTAT] ● DÉCLENCHÉE — LED rouge — buzzer actif")
led_rouge() _stop_buzzer.clear()
print("[ÉTAT] ● DÉCLENCHÉE !") _thread_buzzer = threading.Thread(
_stop_buzzer.clear() target=_buzzer_continu, args=(_stop_buzzer,), daemon=True
_thread_buzzer = threading.Thread(target=_buzzer_continu, args=(_stop_buzzer,), daemon=True) )
_thread_buzzer.start() _thread_buzzer.start()
# --- SURVEILLANCE ---
# ════════════════════════════════════════════════════════════════════════════
# 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."""
print("[PIR] Surveillance démarrée")
while not stop_evt.is_set(): while not stop_evt.is_set():
with etat_lock: with etat_lock:
local_etat = etat_alarme etat_local = etat
if local_etat == "Armée" and GPIO.input(PIN_PIR) == GPIO.HIGH: if etat_local == "armee" and GPIO.input(PIN_PIR) == GPIO.HIGH:
print("[PIR] ⚠ Mouvement détecté !")
passer_en_declenchee() passer_en_declenchee()
time.sleep(0.2) time.sleep(0.1)
# ════════════════════════════════════════════════════════════════════════════
# Boucle principale
# ════════════════════════════════════════════════════════════════════════════
def boucle_principale(): def boucle_principale():
"""Lancée par board1main dans un thread.""" global etat
# Démarrage : LED bleue (désarmée)
passer_en_desarmee() passer_en_desarmee()
stop_pir = threading.Event() # Thread PIR en arrière-plan
thread_pir = threading.Thread(target=_surveiller_pir, args=(stop_pir,), daemon=True) stop_pir = threading.Event()
thread_pir = threading.Thread(
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")
try: try:
while True: while True:
with etat_lock: with etat_lock:
current = etat_alarme etat_local = etat
# CAS 1 : L'alarme est éteinte, on attend le code pour l'allumer # ── DÉSARMÉE : attente d'un code pour armer ──────────────────────
if current == "sarmée": if etat_local == "desarmee":
print(" [DÉSARMÉE] Entrez code pour ARMER...") print(" → Saisir le code pour ARMER :")
code = lire_code(len(CODE_SECRET)) code = lire_code(nb_chiffres=len(CODE_SECRET))
if code == CODE_SECRET: if code == CODE_SECRET:
print(" ✔ Code correct → armement")
passer_en_armee() passer_en_armee()
elif code != "": elif code != "":
bip(1, 0.5) print(" ✘ Code incorrect")
bip(nb=1, duree=0.4) # 1 bip long = erreur
# CAS 2 : L'alarme est allumée, on attend le code pour l'éteindre # ── ARMÉE : le thread PIR gère le déclenchement ──────────────────
elif current == "Armée": elif etat_local == "armee":
# On réutilise lire_code ici pour permettre le désarmement manuel time.sleep(0.1)
print(" [ARMÉE] Entrez code pour DÉSARMER...")
code = lire_code(len(CODE_SECRET)) # ── DÉCLENCHÉE : attente du code pour désarmer ───────────────────
elif etat_local == "declenchee":
print(" → Saisir le code pour DÉSARMER :")
code = lire_code(nb_chiffres=len(CODE_SECRET))
if code == CODE_SECRET: if code == CODE_SECRET:
print(" ✔ Code correct → désarmement")
passer_en_desarmee() passer_en_desarmee()
elif code != "": elif code != "":
bip(1, 0.5) print(" ✘ Code incorrect — alarme maintenue")
except KeyboardInterrupt:
print("\n[INFO] Arrêt demandé (Ctrl+C)")
# CAS 3 : L'alarme sonne, on attend le code pour stopper le buzzer
elif current == "Déclenchée":
print(" [ALERTE] Entrez code pour STOPPER...")
code = lire_code(len(CODE_SECRET))
if code == CODE_SECRET:
passer_en_desarmee()
time.sleep(0.1)
finally: finally:
stop_pir.set() stop_pir.set()
_stop_buzzer.set() _stop_buzzer.set()
led_off() led_off()
GPIO.cleanup()
print("[INFO] GPIO libérés. Fin du programme.")

View File

@@ -5,7 +5,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Loustiques Home — Dashboard</title> <title>Loustiques Home — Dashboard</title>
<style> <style>
/* Style conservé pour l'interface sombre et moderne */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: system-ui, sans-serif; background: #1f1f1f; color: #f0f0f0; min-height: 100vh; } body { font-family: system-ui, sans-serif; background: #1f1f1f; color: #f0f0f0; min-height: 100vh; }
header { display: flex; align-items: center; justify-content: space-between; padding: 18px 32px; border-bottom: 1px solid #2e2e2e; background: #1a1a1a; } header { display: flex; align-items: center; justify-content: space-between; padding: 18px 32px; border-bottom: 1px solid #2e2e2e; background: #1a1a1a; }

View File

@@ -120,7 +120,7 @@
<div class="carte"> <div class="carte">
<h1>Loustiques Home</h1> <h1>Loustiques Home</h1>
<p class="soustitre">Connectez-vous pour accéder à votre espace.</p> <p class="soustitre">Connectez-vous via mot de passe ou avec votre bagde pour accéder à votre espace.</p>
<div class="champ"> <div class="champ">
<label for="username">Identifiant</label> <label for="username">Identifiant</label>
@@ -129,7 +129,7 @@
<div class="champ"> <div class="champ">
<label for="password">Mot de passe</label> <label for="password">Mot de passe</label>
<input type="password" id="password" placeholder="Mot de passe du loustique" autocomplete="current-password" /> <input type="password" id="password" placeholder="Wola ouais" autocomplete="current-password" />
</div> </div>
<button id="btn" onclick="handleLogin()">Se connecter</button> <button id="btn" onclick="handleLogin()">Se connecter</button>
@@ -139,15 +139,13 @@
<p class="footer">Accès réservé aux utilisateurs ajoutés par les loustiques.</p> <p class="footer">Accès réservé aux utilisateurs ajoutés par les loustiques.</p>
</div> </div>
<script> <script>
["username", "password"].forEach(id => { ["username", "password"].forEach(id => {
document.getElementById(id).addEventListener("keydown", e => { document.getElementById(id).addEventListener("keydown", e => {
if (e.key === "Enter") handleLogin(); if (e.key === "Enter") handleLogin();
}); });
}); });
-
async function handleLogin() { async function handleLogin() {
const username = document.getElementById("username").value.trim(); const username = document.getElementById("username").value.trim();
const password = document.getElementById("password").value; const password = document.getElementById("password").value;
@@ -189,10 +187,12 @@
btn.textContent = "Se connecter"; btn.textContent = "Se connecter";
} }
} }
setInterval(async () => { setInterval(async () => {
try { try {
// Attention : Vérifie que cette route correspond bien à celle dans main.py
const res = await fetch('/rfid-scan'); // (J'avais mis /check-rfid-login dans mon exemple précédent)
const res = await fetch('/check-rfid-login');
const data = await res.json(); const data = await res.json();
if (data.success) { if (data.success) {
@@ -211,10 +211,9 @@
}, 1000); }, 1000);
} }
} catch (e) { } catch (e) {
// Erreurs ignorées silencieusement
} }
}, 2000); }, 1500);
</script> </script>
</body> </body>
</html> </html>