Version 2.1.0
Increase stability of operation.
This commit is contained in:
		| @@ -18,10 +18,11 @@ void app_main(void) | ||||
|     esp_wifi_start(); | ||||
|     zh_espnow_init_config_t espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT(); | ||||
|     zh_espnow_init(&espnow_init_config); | ||||
|     xTaskCreatePinnedToCore(&zh_send_switch_attributes_message_task, "switch_attributes_message_task", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->switch_attributes_message_task, tskNO_AFFINITY); | ||||
|     vTaskSuspend(switch_config->switch_attributes_message_task); | ||||
|     xTaskCreatePinnedToCore(&zh_send_switch_keep_alive_message_task, "switch_keep_alive_message_task", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->switch_keep_alive_message_task, tskNO_AFFINITY); | ||||
|     vTaskSuspend(switch_config->switch_keep_alive_message_task); | ||||
|     xTaskCreatePinnedToCore(&zh_send_switch_attributes_message_task, "switch_attributes_message_task", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, NULL, tskNO_AFFINITY); | ||||
|     xTaskCreatePinnedToCore(&zh_send_switch_keep_alive_message_task, "switch_keep_alive_message_task", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, NULL, tskNO_AFFINITY); | ||||
|     xTaskCreatePinnedToCore(&zh_send_switch_config_message_task, "switch_config_message_task", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, NULL, tskNO_AFFINITY); | ||||
|     xTaskCreatePinnedToCore(&zh_send_switch_hardware_config_message_task, "switch_hardware_config_message_task", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, NULL, tskNO_AFFINITY); | ||||
|     xTaskCreatePinnedToCore(&zh_send_switch_status_message_task, "switch_status_message_task", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, NULL, tskNO_AFFINITY); | ||||
| #ifdef CONFIG_IDF_TARGET_ESP8266 | ||||
|     esp_event_handler_register(ZH_ESPNOW, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, switch_config); | ||||
| #else | ||||
| @@ -262,10 +263,7 @@ void zh_gpio_processing_task(void *pvParameter) | ||||
|         switch_config->status.status = (switch_config->status.status == HAONOFT_ON) ? HAONOFT_OFF : HAONOFT_ON; | ||||
|         zh_gpio_set_level(switch_config); | ||||
|         zh_save_status(switch_config); | ||||
|         if (switch_config->gateway_is_available == true) | ||||
|         { | ||||
|             zh_send_switch_status_message(switch_config); | ||||
|         } | ||||
|         zh_send_switch_status_message(switch_config); | ||||
|         vTaskDelay(500 / portTICK_PERIOD_MS); // To prevent button contact rattling. Value is selected experimentally. | ||||
|         switch_config->gpio_processing = false; | ||||
|     } | ||||
| @@ -296,8 +294,9 @@ void zh_send_switch_attributes_message_task(void *pvParameter) | ||||
|     vTaskDelete(NULL); | ||||
| } | ||||
|  | ||||
| void zh_send_switch_config_message(const switch_config_t *switch_config) | ||||
| void zh_send_switch_config_message_task(void *pvParameter) | ||||
| { | ||||
|     switch_config_t *switch_config = pvParameter; | ||||
|     zh_espnow_data_t data = {0}; | ||||
|     data.device_type = ZHDT_SWITCH; | ||||
|     data.payload_type = ZHPT_CONFIG; | ||||
| @@ -309,11 +308,17 @@ void zh_send_switch_config_message(const switch_config_t *switch_config) | ||||
|     data.payload_data.config_message.switch_config_message.optimistic = false; | ||||
|     data.payload_data.config_message.switch_config_message.qos = 2; | ||||
|     data.payload_data.config_message.switch_config_message.retain = true; | ||||
|     zh_espnow_send(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); | ||||
|     for (;;) | ||||
|     { | ||||
|         zh_espnow_send(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); | ||||
|         vTaskDelay(ZH_SWITCH_CONFIG_MESSAGE_FREQUENCY * 1000 / portTICK_PERIOD_MS); | ||||
|     } | ||||
|     vTaskDelete(NULL); | ||||
| } | ||||
|  | ||||
| void zh_send_switch_hardware_config_message(const switch_config_t *switch_config) | ||||
| void zh_send_switch_hardware_config_message_task(void *pvParameter) | ||||
| { | ||||
|     switch_config_t *switch_config = pvParameter; | ||||
|     zh_espnow_data_t data = {0}; | ||||
|     data.device_type = ZHDT_SWITCH; | ||||
|     data.payload_type = ZHPT_HARDWARE; | ||||
| @@ -325,7 +330,12 @@ void zh_send_switch_hardware_config_message(const switch_config_t *switch_config | ||||
|     data.payload_data.config_message.switch_hardware_config_message.int_button_on_level = switch_config->hardware_config.int_button_on_level; | ||||
|     data.payload_data.config_message.switch_hardware_config_message.ext_button_pin = switch_config->hardware_config.ext_button_pin; | ||||
|     data.payload_data.config_message.switch_hardware_config_message.ext_button_on_level = switch_config->hardware_config.ext_button_on_level; | ||||
|     zh_espnow_send(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); | ||||
|     for (;;) | ||||
|     { | ||||
|         zh_espnow_send(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); | ||||
|         vTaskDelay(ZH_SWITCH_HARDWARE_CONFIG_MESSAGE_FREQUENCY * 1000 / portTICK_PERIOD_MS); | ||||
|     } | ||||
|     vTaskDelete(NULL); | ||||
| } | ||||
|  | ||||
| void zh_send_switch_keep_alive_message_task(void *pvParameter) | ||||
| @@ -344,6 +354,21 @@ void zh_send_switch_keep_alive_message_task(void *pvParameter) | ||||
|     vTaskDelete(NULL); | ||||
| } | ||||
|  | ||||
| void zh_send_switch_status_message_task(void *pvParameter) | ||||
| { | ||||
|     switch_config_t *switch_config = pvParameter; | ||||
|     zh_espnow_data_t data = {0}; | ||||
|     data.device_type = ZHDT_SWITCH; | ||||
|     data.payload_type = ZHPT_STATE; | ||||
|     for (;;) | ||||
|     { | ||||
|         data.payload_data.status_message.switch_status_message.status = switch_config->status.status; | ||||
|         zh_espnow_send(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); | ||||
|         vTaskDelay(ZH_SWITCH_STATUS_MESSAGE_FREQUENCY * 1000 / portTICK_PERIOD_MS); | ||||
|     } | ||||
|     vTaskDelete(NULL); | ||||
| } | ||||
|  | ||||
| void zh_send_switch_status_message(const switch_config_t *switch_config) | ||||
| { | ||||
|     zh_espnow_data_t data = {0}; | ||||
| @@ -371,25 +396,7 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve | ||||
|             switch (data->payload_type) | ||||
|             { | ||||
|             case ZHPT_KEEP_ALIVE: | ||||
|                 if (data->payload_data.keep_alive_message.online_status == ZH_ONLINE) | ||||
|                 { | ||||
|                     memcpy(switch_config->gateway_mac, recv_data->mac_addr, 6); | ||||
|                     if (switch_config->gateway_is_available == false) | ||||
|                     { | ||||
|                         switch_config->gateway_is_available = true; | ||||
|                         zh_send_switch_hardware_config_message(switch_config); | ||||
|                         zh_send_switch_config_message(switch_config); | ||||
|                         zh_send_switch_status_message(switch_config); | ||||
|                         vTaskResume(switch_config->switch_attributes_message_task); | ||||
|                         vTaskResume(switch_config->switch_keep_alive_message_task); | ||||
|                     } | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     switch_config->gateway_is_available = false; | ||||
|                     vTaskSuspend(switch_config->switch_attributes_message_task); | ||||
|                     vTaskSuspend(switch_config->switch_keep_alive_message_task); | ||||
|                 } | ||||
|                 memcpy(switch_config->gateway_mac, recv_data->mac_addr, 6); | ||||
|                 break; | ||||
|             case ZHPT_SET: | ||||
|                 switch_config->status.status = data->payload_data.status_message.switch_status_message.status; | ||||
| @@ -477,14 +484,7 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve | ||||
|     ZH_ESPNOW_EVENT_HANDLER_EXIT: | ||||
|         heap_caps_free(recv_data->data); | ||||
|         break; | ||||
|     case ZH_ESPNOW_ON_SEND_EVENT:; | ||||
|         zh_espnow_event_on_send_t *send_data = event_data; | ||||
|         if (send_data->status == ZH_ESPNOW_SEND_FAIL) | ||||
|         { | ||||
|             switch_config->gateway_is_available = false; | ||||
|             vTaskSuspend(switch_config->switch_attributes_message_task); | ||||
|             vTaskSuspend(switch_config->switch_keep_alive_message_task); | ||||
|         } | ||||
|     case ZH_ESPNOW_ON_SEND_EVENT: | ||||
|         break; | ||||
|     default: | ||||
|         break; | ||||
|   | ||||
| @@ -35,8 +35,11 @@ | ||||
| #define get_app_description() esp_app_get_description() | ||||
| #endif | ||||
|  | ||||
| #define ZH_SWITCH_KEEP_ALIVE_MESSAGE_FREQUENCY 10 // Frequency of sending a switch keep alive message to the gateway (in seconds). | ||||
| #define ZH_SWITCH_ATTRIBUTES_MESSAGE_FREQUENCY 60 // Frequency of sending a switch attributes message to the gateway (in seconds). | ||||
| #define ZH_SWITCH_KEEP_ALIVE_MESSAGE_FREQUENCY 10       // Frequency of sending a switch keep alive message to the gateway (in seconds). | ||||
| #define ZH_SWITCH_ATTRIBUTES_MESSAGE_FREQUENCY 60       // Frequency of sending a switch attributes message to the gateway (in seconds). | ||||
| #define ZH_SWITCH_HARDWARE_CONFIG_MESSAGE_FREQUENCY 300 // Frequency of sending a switch hardware config message to the gateway (in seconds). | ||||
| #define ZH_SWITCH_CONFIG_MESSAGE_FREQUENCY 300          // Frequency of sending a switch config message to the gateway (in seconds). | ||||
| #define ZH_SWITCH_STATUS_MESSAGE_FREQUENCY 300          // Frequency of sending a switch status message to the gateway (in seconds). | ||||
|  | ||||
| #define ZH_GPIO_TASK_PRIORITY 10   // Prioritize the task of GPIO processing. | ||||
| #define ZH_GPIO_STACK_SIZE 2048    // The stack size of the task of GPIO processing. | ||||
| @@ -60,15 +63,12 @@ typedef struct // Structure of data exchange between tasks, functions and event | ||||
|     { | ||||
|         ha_on_off_type_t status; // Status of the zh_espnow_switch. @note Example - ON / OFF. @attention Must be same with set on switch_config_message structure (zh_config). | ||||
|     } status; | ||||
|     volatile bool gpio_processing;               // GPIO processing flag. @note Used to prevent a repeated interrupt from triggering during GPIO processing. | ||||
|     volatile bool gateway_is_available;          // Gateway availability status flag. @note Used to control the tasks when the gateway connection is established / lost. | ||||
|     uint8_t gateway_mac[6];                      // Gateway MAC address. | ||||
|     TaskHandle_t switch_attributes_message_task; // Unique task handle for zh_send_switsh_attributes_message_task(). | ||||
|     TaskHandle_t switch_keep_alive_message_task; // Unique task handle for zh_send_switch_keep_alive_message_task(). | ||||
|     SemaphoreHandle_t button_pushing_semaphore;  // Unique semaphore handle for GPIO processing tasks. | ||||
|     const esp_partition_t *update_partition;     // Next update partition. | ||||
|     esp_ota_handle_t update_handle;              // Unique OTA handle. | ||||
|     uint16_t ota_message_part_number;            // The sequence number of the firmware upgrade part. @note Used to verify that all parts have been received. | ||||
|     volatile bool gpio_processing;              // GPIO processing flag. @note Used to prevent a repeated interrupt from triggering during GPIO processing. | ||||
|     uint8_t gateway_mac[6];                     // Gateway MAC address. | ||||
|     SemaphoreHandle_t button_pushing_semaphore; // Unique semaphore handle for GPIO processing tasks. | ||||
|     const esp_partition_t *update_partition;    // Next update partition. | ||||
|     esp_ota_handle_t update_handle;             // Unique OTA handle. | ||||
|     uint16_t ota_message_part_number;           // The sequence number of the firmware upgrade part. @note Used to verify that all parts have been received. | ||||
| } switch_config_t; | ||||
|  | ||||
| /** | ||||
| @@ -135,18 +135,18 @@ void zh_gpio_processing_task(void *pvParameter); | ||||
| void zh_send_switch_attributes_message_task(void *pvParameter); | ||||
|  | ||||
| /** | ||||
|  * @brief Function for prepare the switch configuration message and sending it to the gateway. | ||||
|  * @brief Task for prepare the switch configuration message and sending it to the gateway. | ||||
|  * | ||||
|  * @param[in] switch_config Pointer to the structure of data exchange between tasks, functions and event handlers. | ||||
|  * @param[in] pvParameter Pointer to the structure of data exchange between tasks, functions and event handlers. | ||||
|  */ | ||||
| void zh_send_switch_config_message(const switch_config_t *switch_config); | ||||
| void zh_send_switch_config_message_task(void *pvParameter); | ||||
|  | ||||
| /** | ||||
|  * @brief Function for prepare the switch hardware configuration message and sending it to the gateway. | ||||
|  * @brief Task for prepare the switch hardware configuration message and sending it to the gateway. | ||||
|  * | ||||
|  * @param[in] switch_config Pointer to the structure of data exchange between tasks, functions and event handlers. | ||||
|  * @param[in] pvParameter Pointer to the structure of data exchange between tasks, functions and event handlers. | ||||
|  */ | ||||
| void zh_send_switch_hardware_config_message(const switch_config_t *switch_config); | ||||
| void zh_send_switch_hardware_config_message_task(void *pvParameter); | ||||
|  | ||||
| /** | ||||
|  * @brief Task for prepare the switch keep alive message and sending it to the gateway. | ||||
| @@ -155,6 +155,13 @@ void zh_send_switch_hardware_config_message(const switch_config_t *switch_config | ||||
|  */ | ||||
| void zh_send_switch_keep_alive_message_task(void *pvParameter); | ||||
|  | ||||
| /** | ||||
|  * @brief Task for prepare the switch status message and sending it to the gateway. | ||||
|  * | ||||
|  * @param[in] pvParameter Pointer to the structure of data exchange between tasks, functions and event handlers. | ||||
|  */ | ||||
| void zh_send_switch_status_message_task(void *pvParameter); | ||||
|  | ||||
| /** | ||||
|  * @brief Function for prepare the switch status message and sending it to the gateway. | ||||
|  * | ||||
|   | ||||
		Reference in New Issue
	
	Block a user