MQTT
下载mqtt C语言库与编译
首先我们要下载支持MQTT的C语言库,直接使用git命令拉取:
git clone https://github.com/eclipse/paho.mqtt.c.git
API文档介绍:http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/index.html
然后我们进入下载好的源码目录,使用make命令编译得到我们需要的库文件
然后我们可以在paho.mqtt.c/build/output下可以找到如下的输出文件:
然后我们使用make install
则是将生成的库文件移动到系统路径之下。
在paho.mqtt.c/src/samples目录下有一些示例:
因为我们要用到动态库文件paho-mqtt3c.so
,使用在编译的时候要加上参数-lpaho-mqtt3c
,我们编译,运行
示例代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <unistd.h> 5 #include "MQTTClient.h" 6 7 #define MQTT_Uri "tcp://39.96.35.207:1883" // MQTT服务器的地址和端口号 8 #define ClientId "ubuntu16" // ClientId需要唯一 9 #define UserName "ubuntu16" // 用户名 10 #define PassWord "123456" // 用户名对应的密码 11 12 // 失去连接回调函数 13 void connect_lost(void *context, char *cause) 14 { 15 printf("Connection lost,The reason: %s \n",cause); 16 } 17 18 // 收到主题信息回调函数 19 int message_arrived(void *context, char *topicName, int topicLen, MQTTClient_message *message) 20 { 21 printf("Receive topic: %s, message data: \n", topicName); 22 printf("%.*s\n", message->payloadlen, (char*)message->payload); 23 MQTTClient_freeMessage(&message); 24 MQTTClient_free(topicName); 25 return 1; 26 } 27 28 // 主题发布成功回调函数 29 void delivery_complete(void *context, MQTTClient_deliveryToken dt) 30 { 31 printf("publish topic success,token = %d \n", dt); 32 } 33 34 int main(int argc, char* argv[]) 35 { 36 // 1、定义一个MQTT客户端结构体指针 37 MQTTClient client; 38 39 // 2、创建一个MQTT客户端 40 int rc; 41 if ((rc = MQTTClient_create(&client, MQTT_Uri, ClientId, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS) 42 { 43 printf("Failed to create client, return code %d\n", rc); 44 exit(EXIT_FAILURE); 45 goto exit; 46 } 47 48 // 3、创建一个MQTT连接配置结构体,并配置其参数 49 MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; 50 conn_opts.username = UserName; // 用户名 51 conn_opts.password = PassWord; // 用户名对应的密码 52 conn_opts.keepAliveInterval = 60; // 心跳时间 53 conn_opts.cleansession = 1; // 清除会话 54 55 // 4、设置MQTT连接时的回调函数 56 if ((rc = MQTTClient_setCallbacks(client, NULL, connect_lost, message_arrived, delivery_complete)) != MQTTCLIENT_SUCCESS) 57 { 58 printf("Failed to set callbacks, return code %d\n", rc); 59 rc = EXIT_FAILURE; 60 goto destroy_exit; 61 } 62 63 // 5、开始连接到MQTT服务器 64 if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) 65 { 66 printf("Failed to connect, return code %d\n", rc); 67 exit(EXIT_FAILURE); 68 goto destroy_exit; 69 } 70 71 // 6、定义一个主题消息存储结构体 72 MQTTClient_message pubmsg = MQTTClient_message_initializer; 73 char mag_data[] = "I am Ubuntu16."; 74 pubmsg.payload = mag_data; 75 pubmsg.payloadlen = (int)strlen(mag_data); 76 pubmsg.qos = 1; // qos等级为1 77 pubmsg.retained = 0; // 服务器不保留消息 78 MQTTClient_deliveryToken token; // 标记MQTT消息的值,用来检查消息是否发送成功 79 80 // 7、发布主题信息 81 if ((rc = MQTTClient_publishMessage(client, "ubuntu16_publish", &pubmsg, &token)) != MQTTCLIENT_SUCCESS) 82 { 83 printf("Failed to publish message, return code %d\n", rc); 84 exit(EXIT_FAILURE); 85 } 86 87 // 8、订阅主题 88 if ((rc = MQTTClient_subscribe(client, "ubuntu16_subscribe", 1)) != MQTTCLIENT_SUCCESS) 89 { 90 printf("Failed to subscribe, return code %d\n", rc); 91 rc = EXIT_FAILURE; 92 } 93 94 // 9、等待输入'Q'或'q'退出 95 printf("Press Q or q + <Enter> to quit\n\n"); 96 int ch; 97 do 98 { 99 ch = getchar(); 100 } while (ch!='Q' && ch != 'q'); 101 102 if ((rc = MQTTClient_unsubscribe(client, "ubuntu16_subscribe")) != MQTTCLIENT_SUCCESS) 103 { 104 printf("Failed to unsubscribe, return code %d\n", rc); 105 rc = EXIT_FAILURE; 106 } 107 108 // 10、断开连接 109 if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS) 110 { 111 printf("Failed to disconnect, return code %d\n", rc); 112 rc = EXIT_FAILURE; 113 } 114 115 destroy_exit: 116 MQTTClient_destroy(&client); 117 exit: 118 return rc; 119 }
https://blog.csdn.net/qq_38113006/article/details/105665658
https://blog.csdn.net/vincent_yuan89/article/details/84306001