Initial commit for ESP32
This commit is contained in:
7
esp32/main/CMakeLists.txt
Normal file
7
esp32/main/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
idf_component_register(
|
||||
SRCS "main.cpp" "protocol.cpp"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES nvs_flash esp_wifi esp_netif freertos log cjson
|
||||
)
|
||||
# добавляем кастомный Kconfig
|
||||
set(COMPONENT_KCONFIG "Kconfig")
|
||||
27
esp32/main/Kconfig
Normal file
27
esp32/main/Kconfig
Normal file
@@ -0,0 +1,27 @@
|
||||
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"
|
||||
|
||||
config SERVER_HOST
|
||||
string "Backend Host"
|
||||
default "192.168.1.100"
|
||||
|
||||
config SERVER_PORT
|
||||
int "Backend Port"
|
||||
default 8080
|
||||
|
||||
config DEVICE_ID
|
||||
string "Device ID"
|
||||
default "esp32_fish_01"
|
||||
|
||||
config PROTOCOL_VERSION
|
||||
int "Protocol Version"
|
||||
default 1
|
||||
|
||||
endmenu
|
||||
83
esp32/main/main.cpp
Normal file
83
esp32/main/main.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#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 <string>
|
||||
|
||||
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, остановить таймеры, освободить память и т.д.
|
||||
}
|
||||
52
esp32/main/protocol.cpp
Normal file
52
esp32/main/protocol.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "protocol.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
cJSON* protocol_message_to_json(const ProtocolMessage* msg) {
|
||||
cJSON* root = cJSON_CreateObject();
|
||||
|
||||
cJSON_AddNumberToObject(root, "v", msg->v);
|
||||
cJSON_AddStringToObject(root, "id", msg->id.c_str());
|
||||
cJSON_AddStringToObject(root, "type", msg->type.c_str());
|
||||
cJSON_AddNumberToObject(root, "ts", msg->ts);
|
||||
cJSON_AddStringToObject(root, "deviceId", msg->deviceId.c_str());
|
||||
|
||||
cJSON* payload_obj = nullptr;
|
||||
if (msg->payload) {
|
||||
payload_obj = cJSON_Duplicate(msg->payload, 1); // deep copy
|
||||
} else {
|
||||
payload_obj = cJSON_CreateObject();
|
||||
}
|
||||
cJSON_AddItemToObject(root, "payload", payload_obj);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
ProtocolMessage protocol_message_from_json(const cJSON* root) {
|
||||
ProtocolMessage msg;
|
||||
|
||||
const cJSON* item = nullptr;
|
||||
|
||||
item = cJSON_GetObjectItem(root, "v");
|
||||
if (item && cJSON_IsNumber(item)) msg.v = item->valueint;
|
||||
|
||||
item = cJSON_GetObjectItem(root, "id");
|
||||
if (item && cJSON_IsString(item)) msg.id = item->valuestring;
|
||||
|
||||
item = cJSON_GetObjectItem(root, "type");
|
||||
if (item && cJSON_IsString(item)) msg.type = item->valuestring;
|
||||
|
||||
item = cJSON_GetObjectItem(root, "ts");
|
||||
if (item && cJSON_IsNumber(item)) msg.ts = item->valuedouble; // uint64_t
|
||||
|
||||
item = cJSON_GetObjectItem(root, "deviceId");
|
||||
if (item && cJSON_IsString(item)) msg.deviceId = item->valuestring;
|
||||
|
||||
item = cJSON_GetObjectItem(root, "payload");
|
||||
if (item && cJSON_IsObject(item)) {
|
||||
msg.payload = cJSON_Duplicate(item, 1); // deep copy
|
||||
} else {
|
||||
msg.payload = cJSON_CreateObject();
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
18
esp32/main/protocol.h
Normal file
18
esp32/main/protocol.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include "cJSON.h"
|
||||
#include <string>
|
||||
|
||||
struct ProtocolMessage {
|
||||
int v;
|
||||
std::string id;
|
||||
std::string type;
|
||||
uint64_t ts;
|
||||
std::string deviceId; // C++ строка
|
||||
cJSON* payload;
|
||||
};
|
||||
|
||||
// Создание JSON из структуры
|
||||
cJSON* protocol_message_to_json(const ProtocolMessage *msg);
|
||||
|
||||
// Разбор JSON в структуру
|
||||
int protocol_json_to_message(const char *json_str, ProtocolMessage *msg);
|
||||
Reference in New Issue
Block a user