当前位置:   article > 正文

pahomqtt_c++ demo qt环境编译_mqtt python c++diama

mqtt python c++diama
  1. #define MQTTCLIENT_QOS2 1
  2. #include <memory.h>
  3. #include "MQTTClient.h"
  4. #define DEFAULT_STACK_SIZE -1
  5. #include "linux.cpp"
  6. int arrivedcount = 0;
  7. void messageArrived(MQTT::MessageData& md)
  8. {
  9. MQTT::Message &message = md.message;
  10. printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n",
  11. ++arrivedcount, message.qos, message.retained, message.dup, message.id);
  12. printf("Payload %.*s\n", (int)message.payloadlen, (char*)message.payload);
  13. }
  14. int main(int argc, char* argv[])
  15. {
  16. IPStack ipstack = IPStack(); //new一个连接对象
  17. float version = 0.3;
  18. const char* topic = "mbed-sample"; //订阅的主题
  19. MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack); //new一个mqtt连接对象
  20. const char* hostname = "iot.eclipse.org"; //域名
  21. int port = 1883; //端口号
  22. printf("Connecting to %s:%d\n", hostname, port);
  23. int rc = ipstack.connect(hostname, port); //建立tcp连接到iot.eclipse.org服务器
  24. if (rc != 0)
  25. printf("rc from TCP connect is %d\n", rc);
  26. printf("MQTT connecting\n");
  27. //详见 MQTTPacket_connectData 定义
  28. MQTTPacket_connectData data = MQTTPacket_connectData_initializer; //初始化一个mqtt消息结构体,并且写入固定头
  29. data.MQTTVersion = 3; //定义mqtt版本
  30. data.clientID.cstring = (char*)"mbed-icraggs"; //定义客户端标识符
  31. //data.username.cstring; //定义用户名
  32. //data.password.cstring; //定义密码
  33. data.keepAliveInterval = 10; //定义时间间隔内
  34. data.cleansession = 1; //定义会话清除标识
  35. rc = client.connect(data); //建立mqtt连接
  36. if (rc != 0)
  37. printf("rc from MQTT connect is %d\n", rc);
  38. printf("MQTT connected\n");
  39. rc = client.subscribe(topic, MQTT::QOS2, messageArrived); //订阅主题 , 服务质量 , 以及传入接收函数指针
  40. if (rc != 0)
  41. printf("rc from MQTT subscribe is %d\n", rc);
  42. MQTT::Message message; //初始化mqtt信息变量message
  43. // QoS 0
  44. char buf[100];
  45. sprintf(buf, "Hello World! QoS 0 message from app version %f", version);
  46. message.qos = MQTT::QOS0; //定义服务质量
  47. message.retained = false; //是否任何新的订阅者都会收到这条消息
  48. message.dup = false; //是否是重发的消息 发送者若在一段时间内接收不到PUBACK消息,发送者需要打开DUB标记为1,然后重新发送PUBLISH消息
  49. message.payload = (void*)buf; //消息内容
  50. message.payloadlen = strlen(buf)+1; //消息内容长度
  51. rc = client.publish(topic, message); //发布消息
  52. if (rc != 0)
  53. printf("Error %d from sending QoS 0 message\n", rc);
  54. else while (arrivedcount == 0) //等待订阅消息接收
  55. client.yield(100); //延时?
  56. // QoS 1
  57. printf("Now QoS 1\n");
  58. sprintf(buf, "Hello World! QoS 1 message from app version %f", version);
  59. message.qos = MQTT::QOS1;
  60. message.payloadlen = strlen(buf)+1;
  61. rc = client.publish(topic, message);
  62. if (rc != 0)
  63. printf("Error %d from sending QoS 1 message\n", rc);
  64. else while (arrivedcount == 1)
  65. client.yield(100);
  66. // QoS 2
  67. sprintf(buf, "Hello World! QoS 2 message from app version %f", version);
  68. message.qos = MQTT::QOS2;
  69. message.payloadlen = strlen(buf)+1;
  70. rc = client.publish(topic, message);
  71. if (rc != 0)
  72. printf("Error %d from sending QoS 2 message\n", rc);
  73. while (arrivedcount == 2)
  74. client.yield(100);
  75. rc = client.unsubscribe(topic); //取消订阅
  76. if (rc != 0)
  77. printf("rc from unsubscribe was %d\n", rc);
  78. rc = client.disconnect(); //断开mqtt
  79. if (rc != 0)
  80. printf("rc from disconnect was %d\n", rc);
  81. ipstack.disconnect(); //断开tcp
  82. printf("Finishing with %d messages received\n", arrivedcount);
  83. return 0;
  84. }

 

源码:

链接:https://pan.baidu.com/s/12ebMkdV9qYSpH6G0Tpyecg 
提取码:609w 
复制这段内容后打开百度网盘手机App,操作更方便哦

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/176515
推荐阅读
相关标签
  

闽ICP备14008679号