qt使用MQTT协议连接阿里云demo

qt使用Mqtt协议连接阿里云。

在配置好qt关于MQTT的环境之后,主要就是根据MQTT的连接参数进行连接即可。

环境配置推荐链接QT编译并部署QtMqtt相关环境+跑测demo【超详细教程】_mqtt qt开发教程-CSDN博客

连接核心代码,主要就是根据阿里云的MQTT相关参数进行配置实现连接。

复制代码
void MqttClient::mqtt_init() {
    // 定义参数
    QString productKey = "yours";
    QString deviceName = "yours";
    QString deviceSecret = "yours";
    

    QMap<QString, QString> params;
    params.insert("productKey", productKey);
    params.insert("deviceName", deviceName);
    params.insert("clientId", clientId);
    QString timestamp = QString::number(QDateTime::currentMSecsSinceEpoch());
    params.insert("timestamp", timestamp);

    // 生成 MQTT Broker 地址
    QString host_url = QString("iot-06z00ies461746g.mqtt.iothub.aliyuncs.com");

   
    QString client_id_str = "yours";

    // 生成用户名
    QString user_name = QString("%1&%2").arg(deviceName).arg(productKey);

    // 生成密码
   // QString password = sign(params, deviceSecret);
    QString password = "yours";
    // 设置 MQTT 客户端参数
    client->setHostname(host_url); // 设置主机名
    client->setPort(1883);         // 设置端口号
    client->setClientId(client_id_str); // 设置客户端 ID
    client->setUsername(user_name);     // 设置用户名
    client->setPassword(password.toUtf8()); // 设置密码

    qDebug() << "Connecting to MQTT broker:" << host_url;
    qDebug() << "Client ID:" << client_id_str;
    qDebug() << "Username:" << user_name;
    qDebug() << "Password:" << password;

    // 连接到 MQTT Broker
    client->connectToHost();
}
相关推荐
Wenweno0o1 天前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
chenjingming6661 天前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
cch89181 天前
Python主流框架全解析
开发语言·python
不爱吃炸鸡柳1 天前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发1 天前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
Momentary_SixthSense1 天前
设计模式之工厂模式
java·开发语言·设计模式
‎ദ്ദിᵔ.˛.ᵔ₎1 天前
STL 栈 队列
开发语言·c++
勿忘,瞬间1 天前
数据结构—顺序表
java·开发语言
张張4081 天前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_423533991 天前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python