main.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/gpio.h"
  5. #include "esp_log.h"
  6. #include <stdbool.h>
  7. #include "sdkconfig.h"
  8. #include "sound.h"
  9. /* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
  10. or you can edit the following line and set a number here.
  11. */
  12. #define BLINK_GPIO CONFIG_BLINK_GPIO
  13. #define PHOTO_SENSOR_PIN GPIO_NUM_18
  14. #define ACTIVE_THRESHOLD_MS 5000 // 60 seconds in milliseconds
  15. static void configure_led(void)
  16. {
  17. ESP_LOGI("LED", "Example configured to blink GPIO LED!");
  18. gpio_reset_pin(BLINK_GPIO);
  19. /* Set the GPIO as a push/pull output */
  20. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  21. }
  22. // GPIO setup
  23. void setup_photo_sensor() {
  24. gpio_config_t io_conf = {
  25. .intr_type = GPIO_INTR_ANYEDGE, // Trigger on both rising & falling edges
  26. .pin_bit_mask = (1ULL << PHOTO_SENSOR_PIN),
  27. .mode = GPIO_MODE_INPUT,
  28. .pull_up_en = GPIO_PULLUP_DISABLE,
  29. .pull_down_en = GPIO_PULLDOWN_DISABLE
  30. };
  31. gpio_config(&io_conf);
  32. ESP_LOGI("PHOTO", "Photomicrosensor configured on GPIO %d", PHOTO_SENSOR_PIN);
  33. }
  34. void check_sensor_active_time(bool fridgeDoorClosed) {
  35. static int64_t activeStartTime = 0;
  36. //ESP_EARLY_LOGI("CHECK", "State: %s", fridgeDoorClosed == true ? "CLOSED" : "OPEN");
  37. if (fridgeDoorClosed == false) {
  38. if (activeStartTime == 0) {
  39. activeStartTime = esp_timer_get_time() / 1000; // Convert to milliseconds
  40. ESP_LOGI("CHECK", "Door timer start time: %lld", activeStartTime);
  41. } else {
  42. int64_t elapsedTime = (esp_timer_get_time() / 1000) - activeStartTime;
  43. //ESP_LOGI("CHECK", "Elapsed time: %lld", elapsedTime);
  44. if (elapsedTime > ACTIVE_THRESHOLD_MS) {
  45. enableNotificationSound(true);
  46. }
  47. }
  48. } else {
  49. activeStartTime = 0;
  50. enableNotificationSound(false);
  51. }
  52. }
  53. void read_photo_sensor_task(void *pvParameters) {
  54. static bool prevState = false;
  55. while (1) {
  56. bool fridgeDoorClosed = gpio_get_level(PHOTO_SENSOR_PIN) == 0 ? false : true;
  57. if (fridgeDoorClosed != prevState) {
  58. prevState = fridgeDoorClosed;
  59. ESP_EARLY_LOGI("PHOTO", "Triggered! State: %s", fridgeDoorClosed == true ? "CLOSED" : "OPEN");
  60. if( fridgeDoorClosed == false ) {
  61. gpio_set_level(BLINK_GPIO, 1);
  62. }
  63. else {
  64. gpio_set_level(BLINK_GPIO, 0);
  65. }
  66. }
  67. check_sensor_active_time(fridgeDoorClosed);
  68. vTaskDelay(pdMS_TO_TICKS(50)); // Delay 50ms (20 times per second)
  69. }
  70. }
  71. void app_main(void)
  72. {
  73. /* Configure the peripheral according to the LED type */
  74. configure_led();
  75. setup_photo_sensor();
  76. initSound();
  77. xTaskCreate(read_photo_sensor_task, "read_sensor_task", 2048, NULL, 10, NULL);
  78. }