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