Guest User

Untitled

a guest
Jun 26th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "MQTTAsync.h"
  6.  
  7. #if !defined(WIN32)
  8. #include <unistd.h>
  9. #else
  10. #include <windows.h>
  11. #endif
  12.  
  13. #if defined(_WRS_KERNEL)
  14. #include <OsWrapper.h>
  15. #endif
  16.  
  17. #define ADDRESS "ssl://xxxxx.azure-devices.net:8883" //tcp://localhost:1883"
  18. #define CLIENTID "EnergyMeter" //"ExampleClientPub"
  19. #define TOPIC "devices/EnergyMeter/messages/events/" //"MQTT Examples"
  20. #define PAYLOAD "Hello World!"
  21. #define QOS 1
  22. #define TIMEOUT 10000L
  23.  
  24. volatile MQTTAsync_token deliveredtoken;
  25.  
  26. int finished = 0;
  27.  
  28. void connlost(void *context, char *cause)
  29. {
  30. MQTTAsync client = (MQTTAsync)context;
  31. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  32. int rc;
  33.  
  34. printf("nConnection lostn");
  35. printf(" cause: %sn", cause);
  36.  
  37. printf("Reconnectingn");
  38. conn_opts.keepAliveInterval = 20;
  39. conn_opts.cleansession = 1;
  40. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  41. {
  42. printf("Failed to start connect, return code %dn", rc);
  43. finished = 1;
  44. }
  45. }
  46.  
  47.  
  48. void onDisconnect(void* context, MQTTAsync_successData* response)
  49. {
  50. printf("Successful disconnectionn");
  51. finished = 1;
  52. }
  53.  
  54.  
  55. void onSend(void* context, MQTTAsync_successData* response)
  56. {
  57. MQTTAsync client = (MQTTAsync)context;
  58. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  59. int rc;
  60.  
  61. printf("Message with token value %d delivery confirmedn", response->token);
  62.  
  63. opts.onSuccess = onDisconnect;
  64. opts.context = client;
  65.  
  66. if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
  67. {
  68. printf("Failed to start sendMessage, return code %dn", rc);
  69. exit(EXIT_FAILURE);
  70. }
  71. }
  72.  
  73.  
  74. void onConnectFailure(void* context, MQTTAsync_failureData* response)
  75. {
  76. printf("Connect failed, rc %dn", response ? response->code : 0);
  77. finished = 1;
  78. }
  79.  
  80.  
  81. void onConnect(void* context, MQTTAsync_successData* response)
  82. {
  83. printf("hello");
  84. MQTTAsync client = (MQTTAsync)context;
  85. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  86. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  87. int rc;
  88.  
  89. printf("Successful connectionn");
  90.  
  91. opts.onSuccess = onSend;
  92. opts.context = client;
  93.  
  94. pubmsg.payload = PAYLOAD;
  95. pubmsg.payloadlen = strlen(PAYLOAD);
  96. pubmsg.qos = QOS;
  97. pubmsg.retained = 0;
  98. deliveredtoken = 0;
  99.  
  100. if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
  101. {
  102. printf("Failed to start sendMessage, return code %dn", rc);
  103. exit(EXIT_FAILURE);
  104. }
  105. }
  106.  
  107.  
  108. int main(int argc, char* argv[])
  109. {
  110. MQTTAsync client;
  111. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  112. int rc = 5;
  113. int a;
  114. a = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  115. printf("a is %d", a);
  116. printf("Hey");
  117. MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);
  118. printf("check1n");
  119. conn_opts.keepAliveInterval = 5;
  120. //conn_opts.username = "ssl://Ironman.azure-devices.net/stark";
  121. //conn_opts.password = "SharedAccessSignature sr=Ironman.azure-devices.net%2Fdevices%2Fstark&sig=eoAum3Hl2gWWiFVJT%2FhmUMwYH4NFAT%2B5fG8tRTm%2BbaA%3D&se=1558677763";
  122. conn_opts.cleansession = 1;
  123. printf("check2n");
  124. conn_opts.onSuccess = onConnect;
  125. conn_opts.username = "CCMSIoTHub.azure-devices.net/EnergyMeter/api-version=2016-11-14";
  126. conn_opts.password = "SharedAccessSignature sr=xxxxxxx.azure-devices.net%2Fdevices%2FEnergyMeter&sig=undNm%xxxxxxxxz87I%3D&se=xxxxxxxxx";
  127. printf("check3n");
  128. conn_opts.onFailure = onConnectFailure;
  129. conn_opts.context = client;
  130.  
  131. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  132. {
  133. printf("Failed to start connect, return code %dn", rc);
  134. exit(EXIT_FAILURE);
  135. }
  136. else
  137. {
  138. printf("rc is %d ", rc);
  139. }
  140. printf("Waiting for publication of %sn"
  141. "on topic %s for client with ClientID: %sn",
  142. PAYLOAD, TOPIC, CLIENTID);
  143. while (!finished)
  144. #if defined(WIN32) || defined(WIN64)
  145. Sleep(100);
  146. #else
  147. usleep(10000L);
  148. #endif
  149.  
  150.  
  151. MQTTAsync_destroy(&client);
  152. return rc;
  153. }
Add Comment
Please, Sign In to add comment