diff --git a/components/zh_config b/components/zh_config index 49a4564..db717bf 160000 --- a/components/zh_config +++ b/components/zh_config @@ -1 +1 @@ -Subproject commit 49a4564b99119d9713bb60790bbf341782c03647 +Subproject commit db717bf6eec149759082c00ecf2fca81faf94adf diff --git a/components/zh_espnow b/components/zh_espnow index 742c807..39fdb2e 160000 --- a/components/zh_espnow +++ b/components/zh_espnow @@ -1 +1 @@ -Subproject commit 742c807c416d9f80c0571f8daea85d275e8d4c93 +Subproject commit 39fdb2eb4968172ce16269d6b24ff1672c6f5351 diff --git a/components/zh_network b/components/zh_network index 783b44f..10fefb2 160000 --- a/components/zh_network +++ b/components/zh_network @@ -1 +1 @@ -Subproject commit 783b44fbca0ed467a868b4420f191c6d9f6caea5 +Subproject commit 10fefb2912a2489d4be23ba41eedae47290b0fc0 diff --git a/main/zh_espnow_sensor.c b/main/zh_espnow_sensor.c index 06fc161..25d4a94 100644 --- a/main/zh_espnow_sensor.c +++ b/main/zh_espnow_sensor.c @@ -291,8 +291,9 @@ void zh_sensor_deep_sleep(sensor_config_t *sensor_config) #ifndef CONFIG_IDF_TARGET_ESP8266 esp_sleep_enable_timer_wakeup(sensor_config->hardware_config.measurement_frequency * 1000000); #endif - uint8_t required_message_quantity = 1; + uint8_t required_message_quantity = 2; zh_send_sensor_hardware_config_message(sensor_config); + zh_send_sensor_keep_alive_message_task(sensor_config); if (sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED && sensor_config->hardware_config.sensor_type != HAST_NONE) { required_message_quantity += zh_send_sensor_config_message(sensor_config); @@ -385,7 +386,7 @@ uint8_t zh_send_sensor_config_message(const sensor_config_t *sensor_config) case HAST_DHT: case HAST_AHT: case HAST_SHT: // For future development. - case HAST_HTU: // For future development. + case HAST_HTU: // For future development. case HAST_HDC1080: // For future development. data.payload_data.config_message.sensor_config_message.unique_id = 2; data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE; @@ -506,7 +507,9 @@ void zh_send_sensor_status_message_task(void *pvParameter) } else { - zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); // For future development. Will be changed for sensor read error message. + data.payload_type = ZHPT_ERROR; + strcpy(data.payload_data.status_message.error_message.message, "Sensor reading error."); + zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); } if (gpio_get_level(sensor_config->hardware_config.power_pin) == 1) { @@ -521,6 +524,26 @@ void zh_send_sensor_status_message_task(void *pvParameter) vTaskDelete(NULL); } +void zh_send_sensor_keep_alive_message_task(void *pvParameter) +{ + sensor_config_t *sensor_config = pvParameter; + zh_espnow_data_t data = {0}; + data.device_type = ZHDT_SENSOR; + data.payload_type = ZHPT_KEEP_ALIVE; + data.payload_data.keep_alive_message.online_status = ZH_ONLINE; + data.payload_data.keep_alive_message.message_frequency = ZH_SENSOR_KEEP_ALIVE_MESSAGE_FREQUENCY; + for (;;) + { + zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); + if (sensor_config->hardware_config.battery_power == true) + { + return; + } + vTaskDelay(ZH_SENSOR_KEEP_ALIVE_MESSAGE_FREQUENCY * 1000 / portTICK_PERIOD_MS); + } + vTaskDelete(NULL); +} + void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { sensor_config_t *sensor_config = arg; @@ -560,6 +583,7 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve zh_send_sensor_config_message(sensor_config); xTaskCreatePinnedToCore(&zh_send_sensor_status_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, sensor_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&sensor_config->status_message_task, tskNO_AFFINITY); xTaskCreatePinnedToCore(&zh_send_sensor_attributes_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, sensor_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&sensor_config->attributes_message_task, tskNO_AFFINITY); + xTaskCreatePinnedToCore(&zh_send_sensor_keep_alive_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, sensor_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&sensor_config->keep_alive_message_task, tskNO_AFFINITY); } } } @@ -572,6 +596,7 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve { vTaskDelete(sensor_config->status_message_task); vTaskDelete(sensor_config->attributes_message_task); + vTaskDelete(sensor_config->keep_alive_message_task); } } } @@ -667,6 +692,7 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve { vTaskDelete(sensor_config->status_message_task); vTaskDelete(sensor_config->attributes_message_task); + vTaskDelete(sensor_config->keep_alive_message_task); } } } @@ -690,6 +716,7 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve { vTaskDelete(sensor_config->status_message_task); vTaskDelete(sensor_config->attributes_message_task); + vTaskDelete(sensor_config->keep_alive_message_task); } } } diff --git a/main/zh_espnow_sensor.h b/main/zh_espnow_sensor.h index dc7f706..e9386c7 100644 --- a/main/zh_espnow_sensor.h +++ b/main/zh_espnow_sensor.h @@ -54,7 +54,8 @@ #define DS18B20_POWER_STABILIZATION_PERIOD 500 // Power stabilization period after the sensor is turned on. The value is selected experimentally. #define DHT_POWER_STABILIZATION_PERIOD 2000 // Power stabilization period after the sensor is turned on. The value is selected experimentally. -#define ZH_SENSOR_ATTRIBUTES_MESSAGE_FREQUENCY 60 // Frequency of transmission of keep alive messages to the gateway (in seconds). +#define ZH_SENSOR_KEEP_ALIVE_MESSAGE_FREQUENCY 10 // Frequency of sending a keep alive message to the gateway (in seconds). +#define ZH_SENSOR_ATTRIBUTES_MESSAGE_FREQUENCY 60 // Frequency of transmission a sensor attributes message to the gateway (in seconds). #define ZH_MESSAGE_TASK_PRIORITY 2 // Prioritize the task of sending messages to the gateway. #define ZH_MESSAGE_STACK_SIZE 2048 // The stack size of the task of sending messages to the gateway. @@ -75,6 +76,7 @@ typedef struct // Structure of data exchange between tasks, functions and event uint8_t sent_message_quantity; // System counter for the number of sended messages. @note Used only when powered by battery. TaskHandle_t attributes_message_task; // Unique task handle for zh_send_sensor_attributes_message_task(). @note Used only when external powered. TaskHandle_t status_message_task; // Unique task handle for zh_send_sensor_status_message_task(). @note Used only when external powered. + TaskHandle_t keep_alive_message_task; // Unique task handle for zh_send_sensor_keep_alive_message_task(). @note Used only when external powered. const esp_partition_t *update_partition; // Unique handle for next OTA update partition. @note Used only when external powered. esp_ota_handle_t update_handle; // Unique handle for OTA functions. @note Used only when external powered. uint16_t ota_message_part_number; // System counter for the number of received OTA messages. @note Used only when external powered. @@ -157,6 +159,13 @@ uint8_t zh_send_sensor_config_message(const sensor_config_t *sensor_config); */ void zh_send_sensor_status_message_task(void *pvParameter); +/** + * @brief Task for prepare the sensor keep alive 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_sensor_keep_alive_message_task(void *pvParameter); + /** * @brief Function for ESP-NOW event processing. * diff --git a/version.txt b/version.txt index e6d5cb8..e4c0d46 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.2 \ No newline at end of file +1.0.3 \ No newline at end of file