Updated submodules.
Removed sensor support.
This commit is contained in:
		@@ -15,21 +15,11 @@ void app_main(void)
 | 
			
		||||
    wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
 | 
			
		||||
    esp_wifi_init(&wifi_init_config);
 | 
			
		||||
    esp_wifi_set_mode(WIFI_MODE_STA);
 | 
			
		||||
#ifdef CONFIG_IDF_TARGET_ESP8266
 | 
			
		||||
    esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
 | 
			
		||||
#else
 | 
			
		||||
    esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR);
 | 
			
		||||
#endif
 | 
			
		||||
    esp_wifi_start();
 | 
			
		||||
#ifdef CONFIG_NETWORK_TYPE_DIRECT
 | 
			
		||||
    zh_espnow_init_config_t espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT();
 | 
			
		||||
    zh_espnow_init(&espnow_init_config);
 | 
			
		||||
#else
 | 
			
		||||
    zh_network_init_config_t network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT();
 | 
			
		||||
    zh_network_init(&network_init_config);
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef CONFIG_IDF_TARGET_ESP8266
 | 
			
		||||
    esp_event_handler_register(ZH_EVENT, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, switch_config);
 | 
			
		||||
    esp_event_handler_register(ZH_ESPNOW, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, switch_config);
 | 
			
		||||
#else
 | 
			
		||||
    esp_event_handler_instance_register(ZH_EVENT, ESP_EVENT_ANY_ID, &zh_espnow_event_handler, switch_config, NULL);
 | 
			
		||||
    const esp_partition_t *running = esp_ota_get_running_partition();
 | 
			
		||||
@@ -92,19 +82,6 @@ void zh_load_config(switch_config_t *switch_config)
 | 
			
		||||
        switch_config->hardware_config.ext_button_on_level = ZH_HIGH;
 | 
			
		||||
#else
 | 
			
		||||
        switch_config->hardware_config.ext_button_on_level = ZH_LOW;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef CONFIG_SENSOR_TYPE_DS18B20
 | 
			
		||||
        switch_config->hardware_config.sensor_pin = CONFIG_SENSOR_PIN;
 | 
			
		||||
        switch_config->hardware_config.sensor_type = HAST_DS18B20;
 | 
			
		||||
        switch_config->hardware_config.measurement_frequency = CONFIG_MEASUREMENT_FREQUENCY;
 | 
			
		||||
#elif CONFIG_SENSOR_TYPE_DHT
 | 
			
		||||
        switch_config->hardware_config.sensor_pin = CONFIG_SENSOR_PIN;
 | 
			
		||||
        switch_config->hardware_config.sensor_type = HAST_DHT;
 | 
			
		||||
        switch_config->hardware_config.measurement_frequency = CONFIG_MEASUREMENT_FREQUENCY;
 | 
			
		||||
#else
 | 
			
		||||
        switch_config->hardware_config.sensor_pin = ZH_NOT_USED;
 | 
			
		||||
        switch_config->hardware_config.sensor_type = HAST_NONE;
 | 
			
		||||
        switch_config->hardware_config.measurement_frequency = ZH_NOT_USED;
 | 
			
		||||
#endif
 | 
			
		||||
        zh_save_config(switch_config);
 | 
			
		||||
        return;
 | 
			
		||||
@@ -118,9 +95,6 @@ void zh_load_config(switch_config_t *switch_config)
 | 
			
		||||
    err += nvs_get_u8(nvs_handle, "int_btn_lvl", (uint8_t *)&switch_config->hardware_config.int_button_on_level);
 | 
			
		||||
    err += nvs_get_u8(nvs_handle, "ext_btn_pin", &switch_config->hardware_config.ext_button_pin);
 | 
			
		||||
    err += nvs_get_u8(nvs_handle, "ext_btn_lvl", (uint8_t *)&switch_config->hardware_config.ext_button_on_level);
 | 
			
		||||
    err += nvs_get_u8(nvs_handle, "sensor_pin", &switch_config->hardware_config.sensor_pin);
 | 
			
		||||
    err += nvs_get_u8(nvs_handle, "sensor_type", (uint8_t *)&switch_config->hardware_config.sensor_type);
 | 
			
		||||
    err += nvs_get_u16(nvs_handle, "frequency", &switch_config->hardware_config.measurement_frequency);
 | 
			
		||||
    nvs_close(nvs_handle);
 | 
			
		||||
    if (err != ESP_OK)
 | 
			
		||||
    {
 | 
			
		||||
@@ -140,9 +114,6 @@ void zh_save_config(const switch_config_t *switch_config)
 | 
			
		||||
    nvs_set_u8(nvs_handle, "int_btn_lvl", switch_config->hardware_config.int_button_on_level);
 | 
			
		||||
    nvs_set_u8(nvs_handle, "ext_btn_pin", switch_config->hardware_config.ext_button_pin);
 | 
			
		||||
    nvs_set_u8(nvs_handle, "ext_btn_lvl", switch_config->hardware_config.ext_button_on_level);
 | 
			
		||||
    nvs_set_u8(nvs_handle, "sensor_pin", switch_config->hardware_config.sensor_pin);
 | 
			
		||||
    nvs_set_u8(nvs_handle, "sensor_type", switch_config->hardware_config.sensor_type);
 | 
			
		||||
    nvs_set_u16(nvs_handle, "frequency", switch_config->hardware_config.measurement_frequency);
 | 
			
		||||
    nvs_close(nvs_handle);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -187,7 +158,6 @@ void zh_gpio_init(switch_config_t *switch_config)
 | 
			
		||||
            switch_config->hardware_config.led_pin = ZH_NOT_USED;
 | 
			
		||||
            switch_config->hardware_config.int_button_pin = ZH_NOT_USED;
 | 
			
		||||
            switch_config->hardware_config.ext_button_pin = ZH_NOT_USED;
 | 
			
		||||
            switch_config->hardware_config.sensor_pin = ZH_NOT_USED;
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        if (switch_config->hardware_config.led_pin != ZH_NOT_USED)
 | 
			
		||||
@@ -240,30 +210,6 @@ void zh_gpio_init(switch_config_t *switch_config)
 | 
			
		||||
                gpio_isr_handler_add(switch_config->hardware_config.ext_button_pin, zh_gpio_isr_handler, switch_config);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED)
 | 
			
		||||
        {
 | 
			
		||||
            switch (switch_config->hardware_config.sensor_type)
 | 
			
		||||
            {
 | 
			
		||||
            case HAST_DS18B20:
 | 
			
		||||
                if (zh_onewire_init(switch_config->hardware_config.sensor_pin) != ESP_OK)
 | 
			
		||||
                {
 | 
			
		||||
                    switch_config->hardware_config.sensor_pin = ZH_NOT_USED;
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            case HAST_DHT:;
 | 
			
		||||
                zh_dht_init_config_t dht_init_config = ZH_DHT_INIT_CONFIG_DEFAULT();
 | 
			
		||||
                dht_init_config.sensor_pin = switch_config->hardware_config.sensor_pin;
 | 
			
		||||
                if (zh_dht_init(&dht_init_config) != ESP_OK)
 | 
			
		||||
                {
 | 
			
		||||
                    switch_config->hardware_config.sensor_pin = ZH_NOT_USED;
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                switch_config->hardware_config.sensor_type = HAST_NONE;
 | 
			
		||||
                switch_config->hardware_config.sensor_pin = ZH_NOT_USED;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -375,8 +321,6 @@ 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;
 | 
			
		||||
    data.payload_data.config_message.switch_hardware_config_message.sensor_pin = switch_config->hardware_config.sensor_pin;
 | 
			
		||||
    data.payload_data.config_message.switch_hardware_config_message.sensor_type = switch_config->hardware_config.sensor_type;
 | 
			
		||||
    zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -405,156 +349,17 @@ void zh_send_switch_status_message(const switch_config_t *switch_config)
 | 
			
		||||
    zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void zh_send_sensor_config_message(const switch_config_t *switch_config)
 | 
			
		||||
{
 | 
			
		||||
    zh_espnow_data_t data = {0};
 | 
			
		||||
    data.device_type = ZHDT_SENSOR;
 | 
			
		||||
    data.payload_type = ZHPT_CONFIG;
 | 
			
		||||
    data.payload_data.config_message.sensor_config_message.suggested_display_precision = 1;
 | 
			
		||||
    data.payload_data.config_message.sensor_config_message.expire_after = switch_config->hardware_config.measurement_frequency * 1.5; // + 50% just in case.
 | 
			
		||||
    data.payload_data.config_message.sensor_config_message.enabled_by_default = true;
 | 
			
		||||
    data.payload_data.config_message.sensor_config_message.force_update = true;
 | 
			
		||||
    data.payload_data.config_message.sensor_config_message.qos = 2;
 | 
			
		||||
    data.payload_data.config_message.sensor_config_message.retain = true;
 | 
			
		||||
    char *unit_of_measurement = NULL;
 | 
			
		||||
    // For compatibility with zh_espnow_sensor.
 | 
			
		||||
    data.payload_data.config_message.sensor_config_message.unique_id = 2;
 | 
			
		||||
    data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_VOLTAGE;
 | 
			
		||||
    unit_of_measurement = "V";
 | 
			
		||||
    strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
 | 
			
		||||
    zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
 | 
			
		||||
    // For compatibility with zh_espnow_sensor.
 | 
			
		||||
    switch (switch_config->hardware_config.sensor_type)
 | 
			
		||||
    {
 | 
			
		||||
    case HAST_DS18B20:
 | 
			
		||||
        data.payload_data.config_message.sensor_config_message.unique_id = 3;
 | 
			
		||||
        data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
 | 
			
		||||
        unit_of_measurement = "°C";
 | 
			
		||||
        strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
 | 
			
		||||
        zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
 | 
			
		||||
        break;
 | 
			
		||||
    case HAST_DHT:
 | 
			
		||||
        data.payload_data.config_message.sensor_config_message.unique_id = 3;
 | 
			
		||||
        data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
 | 
			
		||||
        unit_of_measurement = "°C";
 | 
			
		||||
        strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
 | 
			
		||||
        zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
 | 
			
		||||
        data.payload_data.config_message.sensor_config_message.unique_id = 4;
 | 
			
		||||
        data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_HUMIDITY;
 | 
			
		||||
        unit_of_measurement = "%";
 | 
			
		||||
        strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
 | 
			
		||||
        zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
 | 
			
		||||
        break;
 | 
			
		||||
    default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void zh_send_sensor_attributes_message_task(void *pvParameter)
 | 
			
		||||
{
 | 
			
		||||
    switch_config_t *switch_config = pvParameter;
 | 
			
		||||
    const esp_app_desc_t *app_info = get_app_description();
 | 
			
		||||
    zh_espnow_data_t data = {0};
 | 
			
		||||
    data.device_type = ZHDT_SENSOR;
 | 
			
		||||
    data.payload_type = ZHPT_ATTRIBUTES;
 | 
			
		||||
    data.payload_data.attributes_message.chip_type = ZH_CHIP_TYPE;
 | 
			
		||||
    data.payload_data.attributes_message.sensor_type = switch_config->hardware_config.sensor_type;
 | 
			
		||||
    strcpy(data.payload_data.attributes_message.flash_size, CONFIG_ESPTOOLPY_FLASHSIZE);
 | 
			
		||||
    data.payload_data.attributes_message.cpu_frequency = ZH_CPU_FREQUENCY;
 | 
			
		||||
    data.payload_data.attributes_message.reset_reason = (uint8_t)esp_reset_reason();
 | 
			
		||||
    strcpy(data.payload_data.attributes_message.app_name, app_info->project_name);
 | 
			
		||||
    strcpy(data.payload_data.attributes_message.app_version, app_info->version);
 | 
			
		||||
    for (;;)
 | 
			
		||||
    {
 | 
			
		||||
        data.payload_data.attributes_message.heap_size = esp_get_free_heap_size();
 | 
			
		||||
        data.payload_data.attributes_message.min_heap_size = esp_get_minimum_free_heap_size();
 | 
			
		||||
        data.payload_data.attributes_message.uptime = esp_timer_get_time() / 1000000;
 | 
			
		||||
        zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
 | 
			
		||||
        vTaskDelay(ZH_SENSOR_ATTRIBUTES_MESSAGE_FREQUENCY * 1000 / portTICK_PERIOD_MS);
 | 
			
		||||
    }
 | 
			
		||||
    vTaskDelete(NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void zh_send_sensor_status_message_task(void *pvParameter)
 | 
			
		||||
{
 | 
			
		||||
    switch_config_t *switch_config = pvParameter;
 | 
			
		||||
    float humidity = 0.0;
 | 
			
		||||
    float temperature = 0.0;
 | 
			
		||||
    zh_espnow_data_t data = {0};
 | 
			
		||||
    data.device_type = ZHDT_SENSOR;
 | 
			
		||||
    for (;;)
 | 
			
		||||
    {
 | 
			
		||||
        uint8_t attempts = 0;
 | 
			
		||||
    READ_SENSOR:;
 | 
			
		||||
        esp_err_t err = ESP_OK;
 | 
			
		||||
        switch (switch_config->hardware_config.sensor_type)
 | 
			
		||||
        {
 | 
			
		||||
        case HAST_DS18B20:
 | 
			
		||||
            err = zh_ds18b20_read(NULL, &temperature);
 | 
			
		||||
            if (err == ESP_OK)
 | 
			
		||||
            {
 | 
			
		||||
                data.payload_data.status_message.sensor_status_message.temperature = temperature;
 | 
			
		||||
                data.payload_data.status_message.sensor_status_message.voltage = 3.3; // For future development.
 | 
			
		||||
            }
 | 
			
		||||
            break;
 | 
			
		||||
        case HAST_DHT:
 | 
			
		||||
            err = zh_dht_read(&humidity, &temperature);
 | 
			
		||||
            if (err == ESP_OK)
 | 
			
		||||
            {
 | 
			
		||||
                data.payload_data.status_message.sensor_status_message.humidity = humidity;
 | 
			
		||||
                data.payload_data.status_message.sensor_status_message.temperature = temperature;
 | 
			
		||||
                data.payload_data.status_message.sensor_status_message.voltage = 3.3; // For future development.
 | 
			
		||||
            }
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        if (err == ESP_OK)
 | 
			
		||||
        {
 | 
			
		||||
            data.payload_type = ZHPT_STATE;
 | 
			
		||||
            data.payload_data.status_message.sensor_status_message.sensor_type = switch_config->hardware_config.sensor_type;
 | 
			
		||||
            zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            if (++attempts < ZH_SENSOR_READ_MAXIMUM_RETRY)
 | 
			
		||||
            {
 | 
			
		||||
                vTaskDelay(1000 / portTICK_PERIOD_MS);
 | 
			
		||||
                goto READ_SENSOR;
 | 
			
		||||
            }
 | 
			
		||||
            data.payload_type = ZHPT_ERROR;
 | 
			
		||||
            char *message = (char *)heap_caps_malloc(150, MALLOC_CAP_8BIT);
 | 
			
		||||
            memset(message, 0, 150);
 | 
			
		||||
            sprintf(message, "Sensor %s reading error. Error - %s.", zh_get_sensor_type_value_name(switch_config->hardware_config.sensor_type), esp_err_to_name(err));
 | 
			
		||||
            strcpy(data.payload_data.status_message.error_message.message, message);
 | 
			
		||||
            zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
 | 
			
		||||
            heap_caps_free(message);
 | 
			
		||||
        }
 | 
			
		||||
        vTaskDelay(switch_config->hardware_config.measurement_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)
 | 
			
		||||
{
 | 
			
		||||
    switch_config_t *switch_config = arg;
 | 
			
		||||
    switch (event_id)
 | 
			
		||||
    {
 | 
			
		||||
#ifdef CONFIG_NETWORK_TYPE_DIRECT
 | 
			
		||||
    case ZH_ESPNOW_ON_RECV_EVENT:;
 | 
			
		||||
        zh_espnow_event_on_recv_t *recv_data = event_data;
 | 
			
		||||
        if (recv_data->data_len != sizeof(zh_espnow_data_t))
 | 
			
		||||
        {
 | 
			
		||||
            goto ZH_ESPNOW_EVENT_HANDLER_EXIT;
 | 
			
		||||
        }
 | 
			
		||||
#else
 | 
			
		||||
    case ZH_NETWORK_ON_RECV_EVENT:;
 | 
			
		||||
        zh_network_event_on_recv_t *recv_data = event_data;
 | 
			
		||||
        if (recv_data->data_len != sizeof(zh_espnow_data_t))
 | 
			
		||||
        {
 | 
			
		||||
            goto ZH_NETWORK_EVENT_HANDLER_EXIT;
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
        zh_espnow_data_t *data = (zh_espnow_data_t *)recv_data->data;
 | 
			
		||||
        switch (data->device_type)
 | 
			
		||||
        {
 | 
			
		||||
@@ -573,30 +378,16 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
 | 
			
		||||
                        {
 | 
			
		||||
                            zh_send_switch_config_message(switch_config);
 | 
			
		||||
                            zh_send_switch_status_message(switch_config);
 | 
			
		||||
                            if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                            {
 | 
			
		||||
                                zh_send_sensor_config_message(switch_config);
 | 
			
		||||
                            }
 | 
			
		||||
                            if (switch_config->is_first_connection == false)
 | 
			
		||||
                            {
 | 
			
		||||
                                xTaskCreatePinnedToCore(&zh_send_switch_attributes_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->switch_attributes_message_task, tskNO_AFFINITY);
 | 
			
		||||
                                xTaskCreatePinnedToCore(&zh_send_switch_keep_alive_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->switch_keep_alive_message_task, tskNO_AFFINITY);
 | 
			
		||||
                                if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                                {
 | 
			
		||||
                                    xTaskCreatePinnedToCore(&zh_send_sensor_status_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->sensor_status_message_task, tskNO_AFFINITY);
 | 
			
		||||
                                    xTaskCreatePinnedToCore(&zh_send_sensor_attributes_message_task, "NULL", ZH_MESSAGE_STACK_SIZE, switch_config, ZH_MESSAGE_TASK_PRIORITY, (TaskHandle_t *)&switch_config->sensor_attributes_message_task, tskNO_AFFINITY);
 | 
			
		||||
                                }
 | 
			
		||||
                                switch_config->is_first_connection = true;
 | 
			
		||||
                            }
 | 
			
		||||
                            else
 | 
			
		||||
                            {
 | 
			
		||||
                                vTaskResume(switch_config->switch_attributes_message_task);
 | 
			
		||||
                                vTaskResume(switch_config->switch_keep_alive_message_task);
 | 
			
		||||
                                if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                                {
 | 
			
		||||
                                    vTaskResume(switch_config->sensor_status_message_task);
 | 
			
		||||
                                    vTaskResume(switch_config->sensor_attributes_message_task);
 | 
			
		||||
                                }
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
@@ -610,11 +401,6 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
 | 
			
		||||
                        {
 | 
			
		||||
                            vTaskSuspend(switch_config->switch_attributes_message_task);
 | 
			
		||||
                            vTaskSuspend(switch_config->switch_keep_alive_message_task);
 | 
			
		||||
                            if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                            {
 | 
			
		||||
                                vTaskSuspend(switch_config->sensor_attributes_message_task);
 | 
			
		||||
                                vTaskSuspend(switch_config->sensor_status_message_task);
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
@@ -634,19 +420,11 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
 | 
			
		||||
                switch_config->hardware_config.int_button_on_level = data->payload_data.config_message.switch_hardware_config_message.int_button_on_level;
 | 
			
		||||
                switch_config->hardware_config.ext_button_pin = data->payload_data.config_message.switch_hardware_config_message.ext_button_pin;
 | 
			
		||||
                switch_config->hardware_config.ext_button_on_level = data->payload_data.config_message.switch_hardware_config_message.ext_button_on_level;
 | 
			
		||||
                switch_config->hardware_config.sensor_pin = data->payload_data.config_message.switch_hardware_config_message.sensor_pin;
 | 
			
		||||
                switch_config->hardware_config.sensor_type = data->payload_data.config_message.switch_hardware_config_message.sensor_type;
 | 
			
		||||
                switch_config->hardware_config.measurement_frequency = data->payload_data.config_message.switch_hardware_config_message.measurement_frequency;
 | 
			
		||||
                zh_save_config(switch_config);
 | 
			
		||||
                if (switch_config->hardware_config.relay_pin != ZH_NOT_USED)
 | 
			
		||||
                {
 | 
			
		||||
                    vTaskDelete(switch_config->switch_attributes_message_task);
 | 
			
		||||
                    vTaskDelete(switch_config->switch_keep_alive_message_task);
 | 
			
		||||
                    if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                    {
 | 
			
		||||
                        vTaskDelete(switch_config->sensor_attributes_message_task);
 | 
			
		||||
                        vTaskDelete(switch_config->sensor_status_message_task);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                data->device_type = ZHDT_SWITCH;
 | 
			
		||||
                data->payload_type = ZHPT_KEEP_ALIVE;
 | 
			
		||||
@@ -661,11 +439,6 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
 | 
			
		||||
                {
 | 
			
		||||
                    vTaskSuspend(switch_config->switch_attributes_message_task);
 | 
			
		||||
                    vTaskSuspend(switch_config->switch_keep_alive_message_task);
 | 
			
		||||
                    if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                    {
 | 
			
		||||
                        vTaskSuspend(switch_config->sensor_attributes_message_task);
 | 
			
		||||
                        vTaskSuspend(switch_config->sensor_status_message_task);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                data->device_type = ZHDT_SWITCH;
 | 
			
		||||
                data->payload_type = ZHPT_KEEP_ALIVE;
 | 
			
		||||
@@ -714,11 +487,6 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
 | 
			
		||||
                {
 | 
			
		||||
                    vTaskResume(switch_config->switch_attributes_message_task);
 | 
			
		||||
                    vTaskResume(switch_config->switch_keep_alive_message_task);
 | 
			
		||||
                    if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                    {
 | 
			
		||||
                        vTaskResume(switch_config->sensor_attributes_message_task);
 | 
			
		||||
                        vTaskResume(switch_config->sensor_status_message_task);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            case ZHPT_UPDATE_END:
 | 
			
		||||
@@ -731,11 +499,6 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
 | 
			
		||||
                    {
 | 
			
		||||
                        vTaskResume(switch_config->switch_attributes_message_task);
 | 
			
		||||
                        vTaskResume(switch_config->switch_keep_alive_message_task);
 | 
			
		||||
                        if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                        {
 | 
			
		||||
                            vTaskResume(switch_config->sensor_attributes_message_task);
 | 
			
		||||
                            vTaskResume(switch_config->sensor_status_message_task);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
@@ -751,11 +514,6 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
 | 
			
		||||
                {
 | 
			
		||||
                    vTaskDelete(switch_config->switch_attributes_message_task);
 | 
			
		||||
                    vTaskDelete(switch_config->switch_keep_alive_message_task);
 | 
			
		||||
                    if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                    {
 | 
			
		||||
                        vTaskDelete(switch_config->sensor_attributes_message_task);
 | 
			
		||||
                        vTaskDelete(switch_config->sensor_status_message_task);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                data->device_type = ZHDT_SWITCH;
 | 
			
		||||
                data->payload_type = ZHPT_KEEP_ALIVE;
 | 
			
		||||
@@ -772,7 +530,6 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
 | 
			
		||||
        default:
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
#ifdef CONFIG_NETWORK_TYPE_DIRECT
 | 
			
		||||
    ZH_ESPNOW_EVENT_HANDLER_EXIT:
 | 
			
		||||
        heap_caps_free(recv_data->data);
 | 
			
		||||
        break;
 | 
			
		||||
@@ -785,36 +542,9 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
 | 
			
		||||
            {
 | 
			
		||||
                vTaskSuspend(switch_config->switch_attributes_message_task);
 | 
			
		||||
                vTaskSuspend(switch_config->switch_keep_alive_message_task);
 | 
			
		||||
                if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                {
 | 
			
		||||
                    vTaskSuspend(switch_config->sensor_attributes_message_task);
 | 
			
		||||
                    vTaskSuspend(switch_config->sensor_status_message_task);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
#else
 | 
			
		||||
    ZH_NETWORK_EVENT_HANDLER_EXIT:
 | 
			
		||||
        heap_caps_free(recv_data->data);
 | 
			
		||||
        break;
 | 
			
		||||
    case ZH_NETWORK_ON_SEND_EVENT:;
 | 
			
		||||
        zh_network_event_on_send_t *send_data = event_data;
 | 
			
		||||
        if (send_data->status == ZH_NETWORK_SEND_FAIL && switch_config->gateway_is_available == true)
 | 
			
		||||
        {
 | 
			
		||||
            switch_config->gateway_is_available = false;
 | 
			
		||||
            if (switch_config->hardware_config.relay_pin != ZH_NOT_USED)
 | 
			
		||||
            {
 | 
			
		||||
                vTaskSuspend(switch_config->switch_attributes_message_task);
 | 
			
		||||
                vTaskSuspend(switch_config->switch_keep_alive_message_task);
 | 
			
		||||
                if (switch_config->hardware_config.sensor_pin != ZH_NOT_USED && switch_config->hardware_config.sensor_type != HAST_NONE)
 | 
			
		||||
                {
 | 
			
		||||
                    vTaskSuspend(switch_config->sensor_attributes_message_task);
 | 
			
		||||
                    vTaskSuspend(switch_config->sensor_status_message_task);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
#endif
 | 
			
		||||
    default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -8,19 +8,8 @@
 | 
			
		||||
#include "driver/gpio.h"
 | 
			
		||||
#include "esp_timer.h"
 | 
			
		||||
#include "esp_ota_ops.h"
 | 
			
		||||
#include "zh_ds18b20.h"
 | 
			
		||||
#include "zh_dht.h"
 | 
			
		||||
#include "zh_config.h"
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_NETWORK_TYPE_DIRECT
 | 
			
		||||
#include "zh_espnow.h"
 | 
			
		||||
#define zh_send_message(a, b, c) zh_espnow_send(a, b, c)
 | 
			
		||||
#define ZH_EVENT ZH_ESPNOW
 | 
			
		||||
#else
 | 
			
		||||
#include "zh_network.h"
 | 
			
		||||
#define zh_send_message(a, b, c) zh_network_send(a, b, c)
 | 
			
		||||
#define ZH_EVENT ZH_NETWORK
 | 
			
		||||
#endif
 | 
			
		||||
#include "zh_config.h"
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_IDF_TARGET_ESP8266
 | 
			
		||||
#define ZH_CHIP_TYPE HACHT_ESP8266
 | 
			
		||||
@@ -48,42 +37,34 @@
 | 
			
		||||
 | 
			
		||||
#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_SENSOR_ATTRIBUTES_MESSAGE_FREQUENCY 60 // Frequency of sending a sensor attributes message to the gateway (in seconds).
 | 
			
		||||
#define ZH_SENSOR_READ_MAXIMUM_RETRY 5            // Maximum number of read sensor attempts.
 | 
			
		||||
 | 
			
		||||
#define ZH_GPIO_TASK_PRIORITY 3    // Prioritize the task of GPIO processing.
 | 
			
		||||
#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.
 | 
			
		||||
#define ZH_MESSAGE_TASK_PRIORITY 2 // Prioritize the task of sending messages to the gateway.
 | 
			
		||||
#define ZH_MESSAGE_TASK_PRIORITY 5 // 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.
 | 
			
		||||
 | 
			
		||||
typedef struct // Structure of data exchange between tasks, functions and event handlers.
 | 
			
		||||
{
 | 
			
		||||
    struct // Storage structure of switch hardware configuration data.
 | 
			
		||||
    {
 | 
			
		||||
        uint8_t relay_pin;              // Relay GPIO number.
 | 
			
		||||
        bool relay_on_level;            // Relay ON level. @note HIGH (true) / LOW (false).
 | 
			
		||||
        uint8_t led_pin;                // Led GPIO number (if present).
 | 
			
		||||
        bool led_on_level;              // Led ON level (if present). @note HIGH (true) / LOW (false).
 | 
			
		||||
        uint8_t int_button_pin;         // Internal button GPIO number (if present).
 | 
			
		||||
        bool int_button_on_level;       // Internal button trigger level (if present). @note HIGH (true) / LOW (false).
 | 
			
		||||
        uint8_t ext_button_pin;         // External button GPIO number (if present).
 | 
			
		||||
        bool ext_button_on_level;       // External button trigger level (if present). @note HIGH (true) / LOW (false).
 | 
			
		||||
        uint8_t sensor_pin;             // Sensor GPIO number (if present).
 | 
			
		||||
        ha_sensor_type_t sensor_type;   // Sensor types (if present). @note Used to identify the sensor type by ESP-NOW gateway and send the appropriate sensor status messages to MQTT.
 | 
			
		||||
        uint16_t measurement_frequency; // Sensor measurement frequency (if present).
 | 
			
		||||
        uint8_t relay_pin;        // Relay GPIO number.
 | 
			
		||||
        bool relay_on_level;      // Relay ON level. @note HIGH (true) / LOW (false).
 | 
			
		||||
        uint8_t led_pin;          // Led GPIO number (if present).
 | 
			
		||||
        bool led_on_level;        // Led ON level (if present). @note HIGH (true) / LOW (false).
 | 
			
		||||
        uint8_t int_button_pin;   // Internal button GPIO number (if present).
 | 
			
		||||
        bool int_button_on_level; // Internal button trigger level (if present). @note HIGH (true) / LOW (false).
 | 
			
		||||
        uint8_t ext_button_pin;   // External button GPIO number (if present).
 | 
			
		||||
        bool ext_button_on_level; // External button trigger level (if present). @note HIGH (true) / LOW (false).
 | 
			
		||||
    } hardware_config;
 | 
			
		||||
    struct // Storage structure of switch status data.
 | 
			
		||||
    {
 | 
			
		||||
        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.
 | 
			
		||||
        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.
 | 
			
		||||
    volatile bool is_first_connection;           // First connection 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().
 | 
			
		||||
    TaskHandle_t sensor_attributes_message_task; // Unique task handle for zh_send_sensor_attributes_message_task().
 | 
			
		||||
    TaskHandle_t sensor_status_message_task;     // Unique task handle for zh_send_sensor_status_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.
 | 
			
		||||
@@ -181,27 +162,6 @@ void zh_send_switch_keep_alive_message_task(void *pvParameter);
 | 
			
		||||
 */
 | 
			
		||||
void zh_send_switch_status_message(const switch_config_t *switch_config);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Function for prepare the sensor 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.
 | 
			
		||||
 */
 | 
			
		||||
void zh_send_sensor_config_message(const switch_config_t *switch_config);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Task for prepare the sensor attributes message and sending it to the gateway.
 | 
			
		||||
 *
 | 
			
		||||
 * @param[in] pvParameter Pointer to structure of data exchange between tasks, functions and event handlers.
 | 
			
		||||
 */
 | 
			
		||||
void zh_send_sensor_attributes_message_task(void *pvParameter);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Task for prepare the sensor 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_sensor_status_message_task(void *pvParameter);
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Function for ESP-NOW event processing.
 | 
			
		||||
 *
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user