This commit is contained in:
cxh 2025-06-05 09:24:58 +08:00
parent b6211d0546
commit 7748793546
5 changed files with 131 additions and 3 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"files.associations": {
"condition_variable": "cpp",
"future": "cpp"
}
}

View File

@ -4,8 +4,11 @@ project(led_control)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
# #
include_directories(include) include_directories(${CMAKE_SOURCE_DIR}/include)
#
link_directories(${CMAKE_SOURCE_DIR}/lib)
# srccpp # srccpp
file(GLOB SOURCES "src/*.cpp") file(GLOB SOURCES "src/*.cpp")
@ -13,8 +16,21 @@ file(GLOB SOURCES "src/*.cpp")
# #
add_executable(led_app ${SOURCES}) add_executable(led_app ${SOURCES})
# pthreadtoml11toml11 #
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
target_link_libraries(led_app PRIVATE Threads::Threads) target_link_libraries(led_app PRIVATE Threads::Threads)
# paho mqtt
target_link_libraries(led_app
paho-mqttpp3
paho-mqtt3as
ssl
crypto
)
#
set_target_properties(led_app PROPERTIES
BUILD_RPATH ${CMAKE_SOURCE_DIR}/lib
)
# toml11 # toml11

View File

@ -1,3 +1,9 @@
[serial] [serial]
port = "/dev/ttyS0" port = "/dev/ttyS0"
baudrate = 115200 baudrate = 115200
[mqtt]
host = "192.168.4.196:11883"
username = "zxwl"
password = "zxwl1234@"
topic = "/zxwl/vehicle/V060001/info"
qos = 1

View File

@ -0,0 +1,29 @@
#pragma once
#include <string>
#include <mqtt/async_client.h>
class MqttClient
{
public:
MqttClient(const std::string &configPath);
bool start();
private:
std::string broker;
std::string clientId;
std::string username;
std::string password;
std::string topic;
int qos;
class Callback : public virtual mqtt::callback
{
public:
void message_arrived(mqtt::const_message_ptr msg) override;
};
mqtt::async_client *client = nullptr;
mqtt::connect_options connOpts;
Callback callback;
};

71
src/LedMqttSubscriber.cpp Normal file
View File

@ -0,0 +1,71 @@
#include "LedMqttSubscriber.h"
#include "toml.hpp"
#include <iostream>
#include <random>
#include <chrono>
#include <sstream>
// 生成随机clientId辅助函数
std::string generateRandomClientId(const std::string &prefix = "client_")
{
// 用时间戳+随机数生成一个简单的唯一字符串
auto now = std::chrono::system_clock::now().time_since_epoch().count();
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1000, 9999);
std::stringstream ss;
ss << prefix << now << "_" << dis(gen);
return ss.str();
}
MqttClient::MqttClient(const std::string &configPath)
{
try
{
auto data = toml::parse(configPath);
auto mqttConfig = toml::find(data, "mqtt");
broker = toml::find<std::string>(mqttConfig, "host");
clientId = generateRandomClientId();
username = toml::find_or<std::string>(mqttConfig, "username", "");
password = toml::find_or<std::string>(mqttConfig, "password", "");
topic = toml::find<std::string>(mqttConfig, "topic");
qos = toml::find_or<int>(mqttConfig, "qos", 1);
client = new mqtt::async_client(broker, clientId);
connOpts.set_clean_session(true);
if (!username.empty())
connOpts.set_user_name(username);
if (!password.empty())
connOpts.set_password(password);
client->set_callback(callback);
}
catch (const std::exception &e)
{
std::cerr << "加载 MQTT 配置失败: " << e.what() << std::endl;
}
}
bool MqttClient::start()
{
try
{
client->connect(connOpts)->wait();
client->subscribe(topic, qos)->wait();
std::cout << "已订阅主题:" << topic << std::endl;
return true;
}
catch (const mqtt::exception &e)
{
std::cerr << "MQTT 启动失败: " << e.what() << std::endl;
return false;
}
}
void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg)
{
std::cout << "接收到消息: " << msg->to_string() << std::endl;
}