基于C++在windows下使用mosquitto实现MQTT发布客户端

1、MOSQUITTO简介

  Mosquitto​是一个开源的 MQTT(Message Queuing Telemetry Transport)消息代理服务器,由 Eclipse Foundation 开发和维护。它采用 C/C++ 编写,以轻量级、高性能著称。Mosquitto 因其简单、稳定、轻量的特点,成为物联网领域最受欢迎的 MQTT 代理之一,特别适合中小型项目和资源受限环境。

2、MOSQUITTO环境部署

  Mosquitto​在windows下支持安装版本x86\x64均支持,下载地址为:https://mosquitto.org/download/,下载安装完成后在编译器中配置所需的头文件环境及库环境(mosquitto.lib),配置完成后即可进行Demo测试。

3、MOSQUITTO发布客户端搭建

  初始化MQTT代码

 1 #include <mosquitto.h>
 2 #define MQTT_USER_NAME "admin"
 3 #define MQTT_PASS_WORD "Admin12345"
 4 #define MQTT_SERVER_IP_ADDRESS "10.50.2.88"
 5 #define MQTT_SERVER_PORT 1883
 6 #define PUBLISH_JSON_DATA_ORG "4321"
 7 #define GR4_HWCS_MQTT_TOPIC "DATA_PUBLISH_DIRECT_ANDON/4321/GYYKQ/GR4_HWCS"
 8 static struct mosquitto* mosq;
 9 static bool mqtt_init_sign;
10 int init_mqtt(const std::string username, const std::string passwd, const std::string hostname, const int port)
11 {
12     mosquitto_lib_init();
13 
14     mosq = mosquitto_new(nullptr, true, nullptr);//第一个形参指定为固定值后,派生类会出现发布数据失败的情况,因为不同的派生类共用一个client_id
15     if (!mosq) {
16         std::cerr << "Failed to create mosquitto instance!" << std::endl;
17         return 0;
18     }
19 
20     const char* usrname = username.c_str();
21     const char* password = passwd.c_str();
22 
23     int rc = mosquitto_username_pw_set(mosq, usrname, password);
24     if (rc != MOSQ_ERR_SUCCESS) {
25         std::cerr << "Failed to set username/password: " << mosquitto_strerror(rc) << std::endl;
26         mosquitto_destroy(mosq);
27         mosquitto_lib_cleanup();
28         return 0;
29     }
30 
31     rc = mosquitto_connect(mosq, hostname.c_str(), port, 60);
32     if (rc != MOSQ_ERR_SUCCESS) {
33         std::cerr << "Connection failed: " << mosquitto_strerror(rc) << std::endl;
34 
35         std::cout << "Trying without authentication..." << std::endl;
36         mosquitto_username_pw_set(mosq, nullptr, nullptr);
37 
38         rc = mosquitto_connect(mosq, hostname.c_str(), port, 60);
39         if (rc != MOSQ_ERR_SUCCESS) {
40             std::cerr << "Still failed: " << mosquitto_strerror(rc) << std::endl;
41             mosquitto_destroy(mosq);
42             mosquitto_lib_cleanup();
43             return 0;
44         }
45     }
46 
47     //std::cout << "Connected to MQTT broker!" << std::endl;
48     mosquitto_loop_start(mosq);
49     mqtt_init_sign = true;
50     return 1;
51 }

  MQTT发布数据

 1 int publish_data_to_server(const std::string topic, const std::string message)
 2 {
 3     if (!mqtt_init_sign)
 4     {
 5         std::cout << "mqtt not init, can not send mqtt msg" << std::endl;
 6         return 0;
 7     }
 8 
 9     int rc = mosquitto_publish(mosq,
10         nullptr,
11         topic.c_str(),
12         message.length(),
13         message.c_str(),
14         0,    // QoS 0
15         false);
16 
17     if (rc == MOSQ_ERR_SUCCESS) {
18         /*std::cout << "Message published successfully!" << std::endl;
19         std::cout << "Topic: " << topic << std::endl;
20         std::cout << "Message: " << message << std::endl;*/
21         ;
22     }
23     else {
24         std::cerr << "Publish failed: " << mosquitto_strerror(rc) << std::endl;
25         return 0;
26     }
27     return 1;
28 }

  测试用例

1 int main()
2 {
3     init_mqtt(MQTT_USER_NAME, MQTT_PASS_WORD, MQTT_SERVER_IP_ADDRESS, MQTT_SERVER_PORT);
4     publish_data_to_server(GR4_HWCS_MQTT_TOPIC,"test message");
5 }
posted @ 2026-01-19 17:42  左边的翼  阅读(0)  评论(0)    收藏  举报