浏览代码

Change to HA controlled alarm. Works now.

Thomas Chef 3 月之前
父节点
当前提交
f33606380d
共有 6 个文件被更改,包括 66 次插入141 次删除
  1. 0 3
      main/config.h
  2. 0 25
      main/fridgeDoor.c
  3. 0 2
      main/main.c
  4. 22 9
      main/mqtt.c
  5. 39 92
      main/sound.c
  6. 5 10
      main/sound.h

+ 0 - 3
main/config.h

@@ -6,9 +6,6 @@
 
 #define SW_VERSION "1.0"
 
-
-#define ACTIVE_THRESHOLD_MS 60000  // 60 seconds in milliseconds
-
 #define PHOTO_SENSOR_PIN GPIO_NUM_18
 #define BLINK_GPIO CONFIG_BLINK_GPIO
 

+ 0 - 25
main/fridgeDoor.c

@@ -12,29 +12,6 @@
 
 #ifdef DOOR_SENSOR_ENABLED
 
-static void check_sensor_active_time(bool fridgeDoorClosed) {
-
-    static int64_t activeStartTime = 0;
-
-    //ESP_EARLY_LOGI("CHECK", "State: %s", fridgeDoorClosed == true ? "CLOSED" : "OPEN");
-
-    if (fridgeDoorClosed == false) {
-        if (activeStartTime == 0) {
-            activeStartTime = esp_timer_get_time() / 1000; // Convert to milliseconds
-            ESP_LOGI("CHECK", "Door timer start time: %lld", activeStartTime);
-        } else {
-            int64_t elapsedTime = (esp_timer_get_time() / 1000) - activeStartTime;
-            //ESP_LOGI("CHECK", "Elapsed time: %lld", elapsedTime);
-            if (elapsedTime > ACTIVE_THRESHOLD_MS) {
-                enableNotificationSound(true);
-            }
-        }
-    } else {
-        activeStartTime = 0;
-        enableNotificationSound(false);
-    }
-}
-
 static void read_photo_sensor_task(void *pvParameters) {
     static bool prevState = false;
     while (1) {
@@ -57,8 +34,6 @@ static void read_photo_sensor_task(void *pvParameters) {
             sendMQTTMessage(mqtt_s, value_s);
         }
 
-        check_sensor_active_time(fridgeDoorClosed);
-
         vTaskDelay(pdMS_TO_TICKS(50)); // Delay 50ms (20 times per second)
 
     }

+ 0 - 2
main/main.c

@@ -29,6 +29,4 @@ void app_main(void)
     configure_led();
     setup_photo_sensor();
     initSound();
-
-    alarmMasterControl(ALARM_ON);
 }

+ 22 - 9
main/mqtt.c

@@ -52,7 +52,11 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
             connected = true;
             ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
             esp_mqtt_client_publish(client, "kitchen/fridge/available", "online", 0, 1, true);
-            esp_mqtt_client_subscribe(client, "kitchen/fridge/doorAlarmControl", 0);
+            esp_mqtt_client_publish(client, "kitchen/alarm/available", "online", 0, 1, true);
+            esp_mqtt_client_publish(client, "kitchen/alarm/state", "off", 0, 1, true);
+            esp_mqtt_client_publish(client, "kitchen/fridge/doorState","closed", 0, 1, true);
+            esp_mqtt_client_subscribe(client, "kitchen/alarm/set", 0);
+            esp_mqtt_client_subscribe(client, "kitchen/alarm/notification", 0);
             break;
         case MQTT_EVENT_DISCONNECTED:
             connected = false;
@@ -66,18 +70,27 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
             ESP_LOGI(TAG, "MQTT_EVENT_DATA");
             printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
             printf("DATA=%.*s\r\n", event->data_len, event->data);
-            if (strncmp(event->topic, "kitchen/fridge/doorAlarmControl", event->topic_len) == 0) {
-                if (strncmp(event->data, "OFF", event->data_len) == 0) {
+            if (strncmp(event->topic, "kitchen/alarm/set", event->topic_len) == 0) {
+                if (strncmp(event->data, "off", event->data_len) == 0) {
                     ESP_LOGI("MQTT", "Fridge alarm disabled");
-                    alarmMasterControl(ALARM_OFF);
-                } else if (strncmp(event->data, "ON", event->data_len) == 0) {
+                    setAlarmState(false);
+                    esp_mqtt_client_publish(client, "kitchen/alarm/state", "off", 0, 1, true);
+                } else if (strncmp(event->data, "on", event->data_len) == 0) {
                     ESP_LOGI("MQTT", "Fridge alarm enabled");
-                    alarmMasterControl(ALARM_ON);
-                } else if (strncmp(event->data, "TEST", event->data_len) == 0) {
-                    ESP_LOGI("MQTT", "Fridge alarm test");
-                    alarmMasterControl(ALARM_TEST);
+                    setAlarmState(true);
+                    esp_mqtt_client_publish(client, "kitchen/alarm/state", "on", 0, 1, true);
                 }
             }
+            else if (strncmp(event->topic, "kitchen/alarm/notification", event->topic_len) == 0) {
+                if (strncmp(event->data, "notification", event->data_len) == 0) {
+                    ESP_LOGI("MQTT", "Fridge alarm notification enabled");
+                    triggerNotificationSound();
+                }
+            }
+            else {
+                ESP_LOGI(TAG, "Unknown topic");
+            }
+
             break;
         case MQTT_EVENT_ERROR:
             ESP_LOGI(TAG, "MQTT_EVENT_ERROR");

+ 39 - 92
main/sound.c

@@ -5,133 +5,80 @@
 //#include "driver/gpio.h"
 #include "esp_log.h"
 #include "sdkconfig.h"
-#include "driver/dac.h"
+//#include "driver/dac.h"
 #include "math.h"
 #include "config.h"
 #include "mqtt.h"
 
 #ifdef SOUND_ENABLED
 
-#define DAC_CHANNEL       1
-#define DAC_PIN           GPIO_NUM_25   // GPIO pin for DAC output GPIO25=Channel 1
-#define TONE_FREQ         440           // Frequency of the tone in Hz
-#define TONE_DURATION     1000          // Duration of the tone in ms
-#define QUIET_DURATION    1000          // Duration of the quiet period in ms
+#define BUZZ_PIN          GPIO_NUM_25   // GPIO pin for DAC output GPIO25=Channel 1
+#define BUZZ_SHORT_DURATION     200          // Duration of the buzz in ms
+#define QUIET_DURATION         1000          // Duration of the quiet period in ms
 
-// Internal variable used to control the actual sound state
-static bool soundEnabled = false;
-static bool alarmMasterEnabled = true;  // Master control, default ON
+static bool alarmEnabled = false; // Tracks the state of the normal alarm
 
-static void play_tone(int frequency, float duration, int quietDuration, bool override) {
-
-    int semitones = 0;
-
-    int t_freq = frequency * pow(2.0, semitones / 12.0);
-
-    int8_t cw_offset = 0;
-    dac_cw_config_t cosineConf = {DAC_CHANNEL_1, DAC_CW_SCALE_8, DAC_CW_PHASE_0, t_freq, cw_offset};
-
-    if (frequency == 0) {
-        dac_output_disable(DAC_CHANNEL_1);
-    } else {
-        dac_cw_generator_config(&cosineConf);
-        dac_output_enable(DAC_CHANNEL_1);
-    }
-
-    // Loop with 50mS loops until the duration is complete
-    for(int i = 0; i < duration; i+=50) {
-        vTaskDelay(pdMS_TO_TICKS(50));
-        if( soundEnabled == false && override == false ) {
-            break;
-        }
+static void activate_buzzer(int on_time, int off_time, int repetitions) {
+    
+    for (int i = 0; i < repetitions; i++) {
+        gpio_set_level(BUZZ_PIN, 1); // Set GPIO high
+        vTaskDelay(pdMS_TO_TICKS(on_time)); // Wait for the specified on time
+        gpio_set_level(BUZZ_PIN, 0); // Set GPIO low
+        vTaskDelay(pdMS_TO_TICKS(off_time)); // Wait for the specified off time
     }
-    dac_output_disable(DAC_CHANNEL_1);
 }
 
 static void setupSound() {
 
-    int8_t cw_offset = 0;
-    dac_cw_config_t cosineConf = {DAC_CHANNEL_1, DAC_CW_SCALE_8, DAC_CW_PHASE_0, 440, cw_offset};
+    // Configure GPIO25 as output and set it to inactive (low) by default
+    gpio_config_t io_conf = {
+        .pin_bit_mask = (1ULL << BUZZ_PIN),
+        .mode = GPIO_MODE_OUTPUT,
+        .pull_up_en = GPIO_PULLUP_DISABLE,
+        .pull_down_en = GPIO_PULLDOWN_DISABLE,
+        .intr_type = GPIO_INTR_DISABLE
+    };
+    gpio_config(&io_conf);
 
-    dac_output_disable(DAC_CHANNEL_1);
-    dac_cw_generator_config(&cosineConf);
-    dac_cw_generator_enable();
-    dac_output_disable(DAC_CHANNEL_1);
+    // Set GPIO25 to low (inactive)
+    gpio_set_level(BUZZ_PIN, 0);
 
     ESP_LOGI("SOUND", "Sound setup");
 }
 
 static void sound_task(void *pvParameters) {
-
     ESP_LOGI("SOUND", "Sound task starts....");
 
     setupSound();
 
+    activate_buzzer(50, 0, 1); // Initial short beep to indicate sound system is ready
+
     while (1) {
-        vTaskDelay(pdMS_TO_TICKS(1000));
-        if( soundEnabled ) {
-            play_tone(TONE_FREQ, TONE_DURATION, QUIET_DURATION, false);
+        while( alarmEnabled ) {
+            activate_buzzer(BUZZ_SHORT_DURATION, QUIET_DURATION, 1);
         }
+        vTaskDelay(pdMS_TO_TICKS(100)); // Check alarm state every second
     }
 }
 
-void initSound() {
-
-    xTaskCreate(sound_task, "sound_task", 2048, NULL, 10, NULL);
-    ESP_LOGI("SOUND", "Sound initialized");
+void setAlarmState(bool enabled) {
+    alarmEnabled = enabled;
+    //ESP_LOGI("SOUND", "Alarm state set to: %s", enabled ? "ENABLED" : "DISABLED");
 }
 
-// FreeRTOS Task: Handles the test beep asynchronously
-static void alarm_test_task(void *pvParameters) {
-    play_tone(TONE_FREQ, TONE_DURATION, QUIET_DURATION, true);
-    vTaskDelete(NULL);  // Delete task after completion
+void triggerNotificationSound() {
+    ESP_LOGI("SOUND", "Triggering short notification sound");
+    activate_buzzer(100, 0, 1); // 100ms beep, no quiet time, single iteration
 }
 
-
-// Function to enable/disable the notification sound (called by door sensor)
-void enableNotificationSound(bool enable) {
-    if (!alarmMasterEnabled) {
-        // If master is OFF, sound should always be OFF
-        enable = false;
-    }
-
-    if (soundEnabled != enable) {  // Only send MQTT if there's a change
-        ESP_LOGI("SOUND", "Notification sound %s", enable ? "enabled" : "disabled");
-
-        char mqtt_s[50];
-        char value_s[10];
-
-        sprintf(mqtt_s, "kitchen/fridge/doorAlarm");
-        sprintf(value_s, "%s", enable ? "active" : "inactive");
-
-        sendMQTTMessage(mqtt_s, value_s);
-        soundEnabled = enable;  // Update state AFTER sending message
-    }
-}
-
-// Master control for the alarm system (called by Home Assistant)
-void alarmMasterControl(alarmState_t state) {
-    switch (state) {
-        case ALARM_OFF:
-            alarmMasterEnabled = false;  // Master disables alarm
-            enableNotificationSound(false);  // Ensure sound is OFF
-            break;
-
-        case ALARM_ON:
-            alarmMasterEnabled = true;   // Master enables alarm
-            break;
-
-        case ALARM_TEST:
-            // Create a background task for the test beep, even if master is OFF
-            xTaskCreate(alarm_test_task, "alarm_test_task", 2048, NULL, 5, NULL);
-            break;
-    }
+void initSound() {
+    xTaskCreate(sound_task, "sound_task", 2048, NULL, 10, NULL);
+    ESP_LOGI("SOUND", "Sound initialized");
 }
 
-
 #else
 void initSound() {};
-void enableNotificationSound(bool enable) {};
-void alarmMasterControl(alarmState_t state) {};
+void triggerNotificationSound() {};
+void setAlarmState(bool enabled) {};
 #endif
 

+ 5 - 10
main/sound.h

@@ -3,17 +3,12 @@
 
 #include <stdbool.h>
 
-typedef enum {
-    ALARM_OFF,
-    ALARM_ON,
-    ALARM_TEST
-} alarmState_t;
+// Enable or disable the normal alarm
+void setAlarmState(bool enabled);
 
-void initSound();
-
-void enableNotificationSound(bool enable);
-
-void alarmMasterControl(alarmState_t state);
+// Trigger a short notification sound (100ms)
+void triggerNotificationSound();
 
+void initSound();
 
 #endif