diff --git a/orchestrateur/main.py b/orchestrateur/main.py index d1c117a..a1abe02 100644 --- a/orchestrateur/main.py +++ b/orchestrateur/main.py @@ -299,6 +299,10 @@ async def process_messages_task(): n_state = data["data"].get("new_cooking_state") print(f"[LoRa] Microwave {mw_id} state changed to: {n_state}") + if n_state in (CookingStates.DONE, CookingStates.STIRRING_REQUIRED, CookingStates.ALERT): + print("buzzer alert triggered!") + beats = 5 if n_state == CookingStates.ALERT else (1 if n_state == CookingStates.STIRRING_REQUIRED else 3) + buzzer.buzzer_siren(beatsNb=beats) if n_state == CookingStates.IDLE and microwave_states.get(mw_id) == MicrowaveState.COOKING: microwave_states[mw_id] = MicrowaveState.DONE print(f"[{mw_id}] Cooking finished. Waiting for user to remove dish.") diff --git a/orchestrateur/sensors/__init__.py b/orchestrateur/sensors/__init__.py index de12db0..dd0a856 100644 --- a/orchestrateur/sensors/__init__.py +++ b/orchestrateur/sensors/__init__.py @@ -4,4 +4,5 @@ import sensors.ultrasonicRanger as ultrasonicRanger import sensors.temp_hum as temp_hum import sensors.button as button import sensors.gps as gps -import sensors.camera as camera \ No newline at end of file +import sensors.camera as camera +import sensors.buzzer as buzzer \ No newline at end of file diff --git a/orchestrateur/sensors/buzzer.py b/orchestrateur/sensors/buzzer.py new file mode 100644 index 0000000..49f5d23 --- /dev/null +++ b/orchestrateur/sensors/buzzer.py @@ -0,0 +1,33 @@ +import time +import grovepi +from sensors.lock import grove_lock +from shared import config + +# Pin definition +BUZZER_PIN = 8 + +grovepi.pinMode(BUZZER_PIN, "OUTPUT") +buzzer_state = 0 + +def set_buzzer(state: int): + global buzzer_state + buzzer_state = state + if not config.BUZZER_ACTIVATED: + return + try: + with grove_lock: + grovepi.digitalWrite(BUZZER_PIN, state) + except Exception as e: + print(f"[BUZZER] Error occurred while setting buzzer state: {e}") + +def buzzer_toggle(): + global buzzer_state + new_state = 1 - buzzer_state + set_buzzer(new_state) + +def buzzer_siren(beatsNb: int = 3, delay: float = 0.3): + for _ in range(beatsNb): + set_buzzer(1) + time.sleep(delay) + set_buzzer(0) + time.sleep(delay) \ No newline at end of file diff --git a/shared/config.py b/shared/config.py index be1e467..66ab8a6 100644 --- a/shared/config.py +++ b/shared/config.py @@ -15,4 +15,6 @@ MQTT_QOS = 1 MQTT_HELLO_INTERVAL = 30 # Microwave Model -COOKING_COMPARTMENT_HEIGHT = 30 # cm \ No newline at end of file +COOKING_COMPARTMENT_HEIGHT = 30 # cm + +BUZZER_ACTIVATED = False \ No newline at end of file