wifi_management_task.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <esp_log.h>
  7. #include <freertos/FreeRTOS.h>
  8. #include <freertos/event_groups.h>
  9. #include "wifi.h"
  10. #include "ds3231.h"
  11. #include "time_conversion.h"
  12. #define TAG "wifi_management_task"
  13. #define SECONDS_PER_DAY 86400
  14. static uint32_t calculate_current_second(struct tm* local_time) {
  15. return local_time->tm_hour * 60 * 60 + local_time->tm_min * 60 + local_time->tm_sec;
  16. }
  17. static uint32_t determine_sleep_seconds(struct tm* local_time) {
  18. uint32_t current_second = calculate_current_second(local_time);
  19. uint32_t reference_second;
  20. if (local_time->tm_hour >= 22) {
  21. reference_second = (SECONDS_PER_DAY - current_second) + 6 * 60 * 60;
  22. } else {
  23. reference_second = (22 * 60 * 60) - current_second;
  24. }
  25. return reference_second;
  26. }
  27. void wifi_management_task(void *pvParameters) {
  28. struct tm current_time;
  29. uint8_t* wifi_enabled_flag = (uint8_t*) pvParameters;
  30. while(1) {
  31. ESP_LOGI(TAG, "wifi_managent_task is executed");
  32. struct tm* local_time;
  33. ds3231_read_date_time(&current_time);
  34. local_time = convert_to_local(&current_time);
  35. ESP_LOGI(TAG, "Current local hour: %d", local_time->tm_hour);
  36. if (local_time->tm_hour >= 22) {
  37. ESP_LOGI(TAG, "Stopping wifi");
  38. wifi_stop();
  39. } else if (local_time->tm_hour >= 6 && !wifi_enabled_flag) {
  40. ESP_LOGI(TAG, "Starting wifi");
  41. wifi_start();
  42. }
  43. uint32_t sleep_seconds = determine_sleep_seconds(local_time);
  44. ESP_LOGI(TAG, "Sleeping for %ld seconds", sleep_seconds);
  45. vTaskDelay(sleep_seconds * 1000 / portTICK_PERIOD_MS);
  46. }
  47. vTaskDelete( NULL );
  48. }