7 Commits

Author SHA1 Message Date
708bf211fa Version 1.0.5
Changed WiFi protocol.
Reduced sensor status message fruquency.
Reduced time threshold for sensor availability.
Changed sensor reading error message.
Added offline message.
Changed task management.
2024-07-18 13:30:09 +03:00
31b7789873 Version 1.0.4
Added sensor reading error message.
Updated components.
2024-07-16 14:12:28 +03:00
6e27f40bf1 Update README.md 2024-07-05 13:48:21 +03:00
577d5478a8 Version 1.0.3
Updated sensor messages for compatibility with zh_espnow_sensor.
Updated components.
2024-06-26 09:24:40 +03:00
10640f64b5 Updated components 2024-06-23 22:23:47 +03:00
1699554c74 Update README.md 2024-06-23 22:16:27 +03:00
2f6c37038b Update README.md 2024-06-23 12:21:59 +03:00
9 changed files with 153 additions and 69 deletions

View File

@ -12,7 +12,7 @@ ESP-NOW based switch for ESP32 ESP-IDF and ESP8266 RTOS SDK. Alternate firmware
1. Saves the last state when the power is turned off. 1. Saves the last state when the power is turned off.
2. Automatically adds switch configuration to Home Assistan via MQTT discovery as a switch. 2. Automatically adds switch configuration to Home Assistan via MQTT discovery as a switch.
3. Update firmware from HTTPS server via ESP-NOW. 3. Update firmware from HTTPS server via ESP-NOW.
4. Optional support one external sensor (DS18B20, DHT11, DHT22). 4. Optional support one external 1-wire sensor (DS18B20/DHT11/DHT22/AM2302/AM2320).
5. Direct or mesh work mode. 5. Direct or mesh work mode.
## Notes ## Notes
@ -21,7 +21,7 @@ ESP-NOW based switch for ESP32 ESP-IDF and ESP8266 RTOS SDK. Alternate firmware
2. ESP-NOW mesh network based on the [zh_network](https://github.com/aZholtikov/zh_network). 2. ESP-NOW mesh network based on the [zh_network](https://github.com/aZholtikov/zh_network).
3. For initial settings use "menuconfig -> ZH ESP-NOW Switch Configuration". After first boot all settings will be stored in NVS memory for prevente change during OTA firmware update. 3. For initial settings use "menuconfig -> ZH ESP-NOW Switch Configuration". After first boot all settings will be stored in NVS memory for prevente change during OTA firmware update.
4. To restart the switch, send the "restart" command to the root topic of the switch (example - "homeassistant/espnow_switch/24-62-AB-F9-1F-A8"). 4. To restart the switch, send the "restart" command to the root topic of the switch (example - "homeassistant/espnow_switch/24-62-AB-F9-1F-A8").
5. To update the switch firmware, send the "update" command to the root topic of the switch (example - "homeassistant/espnow_switch/70-03-9F-44-BE-F7"). The update path should be like as "https://your_server/zh_espnow_switch_esp32.bin" (for ESP32) or "https://your_server/zh_espnow_switch_esp8266.app1.bin + https://your_server/zh_espnow_switch_esp8266.app2.bin" (for ESP8266). The time and success of the update depends on the load on WiFi channel 1. Average update time is up to five minutes. The online status of the update will be displayed in the root switch topic. 5. To update the switch firmware, send the "update" command to the root topic of the switch (example - "homeassistant/espnow_switch/70-03-9F-44-BE-F7"). The update path should be like as "https://your_server/zh_espnow_switch_esp32.bin" (for ESP32) or "https://your_server/zh_espnow_switch_esp8266.app1.bin + https://your_server/zh_espnow_switch_esp8266.app2.bin" (for ESP8266). Average update time is up to some minutes. The online status of the update will be displayed in the root switch topic.
6. To change initial settings of the switch (except work mode), send the X1,X2,X3,X4,X5,X6,X7,X8,X9,X10 command to the hardware topic of the switch (example - "homeassistant/espnow_switch/70-03-9F-44-BE-F7/hardware"). The configuration will only be accepted if it does not cause errors. The current configuration status is displayed in the configuration topic of the switch (example - "homeassistant/espnow_switch/70-03-9F-44-BE-F7/config"). 6. To change initial settings of the switch (except work mode), send the X1,X2,X3,X4,X5,X6,X7,X8,X9,X10 command to the hardware topic of the switch (example - "homeassistant/espnow_switch/70-03-9F-44-BE-F7/hardware"). The configuration will only be accepted if it does not cause errors. The current configuration status is displayed in the configuration topic of the switch (example - "homeassistant/espnow_switch/70-03-9F-44-BE-F7/config").
MQTT configuration message should filled according to the template "X1,X2,X3,X4,X5,X6,X7,X8,X9,X10". Where: MQTT configuration message should filled according to the template "X1,X2,X3,X4,X5,X6,X7,X8,X9,X10". Where:
@ -35,7 +35,7 @@ MQTT configuration message should filled according to the template "X1,X2,X3,X4,
7. X7 - External button GPIO number. 0 - 48 (according to the module used), 255 if not used. 7. X7 - External button GPIO number. 0 - 48 (according to the module used), 255 if not used.
8. X8 - External button trigger level. 1 for high, 0 for low. Only affected when X7 is used. 8. X8 - External button trigger level. 1 for high, 0 for low. Only affected when X7 is used.
9. X9 - Sensor GPIO number. 0 - 48 (according to the module used). 255 if not used. 9. X9 - Sensor GPIO number. 0 - 48 (according to the module used). 255 if not used.
10. X10 - Sensor type. 1 for DS18B20, 2 for DHT11, 3 for DHT22. Only affected when X9 is used. 10. X10 - Sensor type. 1 for DS18B20, 8 for DHT. Only affected when X9 is used.
## Build and flash ## Build and flash

View File

@ -15,7 +15,11 @@ void app_main(void)
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&wifi_init_config); esp_wifi_init(&wifi_init_config);
esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B); #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(); esp_wifi_start();
#ifdef CONFIG_NETWORK_TYPE_DIRECT #ifdef CONFIG_NETWORK_TYPE_DIRECT
zh_espnow_init_config_t espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT(); zh_espnow_init_config_t espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT();
@ -396,28 +400,35 @@ void zh_send_sensor_config_message(const switch_config_t *switch_config)
data.device_type = ZHDT_SENSOR; data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_CONFIG; 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.suggested_display_precision = 1;
data.payload_data.config_message.sensor_config_message.expire_after = ZH_SENSOR_STATUS_MESSAGE_FREQUENCY * 3; data.payload_data.config_message.sensor_config_message.expire_after = ZH_SENSOR_STATUS_MESSAGE_FREQUENCY * 1.25; // + 25% just in case.
data.payload_data.config_message.sensor_config_message.enabled_by_default = true; 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.force_update = true;
data.payload_data.config_message.sensor_config_message.qos = 2; data.payload_data.config_message.sensor_config_message.qos = 2;
data.payload_data.config_message.sensor_config_message.retain = true; data.payload_data.config_message.sensor_config_message.retain = true;
char *unit_of_measurement = NULL; 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) switch (switch_config->hardware_config.sensor_type)
{ {
case HAST_DS18B20: case HAST_DS18B20:
data.payload_data.config_message.sensor_config_message.unique_id = 2; data.payload_data.config_message.sensor_config_message.unique_id = 3;
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE; data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
unit_of_measurement = "°C"; unit_of_measurement = "°C";
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, 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)); zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
break; break;
case HAST_DHT: case HAST_DHT:
data.payload_data.config_message.sensor_config_message.unique_id = 2; data.payload_data.config_message.sensor_config_message.unique_id = 3;
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE; data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
unit_of_measurement = "°C"; unit_of_measurement = "°C";
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, 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)); 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 = 3; data.payload_data.config_message.sensor_config_message.unique_id = 4;
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_HUMIDITY; data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_HUMIDITY;
unit_of_measurement = "%"; unit_of_measurement = "%";
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement); strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
@ -460,59 +471,47 @@ void zh_send_sensor_status_message_task(void *pvParameter)
float temperature = 0.0; float temperature = 0.0;
zh_espnow_data_t data = {0}; zh_espnow_data_t data = {0};
data.device_type = ZHDT_SENSOR; data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_STATE;
data.payload_data.status_message.sensor_status_message.sensor_type = switch_config->hardware_config.sensor_type;
for (;;) for (;;)
{ {
esp_err_t err = ESP_OK;
switch (switch_config->hardware_config.sensor_type) switch (switch_config->hardware_config.sensor_type)
{ {
case HAST_DS18B20: case HAST_DS18B20:
ZH_DS18B20_READ: err = zh_ds18b20_read(NULL, &temperature);
switch (zh_ds18b20_read(NULL, &temperature)) if (err == ESP_OK)
{ {
case ESP_OK:
data.payload_data.status_message.sensor_status_message.temperature = temperature; data.payload_data.status_message.sensor_status_message.temperature = temperature;
break; data.payload_data.status_message.sensor_status_message.voltage = 3.3; // For future development.
case ESP_FAIL:
vTaskDelay(10000 / portTICK_PERIOD_MS);
goto ZH_DS18B20_READ;
break;
case ESP_ERR_INVALID_CRC:
vTaskDelay(1000 / portTICK_PERIOD_MS);
goto ZH_DS18B20_READ;
break;
default:
break;
} }
break; break;
case HAST_DHT: case HAST_DHT:
ZH_DHT_READ: err = zh_dht_read(&humidity, &temperature);
switch (zh_dht_read(&humidity, &temperature)) if (err == ESP_OK)
{ {
case ESP_OK:
data.payload_data.status_message.sensor_status_message.humidity = humidity; 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.temperature = temperature;
break; data.payload_data.status_message.sensor_status_message.voltage = 3.3; // For future development.
case ESP_ERR_INVALID_RESPONSE:
vTaskDelay(10000 / portTICK_PERIOD_MS);
goto ZH_DHT_READ;
break;
case ESP_ERR_TIMEOUT:
vTaskDelay(10000 / portTICK_PERIOD_MS);
goto ZH_DHT_READ;
break;
case ESP_ERR_INVALID_CRC:
vTaskDelay(3000 / portTICK_PERIOD_MS);
goto ZH_DHT_READ;
break;
default:
break;
} }
break; break;
default: default:
break; break;
} }
zh_send_message(switch_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t)); 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
{
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(ZH_SENSOR_STATUS_MESSAGE_FREQUENCY * 1000 / portTICK_PERIOD_MS); vTaskDelay(ZH_SENSOR_STATUS_MESSAGE_FREQUENCY * 1000 / portTICK_PERIOD_MS);
} }
vTaskDelete(NULL); vTaskDelete(NULL);
@ -556,13 +555,30 @@ 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_config_message(switch_config);
zh_send_switch_status_message(switch_config); zh_send_switch_status_message(switch_config);
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) 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); zh_send_sensor_config_message(switch_config);
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); 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);
}
} }
} }
} }
@ -574,12 +590,12 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
switch_config->gateway_is_available = false; switch_config->gateway_is_available = false;
if (switch_config->hardware_config.relay_pin != ZH_NOT_USED) if (switch_config->hardware_config.relay_pin != ZH_NOT_USED)
{ {
vTaskDelete(switch_config->switch_attributes_message_task); vTaskSuspend(switch_config->switch_attributes_message_task);
vTaskDelete(switch_config->switch_keep_alive_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) 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); vTaskSuspend(switch_config->sensor_attributes_message_task);
vTaskDelete(switch_config->sensor_status_message_task); vTaskSuspend(switch_config->sensor_status_message_task);
} }
} }
} }
@ -603,9 +619,41 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
switch_config->hardware_config.sensor_pin = 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_pin;
switch_config->hardware_config.sensor_type = data->payload_data.config_message.switch_hardware_config_message.sensor_type; switch_config->hardware_config.sensor_type = data->payload_data.config_message.switch_hardware_config_message.sensor_type;
zh_save_config(switch_config); zh_save_config(switch_config);
switch_config->gateway_is_available = false;
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;
data->payload_data.keep_alive_message.online_status = ZH_OFFLINE;
data->payload_data.keep_alive_message.message_frequency = ZH_SWITCH_KEEP_ALIVE_MESSAGE_FREQUENCY;
zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
vTaskDelay(1000 / portTICK_PERIOD_MS);
esp_restart(); esp_restart();
break; break;
case ZHPT_UPDATE:; case ZHPT_UPDATE:;
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);
}
}
data->device_type = ZHDT_SWITCH;
data->payload_type = ZHPT_KEEP_ALIVE;
data->payload_data.keep_alive_message.online_status = ZH_OFFLINE;
data->payload_data.keep_alive_message.message_frequency = ZH_SWITCH_KEEP_ALIVE_MESSAGE_FREQUENCY;
zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
const esp_app_desc_t *app_info = get_app_description(); const esp_app_desc_t *app_info = get_app_description();
switch_config->update_partition = esp_ota_get_next_update_partition(NULL); switch_config->update_partition = esp_ota_get_next_update_partition(NULL);
strcpy(data->payload_data.ota_message.espnow_ota_data.app_version, app_info->version); strcpy(data->payload_data.ota_message.espnow_ota_data.app_version, app_info->version);
@ -618,7 +666,6 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
#else #else
strcpy(data->payload_data.ota_message.espnow_ota_data.app_name, app_info->project_name); strcpy(data->payload_data.ota_message.espnow_ota_data.app_name, app_info->project_name);
#endif #endif
data->device_type = ZHDT_SWITCH;
data->payload_type = ZHPT_UPDATE; data->payload_type = ZHPT_UPDATE;
zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t)); zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
break; break;
@ -645,6 +692,16 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
break; break;
case ZHPT_UPDATE_ERROR: case ZHPT_UPDATE_ERROR:
esp_ota_end(switch_config->update_handle); esp_ota_end(switch_config->update_handle);
if (switch_config->hardware_config.relay_pin != ZH_NOT_USED)
{
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; break;
case ZHPT_UPDATE_END: case ZHPT_UPDATE_END:
if (esp_ota_end(switch_config->update_handle) != ESP_OK) if (esp_ota_end(switch_config->update_handle) != ESP_OK)
@ -652,6 +709,16 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
data->device_type = ZHDT_SWITCH; data->device_type = ZHDT_SWITCH;
data->payload_type = ZHPT_UPDATE_FAIL; data->payload_type = ZHPT_UPDATE_FAIL;
zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t)); zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
if (switch_config->hardware_config.relay_pin != ZH_NOT_USED)
{
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; break;
} }
esp_ota_set_boot_partition(switch_config->update_partition); esp_ota_set_boot_partition(switch_config->update_partition);
@ -662,6 +729,22 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
esp_restart(); esp_restart();
break; break;
case ZHPT_RESTART: case ZHPT_RESTART:
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;
data->payload_data.keep_alive_message.online_status = ZH_OFFLINE;
data->payload_data.keep_alive_message.message_frequency = ZH_SWITCH_KEEP_ALIVE_MESSAGE_FREQUENCY;
zh_send_message(switch_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
vTaskDelay(1000 / portTICK_PERIOD_MS);
esp_restart(); esp_restart();
break; break;
default: default:
@ -682,12 +765,12 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
switch_config->gateway_is_available = false; switch_config->gateway_is_available = false;
if (switch_config->hardware_config.relay_pin != ZH_NOT_USED) if (switch_config->hardware_config.relay_pin != ZH_NOT_USED)
{ {
vTaskDelete(switch_config->switch_attributes_message_task); vTaskSuspend(switch_config->switch_attributes_message_task);
vTaskDelete(switch_config->switch_keep_alive_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) 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); vTaskSuspend(switch_config->sensor_attributes_message_task);
vTaskDelete(switch_config->sensor_status_message_task); vTaskSuspend(switch_config->sensor_status_message_task);
} }
} }
} }
@ -703,12 +786,12 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
switch_config->gateway_is_available = false; switch_config->gateway_is_available = false;
if (switch_config->hardware_config.relay_pin != ZH_NOT_USED) if (switch_config->hardware_config.relay_pin != ZH_NOT_USED)
{ {
vTaskDelete(switch_config->switch_attributes_message_task); vTaskSuspend(switch_config->switch_attributes_message_task);
vTaskDelete(switch_config->switch_keep_alive_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) 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); vTaskSuspend(switch_config->sensor_attributes_message_task);
vTaskDelete(switch_config->sensor_status_message_task); vTaskSuspend(switch_config->sensor_status_message_task);
} }
} }
} }

View File

@ -48,7 +48,7 @@
#define ZH_SWITCH_KEEP_ALIVE_MESSAGE_FREQUENCY 10 // Frequency of sending a switch keep alive 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_ATTRIBUTES_MESSAGE_FREQUENCY 60 // Frequency of sending a switch attributes message to the gateway (in seconds).
#define ZH_SENSOR_STATUS_MESSAGE_FREQUENCY 300 // // Frequency of sending a sensor status message to the gateway (in seconds). #define ZH_SENSOR_STATUS_MESSAGE_FREQUENCY 60 // Frequency of sending a sensor status 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_ATTRIBUTES_MESSAGE_FREQUENCY 60 // Frequency of sending a sensor attributes message to the gateway (in seconds).
#define ZH_GPIO_TASK_PRIORITY 3 // Prioritize the task of GPIO processing. #define ZH_GPIO_TASK_PRIORITY 3 // Prioritize the task of GPIO processing.
@ -77,6 +77,7 @@ typedef struct // Structure of data exchange between tasks, functions and event
} status; } status;
volatile bool gpio_processing; // GPIO processing flag. @note Used to prevent a repeated interrupt from triggering during GPIO processing. 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 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. 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_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 switch_keep_alive_message_task; // Unique task handle for zh_send_switch_keep_alive_message_task().
@ -159,14 +160,14 @@ void zh_send_switch_attributes_message_task(void *pvParameter);
void zh_send_switch_config_message(const switch_config_t *switch_config); void zh_send_switch_config_message(const switch_config_t *switch_config);
/** /**
* @brief Function for prepare the sensor hardware configuration message and sending it to the gateway. * @brief Function 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] switch_config 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(const switch_config_t *switch_config);
/** /**
* @brief Task for prepare the sensor keep alive message and sending it to the gateway. * @brief Task for prepare the switch 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. * @param[in] pvParameter Pointer to the structure of data exchange between tasks, functions and event handlers.
*/ */

View File

@ -1 +1 @@
1.0.2 1.0.5