相关文档

  1. 阿里云(C语言接入 阿里云Mqtt微消息队列):
  2. https://help.aliyun.com/document_detail/146611.html?spm=a2c4g.11186623.6.1007.5c5725932sKjsl
  3. https://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/index.html?spm=a2c4g.11186623.2.8.72a8379bBBeLUj
  4. CSDN
  5. https://blog.csdn.net/qq_43260665/article/details/88370100

1:下载一个Paho C库

  1. 1:先进入一个文件夹
  2. cd /opt/iot
  3. 2:使用git命令下载库
  4. git clone https://github.com.cnpmjs.org/eclipse/paho.mqtt.c.git
  5. 3:进行编译安装:
  6. 1cd paho.mqtt.c
  7. //编译
  8. 2make
  9. //安装
  10. 3sudo make install

2:查看动态库

  1. cd build/output
  2. 存在以下文件
  3. paho-mqtt3a 一般实际开发中就是使用这个,a表示的是异步消息推送(asynchronous)。
  4. paho-mqtt3as as表示的是 异步+加密(asynchronous+OpenSSL)。
  5. paho-mqtt3c c 表示的应该是同步(Synchronize),一般性能较差,是发送+等待模式。
  6. paho-mqtt3cs cs表示的是 同步+加密(Synchronize+OpenSSL)。
  7. samples中还会有一些示例代码;
  8. MQTTAsync_publish MQTTAsync_subscribe
  9. MQTTClient_publish MQTTClient_subscribe MQTTClient_publish_async
  10. paho_c_pub paho_cs_sub
  11. paho_cs_pub paho_c_sub

3:编写 发布端(客户端)代码 mqtt_publish.c

vim

  1. ////引用相关库
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "MQTTClient.h"
  6. //定义常量
  7. #define ADDRESS "tcp://post-cn-nif23a2s52f.mqtt.aliyuncs.com:1883"
  8. #define CLIENTID "ExampleClientPub"
  9. #define TOPIC "topic_mqtt"
  10. //#define PAYLOAD "Hello World!"
  11. #define QOS 1
  12. #define TIMEOUT 10000
  13. //以下为 发布者客户端
  14. int main(int argc, char* argv[])
  15. {
  16. MQTTClient client;
  17. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  18. MQTTClient_message pubmsg = MQTTClient_message_initializer;
  19. MQTTClient_deliveryToken token;
  20. //临时参数
  21. int rc;
  22. //创建一个发布者 客服端
  23. MQTTClient_create(&client, ADDRESS, CLIENTID,MQTTCLIENT_PERSISTENCE_NONE, NULL);
  24. //设置相关的连接参数
  25. conn_opts.keepAliveInterval = 60;
  26. conn_opts.cleansession = 1;
  27. //使用客户端连接服务端
  28. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  29. {
  30. printf("Failed to connect, return code %d\n", rc);
  31. exit(EXIT_FAILURE);
  32. }
  33. //设置发送的消息
  34. pubmsg.qos = QOS;
  35. pubmsg.retained = 0;
  36. char buf[1024];
  37. while(1){
  38. printf("enter the message you want to send\n");
  39. fgets(buf,sizeof(buf),stdin);
  40. pubmsg.payload = (void *)buf;
  41. pubmsg.payloadlen = strlen(buf);
  42. MQTTClient_publishMessage(client,TOPIC,&pubmsg,&token);
  43. printf("waiting for %d seconds for publication of %s on topic %s for client with CLIENTID :%s\n",TIMEOUT/1000,buf,TOPIC,CLIENTID);
  44. int rv=MQTTClient_waitForCompletion(client,token,TIMEOUT);
  45. printf("Message with delivery token %d delivered\n",rv);
  46. //用于测试
  47. printf("%s\n",buf);
  48. sleep(3);
  49. }
  50. }

4:进行编译 运行 mqtt_publish.c

  1. 编译:gcc mqtt_publish.c
  2. -L(大写的L -I(大写的I
  3. 查找对应的包:-o mqtt_publish -lpaho-mqtt3c -L ./opt/iot/paho.mqtt.c/ -I ./opt/iot/paho.mqtt.c/src/
  4. 说明 :如果没有将头文件和动态库放到编译器默认寻找的位置 需要在编译时指定包
  5. 进行编译:
  6. gcc mqtt_publish.c -o mqtt_publish -lpaho-mqtt3c -L ./opt/iot/paho.mqtt.c/ -I ./opt/iot/paho.mqtt.c/src/
  7. 进行运行
  8. ./mqtt_publish
  9. 返回: mqtt的客户端还没运行
  10. MQTTClient_connect failure:Transport endpoint is not connected

5:编写 订阅端(客户端)代码 mqtt_subscribe.c

  1. ////引用相关库
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "MQTTClient.h"
  6. //定义常量
  7. #define ADDRESS "tcp://post-cn-nif23a2s52f.mqtt.aliyuncs.com:1883"
  8. #define CLIENTID "ExampleClientSub"
  9. #define TOPIC "topic_mqtt"
  10. #define PAYLOAD "Hello World!"
  11. #define QOS 1
  12. #define TIMEOUT 10000L
  13. volatile MQTTClient_deliveryToken token;
  14. void delivered(void *context, MQTTClient_deliveryToken dt)
  15. {
  16. printf("Message with token value %d delivery confirmed\n", dt);
  17. token = dt;
  18. }
  19. int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
  20. {
  21. //进行打印
  22. int i;
  23. char* payloadptr;
  24. printf("Message arrived\n");
  25. printf(" topic: %s\n", topicName);
  26. printf(" message: ");
  27. payloadptr = message->payload;
  28. for(i=0; i<message->payloadlen; i++)
  29. {
  30. putchar(*payloadptr++);
  31. }
  32. putchar('\n');
  33. MQTTClient_freeMessage(&message);
  34. MQTTClient_free(topicName);
  35. return 1;
  36. }
  37. void connlost(void *context, char *cause)
  38. {
  39. printf("\nConnection lost\n");
  40. printf(" cause: %s\n", cause);
  41. }
  42. int main(int argc, char* argv[])
  43. {
  44. MQTTClient client;
  45. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  46. //临时参数
  47. int rc;
  48. int ch;
  49. //创建一个订阅者 客服端
  50. MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  51. //设置相关的连接参数
  52. conn_opts.keepAliveInterval = 20;
  53. conn_opts.cleansession = 1;
  54. //设置为订阅者 客服端 异步消费消息
  55. MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
  56. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  57. {
  58. printf("Failed to connect, return code %d\n", rc);
  59. exit(EXIT_FAILURE);
  60. }
  61. printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
  62. "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
  63. //订阅者 客服端订阅消息
  64. MQTTClient_subscribe(client, TOPIC, QOS);
  65. do
  66. {
  67. q = getchar();
  68. } while(ch!='Q' && ch != 'q');
  69. //断开客户端连接
  70. MQTTClient_disconnect(client, 10000);
  71. //销毁客户端连接
  72. MQTTClient_destroy(&client);
  73. return rc;
  74. }

6:进行编译 运行 mqtt_subscribe.c

  1. 先打开另一个客户端
  2. 然后进入该文件夹 /opt/iot/paho.mqtt.c/build/code
  3. 进行编译:
  4. gcc mqtt_subscribe.c -o mqtt_subscribe -lpaho-mqtt3c -L ./opt/iot/paho.mqtt.c/build/output/
  5. 进行运行:
  6. ./mqtt_subscribe
  7. 报错:
  8. MQTTClient_connect failure:Transport endpoint is not connected
  9. 先去阿里云服务器管理 放开1883端口

7:测试

  1. 在发布端(客户端)进行消息的发送
  2. 在订阅端(客户端) 进行消息的接收
  3. 查看相关的打印 并查看阿里云平台的mqtt