iothub_ll_telemetry_sample.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. // CAVEAT: This sample is to demonstrate azure IoT client concepts only and is not a guide design principles or style
  4. // Checking of return codes and error values shall be omitted for brevity. Please practice sound engineering practices
  5. // when writing production code.
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "iothub.h"
  9. #include "iothub_device_client_ll.h"
  10. #include "iothub_client_options.h"
  11. #include "iothub_message.h"
  12. #include "azure_c_shared_utility/threadapi.h"
  13. #include "azure_c_shared_utility/crt_abstractions.h"
  14. #include "azure_c_shared_utility/shared_util_options.h"
  15. #include <rtthread.h>
  16. #ifdef FINSH_USING_MSH
  17. #include <finsh.h>
  18. #endif
  19. #ifdef SET_TRUSTED_CERT_IN_SAMPLES
  20. #include "certs/certs.h"
  21. #endif // SET_TRUSTED_CERT_IN_SAMPLES
  22. /* This sample uses the _LL APIs of iothub_client for example purposes.
  23. Simply changing the using the convenience layer (functions not having _LL)
  24. and removing calls to _DoWork will yield the same results. */
  25. // The protocol you wish to use should be uncommented
  26. //
  27. #ifdef PKG_USING_AZURE_MQTT_PROTOCOL
  28. #define SAMPLE_MQTT
  29. #endif
  30. #ifdef PKG_USING_AZURE_HTTP_PROTOCOL
  31. #define SAMPLE_HTTP
  32. #endif
  33. //#define SAMPLE_MQTT_OVER_WEBSOCKETS
  34. //#define SAMPLE_AMQP
  35. //#define SAMPLE_AMQP_OVER_WEBSOCKETS
  36. #ifdef SAMPLE_MQTT
  37. #include "iothubtransportmqtt.h"
  38. #endif // SAMPLE_MQTT
  39. #ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
  40. #include "iothubtransportmqtt_websockets.h"
  41. #endif // SAMPLE_MQTT_OVER_WEBSOCKETS
  42. #ifdef SAMPLE_AMQP
  43. #include "iothubtransportamqp.h"
  44. #endif // SAMPLE_AMQP
  45. #ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
  46. #include "iothubtransportamqp_websockets.h"
  47. #endif // SAMPLE_AMQP_OVER_WEBSOCKETS
  48. #ifdef SAMPLE_HTTP
  49. #include "iothubtransporthttp.h"
  50. #endif // SAMPLE_HTTP
  51. /* Paste in the your iothub connection string */
  52. static const char* connectionString ="Your device connection string copied from the device explorer";
  53. #define MESSAGE_COUNT 5
  54. static bool g_continueRunning = true;
  55. static size_t g_message_count_send_confirmations = 0;
  56. static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
  57. {
  58. (void)userContextCallback;
  59. // When a message is sent this callback will get envoked
  60. g_message_count_send_confirmations++;
  61. (void)printf("Confirmation callback received for message %zu with result %s\r\n", g_message_count_send_confirmations, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
  62. }
  63. static void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* user_context)
  64. {
  65. (void)reason;
  66. (void)user_context;
  67. // This sample DOES NOT take into consideration network outages.
  68. if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED)
  69. {
  70. (void)printf("The device client is connected to iothub\r\n");
  71. }
  72. else
  73. {
  74. (void)printf("The device client has been disconnected\r\n");
  75. }
  76. }
  77. void azure_iothub_ll_telemetry_sample(void *parameter)
  78. {
  79. IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol;
  80. IOTHUB_MESSAGE_HANDLE message_handle;
  81. size_t messages_sent = 0;
  82. const char* telemetry_msg = "test_message";
  83. // Select the Protocol to use with the connection
  84. #ifdef SAMPLE_MQTT
  85. protocol = MQTT_Protocol;
  86. #endif // SAMPLE_MQTT
  87. #ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
  88. protocol = MQTT_WebSocket_Protocol;
  89. #endif // SAMPLE_MQTT_OVER_WEBSOCKETS
  90. #ifdef SAMPLE_AMQP
  91. protocol = AMQP_Protocol;
  92. #endif // SAMPLE_AMQP
  93. #ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
  94. protocol = AMQP_Protocol_over_WebSocketsTls;
  95. #endif // SAMPLE_AMQP_OVER_WEBSOCKETS
  96. #ifdef SAMPLE_HTTP
  97. protocol = HTTP_Protocol;
  98. #endif // SAMPLE_HTTP
  99. // Used to initialize IoTHub SDK subsystem
  100. (void)IoTHub_Init();
  101. IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle;
  102. (void)rt_kprintf("Creating IoTHub Device handle\r\n");
  103. // Create the iothub handle here
  104. device_ll_handle = IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol);
  105. if (device_ll_handle == NULL)
  106. {
  107. (void)rt_kprintf("Failure createing Iothub device. Hint: Check you connection string.\r\n");
  108. }
  109. else
  110. {
  111. // Set any option that are neccessary.
  112. // For available options please see the iothub_sdk_options.md documentation
  113. #ifndef SAMPLE_HTTP
  114. bool traceOn = true;
  115. IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_LOG_TRACE, &traceOn);
  116. #endif
  117. #ifdef SET_TRUSTED_CERT_IN_SAMPLES
  118. // Setting the Trusted Certificate. This is only necessary on system with without
  119. // built in certificate stores.
  120. IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_TRUSTED_CERT, certificates);
  121. #endif // SET_TRUSTED_CERT_IN_SAMPLES
  122. #if defined SAMPLE_MQTT || defined SAMPLE_MQTT_WS
  123. //Setting the auto URL Encoder (recommended for MQTT). Please use this option unless
  124. //you are URL Encoding inputs yourself.
  125. //ONLY valid for use with MQTT
  126. // bool urlEncodeOn = true;
  127. // IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn);
  128. // bool keep_alive = true;
  129. // IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_KEEP_ALIVE, &keep_alive);
  130. #endif
  131. // Setting connection status callback to get indication of connection to iothub
  132. (void)IoTHubDeviceClient_LL_SetConnectionStatusCallback(device_ll_handle, connection_status_callback, NULL);
  133. do
  134. {
  135. if (messages_sent < MESSAGE_COUNT)
  136. {
  137. // Construct the iothub message from a string or a byte array
  138. message_handle = IoTHubMessage_CreateFromString(telemetry_msg);
  139. //message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)));
  140. // Set Message property
  141. /*(void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID");
  142. (void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID");
  143. (void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson");
  144. (void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8");*/
  145. // Add custom properties to message
  146. (void)IoTHubMessage_SetProperty(message_handle, "hello", "RT-Thread");
  147. (void)rt_kprintf("Sending message %d to IoTHub\r\n", (int)(messages_sent + 1));
  148. IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, message_handle, send_confirm_callback, NULL);
  149. // The message is copied to the sdk so the we can destroy it
  150. IoTHubMessage_Destroy(message_handle);
  151. messages_sent++;
  152. }
  153. else if (g_message_count_send_confirmations >= MESSAGE_COUNT)
  154. {
  155. // After all messages are all received stop running
  156. g_continueRunning = false;
  157. }
  158. IoTHubDeviceClient_LL_DoWork(device_ll_handle);
  159. ThreadAPI_Sleep(1);
  160. } while (g_continueRunning);
  161. // Clean up the iothub sdk handle
  162. IoTHubDeviceClient_LL_Destroy(device_ll_handle);
  163. }
  164. // Free all the sdk subsystem
  165. IoTHub_Deinit();
  166. rt_kprintf("Azure Sample Exit");
  167. //getchar();
  168. }
  169. #define THREAD_PRIORITY 10
  170. #define THREAD_STACK_SIZE 8192
  171. #define THREAD_TIMESLICE 5
  172. int azure_telemetry_sample()
  173. {
  174. rt_thread_t tid1 = RT_NULL;
  175. tid1 = rt_thread_create("azure",
  176. azure_iothub_ll_telemetry_sample, (void*)1,
  177. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  178. if (tid1 != RT_NULL)
  179. rt_thread_startup(tid1);
  180. else
  181. return -1;
  182. return 0;
  183. }
  184. MSH_CMD_EXPORT(azure_telemetry_sample, iothub_ll_telemetry_sample);