2 Commits

Author SHA1 Message Date
d1d9c79369 Version 1.0.3
Added keep_alive_message_task.
Added sensor reading error message.
Updated components.
2024-07-16 14:01:46 +03:00
2bd120b5d3 Update README.md 2024-07-05 13:47:54 +03:00
7 changed files with 45 additions and 9 deletions

View File

@ -29,7 +29,7 @@ ESP-NOW based sensor for ESP32 ESP-IDF and ESP8266 RTOS SDK.
3. For initial settings use "menuconfig -> ZH ESP-NOW Sensor 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 Sensor Configuration". After first boot all settings will be stored in NVS memory for prevente change during OTA firmware update.
4. Only one sensor can be used at a time. 4. Only one sensor can be used at a time.
5. To restart the sensor, send the "restart" command to the root topic of the sensor (example - "homeassistant/espnow_sensor/24-62-AB-F9-1F-A8"). 5. To restart the sensor, send the "restart" command to the root topic of the sensor (example - "homeassistant/espnow_sensor/24-62-AB-F9-1F-A8").
6. To update the sensor firmware, send the "update" command to the root topic of the sensor (example - "homeassistant/espnow_sensor/70-03-9F-44-BE-F7"). The update path should be like as "https://your_server/zh_espnow_sensor_esp32.bin" (for ESP32) or "https://your_server/zh_espnow_sensor_esp8266.app1.bin + https://your_server/zh_espnow_sensor_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 is displayed in the root sensor topic. 6. To update the sensor firmware, send the "update" command to the root topic of the sensor (example - "homeassistant/espnow_sensor/70-03-9F-44-BE-F7"). The update path should be like as "https://your_server/zh_espnow_sensor_esp32.bin" (for ESP32) or "https://your_server/zh_espnow_sensor_esp8266.app1.bin + https://your_server/zh_espnow_sensor_esp8266.app2.bin" (for ESP8266). Average update time is up to some minutes. The online status of the update is displayed in the root sensor topic.
7. To change initial settings of the sensor (except work mode and power selection GPIO), send the X1,X2,X3,X4,X5,X6 command to the hardware topic of the sensor (example - "homeassistant/espnow_sensor/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 sensor (example - "homeassistant/espnow_sensor/70-03-9F-44-BE-F7/config"). 7. To change initial settings of the sensor (except work mode and power selection GPIO), send the X1,X2,X3,X4,X5,X6 command to the hardware topic of the sensor (example - "homeassistant/espnow_sensor/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 sensor (example - "homeassistant/espnow_sensor/70-03-9F-44-BE-F7/config").
MQTT configuration message should filled according to the template "X1,X2,X3,X4,X5,X6". Where: MQTT configuration message should filled according to the template "X1,X2,X3,X4,X5,X6". Where:

View File

@ -291,8 +291,9 @@ void zh_sensor_deep_sleep(sensor_config_t *sensor_config)
#ifndef CONFIG_IDF_TARGET_ESP8266 #ifndef CONFIG_IDF_TARGET_ESP8266
esp_sleep_enable_timer_wakeup(sensor_config->hardware_config.measurement_frequency * 1000000); esp_sleep_enable_timer_wakeup(sensor_config->hardware_config.measurement_frequency * 1000000);
#endif #endif
uint8_t required_message_quantity = 1; uint8_t required_message_quantity = 2;
zh_send_sensor_hardware_config_message(sensor_config); 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) 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); 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_DHT:
case HAST_AHT: case HAST_AHT:
case HAST_SHT: // For future development. case HAST_SHT: // For future development.
case HAST_HTU: // For future development. case HAST_HTU: // For future development.
case HAST_HDC1080: // 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.unique_id = 2;
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;
@ -506,7 +507,9 @@ void zh_send_sensor_status_message_task(void *pvParameter)
} }
else 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) 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); 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) 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; 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); 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_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_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->status_message_task);
vTaskDelete(sensor_config->attributes_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->status_message_task);
vTaskDelete(sensor_config->attributes_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->status_message_task);
vTaskDelete(sensor_config->attributes_message_task); vTaskDelete(sensor_config->attributes_message_task);
vTaskDelete(sensor_config->keep_alive_message_task);
} }
} }
} }

View File

@ -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 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 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_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. #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. 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 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 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. 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. 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. 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); 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. * @brief Function for ESP-NOW event processing.
* *

View File

@ -1 +1 @@
1.0.2 1.0.3