#u python script to interface IR sensor with ESP32
from machine import Pin
from time import sleep
led= Pin(2, Pin.OUT)
buzzer= Pin(21, Pin.OUT)
pir= Pin(34, Pin.IN)
bttn=Pin(35,Pin.IN)
motion_detected = False
# Fonction appelée lors de l'interruption du capteur PIR
def handle_interrupt(pin):
global motion_detected
motion_detected = True # Détecter le mouvement
# Fonction principale pour gérer le comportement
def motion_handler():
global motion_detected
if motion_detected:
print('Motion detected!')
led.value(1) # Allumer la LED
buzzer.value(1) # Activer le buzzer
# Attendre que le bouton soit pressé pour éteindre LED et buzzer
while bttn.value() == 0: # Tant que le bouton n'est pas pressé
pass # On attend
# Bouton pressé, éteindre LED et buzzer
print('NO Motion detected!')
led.value(0)
buzzer.value(0)
motion_detected = False # Réinitialiser l'état du mouvement
# Configuration de l'interruption pour le capteur PIR
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
# Boucle principale
while True:
motion_handler() # Appeler la fonction pour gérer la détection et les actions