Advertisement
GeeckoDev

Untitled

Aug 6th, 2014
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <MQTTAsync.h>
  2. #include <stdio.h>
  3.  
  4. int connection = 0;
  5.  
  6. void connlost(void *context, char *cause)
  7. {
  8.     connection = 0;
  9.     printf("Connection lost! (%s)\n", cause);
  10. }
  11.  
  12. void connsuccess(void *context, MQTTAsync_successData *response)
  13. {
  14.     connection = 1;
  15.     printf("Connection success!\n");
  16. }
  17.  
  18. void connfailure(void *context, MQTTAsync_failureData *response)
  19. {
  20.     connection = 0;
  21.     printf("Connection failure!\n");
  22. }
  23.  
  24. int main()
  25. {
  26.     MQTTAsync client;
  27.     MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  28.  
  29.     conn_opts.keepAliveInterval = 3;
  30.     conn_opts.connectTimeout = 10;
  31.     conn_opts.retryInterval = 5;
  32.     conn_opts.cleansession = 1;
  33.     conn_opts.onSuccess = connsuccess;
  34.     conn_opts.onFailure = connfailure;
  35.     conn_opts.context = NULL;
  36.    
  37.     MQTTAsync_create(&client, "localhost", "idddd", MQTTCLIENT_PERSISTENCE_NONE, NULL);
  38.     MQTTAsync_setCallbacks(client, 0, connlost, NULL, NULL);
  39.    
  40.     for (;;) {
  41.         if (!connection) {
  42.             MQTTAsync_connect(client, &conn_opts);
  43.         }
  44.         sleep(1000);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement