Browse Source

wifi_management_task: Add task to disable wifi during the night

Helmut Pozimski 1 year ago
parent
commit
993013ad3a
4 changed files with 71 additions and 4 deletions
  1. 1 1
      main/CMakeLists.txt
  2. 7 3
      main/esp32_alarm_clock_main.c
  3. 56 0
      main/wifi_management_task.c
  4. 7 0
      main/wifi_management_task.h

+ 1 - 1
main/CMakeLists.txt

@@ -1,2 +1,2 @@
-idf_component_register(SRCS "esp32_alarm_clock_main.c" "wifi.c" "storage.c" "api.c" "ds3231.c" "bcd.c" "tm1637.c" "time_sync.c" "time_display.c" "alarm.c" "time_conversion.c" "alarm_task.c" "cleanup_task.c"
+idf_component_register(SRCS "esp32_alarm_clock_main.c" "wifi.c" "storage.c" "api.c" "ds3231.c" "bcd.c" "tm1637.c" "time_sync.c" "time_display.c" "alarm.c" "time_conversion.c" "alarm_task.c" "cleanup_task.c" "wifi_management_task.c"
                     INCLUDE_DIRS "")

+ 7 - 3
main/esp32_alarm_clock_main.c

@@ -23,6 +23,7 @@
 #include "api.h"
 #include "storage.h"
 #include "cleanup_task.h"
+#include "wifi_management_task.h"
 
 
 static const char* STORAGE_NAMESPACE = "a1";
@@ -33,13 +34,14 @@ static void IRAM_ATTR gpio_interrupt_handler(void *args)
     *button_pressed_flag = 1;
 }
 
-static void init_peripherals(uint8_t* button_pressed_flag) {
+static void init_peripherals(uint8_t* button_pressed_flag, uint8_t* wifi_enabled_flag) {
 	ESP_ERROR_CHECK(nvs_flash_init());
 	ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
 
 	wifi_init();
   	wifi_start();
+  	*wifi_enabled_flag = 1;
 
 	ESP_ERROR_CHECK(storage_init(STORAGE_NAMESPACE));
 	ds3231_init(DS3231_SDA_PIN, DS3231_SCL_PIN);
@@ -57,6 +59,7 @@ static void init_peripherals(uint8_t* button_pressed_flag) {
 void app_main(void) {
 	static httpd_handle_t server;
 	static uint8_t button_pressed_flag = 0;
+	static uint8_t wifi_enabled_flag = 0;
 	static TaskHandle_t alarm_task_handle;
 	static alarm_parameters alarm_task_parameters = {
 		.button_pressed_flag = &button_pressed_flag,
@@ -64,7 +67,7 @@ void app_main(void) {
 	};
 	struct tm current_time;
 
-	init_peripherals(&button_pressed_flag);
+	init_peripherals(&button_pressed_flag, &wifi_enabled_flag);
 
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
@@ -78,5 +81,6 @@ void app_main(void) {
 	
 	xTaskCreate(display_update_task, "display_update_task", 2048, NULL, 8, NULL);
 	xTaskCreate(alarm_task, "alarm_task", 2048, (void*) &alarm_task_parameters, 7, NULL);
-	xTaskCreate(cleanup_task, "cleanup_task", 2048, STORAGE_NAMESPACE, 8, NULL);
+	xTaskCreate(cleanup_task, "cleanup_task", 2048, (void*) STORAGE_NAMESPACE, 1, NULL);
+	xTaskCreate(wifi_management_task, "wifi_management_task", 2048, (void*) &wifi_enabled_flag, 1, NULL);
 }

+ 56 - 0
main/wifi_management_task.c

@@ -0,0 +1,56 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+
+#include <esp_log.h>
+#include <freertos/FreeRTOS.h>
+#include <freertos/event_groups.h>
+
+#include "wifi.h"
+#include "ds3231.h"
+#include "time_conversion.h"
+
+#define TAG "wifi_management_task"
+
+#define SECONDS_PER_DAY 86400
+
+static uint32_t calculate_current_second(struct tm* local_time) {
+	return local_time->tm_hour * 60 * 60 + local_time->tm_min * 60 + local_time->tm_sec;
+}
+
+static uint32_t determine_sleep_seconds(struct tm* local_time) {
+	uint32_t current_second = calculate_current_second(local_time);
+	uint32_t reference_second;
+	if (local_time->tm_hour >= 22) {
+		reference_second = (SECONDS_PER_DAY - current_second) + 6 * 60 * 60;
+	} else {
+		reference_second = (22 * 60 * 60) -  current_second;
+	}
+	return reference_second;
+}
+
+void wifi_management_task(void *pvParameters) {
+	struct tm current_time;
+	uint8_t* wifi_enabled_flag = (uint8_t*) pvParameters;
+	
+	while(1) {
+		ESP_LOGI(TAG, "wifi_managent_task is executed");
+		struct tm* local_time;
+		ds3231_read_date_time(&current_time);
+		local_time = convert_to_local(&current_time);
+		ESP_LOGI(TAG, "Current local hour: %d", local_time->tm_hour);
+		if (local_time->tm_hour >= 22) {
+			ESP_LOGI(TAG, "Stopping wifi");
+			wifi_stop();
+		} else if (local_time->tm_hour >= 6 && !wifi_enabled_flag) {
+			ESP_LOGI(TAG, "Starting wifi");
+			wifi_start();		
+		}
+		uint32_t sleep_seconds = determine_sleep_seconds(local_time);
+		ESP_LOGI(TAG, "Sleeping for %ld seconds", sleep_seconds);
+		vTaskDelay(sleep_seconds * 1000 / portTICK_PERIOD_MS);
+	}
+	vTaskDelete( NULL ); 
+}

+ 7 - 0
main/wifi_management_task.h

@@ -0,0 +1,7 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+ 
+void wifi_management_task(void *pvParameters);