From 1681a694d34cfced2353e3b1d108c5defe6c76f2 Mon Sep 17 00:00:00 2001 From: eugene-admin Date: Fri, 3 Apr 2026 23:28:02 +0300 Subject: [PATCH] update project --- .gitignore | 5 +- esp32/main/CMakeLists.txt | 4 +- esp32/main/Kconfig | 5 +- esp32/main/main.cpp | 89 ++++++------------------------------ esp32/main/protocol_test.cpp | 32 +++++++++++++ esp32/main/protocol_test.h | 3 ++ esp32/main/system_init.cpp | 25 ++++++++++ esp32/main/system_init.h | 4 ++ esp32/main/time_sync.cpp | 28 ++++++++++++ esp32/main/time_sync.h | 3 ++ esp32/main/wifi_manager.cpp | 72 +++++++++++++++++++++++++++++ esp32/main/wifi_manager.h | 4 ++ 12 files changed, 191 insertions(+), 83 deletions(-) create mode 100644 esp32/main/protocol_test.cpp create mode 100644 esp32/main/protocol_test.h create mode 100644 esp32/main/system_init.cpp create mode 100644 esp32/main/system_init.h create mode 100644 esp32/main/time_sync.cpp create mode 100644 esp32/main/time_sync.h create mode 100644 esp32/main/wifi_manager.cpp create mode 100644 esp32/main/wifi_manager.h diff --git a/.gitignore b/.gitignore index 533428d..eca1cbd 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,7 @@ __pycache__/ # gitignore for ESP-IDF sdkconfig -build/ \ No newline at end of file +build/ +sdkconfig +sdkconfig.old +Kconfig \ No newline at end of file diff --git a/esp32/main/CMakeLists.txt b/esp32/main/CMakeLists.txt index 417eac7..2e8421e 100644 --- a/esp32/main/CMakeLists.txt +++ b/esp32/main/CMakeLists.txt @@ -1,7 +1,7 @@ idf_component_register( - SRCS "main.cpp" "protocol.cpp" + SRCS "main.cpp" "protocol.cpp" "protocol_test.cpp" "wifi_manager.cpp" "system_init.cpp" "time_sync.cpp" INCLUDE_DIRS "." - REQUIRES nvs_flash esp_wifi esp_netif freertos log cjson + REQUIRES nvs_flash esp_wifi esp_netif freertos log cjson esp_timer ) # добавляем кастомный Kconfig set(COMPONENT_KCONFIG "Kconfig") \ No newline at end of file diff --git a/esp32/main/Kconfig b/esp32/main/Kconfig index e5a803e..d3bcdfb 100644 --- a/esp32/main/Kconfig +++ b/esp32/main/Kconfig @@ -2,15 +2,12 @@ menu "IoT Fish Config" config WIFI_SSID string "Wi-Fi SSID" - default "your_wifi_ssid" config WIFI_PASS - string "Wi-Fi Password" - default "your_wifi_password" + string "Wi-Fi password" config SERVER_HOST string "Backend Host" - default "192.168.1.100" config SERVER_PORT int "Backend Port" diff --git a/esp32/main/main.cpp b/esp32/main/main.cpp index 2d024c6..9902b36 100644 --- a/esp32/main/main.cpp +++ b/esp32/main/main.cpp @@ -1,83 +1,20 @@ -#include "protocol.h" -#include "cJSON.h" -#include "esp_log.h" -#include "nvs_flash.h" -#include "esp_wifi.h" -#include "esp_event.h" -#include "esp_netif.h" -#include "sdkconfig.h" // Kconfig значения -#include +#include "system_init.h" +#include "protocol_test.h" +#include "freertos/FreeRTOS.h" +#include "freertos/projdefs.h" +#include "freertos/task.h" static const char* TAG = "iot_fish"; -// forward declarations -void init(); -void doTests(); -void finalize(); +extern "C" void app_main(void) +{ + system_init(); -extern "C" void app_main(void) { - init(); - doTests(); - finalize(); -} - -// --- Реализация --- -void init() { - ESP_LOGI(TAG, "Initializing system..."); - - // NVS - esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); + for (int i=0; i<10; i++) + { + protocol_run_tests(); + vTaskDelay(pdMS_TO_TICKS(1000)); } - ESP_ERROR_CHECK(ret); - // TCP/IP stack + default event loop - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - - // Wi-Fi init - esp_netif_create_default_wifi_sta(); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - - // Настройки Wi-Fi через Kconfig (menuconfig) - wifi_config_t wifi_config = {}; - strncpy((char*)wifi_config.sta.ssid, CONFIG_WIFI_SSID, sizeof(wifi_config.sta.ssid)); - strncpy((char*)wifi_config.sta.password, CONFIG_WIFI_PASS, sizeof(wifi_config.sta.password)); - - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); - ESP_ERROR_CHECK(esp_wifi_start()); - - ESP_LOGI(TAG, "Wi-Fi started, connecting to %s...", CONFIG_WIFI_SSID); -} - -void doTests() { - ESP_LOGI(TAG, "Running protocol test..."); - - // пример ProtocolMessage - ProtocolMessage msg; - msg.v = 1; - msg.id = "msg-001"; - msg.type = "command"; - msg.ts = 1234567890; - msg.deviceId = "esp32-01"; - msg.payload = cJSON_CreateObject(); - cJSON_AddStringToObject(msg.payload, "command", "ping"); - - cJSON* json = protocol_message_to_json(&msg); - char* json_str = cJSON_Print(json); - ESP_LOGI(TAG, "ProtocolMessage JSON:\n%s", json_str); - - // Очистка - cJSON_Delete(json); - free(json_str); - cJSON_Delete(msg.payload); -} - -void finalize() { - ESP_LOGI(TAG, "Finalizing..."); - // здесь можно закрыть WebSocket, остановить таймеры, освободить память и т.д. + system_finalize(); } \ No newline at end of file diff --git a/esp32/main/protocol_test.cpp b/esp32/main/protocol_test.cpp new file mode 100644 index 0000000..cba1918 --- /dev/null +++ b/esp32/main/protocol_test.cpp @@ -0,0 +1,32 @@ +#include "protocol_test.h" + +#include "protocol.h" +#include "cJSON.h" +#include "esp_log.h" +#include "esp_timer.h" + +static const char* TAG = "protocol_test"; + +void protocol_run_tests() +{ + ESP_LOGI(TAG, "Running protocol test..."); + + ProtocolMessage msg; + msg.v = 1; + msg.id = "msg-001"; + msg.type = "command"; + msg.deviceId = "esp32-01"; + msg.ts = esp_timer_get_time(); + + msg.payload = cJSON_CreateObject(); + cJSON_AddStringToObject(msg.payload, "command", "ping"); + + cJSON* json = protocol_message_to_json(&msg); + char* json_str = cJSON_Print(json); + + ESP_LOGI(TAG, "ProtocolMessage JSON:\n%s", json_str); + + cJSON_Delete(json); + free(json_str); + cJSON_Delete(msg.payload); +} diff --git a/esp32/main/protocol_test.h b/esp32/main/protocol_test.h new file mode 100644 index 0000000..6101e43 --- /dev/null +++ b/esp32/main/protocol_test.h @@ -0,0 +1,3 @@ +#pragma once + +void protocol_run_tests(); \ No newline at end of file diff --git a/esp32/main/system_init.cpp b/esp32/main/system_init.cpp new file mode 100644 index 0000000..6fc13bd --- /dev/null +++ b/esp32/main/system_init.cpp @@ -0,0 +1,25 @@ +#include "system_init.h" +#include "wifi_manager.h" +#include "time_sync.h" +#include "sdkconfig.h" + +#include "esp_log.h" +#include "esp_event.h" +#include "esp_netif.h" + +static const char* TAG = "system"; + +void system_init() +{ + ESP_LOGI("SYSTEM", "Initializing system..."); + + init_wifi(CONFIG_WIFI_SSID, CONFIG_WIFI_PASS); // Запуск WiFI + + init_time(); // Установка времени + +} + +void system_finalize() +{ + ESP_LOGI(TAG, "Finalizing..."); +} \ No newline at end of file diff --git a/esp32/main/system_init.h b/esp32/main/system_init.h new file mode 100644 index 0000000..17e3830 --- /dev/null +++ b/esp32/main/system_init.h @@ -0,0 +1,4 @@ +#pragma once + +void system_init(); +void system_finalize(); \ No newline at end of file diff --git a/esp32/main/time_sync.cpp b/esp32/main/time_sync.cpp new file mode 100644 index 0000000..c112f99 --- /dev/null +++ b/esp32/main/time_sync.cpp @@ -0,0 +1,28 @@ +#include "time_sync.h" +#include "esp_sntp.h" +#include "esp_log.h" +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +void init_time() { + ESP_LOGI("TIME", "Initializing SNTP"); + + esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); + esp_sntp_setservername(0, "pool.ntp.org"); + esp_sntp_init(); + + setenv("TZ", "EET-2EEST,M3.5.0/3,M10.5.0/4", 1); + tzset(); + + time_t now = 0; + struct tm timeinfo{}; // ← ВАЖНО + + int retry = 0; + while (timeinfo.tm_year < (2020 - 1900) && retry++ < 10) { + ESP_LOGI("TIME", "Waiting for time..."); + vTaskDelay(2000 / portTICK_PERIOD_MS); + time(&now); + localtime_r(&now, &timeinfo); + } +} \ No newline at end of file diff --git a/esp32/main/time_sync.h b/esp32/main/time_sync.h new file mode 100644 index 0000000..ea44996 --- /dev/null +++ b/esp32/main/time_sync.h @@ -0,0 +1,3 @@ +#pragma once + +void init_time(); \ No newline at end of file diff --git a/esp32/main/wifi_manager.cpp b/esp32/main/wifi_manager.cpp new file mode 100644 index 0000000..27f29a5 --- /dev/null +++ b/esp32/main/wifi_manager.cpp @@ -0,0 +1,72 @@ +#include "wifi_manager.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "nvs_flash.h" + +static bool connected = false; + +static void wifi_event_handler(void* arg, + esp_event_base_t event_base, + int32_t event_id, + void* event_data) { + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { + ESP_LOGI("WIFI", "Disconnected, reconnecting..."); + connected = false; + esp_wifi_connect(); + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + connected = true; + + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + ESP_LOGI("WIFI", "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); + } +} + +static const char* s_ssid = nullptr; +static const char* s_pass = nullptr; + +void init_wifi(const char* ssid, const char* pass) { + + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK(ret); + + s_ssid = ssid; + s_pass = pass; + + esp_netif_init(); + esp_event_loop_create_default(); + esp_netif_create_default_wifi_sta(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + esp_wifi_init(&cfg); + + esp_event_handler_instance_t instance_any_id; + esp_event_handler_instance_t instance_got_ip; + + esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, + &wifi_event_handler, NULL, &instance_any_id); + esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, + &wifi_event_handler, NULL, &instance_got_ip); + + wifi_config_t wifi_config = {}; + + strcpy(reinterpret_cast(wifi_config.sta.ssid), s_ssid); + strcpy(reinterpret_cast(wifi_config.sta.password), s_pass); + + esp_wifi_set_mode(WIFI_MODE_STA); + esp_wifi_set_config(WIFI_IF_STA, &wifi_config); + esp_wifi_start(); + + while(!connected) { + vTaskDelay(500 / portTICK_PERIOD_MS); + } +} + +bool wifi_connected() {return connected;}; \ No newline at end of file diff --git a/esp32/main/wifi_manager.h b/esp32/main/wifi_manager.h new file mode 100644 index 0000000..c729b6c --- /dev/null +++ b/esp32/main/wifi_manager.h @@ -0,0 +1,4 @@ +#pragma once +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +void init_wifi(const char* ssid, const char* pass);