#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 static const char* TAG = "iot_fish"; // forward declarations void init(); void doTests(); void finalize(); 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(); } 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, остановить таймеры, освободить память и т.д. }