123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #include <stdio.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/gpio.h"
- #include "esp_log.h"
- #include <stdbool.h>
- #include "sdkconfig.h"
- #include "fridgeDoor.h"
- #include "config.h"
- #include "sound.h"
- #include "mqtt.h"
- #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) {
- bool fridgeDoorClosed = gpio_get_level(PHOTO_SENSOR_PIN) == 0 ? false : true;
- if (fridgeDoorClosed != prevState) {
- prevState = fridgeDoorClosed;
- ESP_EARLY_LOGI("PHOTO", "Triggered! State: %s", fridgeDoorClosed == true ? "CLOSED" : "OPEN");
- if( fridgeDoorClosed == false ) {
- gpio_set_level(BLINK_GPIO, 1);
- }
- else {
- gpio_set_level(BLINK_GPIO, 0);
- }
- char mqtt_s[50];
- char value_s[10];
- sprintf(mqtt_s,"kitchen/fridge/doorState");
- sprintf(value_s,"%s",fridgeDoorClosed ? "closed" : "open");
- sendMQTTMessage(mqtt_s, value_s);
- }
- check_sensor_active_time(fridgeDoorClosed);
- vTaskDelay(pdMS_TO_TICKS(50)); // Delay 50ms (20 times per second)
- }
- }
- void setup_photo_sensor() {
- gpio_config_t io_conf = {
- .intr_type = GPIO_INTR_ANYEDGE, // Trigger on both rising & falling edges
- .pin_bit_mask = (1ULL << PHOTO_SENSOR_PIN),
- .mode = GPIO_MODE_INPUT,
- .pull_up_en = GPIO_PULLUP_DISABLE,
- .pull_down_en = GPIO_PULLDOWN_DISABLE
- };
- gpio_config(&io_conf);
- xTaskCreate(read_photo_sensor_task, "read_sensor_task", 2048, NULL, 10, NULL);
- ESP_LOGI("PHOTO", "Photomicrosensor configured on GPIO %d", PHOTO_SENSOR_PIN);
- }
- #else
- void setup_photo_sensor() {};
- #endif
|