update project
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -26,4 +26,7 @@ __pycache__/
|
|||||||
|
|
||||||
# gitignore for ESP-IDF
|
# gitignore for ESP-IDF
|
||||||
sdkconfig
|
sdkconfig
|
||||||
build/
|
build/
|
||||||
|
sdkconfig
|
||||||
|
sdkconfig.old
|
||||||
|
Kconfig
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
idf_component_register(
|
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 "."
|
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
|
# добавляем кастомный Kconfig
|
||||||
set(COMPONENT_KCONFIG "Kconfig")
|
set(COMPONENT_KCONFIG "Kconfig")
|
||||||
@@ -2,15 +2,12 @@ menu "IoT Fish Config"
|
|||||||
|
|
||||||
config WIFI_SSID
|
config WIFI_SSID
|
||||||
string "Wi-Fi SSID"
|
string "Wi-Fi SSID"
|
||||||
default "your_wifi_ssid"
|
|
||||||
|
|
||||||
config WIFI_PASS
|
config WIFI_PASS
|
||||||
string "Wi-Fi Password"
|
string "Wi-Fi password"
|
||||||
default "your_wifi_password"
|
|
||||||
|
|
||||||
config SERVER_HOST
|
config SERVER_HOST
|
||||||
string "Backend Host"
|
string "Backend Host"
|
||||||
default "192.168.1.100"
|
|
||||||
|
|
||||||
config SERVER_PORT
|
config SERVER_PORT
|
||||||
int "Backend Port"
|
int "Backend Port"
|
||||||
|
|||||||
@@ -1,83 +1,20 @@
|
|||||||
#include "protocol.h"
|
#include "system_init.h"
|
||||||
#include "cJSON.h"
|
#include "protocol_test.h"
|
||||||
#include "esp_log.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "nvs_flash.h"
|
#include "freertos/projdefs.h"
|
||||||
#include "esp_wifi.h"
|
#include "freertos/task.h"
|
||||||
#include "esp_event.h"
|
|
||||||
#include "esp_netif.h"
|
|
||||||
#include "sdkconfig.h" // Kconfig значения
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
static const char* TAG = "iot_fish";
|
static const char* TAG = "iot_fish";
|
||||||
|
|
||||||
// forward declarations
|
extern "C" void app_main(void)
|
||||||
void init();
|
{
|
||||||
void doTests();
|
system_init();
|
||||||
void finalize();
|
|
||||||
|
|
||||||
extern "C" void app_main(void) {
|
for (int i=0; i<10; i++)
|
||||||
init();
|
{
|
||||||
doTests();
|
protocol_run_tests();
|
||||||
finalize();
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||||
}
|
|
||||||
|
|
||||||
// --- Реализация ---
|
|
||||||
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
|
system_finalize();
|
||||||
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, остановить таймеры, освободить память и т.д.
|
|
||||||
}
|
}
|
||||||
32
esp32/main/protocol_test.cpp
Normal file
32
esp32/main/protocol_test.cpp
Normal file
@@ -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);
|
||||||
|
}
|
||||||
3
esp32/main/protocol_test.h
Normal file
3
esp32/main/protocol_test.h
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
void protocol_run_tests();
|
||||||
25
esp32/main/system_init.cpp
Normal file
25
esp32/main/system_init.cpp
Normal file
@@ -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...");
|
||||||
|
}
|
||||||
4
esp32/main/system_init.h
Normal file
4
esp32/main/system_init.h
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
void system_init();
|
||||||
|
void system_finalize();
|
||||||
28
esp32/main/time_sync.cpp
Normal file
28
esp32/main/time_sync.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include "time_sync.h"
|
||||||
|
#include "esp_sntp.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include <time.h>
|
||||||
|
#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);
|
||||||
|
}
|
||||||
|
}
|
||||||
3
esp32/main/time_sync.h
Normal file
3
esp32/main/time_sync.h
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
void init_time();
|
||||||
72
esp32/main/wifi_manager.cpp
Normal file
72
esp32/main/wifi_manager.cpp
Normal file
@@ -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<char*>(wifi_config.sta.ssid), s_ssid);
|
||||||
|
strcpy(reinterpret_cast<char*>(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;};
|
||||||
4
esp32/main/wifi_manager.h
Normal file
4
esp32/main/wifi_manager.h
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/event_groups.h"
|
||||||
|
void init_wifi(const char* ssid, const char* pass);
|
||||||
Reference in New Issue
Block a user