From f2ffa5bebdf31ed5e57ced46bd0c0d74577dde99 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sun, 8 Mar 2026 11:49:48 +0300 Subject: [PATCH] feat: added logging to syslog server --- .gitmodules | 5 +- components/zh_syslog | 1 + main/CMakeLists.txt | 2 +- main/ate0005.c | 150 ++++++++++++++++++++++++++++++------------- main/ate0005.h | 17 +++-- version.txt | 2 +- 6 files changed, 123 insertions(+), 54 deletions(-) create mode 160000 components/zh_syslog diff --git a/.gitmodules b/.gitmodules index 2f5ddf1..9f4cfe4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -21,4 +21,7 @@ url = https://github.com/aZholtikov/zh_espnow [submodule "components/zh_tachometer"] path = components/zh_tachometer - url = https://github.com/aZholtikov/zh_tachometer \ No newline at end of file + url = https://github.com/aZholtikov/zh_tachometer +[submodule "components/zh_syslog"] + path = components/zh_syslog + url = https://github.com/aZholtikov/zh_syslog diff --git a/components/zh_syslog b/components/zh_syslog new file mode 160000 index 0000000..6105b97 --- /dev/null +++ b/components/zh_syslog @@ -0,0 +1 @@ +Subproject commit 6105b97292fa7c676ac96b1798693ec9a69c3247 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index a260c14..1f27c48 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1 +1 @@ -idf_component_register(SRCS "ate0005.c" INCLUDE_DIRS "" REQUIRES zh_160x_i2c zh_encoder zh_inclinometer zh_tachometer zh_espnow zh_ota_server esp_wifi nvs_flash esp_timer esp_app_format) \ No newline at end of file +idf_component_register(SRCS "ate0005.c" INCLUDE_DIRS "" REQUIRES zh_160x_i2c zh_encoder zh_inclinometer zh_tachometer zh_espnow zh_ota_server zh_syslog esp_wifi nvs_flash esp_timer esp_app_format) \ No newline at end of file diff --git a/main/ate0005.c b/main/ate0005.c index dda9259..adedadb 100644 --- a/main/ate0005.c +++ b/main/ate0005.c @@ -1,7 +1,5 @@ #include "ate0005.h" -// #define DEBUG - static task_stats_t _task_stats = {A_INCLINOMETER_TASK_STACK_SIZE, B_INCLINOMETER_TASK_STACK_SIZE, A_TACHOMETER_TASK_STACK_SIZE, B_TACHOMETER_TASK_STACK_SIZE}; static httpd_handle_t _http_server_handle = NULL; @@ -19,8 +17,9 @@ static TaskHandle_t _a_inclinometer_task_handle = NULL; static TaskHandle_t _b_inclinometer_task_handle = NULL; static TaskHandle_t _a_tachometer_task_handle = NULL; static TaskHandle_t _b_tachometer_task_handle = NULL; +static TaskHandle_t _zh_stats_task_handle = NULL; -static void _zh_wifi_softap_init(void); +static void _zh_wifi_init(void); static void _zh_webserver_init(void); static void _zh_espnow_init(void); static void _zh_encoder_init(void); @@ -32,29 +31,30 @@ static void _zh_pcf8574_init(void); static void _zh_system_load(void); static void _zh_timer_init(void); static void _zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); -// static void _zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); // To Do. +static void _zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); +static void _wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); static void _zh_inclinometer_reset_timer_on_alarm_cb(void *arg); static void _zh_a_inclinometer_processing_task(void *pvParameter); static void _zh_b_inclinometer_processing_task(void *pvParameter); static void _zh_a_tachometer_processing_task(void *pvParameter); static void _zh_b_tachometer_processing_task(void *pvParameter); -#ifdef DEBUG static void _zh_stats_processing_task(void *pvParameter); -#endif static const char *_main_menu[] = {MAIN_MENU_ANGLE_ANGLE_NAME, MAIN_MENU_TACHO_TACHO_NAME, MAIN_MENU_ANGLE_TACHO_NAME, MAIN_MENU_REMOTE_NAME}; volatile static bool _is_menu_mode = false; volatile static bool _is_reset_mode = false; volatile static bool _is_reset_occurred = false; volatile static uint8_t _current_menu_value = 0; +volatile static uint8_t _wifi_reconnect_retry_num = 0; +volatile static bool _is_wifi_connected = false; void app_main(void) { _lcd_mutex = xSemaphoreCreateMutex(); nvs_flash_init(); esp_event_loop_create_default(); - _zh_wifi_softap_init(); + _zh_wifi_init(); _zh_webserver_init(); _zh_pcf8574_init(); _zh_system_load(); @@ -63,9 +63,6 @@ void app_main(void) _zh_b_inclinometer_init(); _zh_espnow_init(); _zh_timer_init(); -#ifdef DEBUG - xTaskCreatePinnedToCore(&_zh_stats_processing_task, "stats_processing_task", STATS_TASK_STACK_SIZE, NULL, STATS_TASK_PRIORITY, NULL, tskNO_AFFINITY); -#endif const esp_partition_t *running = esp_ota_get_running_partition(); esp_ota_img_states_t ota_state = {0}; esp_ota_get_state_partition(running, &ota_state); @@ -76,23 +73,29 @@ void app_main(void) } } -static void _zh_wifi_softap_init(void) +static void _zh_wifi_init(void) { esp_netif_init(); esp_netif_create_default_wifi_ap(); - wifi_init_config_t wifi_config = WIFI_INIT_CONFIG_DEFAULT(); - esp_wifi_init(&wifi_config); - wifi_config_t ap_config = { - .ap = { - .ssid = WIFI_SSID, - .password = WIFI_PASS, - .channel = WIFI_CHANNEL, - .max_connection = MAX_STA_CONNECTION, - .authmode = WIFI_AUTH_WPA2_PSK, - }, + esp_netif_create_default_wifi_sta(); + wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); + esp_wifi_init(&wifi_init_config); + wifi_config_t wifi_ap_config = { + .ap.ssid = WIFI_AP_SSID, + .ap.password = WIFI_AP_PASS, + .ap.channel = WIFI_AP_CHANNEL, + .ap.max_connection = MAX_AP_CONNECTION, + .ap.authmode = WIFI_AUTH_WPA2_PSK, + }; + wifi_config_t wifi_sta_config = { + .sta.ssid = WIFI_STA_SSID, + .sta.password = WIFI_STA_PASS, }; esp_wifi_set_mode(WIFI_MODE_APSTA); - esp_wifi_set_config(WIFI_IF_AP, &ap_config); + esp_wifi_set_config(WIFI_IF_AP, &wifi_ap_config); + esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_config); + esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_wifi_event_handler, NULL, NULL); + esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &_wifi_event_handler, NULL, NULL); esp_wifi_start(); } @@ -328,10 +331,43 @@ static void _zh_encoder_event_handler(void *arg, esp_event_base_t event_base, in } } -// static void _zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) -// { -// // To Do. -// } +static void _zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) +{ + // To Do. +} + +static void _wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) +{ + switch (event_id) + { + case WIFI_EVENT_STA_START: + esp_wifi_connect(); + break; + case WIFI_EVENT_STA_DISCONNECTED: + if (_is_wifi_connected == true) + { + vTaskDelete(_zh_stats_task_handle); + zh_syslog_deinit(); + _is_wifi_connected = false; + } + if (_wifi_reconnect_retry_num < WIFI_STA_MAXIMUM_RETRY) + { + esp_wifi_connect(); + ++_wifi_reconnect_retry_num; + } + break; + case IP_EVENT_STA_GOT_IP: + _wifi_reconnect_retry_num = 0; + _is_wifi_connected = true; + zh_syslog_init_config_t config = ZH_SYSLOG_INIT_CONFIG_DEFAULT(); + strcpy(config.syslog_ip, SYSLOG_IP); + zh_syslog_init(&config); + xTaskCreatePinnedToCore(&_zh_stats_processing_task, "stats_processing_task", STATS_TASK_STACK_SIZE, NULL, STATS_TASK_PRIORITY, &_zh_stats_task_handle, tskNO_AFFINITY); + break; + default: + break; + } +} static void _zh_inclinometer_reset_timer_on_alarm_cb(void *arg) { @@ -462,31 +498,53 @@ static void _zh_b_tachometer_processing_task(void *pvParameter) vTaskDelete(NULL); } -#ifdef DEBUG static void _zh_stats_processing_task(void *pvParameter) { + const esp_app_desc_t *esp_app_desc = esp_app_get_description(); for (;;) { - printf("Tasks:\n"); - printf("A inclinometer task minimum free stack size: %ld.\n", _task_stats.a_inclinometer_task_min_stack_size); - printf("B inclinometer task minimum free stack size: %ld.\n", _task_stats.b_inclinometer_task_min_stack_size); - printf("A tachometer task minimum free stack size: %ld.\n", _task_stats.a_tachometer_task_min_stack_size); - printf("A tachometer task minimum free stack size: %ld.\n", _task_stats.b_tachometer_task_min_stack_size); - printf("Stats task minimum free stack size: %ld.\n", (uint32_t)uxTaskGetStackHighWaterMark(NULL)); - printf("Encoders:\n"); + char buffer[1024] = {0}; const zh_encoder_stats_t *encoder_stats = zh_encoder_get_stats(); - printf("Number of event post error: %ld.\n", encoder_stats->event_post_error); - printf("Number of queue overflow error: %ld.\n", encoder_stats->queue_overflow_error); - printf("Minimum free stack size: %ld.\n", encoder_stats->min_stack_size); - printf("Expanders:\n"); const zh_pcf8574_stats_t *pcf8574_stats = zh_pcf8574_get_stats(); - printf("Number of i2c driver error: %ld.\n", pcf8574_stats->i2c_driver_error); - printf("Number of event post error: %ld.\n", pcf8574_stats->event_post_error); - printf("Number of vector error: %ld.\n", pcf8574_stats->vector_error); - printf("Number of queue overflow error: %ld.\n", pcf8574_stats->queue_overflow_error); - printf("Minimum free stack size: %ld.\n", pcf8574_stats->min_stack_size); - vTaskDelay(10000 / portTICK_PERIOD_MS); + sprintf(buffer, "A inclinometer task minimum free stack size: %ld.\n", _task_stats.a_inclinometer_task_min_stack_size); + zh_syslog_send(ZH_KERN, _task_stats.a_inclinometer_task_min_stack_size > 100 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_inclinometer", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "B inclinometer task minimum free stack size: %ld.\n", _task_stats.b_inclinometer_task_min_stack_size); + zh_syslog_send(ZH_KERN, _task_stats.b_inclinometer_task_min_stack_size > 100 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_inclinometer", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "A tachometer task minimum free stack size: %ld.\n", _task_stats.b_tachometer_task_min_stack_size); + zh_syslog_send(ZH_KERN, _task_stats.b_tachometer_task_min_stack_size > 100 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_tachometer", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "B tachometer task minimum free stack size: %ld.\n", _task_stats.b_tachometer_task_min_stack_size); + zh_syslog_send(ZH_KERN, _task_stats.b_tachometer_task_min_stack_size > 100 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_tachometer", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "Stats task minimum free stack size: %ld.\n", (uint32_t)uxTaskGetStackHighWaterMark(NULL)); + zh_syslog_send(ZH_KERN, (uint32_t)uxTaskGetStackHighWaterMark(NULL) > 100 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_syslog", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "Encoder task minimum free stack size: %ld.\n", encoder_stats->min_stack_size); + zh_syslog_send(ZH_KERN, encoder_stats->min_stack_size > 100 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_encoder", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "Expander task minimum free stack size: %ld.\n", pcf8574_stats->min_stack_size); + zh_syslog_send(ZH_KERN, pcf8574_stats->min_stack_size > 100 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_pcf8574", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "Number of event post error: %ld.\n", encoder_stats->event_post_error); + zh_syslog_send(ZH_KERN, encoder_stats->event_post_error == 0 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_encoder", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "Number of queue overflow error: %ld.\n", encoder_stats->queue_overflow_error); + zh_syslog_send(ZH_KERN, encoder_stats->queue_overflow_error == 0 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_encoder", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "Number of i2c driver error: %ld.\n", pcf8574_stats->i2c_driver_error); + zh_syslog_send(ZH_KERN, pcf8574_stats->i2c_driver_error == 0 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_pcf8574", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "Number of event post error: %ld.\n", pcf8574_stats->event_post_error); + zh_syslog_send(ZH_KERN, pcf8574_stats->event_post_error == 0 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_pcf8574", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "Number of vector error: %ld.\n", pcf8574_stats->vector_error); + zh_syslog_send(ZH_KERN, pcf8574_stats->vector_error == 0 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_pcf8574", buffer); + vTaskDelay(500 / portTICK_PERIOD_MS); + sprintf(buffer, "Number of queue overflow error: %ld.\n", pcf8574_stats->queue_overflow_error); + zh_syslog_send(ZH_KERN, pcf8574_stats->queue_overflow_error == 0 ? ZH_INFO : ZH_WARNING, esp_app_desc->project_name, "zh_pcf8574", buffer); + vTaskDelay(60000 / portTICK_PERIOD_MS); } vTaskDelete(NULL); -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/main/ate0005.h b/main/ate0005.h index db09ebe..9758eb9 100644 --- a/main/ate0005.h +++ b/main/ate0005.h @@ -13,9 +13,10 @@ #include "zh_tachometer.h" #include "zh_ota_server.h" #include "zh_espnow.h" +#include "zh_syslog.h" #define STATS_TASK_PRIORITY tskIDLE_PRIORITY -#define STATS_TASK_STACK_SIZE configMINIMAL_STACK_SIZE + 512 +#define STATS_TASK_STACK_SIZE configMINIMAL_STACK_SIZE + 1024 #define A_INCLINOMETER_TASK_PRIORITY tskIDLE_PRIORITY + 5 #define A_INCLINOMETER_TASK_STACK_SIZE configMINIMAL_STACK_SIZE + 512 #define B_INCLINOMETER_TASK_PRIORITY tskIDLE_PRIORITY + 5 @@ -42,10 +43,16 @@ #define MODE_REMOTE 3 #define MAIN_MENU_REMOTE_NAME "REMOTE" -#define WIFI_SSID "ATE0005" -#define WIFI_PASS "repairlab" -#define WIFI_CHANNEL 1 -#define MAX_STA_CONNECTION 4 +#define WIFI_AP_SSID "ATE0005" +#define WIFI_AP_PASS "repairlab" +#define WIFI_AP_CHANNEL 1 +#define MAX_AP_CONNECTION 4 + +#define WIFI_STA_SSID "rick_professor" +#define WIFI_STA_PASS "repairlab" +#define WIFI_STA_MAXIMUM_RETRY 10 + +#define SYSLOG_IP "77.51.201.8" #define I2C_SCL_GPIO GPIO_NUM_22 #define I2C_SDA_GPIO GPIO_NUM_21 diff --git a/version.txt b/version.txt index afaf360..1cc5f65 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.0 \ No newline at end of file +1.1.0 \ No newline at end of file