iothub_ll_telemetry_sample
Diff: iothub_ll_telemetry_sample.c
- Revision:
- 2:2b31d7ad244c
- Parent:
- 1:589bbd7948f3
- Child:
- 3:c88858e5d52c
--- a/iothub_ll_telemetry_sample.c Tue Mar 20 10:31:58 2018 -0700 +++ b/iothub_ll_telemetry_sample.c Mon Apr 16 14:28:30 2018 -0700 @@ -4,39 +4,47 @@ #include <stdlib.h> #include "iothub_client.h" +#include "iothub_client_options.h" #include "iothub_message.h" #include "azure_c_shared_utility/threadapi.h" #include "azure_c_shared_utility/crt_abstractions.h" #include "azure_c_shared_utility/platform.h" #include "azure_c_shared_utility/shared_util_options.h" -// USE_MQTT, USE_AMQP, and/or USE_HTTP are set in .\CMakeLists.txt based/ -// on which protocols the IoT C SDK has been set to include during cmake -// generation time. +/* This sample uses the _LL APIs of iothub_client for example purposes. +Simply changing the using the convenience layer (functions not having _LL) +and removing calls to _DoWork will yield the same results. */ -#ifdef USE_MQTT +// The protocol you wish to use should be uncommented +// +#define SAMPLE_MQTT +//#define SAMPLE_MQTT_OVER_WEBSOCKETS +//#define SAMPLE_AMQP +//#define SAMPLE_AMQP_OVER_WEBSOCKETS +//#define SAMPLE_HTTP + +#ifdef SAMPLE_MQTT #include "iothubtransportmqtt.h" - #ifdef USE_WEBSOCKETS - #include "iothubtransportmqtt_websockets.h" - #endif -#endif -#ifdef USE_AMQP +#endif // SAMPLE_MQTT +#ifdef SAMPLE_MQTT_OVER_WEBSOCKETS + #include "iothubtransportmqtt_websockets.h" +#endif // SAMPLE_MQTT_OVER_WEBSOCKETS +#ifdef SAMPLE_AMQP #include "iothubtransportamqp.h" - #ifdef USE_WEBSOCKETS - #include "iothubtransportamqp_websockets.h" - #endif -#endif -#ifdef USE_HTTP +#endif // SAMPLE_AMQP +#ifdef SAMPLE_AMQP_OVER_WEBSOCKETS + #include "iothubtransportamqp_websockets.h" +#endif // SAMPLE_AMQP_OVER_WEBSOCKETS +#ifdef SAMPLE_HTTP #include "iothubtransporthttp.h" -#endif +#endif // SAMPLE_HTTP -#include "iothub_client_options.h" +#ifdef SET_TRUSTED_CERT_IN_SAMPLES #include "certs.h" +#endif // SET_TRUSTED_CERT_IN_SAMPLES -/* String containing Hostname, Device Id & Device Key in the format: */ /* Paste in the your iothub connection string */ static const char* connectionString = "[device connection string]"; - #define MESSAGE_COUNT 5 static bool g_continueRunning = true; static size_t g_message_count_send_confirmations = 0; @@ -56,18 +64,22 @@ size_t messages_sent = 0; const char* telemetry_msg = "test_message"; - // Select the Protocol to use with the connection. -#ifdef USE_HTTP + // Select the Protocol to use with the connection +#ifdef SAMPLE_MQTT + protocol = MQTT_Protocol; +#endif // SAMPLE_MQTT +#ifdef SAMPLE_MQTT_OVER_WEBSOCKETS + protocol = MQTT_WebSocket_Protocol; +#endif // SAMPLE_MQTT_OVER_WEBSOCKETS +#ifdef SAMPLE_AMQP + protocol = AMQP_Protocol; +#endif // SAMPLE_AMQP +#ifdef SAMPLE_AMQP_OVER_WEBSOCKETS + protocol = AMQP_Protocol_over_WebSocketsTls; +#endif // SAMPLE_AMQP_OVER_WEBSOCKETS +#ifdef SAMPLE_HTTP protocol = HTTP_Protocol; -#endif -#ifdef USE_AMQP - //protocol = AMQP_Protocol_over_WebSocketsTls; - protocol = AMQP_Protocol; -#endif -#ifdef USE_MQTT - protocol = MQTT_Protocol; - //protocol = MQTT_WebSocket_Protocol; -#endif +#endif // SAMPLE_HTTP IOTHUB_CLIENT_LL_HANDLE iothub_ll_handle; @@ -80,11 +92,23 @@ // Set any option that are neccessary. // For available options please see the iothub_sdk_options.md documentation + //bool traceOn = true; //IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_LOG_TRACE, &traceOn); + +#ifdef SET_TRUSTED_CERT_IN_SAMPLES // Setting the Trusted Certificate. This is only necessary on system with without // built in certificate stores. IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_TRUSTED_CERT, certificates); +#endif // SET_TRUSTED_CERT_IN_SAMPLES + +#if defined SAMPLE_MQTT || defined SAMPLE_MQTT_WS + //Setting the auto URL Encoder (recommended for MQTT). Please use this option unless + //you are URL Encoding inputs yourself. + //ONLY valid for use with MQTT + //bool urlEncodeOn = true; + //IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn); +#endif do { @@ -97,14 +121,14 @@ // Set Message property (void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID"); (void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID"); - (void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2Fjson"); + (void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson"); (void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8"); // Add custom properties to message MAP_HANDLE propMap = IoTHubMessage_Properties(message_handle); Map_AddOrUpdate(propMap, "property_key", "property_value"); - (void)printf("Sending message %d to IoTHub\r\n", (int)(messages_sent+1) ); + (void)printf("Sending message %d to IoTHub\r\n", (int)(messages_sent + 1)); IoTHubClient_LL_SendEventAsync(iothub_ll_handle, message_handle, send_confirm_callback, NULL); // The message is copied to the sdk so the we can destroy it