iothub_ll_telemetry_sample

Committer:
AzureIoTClient
Date:
Thu Feb 15 13:22:49 2018 -0800
Revision:
0:c979e2f5511a
Child:
1:589bbd7948f3
1.1.32

Who changed what in which revision?

UserRevisionLine numberNew 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 0:c979e2f5511a 3 #include <stdio.h>
AzureIoTClient 0:c979e2f5511a 4 #include <stdlib.h>
AzureIoTClient 0:c979e2f5511a 5
AzureIoTClient 0:c979e2f5511a 6 #include "iothub_client.h"
AzureIoTClient 0:c979e2f5511a 7 #include "iothub_message.h"
AzureIoTClient 0:c979e2f5511a 8 #include "azure_c_shared_utility/threadapi.h"
AzureIoTClient 0:c979e2f5511a 9 #include "azure_c_shared_utility/crt_abstractions.h"
AzureIoTClient 0:c979e2f5511a 10 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 0:c979e2f5511a 11 #include "azure_c_shared_utility/shared_util_options.h"
AzureIoTClient 0:c979e2f5511a 12
AzureIoTClient 0:c979e2f5511a 13 #ifdef USE_MQTT
AzureIoTClient 0:c979e2f5511a 14 #include "iothubtransportmqtt.h"
AzureIoTClient 0:c979e2f5511a 15 #ifdef USE_WEBSOCKETS
AzureIoTClient 0:c979e2f5511a 16 #include "iothubtransportmqtt_websockets.h"
AzureIoTClient 0:c979e2f5511a 17 #endif
AzureIoTClient 0:c979e2f5511a 18 #endif
AzureIoTClient 0:c979e2f5511a 19 #ifdef USE_AMQP
AzureIoTClient 0:c979e2f5511a 20 #include "iothubtransportamqp.h"
AzureIoTClient 0:c979e2f5511a 21 #ifdef USE_WEBSOCKETS
AzureIoTClient 0:c979e2f5511a 22 #include "iothubtransportamqp_websockets.h"
AzureIoTClient 0:c979e2f5511a 23 #endif
AzureIoTClient 0:c979e2f5511a 24 #endif
AzureIoTClient 0:c979e2f5511a 25 #ifdef USE_HTTP
AzureIoTClient 0:c979e2f5511a 26 #include "iothubtransporthttp.h"
AzureIoTClient 0:c979e2f5511a 27 #endif
AzureIoTClient 0:c979e2f5511a 28
AzureIoTClient 0:c979e2f5511a 29 #include "iothub_client_options.h"
AzureIoTClient 0:c979e2f5511a 30 #include "certs.h"
AzureIoTClient 0:c979e2f5511a 31
AzureIoTClient 0:c979e2f5511a 32 /* String containing Hostname, Device Id & Device Key in the format: */
AzureIoTClient 0:c979e2f5511a 33 /* Paste in the your iothub connection string */
AzureIoTClient 0:c979e2f5511a 34 static const char* connectionString = "[device connection string]";
AzureIoTClient 0:c979e2f5511a 35
AzureIoTClient 0:c979e2f5511a 36 #define MESSAGE_COUNT 5
AzureIoTClient 0:c979e2f5511a 37 static bool g_continueRunning = true;
AzureIoTClient 0:c979e2f5511a 38 static size_t g_message_count_send_confirmations = 0;
AzureIoTClient 0:c979e2f5511a 39
AzureIoTClient 0:c979e2f5511a 40 static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 0:c979e2f5511a 41 {
AzureIoTClient 0:c979e2f5511a 42 (void)userContextCallback;
AzureIoTClient 0:c979e2f5511a 43 // When a message is sent this callback will get envoked
AzureIoTClient 0:c979e2f5511a 44 g_message_count_send_confirmations++;
AzureIoTClient 0:c979e2f5511a 45 (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 46 }
AzureIoTClient 0:c979e2f5511a 47
AzureIoTClient 0:c979e2f5511a 48 int main(void)
AzureIoTClient 0:c979e2f5511a 49 {
AzureIoTClient 0:c979e2f5511a 50 IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol;
AzureIoTClient 0:c979e2f5511a 51 IOTHUB_MESSAGE_HANDLE message_handle;
AzureIoTClient 0:c979e2f5511a 52 size_t messages_sent = 0;
AzureIoTClient 0:c979e2f5511a 53 const char* telemetry_msg = "test_message";
AzureIoTClient 0:c979e2f5511a 54
AzureIoTClient 0:c979e2f5511a 55 // Select the Protocol to use with the connection
AzureIoTClient 0:c979e2f5511a 56 #ifdef USE_AMQP
AzureIoTClient 0:c979e2f5511a 57 //protocol = AMQP_Protocol_over_WebSocketsTls;
AzureIoTClient 0:c979e2f5511a 58 protocol = AMQP_Protocol;
AzureIoTClient 0:c979e2f5511a 59 #endif
AzureIoTClient 0:c979e2f5511a 60 #ifdef USE_MQTT
AzureIoTClient 0:c979e2f5511a 61 //protocol = MQTT_Protocol;
AzureIoTClient 0:c979e2f5511a 62 //protocol = MQTT_WebSocket_Protocol;
AzureIoTClient 0:c979e2f5511a 63 #endif
AzureIoTClient 0:c979e2f5511a 64 #ifdef USE_HTTP
AzureIoTClient 0:c979e2f5511a 65 //protocol = HTTP_Protocol;
AzureIoTClient 0:c979e2f5511a 66 #endif
AzureIoTClient 0:c979e2f5511a 67
AzureIoTClient 0:c979e2f5511a 68 IOTHUB_CLIENT_LL_HANDLE iothub_ll_handle;
AzureIoTClient 0:c979e2f5511a 69
AzureIoTClient 0:c979e2f5511a 70 // Used to initialize IoTHub SDK subsystem
AzureIoTClient 0:c979e2f5511a 71 (void)platform_init();
AzureIoTClient 0:c979e2f5511a 72
AzureIoTClient 0:c979e2f5511a 73 (void)printf("Creating IoTHub handle\r\n");
AzureIoTClient 0:c979e2f5511a 74 // Create the iothub handle here
AzureIoTClient 0:c979e2f5511a 75 iothub_ll_handle = IoTHubClient_LL_CreateFromConnectionString(connectionString, protocol);
AzureIoTClient 0:c979e2f5511a 76
AzureIoTClient 0:c979e2f5511a 77 // Set any option that are neccessary.
AzureIoTClient 0:c979e2f5511a 78 // For available options please see the iothub_sdk_options.md documentation
AzureIoTClient 0:c979e2f5511a 79 //bool traceOn = true;
AzureIoTClient 0:c979e2f5511a 80 //IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_LOG_TRACE, &traceOn);
AzureIoTClient 0:c979e2f5511a 81 // Setting the Trusted Certificate. This is only necessary on system with without
AzureIoTClient 0:c979e2f5511a 82 // built in certificate stores.
AzureIoTClient 0:c979e2f5511a 83 IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_TRUSTED_CERT, certificates);
AzureIoTClient 0:c979e2f5511a 84
AzureIoTClient 0:c979e2f5511a 85 do
AzureIoTClient 0:c979e2f5511a 86 {
AzureIoTClient 0:c979e2f5511a 87 if (messages_sent < MESSAGE_COUNT)
AzureIoTClient 0:c979e2f5511a 88 {
AzureIoTClient 0:c979e2f5511a 89 // Construct the iothub message from a string or a byte array
AzureIoTClient 0:c979e2f5511a 90 message_handle = IoTHubMessage_CreateFromString(telemetry_msg);
AzureIoTClient 0:c979e2f5511a 91 //message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)));
AzureIoTClient 0:c979e2f5511a 92
AzureIoTClient 0:c979e2f5511a 93 // Set Message property
AzureIoTClient 0:c979e2f5511a 94 (void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID");
AzureIoTClient 0:c979e2f5511a 95 (void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID");
AzureIoTClient 0:c979e2f5511a 96 (void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2Fjson");
AzureIoTClient 0:c979e2f5511a 97 (void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8");
AzureIoTClient 0:c979e2f5511a 98
AzureIoTClient 0:c979e2f5511a 99 // Add custom properties to message
AzureIoTClient 0:c979e2f5511a 100 MAP_HANDLE propMap = IoTHubMessage_Properties(message_handle);
AzureIoTClient 0:c979e2f5511a 101 Map_AddOrUpdate(propMap, "property_key", "property_value");
AzureIoTClient 0:c979e2f5511a 102
AzureIoTClient 0:c979e2f5511a 103 (void)printf("Sending message %d to IoTHub\r\n", (int)(messages_sent+1) );
AzureIoTClient 0:c979e2f5511a 104 IoTHubClient_LL_SendEventAsync(iothub_ll_handle, message_handle, send_confirm_callback, NULL);
AzureIoTClient 0:c979e2f5511a 105
AzureIoTClient 0:c979e2f5511a 106 // The message is copied to the sdk so the we can destroy it
AzureIoTClient 0:c979e2f5511a 107 IoTHubMessage_Destroy(message_handle);
AzureIoTClient 0:c979e2f5511a 108
AzureIoTClient 0:c979e2f5511a 109 messages_sent++;
AzureIoTClient 0:c979e2f5511a 110 }
AzureIoTClient 0:c979e2f5511a 111 else if (g_message_count_send_confirmations >= MESSAGE_COUNT)
AzureIoTClient 0:c979e2f5511a 112 {
AzureIoTClient 0:c979e2f5511a 113 // After all messages are all received stop running
AzureIoTClient 0:c979e2f5511a 114 g_continueRunning = false;
AzureIoTClient 0:c979e2f5511a 115 }
AzureIoTClient 0:c979e2f5511a 116
AzureIoTClient 0:c979e2f5511a 117 IoTHubClient_LL_DoWork(iothub_ll_handle);
AzureIoTClient 0:c979e2f5511a 118 ThreadAPI_Sleep(1);
AzureIoTClient 0:c979e2f5511a 119
AzureIoTClient 0:c979e2f5511a 120 } while (g_continueRunning);
AzureIoTClient 0:c979e2f5511a 121
AzureIoTClient 0:c979e2f5511a 122 // Clean up the iothub sdk handle
AzureIoTClient 0:c979e2f5511a 123 IoTHubClient_LL_Destroy(iothub_ll_handle);
AzureIoTClient 0:c979e2f5511a 124
AzureIoTClient 0:c979e2f5511a 125 // Free all the sdk subsystem
AzureIoTClient 0:c979e2f5511a 126 platform_deinit();
AzureIoTClient 0:c979e2f5511a 127
AzureIoTClient 0:c979e2f5511a 128 printf("Press any key to continue");
AzureIoTClient 0:c979e2f5511a 129 getchar();
AzureIoTClient 0:c979e2f5511a 130
AzureIoTClient 0:c979e2f5511a 131 return 0;
AzureIoTClient 0:c979e2f5511a 132 }