Version 1.0.1

Updated some components.
Changed main code to support the new major version of zh_config.
Reducing the amount of memory used.
This commit is contained in:
2024-06-09 14:30:15 +03:00
parent db2a33d47f
commit 6ecd5a59bd
30 changed files with 723 additions and 651 deletions

View File

@ -236,12 +236,15 @@ void zh_sensor_deep_sleep(sensor_config_t *sensor_config)
void zh_send_sensor_hardware_config_message(const sensor_config_t *sensor_config)
{
zh_config_message_t config_message = {0};
config_message = (zh_config_message_t)sensor_config->hardware_config;
zh_espnow_data_t data = {0};
data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_HARDWARE;
data.payload_data = (zh_payload_data_t)config_message;
data.payload_data.config_message.sensor_hardware_config_message.sensor_type = sensor_config->hardware_config.sensor_type;
data.payload_data.config_message.sensor_hardware_config_message.sensor_pin_1 = sensor_config->hardware_config.sensor_pin_1;
data.payload_data.config_message.sensor_hardware_config_message.sensor_pin_2 = sensor_config->hardware_config.sensor_pin_2;
data.payload_data.config_message.sensor_hardware_config_message.power_pin = sensor_config->hardware_config.power_pin;
data.payload_data.config_message.sensor_hardware_config_message.measurement_frequency = sensor_config->hardware_config.measurement_frequency;
data.payload_data.config_message.sensor_hardware_config_message.battery_power = sensor_config->hardware_config.battery_power;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
}
@ -249,23 +252,21 @@ void zh_send_sensor_attributes_message_task(void *pvParameter)
{
sensor_config_t *sensor_config = pvParameter;
const esp_app_desc_t *app_info = get_app_description();
zh_attributes_message_t attributes_message = {0};
attributes_message.chip_type = ZH_CHIP_TYPE;
attributes_message.sensor_type = sensor_config->hardware_config.sensor_type;
strcpy(attributes_message.flash_size, CONFIG_ESPTOOLPY_FLASHSIZE);
attributes_message.cpu_frequency = ZH_CPU_FREQUENCY;
attributes_message.reset_reason = (uint8_t)esp_reset_reason();
strcpy(attributes_message.app_name, app_info->project_name);
strcpy(attributes_message.app_version, app_info->version);
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 = sensor_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 (;;)
{
attributes_message.heap_size = esp_get_free_heap_size();
attributes_message.min_heap_size = esp_get_minimum_free_heap_size();
attributes_message.uptime = esp_timer_get_time() / 1000000;
data.payload_data = (zh_payload_data_t)attributes_message;
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(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
if (sensor_config->hardware_config.battery_power == true)
{
@ -282,43 +283,35 @@ uint8_t zh_send_sensor_config_message(const sensor_config_t *sensor_config)
zh_espnow_data_t data = {0};
data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_CONFIG;
zh_config_message_t config_message = {0};
zh_sensor_config_message_t sensor_config_message = {0};
sensor_config_message.suggested_display_precision = 1;
sensor_config_message.expire_after = sensor_config->hardware_config.measurement_frequency * 3;
sensor_config_message.enabled_by_default = true;
sensor_config_message.force_update = true;
sensor_config_message.qos = 2;
sensor_config_message.retain = true;
data.payload_data.config_message.sensor_config_message.suggested_display_precision = 1;
data.payload_data.config_message.sensor_config_message.expire_after = sensor_config->hardware_config.measurement_frequency * 3;
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;
switch (sensor_config->hardware_config.sensor_type)
{
case HAST_DS18B20:
sensor_config_message.unique_id = 1;
sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
data.payload_data.config_message.sensor_config_message.unique_id = 1;
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
unit_of_measurement = "°C";
strcpy(sensor_config_message.unit_of_measurement, unit_of_measurement);
config_message = (zh_config_message_t)sensor_config_message;
data.payload_data = (zh_payload_data_t)config_message;
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
++messages_quantity;
break;
case HAST_DHT11:
case HAST_DHT22:
sensor_config_message.unique_id = 1;
sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
data.payload_data.config_message.sensor_config_message.unique_id = 1;
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_TEMPERATURE;
unit_of_measurement = "°C";
strcpy(sensor_config_message.unit_of_measurement, unit_of_measurement);
config_message = (zh_config_message_t)sensor_config_message;
data.payload_data = (zh_payload_data_t)config_message;
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
++messages_quantity;
sensor_config_message.unique_id = 2;
sensor_config_message.sensor_device_class = HASDC_HUMIDITY;
data.payload_data.config_message.sensor_config_message.unique_id = 2;
data.payload_data.config_message.sensor_config_message.sensor_device_class = HASDC_HUMIDITY;
unit_of_measurement = "%";
strcpy(sensor_config_message.unit_of_measurement, unit_of_measurement);
config_message = (zh_config_message_t)sensor_config_message;
data.payload_data = (zh_payload_data_t)config_message;
strcpy(data.payload_data.config_message.sensor_config_message.unit_of_measurement, unit_of_measurement);
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
++messages_quantity;
break;
@ -333,12 +326,10 @@ void zh_send_sensor_status_message_task(void *pvParameter)
sensor_config_t *sensor_config = pvParameter;
float humidity = 0.0;
float temperature = 0.0;
zh_sensor_status_message_t sensor_status_message = {0};
sensor_status_message.sensor_type = sensor_config->hardware_config.sensor_type;
zh_status_message_t status_message = {0};
zh_espnow_data_t data = {0};
data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_STATE;
data.payload_data.status_message.sensor_status_message.sensor_type = sensor_config->hardware_config.sensor_type;
for (;;)
{
if (sensor_config->hardware_config.power_pin != ZH_NOT_USED && sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED)
@ -353,7 +344,7 @@ void zh_send_sensor_status_message_task(void *pvParameter)
switch (zh_ds18b20_read(NULL, &temperature))
{
case ESP_OK:
sensor_status_message.temperature = temperature;
data.payload_data.status_message.sensor_status_message.temperature = temperature;
break;
case ESP_FAIL:
if (sensor_config->hardware_config.battery_power == false)
@ -379,8 +370,8 @@ void zh_send_sensor_status_message_task(void *pvParameter)
switch (zh_dht_read(&sensor_config->dht_handle, &humidity, &temperature))
{
case ESP_OK:
sensor_status_message.humidity = humidity;
sensor_status_message.temperature = temperature;
data.payload_data.status_message.sensor_status_message.humidity = humidity;
data.payload_data.status_message.sensor_status_message.temperature = temperature;
break;
case ESP_ERR_INVALID_RESPONSE:
if (sensor_config->hardware_config.battery_power == false)
@ -410,8 +401,6 @@ void zh_send_sensor_status_message_task(void *pvParameter)
default:
break;
}
status_message = (zh_status_message_t)sensor_status_message;
data.payload_data = (zh_payload_data_t)status_message;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
if (sensor_config->hardware_config.power_pin != ZH_NOT_USED && sensor_config->hardware_config.sensor_pin_1 != ZH_NOT_USED)
{
@ -429,7 +418,6 @@ void zh_send_sensor_status_message_task(void *pvParameter)
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;
zh_espnow_data_t data = {0};
switch (event_id)
{
#ifdef CONFIG_NETWORK_TYPE_DIRECT
@ -447,14 +435,14 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
goto ZH_NETWORK_EVENT_HANDLER_EXIT;
}
#endif
memcpy(&data, recv_data->data, recv_data->data_len);
switch (data.device_type)
zh_espnow_data_t *data = (zh_espnow_data_t *)recv_data->data;
switch (data->device_type)
{
case ZHDT_GATEWAY:
switch (data.payload_type)
switch (data->payload_type)
{
case ZHPT_KEEP_ALIVE:
if (data.payload_data.keep_alive_message.online_status == ZH_ONLINE)
if (data->payload_data.keep_alive_message.online_status == ZH_ONLINE)
{
if (sensor_config->gateway_is_available == false)
{
@ -483,29 +471,31 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
}
break;
case ZHPT_HARDWARE:
sensor_config->hardware_config = data.payload_data.config_message.sensor_hardware_config_message;
sensor_config->hardware_config.sensor_type = data->payload_data.config_message.sensor_hardware_config_message.sensor_type;
sensor_config->hardware_config.sensor_pin_1 = data->payload_data.config_message.sensor_hardware_config_message.sensor_pin_1;
sensor_config->hardware_config.sensor_pin_2 = data->payload_data.config_message.sensor_hardware_config_message.sensor_pin_2;
sensor_config->hardware_config.power_pin = data->payload_data.config_message.sensor_hardware_config_message.power_pin;
sensor_config->hardware_config.measurement_frequency = data->payload_data.config_message.sensor_hardware_config_message.measurement_frequency;
sensor_config->hardware_config.battery_power = data->payload_data.config_message.sensor_hardware_config_message.battery_power;
zh_save_config(sensor_config);
esp_restart();
break;
case ZHPT_UPDATE:;
const esp_app_desc_t *app_info = get_app_description();
sensor_config->update_partition = esp_ota_get_next_update_partition(NULL);
zh_espnow_ota_message_t espnow_ota_message = {0};
espnow_ota_message.chip_type = ZH_CHIP_TYPE;
strcpy(espnow_ota_message.app_version, app_info->version);
strcpy(data->payload_data.ota_message.espnow_ota_data.app_version, app_info->version);
#ifdef CONFIG_IDF_TARGET_ESP8266
char *app_name = (char *)heap_caps_malloc(strlen(app_info->project_name) + 6, MALLOC_CAP_8BIT);
memset(app_name, 0, strlen(app_info->project_name) + 6);
sprintf(app_name, "%s.app%d", app_info->project_name, sensor_config->update_partition->subtype - ESP_PARTITION_SUBTYPE_APP_OTA_0 + 1);
strcpy(espnow_ota_message.app_name, app_name);
strcpy(data->payload_data.ota_message.espnow_ota_data.app_name, app_name);
heap_caps_free(app_name);
#else
strcpy(espnow_ota_message.app_name, app_info->project_name);
strcpy(data->payload_data.ota_message.espnow_ota_data.app_name, app_info->project_name);
#endif
data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_UPDATE;
data.payload_data = (zh_payload_data_t)espnow_ota_message;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
data->device_type = ZHDT_SENSOR;
data->payload_type = ZHPT_UPDATE;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
break;
case ZHPT_UPDATE_BEGIN:
#ifdef CONFIG_IDF_TARGET_ESP8266
@ -514,19 +504,19 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
esp_ota_begin(sensor_config->update_partition, OTA_SIZE_UNKNOWN, (esp_ota_handle_t *)&sensor_config->update_handle);
#endif
sensor_config->ota_message_part_number = 1;
data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_UPDATE_PROGRESS;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
data->device_type = ZHDT_SENSOR;
data->payload_type = ZHPT_UPDATE_PROGRESS;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
break;
case ZHPT_UPDATE_PROGRESS:
if (sensor_config->ota_message_part_number == data.payload_data.espnow_ota_message.part)
if (sensor_config->ota_message_part_number == data->payload_data.ota_message.espnow_ota_message.part)
{
++sensor_config->ota_message_part_number;
esp_ota_write(sensor_config->update_handle, (const void *)data.payload_data.espnow_ota_message.data, data.payload_data.espnow_ota_message.data_len);
esp_ota_write(sensor_config->update_handle, (const void *)data->payload_data.ota_message.espnow_ota_message.data, data->payload_data.ota_message.espnow_ota_message.data_len);
}
data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_UPDATE_PROGRESS;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
data->device_type = ZHDT_SENSOR;
data->payload_type = ZHPT_UPDATE_PROGRESS;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
break;
case ZHPT_UPDATE_ERROR:
esp_ota_end(sensor_config->update_handle);
@ -534,15 +524,15 @@ void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t eve
case ZHPT_UPDATE_END:
if (esp_ota_end(sensor_config->update_handle) != ESP_OK)
{
data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_UPDATE_FAIL;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
data->device_type = ZHDT_SENSOR;
data->payload_type = ZHPT_UPDATE_FAIL;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
break;
}
esp_ota_set_boot_partition(sensor_config->update_partition);
data.device_type = ZHDT_SENSOR;
data.payload_type = ZHPT_UPDATE_SUCCESS;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)&data, sizeof(zh_espnow_data_t));
data->device_type = ZHDT_SENSOR;
data->payload_type = ZHPT_UPDATE_SUCCESS;
zh_send_message(sensor_config->gateway_mac, (uint8_t *)data, sizeof(zh_espnow_data_t));
vTaskDelay(1000 / portTICK_PERIOD_MS);
esp_restart();
break;

View File

@ -47,36 +47,31 @@
#define get_app_description() esp_app_get_description()
#endif
/**
* @brief Frequency of transmission of keep alive messages to the gateway (in seconds).
*/
#define ZH_SENSOR_ATTRIBUTES_MESSAGE_FREQUENCY 60
#define ZH_SENSOR_ATTRIBUTES_MESSAGE_FREQUENCY 60 // Frequency of transmission of keep alive messages to the gateway (in seconds).
/**
* @brief Prioritize the task of sending messages to the gateway.
*/
#define ZH_MESSAGE_TASK_PRIORITY 2
#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.
/**
* @brief The stack size of the task of sending messages to the gateway.
*/
#define ZH_MESSAGE_STACK_SIZE 2048
/**
* @brief Structure of data exchange between tasks, functions and event handlers.
*/
typedef struct
typedef struct // Structure of data exchange between tasks, functions and event handlers.
{
zh_sensor_hardware_config_message_t hardware_config; // Storage structure of sensor hardware configuration data. @note
volatile bool gateway_is_available; // Gateway availability status flag. @note Used to control the tasks when the gateway connection is established / lost. Used only when external powered.
uint8_t gateway_mac[6]; // Gateway MAC address. @note Used only when external powered.
uint8_t sent_message_quantity; // System counter for the number of sended messages. @note Used only when powered by battery.
zh_dht_handle_t dht_handle; // Unique DTH11/22 sensor handle. @note
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.
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.
struct // Storage structure of sensor hardware configuration data.
{
ha_sensor_type_t sensor_type; // Sensor types.
uint8_t sensor_pin_1; // Sensor GPIO number 1. @note Main pin for 1-wire sensors, SDA pin for I2C sensors.
uint8_t sensor_pin_2; // Sensor GPIO number 2. @note SCL pin for I2C sensors.
uint8_t power_pin; // Power GPIO number (if used sensor power control).
uint16_t measurement_frequency; // Measurement frequency (sleep time on battery powering).
bool battery_power; // Battery powered. @note Battery powering (true) / external powering (false).
} hardware_config;
volatile bool gateway_is_available; // Gateway availability status flag. @note Used to control the tasks when the gateway connection is established / lost. Used only when external powered.
uint8_t gateway_mac[6]; // Gateway MAC address. @note Used only when external powered.
uint8_t sent_message_quantity; // System counter for the number of sended messages. @note Used only when powered by battery.
zh_dht_handle_t dht_handle; // Unique DTH11/22 sensor handle.
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.
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.
} sensor_config_t;
/**