temp
This commit is contained in:
parent
b6211d0546
commit
7748793546
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"condition_variable": "cpp",
|
||||||
|
"future": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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)
|
||||||
|
|
||||||
# 查找src目录下所有cpp文件
|
# 查找src目录下所有cpp文件
|
||||||
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})
|
||||||
|
|
||||||
# 链接需要的库,示例链接pthread和toml11(假设toml11是头文件库,不用链接)
|
# 链接需要的库
|
||||||
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是以静态库形式存在,或有其他依赖库,记得这里添加
|
||||||
|
|||||||
@ -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
|
||||||
29
include/LedMqttSubscriber.h
Normal file
29
include/LedMqttSubscriber.h
Normal 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
71
src/LedMqttSubscriber.cpp
Normal 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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user