Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #include "MQTTClient.h"
  5. #define PAYLOAD "Hello World!"
  6. #define QOS 1
  7. #define TIMEOUT 10000L
  8. #define TOPIC1 "devices/Manoj_Test/messages/events/"
  9. #define ADDRESS "ssl://xxxxxxxx.azure-devices.net:8883"
  10. #define CLIENTID1 "Manoj_Test"
  11.  
  12. int main(void)
  13. {
  14. MQTTClient client;
  15. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  16. MQTTClient_message pubmsg = MQTTClient_message_initializer;
  17. MQTTClient_deliveryToken token;
  18. int rc1;
  19.  
  20. MQTTClient_create(&client, ADDRESS, CLIENTID1, 1, NULL);
  21. conn_opts.cleansession = 1;
  22. conn_opts.username = "xxxxxxxx.azure-devices.net/Manoj_Test";
  23. conn_opts.password = "SharedAccessSignature sr=xxxxxxxx.azure-devices.net%2fdevices%2fManoj_Test&sig=GyizT%2b7uyIpOkMJjTfN%2fpOZh9CnuQedNB%2bre2NrL1Kg%3d&se=1496395529";
  24.  
  25.  
  26. MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
  27.  
  28.  
  29. if ((rc1 = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  30. {
  31. printf("Failed to connect, return code %dn", rc1);
  32. exit(-1);
  33. }
  34.  
  35. MQTTClient_subscribe(client, TOPIC, QOS);
  36. while(1)
  37. {
  38. pubmsg.payload = PAYLOAD;
  39. pubmsg.payloadlen = strlen(PAYLOAD);
  40. pubmsg.qos = 1;
  41. pubmsg.retained = 0;
  42. MQTTClient_publishMessage(client, TOPIC1, &pubmsg, &token);
  43. printf("Waiting for up to %d seconds for publication of %snon topic %s for client with ClientID: %sn", (int)(TIMEOUT/1000), PAYLOAD, TOPIC1, CLIENTID1);
  44. rc1 = MQTTClient_waitForCompletion(client, token, TIMEOUT);
  45. printf("Message with delivery token %d deliveredn", token);
  46. usleep(100000);
  47. }
  48. MQTTClient_disconnect(client, 10000);
  49. MQTTClient_destroy(&client);
  50. return rc1;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement