1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
   | #include "stdio.h" #include "stdlib.h" #include "string.h" #include "src/MQTTClient.h"    #include <sys/time.h>    #include <termios.h> #include <iostream>
  #define ADDRESS     "tcp://localhost:1883"    #define CLIENTID    "ExampleClientPub" #define TOPIC       "MQTT Examples" #define PAYLOAD     "Hello World!" #define QOS         1 #define TIMEOUT     10000L
  using namespace std; int main(int argc, char* argv[]) {     MQTTClient client;     MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;     MQTTClient_message pubmsg = MQTTClient_message_initializer;     MQTTClient_deliveryToken token;
      MQTTClient_message *receivemsg = NULL ;     char* topicName_rev = NULL;     int   topicLen_rev;
      int rc;     int i;
      MQTTClient_create(&client, ADDRESS, CLIENTID,         MQTTCLIENT_PERSISTENCE_NONE, NULL);     conn_opts.keepAliveInterval = 60;     conn_opts.cleansession = 1;
      if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)     {         printf("Failed to connect, return code %d\n", rc);         exit(-1);     }     pubmsg.payload = (void *)PAYLOAD;     pubmsg.payloadlen = strlen(PAYLOAD);     pubmsg.qos = QOS;     pubmsg.retained = 0;     MQTTClient_subscribe(client, "test", 1);                             usleep(10000);      MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);      printf("Waiting for up to %d seconds for publication of %s\n"             "on topic %s for client with ClientID: %s\n",             (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);     rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);     printf("Message with delivery token %d delivered\n", token);     for(;;)     {                if(MQTTClient_isConnected(client) == true)                   {                    printf("alive \n");                }else{                    printf(" no alive \n");                    break;                }             rc = MQTTClient_receive(client,&topicName_rev, &topicLen_rev, &receivemsg,5000);                 if(rc == MQTTCLIENT_SUCCESS)                {                 printf("Message REv  %d delivered\n", rc);                 printf("topicName: %s  topicName_LEN: %d \n", topicName_rev,topicLen_rev);                 if(topicName_rev != NULL)                                           {                     printf("topicName: ");                     for(i=0;i<topicLen_rev;i++)                     {                         printf(" %c ", topicName_rev[i]);                     }                     printf("\n");                     printf("Data: %s  len:%d msgid: %d \n",(char *)receivemsg->payload,receivemsg->payloadlen,receivemsg->msgid);                      if(strcmp((char *)receivemsg->payload,"ESC") == 0)                     {                         printf("ESC \n");                         break;                     }                 }             }
              usleep(10000);              usleep(100000);     }     MQTTClient_disconnect(client, 10000);     MQTTClient_destroy(&client);     return rc; }
   |