RABBIT-MQTT+PMQTT Protocol +paho Library (ubuntu16.04)

Source: Internet
Author: User
Keywords Cloud computing MQTT PMQTT Protocol PAHO Library

RABBIT-MQTT+PMQTT Protocol +paho Library (ubuntu16.04).

Steps

1 Download Installation Rabiit-mq Mqtt-server
Just use the Deb bag.
Another need
sudo apt install Erlang

2 start
Sudo/etc/init.d/rabbitmq-server restart

3 mqtt-3.1 protocol Support
Rabbitmq-plugins Enable Rabbitmq_mqtt

4 Open Start Web Management
Rabbitmq-plugins Enable Rabbitmq_management

5 Download Compile PAHO CPP

6 Running MQTT Pub example, packaged in C + + Hapowapper, combined with Protoc buf

compilation: g++ main.cpp-o-lpaho-mqtt3c--std=c++11 code

C-Call library for lower Hapo is encapsulated in C + +-hapowapper.h

Main.cpp

#include #include #include "hapoWapper.h" using Std::cout; Using Std::endl; Using Std::shared_ptr; #define ADDRESS "127.0.0.1:1883" #define CLIENTID "58daf13e258951000ab2b38f" #define TOPIC1 "#define TOPIC2" Normal man "const char TOKEN =" ybljnekvdovypfjo7ejztfvoya94xv4gbuvb9u85kjy= "; void fdelivered (void *context, Mqttclient_deliverytoken dt) {printf ("message with token value%d IBuySpy confirmed\n", DT ); int Fmsgarrvd (void *context, char *topicname, int topiclen, mqttclient_message *message) {int i; char payloadptr; printf ( "Message arrived\n"); printf ("topic:%s\n", topicname); printf ("message:"); if (!strcmp (Topicname,topic1)) {printf ("Got token size =%d,%s\n", Message->payloadlen, (char) message->payload) ; Payloadptr = (char) message->payload; for (i=0 Ipayloadlen; i++) {Putchar (*payloadptr++);} putchar (' \ n '); Mqttclient_freemessage (&message); Mqttclient_free (topicname); return 1; } void Fconnlost (void *context, char *cause) {printf ("\nconnection lost\n "); printf ("Incorporated:%s\n", Incorporated); int main (int argc, char argv]) {mqttclient_connectoptions conn_opts = Mqttclient_connectoptions_initializer; conn_ Opts.keepaliveinterval = 20; Conn_opts.cleansession = 1; Clientparams paras (address,clientid,conn_opts); Cbgroups CBS (FCONNLOST,FDELIVERED,FMSGARRVD); Pahowapper WAP (paras); Wap.connect (CBS); printf ("Subcribe topic:%s%s\n", topic1,topic2); Wap.subscribe (topic2,1); Wap.subscribe (topic1,1); printf ("Publish with topic:%s\n", TOPIC2); Auto words = "Coooooooool"; Wap.publish (Topic2,words,strlen (words), 1); Char ch; do {ch =getchar (), while (ch!= ' q ' && ch!= ' Q '); return 0; }

HapoWapper.h

#ifndef hapo_wapper_h #define HAPO_WAPPER_H #include "MQTTClient.h" #include #include typedef void (*connlost) (void, char); typedef void (*delivered) (void *context, Mqttclient_deliverytoken dt); typedef INT (* MSGARRVD) (void *context, char *topicname, int topiclen, mqttclient_message *message); Class Clientparams {Public:clientparams () {}; ~clientparams () {}; const char _server_uri; const char _client_id; Mqttclient_connectoptions _conn_opts; Clientparams (const char*a, const char B, const mqttclient_connectoptions & O): _server_uri (a), _client_id (b), _conn_ OPTs (o) {}; Clientparams & operator= (const CLIENTPARAMS & CP) {if (&CP = = this) return *this; _server_uri = Cp._server_uri; _ client_id = cp._client_id; _conn_opts = cp._conn_opts; return *this; }; }; Class Cbgroups {public:connlost CONNECT_CB; delivered DELIVERED_CB; MSGARRVD MSGARRVD_CB (cbgroups C, connlost D, MSGARRVD m): CONNECT_CB (c), DELIVERED_CB (d), MSGARRVD_CB (m) {}; }; Class Pahowapper {public:Pahowapper () {}; ~pahowapper () {}; Pahowapper (const CLIENTPARAMS & paras) {_paras = paras; printf ("Client%s access%s\n", _paras._client_id, _paras._ Server_uri); Mqttclient_create (&_client, _paras._server_uri, _paras._client_id, Mqttclient_persistence_none, NULL); }; int Connect (const cbgroups & CBS) {int rc; Mqttclient_setcallbacks (_client, NULL, CBS.CONNECT_CB, CBS.MSGARRVD_CB, CBS.DELIVERED_CB); if (rc = Mqttclient_connect (_client, & (_paras._conn_opts))!= mqttclient_success) {printf ("Failed to connect, Return code%d\n ", RC); Exit (-1); return RC; }; void Disconnect () {Mqttclient_disconnect (_client, 10000); Mqttclient_destroy (&_client); }; void Publish (const char topic, const void payload, int len, int qos_lvl) {Mqttclient_message pubmsg = Mqttclient_message_ initializer; Mqttclient_deliverytoken token; Pubmsg.payload = (void) payload; Pubmsg.payloadlen = Len; Pubmsg.qos = QOS_LVL; pubmsg.retained = 0; Mqttclient_publishmessage (_client, topic, &pubmsg, &token); printf ("Waiting for publication of%s\n" "" topic%s for client with ClientID:%s\n ", (char) payload, topic, _PARAS._CLIENT_ ID); }; void Subscribe (const char Topic,int qos_lvl) {mqttclient_subscribe (_client, topic, QOS_LVL); Private:mqttclient _client; Clientparams _paras; }; #endif
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.