IoTHub raw messaging client sample using MQTT
Dependencies: EthernetInterface NTPClient mbed-rtos mbed wolfSSL azure_c_shared_utility iothub_client azure_umqtt_c iothub_mqtt_transport
iothub_client_sample_mqtt.c@7:02bc67ebbe23, 2016-05-09 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Mon May 09 14:38:32 2016 -0700
- Revision:
- 7:02bc67ebbe23
- Parent:
- 6:02304619a9fa
- Child:
- 10:f77bb0044e11
1.0.6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 2:65c172c36cf5 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 2:65c172c36cf5 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 2:65c172c36cf5 | 3 | |
Azure.IoT Build | 2:65c172c36cf5 | 4 | #include <stdio.h> |
Azure.IoT Build | 2:65c172c36cf5 | 5 | #include <stdlib.h> |
Azure.IoT Build | 2:65c172c36cf5 | 6 | |
Azure.IoT Build | 2:65c172c36cf5 | 7 | #include "iothub_client.h" |
Azure.IoT Build | 2:65c172c36cf5 | 8 | #include "iothub_message.h" |
AzureIoTClient | 6:02304619a9fa | 9 | #include "azure_c_shared_utility/threadapi.h" |
AzureIoTClient | 6:02304619a9fa | 10 | #include "azure_c_shared_utility/crt_abstractions.h" |
Azure.IoT Build | 2:65c172c36cf5 | 11 | #include "iothubtransportmqtt.h" |
AzureIoTClient | 6:02304619a9fa | 12 | #include "azure_c_shared_utility/platform.h" |
Azure.IoT Build | 2:65c172c36cf5 | 13 | |
Azure.IoT Build | 2:65c172c36cf5 | 14 | #ifdef MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 2:65c172c36cf5 | 15 | #include "certs.h" |
Azure.IoT Build | 2:65c172c36cf5 | 16 | #endif // MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 2:65c172c36cf5 | 17 | |
AzureIoTClient | 7:02bc67ebbe23 | 18 | /*String containing Hostname, Device Id & Device Key in the format: */ |
AzureIoTClient | 7:02bc67ebbe23 | 19 | /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */ |
AzureIoTClient | 7:02bc67ebbe23 | 20 | /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */ |
Azure.IoT Build | 2:65c172c36cf5 | 21 | static const char* connectionString = "[device connection string]"; |
Azure.IoT Build | 2:65c172c36cf5 | 22 | |
Azure.IoT Build | 2:65c172c36cf5 | 23 | static int callbackCounter; |
Azure.IoT Build | 2:65c172c36cf5 | 24 | static char msgText[1024]; |
AzureIoTClient | 6:02304619a9fa | 25 | static char propText[1024]; |
Azure.IoT Build | 2:65c172c36cf5 | 26 | #define MESSAGE_COUNT 5 |
Azure.IoT Build | 2:65c172c36cf5 | 27 | static bool continueRunning; |
Azure.IoT Build | 2:65c172c36cf5 | 28 | |
Azure.IoT Build | 2:65c172c36cf5 | 29 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES); |
Azure.IoT Build | 2:65c172c36cf5 | 30 | |
Azure.IoT Build | 2:65c172c36cf5 | 31 | typedef struct EVENT_INSTANCE_TAG |
Azure.IoT Build | 2:65c172c36cf5 | 32 | { |
Azure.IoT Build | 2:65c172c36cf5 | 33 | IOTHUB_MESSAGE_HANDLE messageHandle; |
Azure.IoT Build | 2:65c172c36cf5 | 34 | int messageTrackingId; // For tracking the messages within the user callback. |
Azure.IoT Build | 2:65c172c36cf5 | 35 | } EVENT_INSTANCE; |
Azure.IoT Build | 2:65c172c36cf5 | 36 | |
Azure.IoT Build | 2:65c172c36cf5 | 37 | |
Azure.IoT Build | 2:65c172c36cf5 | 38 | static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
Azure.IoT Build | 2:65c172c36cf5 | 39 | { |
Azure.IoT Build | 2:65c172c36cf5 | 40 | int* counter = (int*)userContextCallback; |
Azure.IoT Build | 2:65c172c36cf5 | 41 | const char* buffer; |
Azure.IoT Build | 2:65c172c36cf5 | 42 | size_t size; |
Azure.IoT Build | 2:65c172c36cf5 | 43 | if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK) |
Azure.IoT Build | 2:65c172c36cf5 | 44 | { |
Azure.IoT Build | 2:65c172c36cf5 | 45 | (void)printf("unable to retrieve the message data\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 46 | } |
Azure.IoT Build | 2:65c172c36cf5 | 47 | else |
Azure.IoT Build | 2:65c172c36cf5 | 48 | { |
Azure.IoT Build | 2:65c172c36cf5 | 49 | (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size); |
Azure.IoT Build | 2:65c172c36cf5 | 50 | // If we receive the work 'quit' then we stop running |
Azure.IoT Build | 2:65c172c36cf5 | 51 | if (memcmp(buffer, "quit", size) == 0) |
Azure.IoT Build | 2:65c172c36cf5 | 52 | { |
Azure.IoT Build | 2:65c172c36cf5 | 53 | continueRunning = false; |
Azure.IoT Build | 2:65c172c36cf5 | 54 | } |
Azure.IoT Build | 2:65c172c36cf5 | 55 | } |
Azure.IoT Build | 2:65c172c36cf5 | 56 | |
Azure.IoT Build | 2:65c172c36cf5 | 57 | // Retrieve properties from the message |
Azure.IoT Build | 2:65c172c36cf5 | 58 | MAP_HANDLE mapProperties = IoTHubMessage_Properties(message); |
Azure.IoT Build | 2:65c172c36cf5 | 59 | if (mapProperties != NULL) |
Azure.IoT Build | 2:65c172c36cf5 | 60 | { |
Azure.IoT Build | 2:65c172c36cf5 | 61 | const char*const* keys; |
Azure.IoT Build | 2:65c172c36cf5 | 62 | const char*const* values; |
Azure.IoT Build | 2:65c172c36cf5 | 63 | size_t propertyCount = 0; |
Azure.IoT Build | 2:65c172c36cf5 | 64 | if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK) |
Azure.IoT Build | 2:65c172c36cf5 | 65 | { |
Azure.IoT Build | 2:65c172c36cf5 | 66 | if (propertyCount > 0) |
Azure.IoT Build | 2:65c172c36cf5 | 67 | { |
Azure.IoT Build | 2:65c172c36cf5 | 68 | for (size_t index = 0; index < propertyCount; index++) |
Azure.IoT Build | 2:65c172c36cf5 | 69 | { |
Azure.IoT Build | 2:65c172c36cf5 | 70 | (void)printf("\tKey: %s Value: %s\r\n", keys[index], values[index]); |
Azure.IoT Build | 2:65c172c36cf5 | 71 | } |
Azure.IoT Build | 2:65c172c36cf5 | 72 | (void)printf("\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 73 | } |
Azure.IoT Build | 2:65c172c36cf5 | 74 | } |
Azure.IoT Build | 2:65c172c36cf5 | 75 | } |
Azure.IoT Build | 2:65c172c36cf5 | 76 | |
Azure.IoT Build | 2:65c172c36cf5 | 77 | /* Some device specific action code goes here... */ |
Azure.IoT Build | 2:65c172c36cf5 | 78 | (*counter)++; |
Azure.IoT Build | 2:65c172c36cf5 | 79 | return IOTHUBMESSAGE_ACCEPTED; |
Azure.IoT Build | 2:65c172c36cf5 | 80 | } |
Azure.IoT Build | 2:65c172c36cf5 | 81 | |
Azure.IoT Build | 2:65c172c36cf5 | 82 | static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
Azure.IoT Build | 2:65c172c36cf5 | 83 | { |
Azure.IoT Build | 2:65c172c36cf5 | 84 | EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback; |
Azure.IoT Build | 2:65c172c36cf5 | 85 | (void)printf("Confirmation[%d] received for message tracking id = %d with result = %s\r\n", callbackCounter, eventInstance->messageTrackingId, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); |
Azure.IoT Build | 2:65c172c36cf5 | 86 | /* Some device specific action code goes here... */ |
Azure.IoT Build | 2:65c172c36cf5 | 87 | callbackCounter++; |
Azure.IoT Build | 2:65c172c36cf5 | 88 | IoTHubMessage_Destroy(eventInstance->messageHandle); |
Azure.IoT Build | 2:65c172c36cf5 | 89 | } |
Azure.IoT Build | 2:65c172c36cf5 | 90 | |
Azure.IoT Build | 2:65c172c36cf5 | 91 | void iothub_client_sample_mqtt_run(void) |
Azure.IoT Build | 2:65c172c36cf5 | 92 | { |
Azure.IoT Build | 2:65c172c36cf5 | 93 | IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle; |
Azure.IoT Build | 2:65c172c36cf5 | 94 | |
Azure.IoT Build | 2:65c172c36cf5 | 95 | EVENT_INSTANCE messages[MESSAGE_COUNT]; |
Azure.IoT Build | 2:65c172c36cf5 | 96 | |
Azure.IoT Build | 2:65c172c36cf5 | 97 | continueRunning = true; |
Azure.IoT Build | 2:65c172c36cf5 | 98 | srand((unsigned int)time(NULL)); |
Azure.IoT Build | 2:65c172c36cf5 | 99 | double avgWindSpeed = 10.0; |
Azure.IoT Build | 2:65c172c36cf5 | 100 | |
Azure.IoT Build | 2:65c172c36cf5 | 101 | callbackCounter = 0; |
Azure.IoT Build | 2:65c172c36cf5 | 102 | int receiveContext = 0; |
Azure.IoT Build | 2:65c172c36cf5 | 103 | |
Azure.IoT Build | 2:65c172c36cf5 | 104 | if (platform_init() != 0) |
Azure.IoT Build | 2:65c172c36cf5 | 105 | { |
Azure.IoT Build | 2:65c172c36cf5 | 106 | (void)printf("Failed to initialize the platform.\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 107 | } |
Azure.IoT Build | 2:65c172c36cf5 | 108 | else |
Azure.IoT Build | 2:65c172c36cf5 | 109 | { |
Azure.IoT Build | 2:65c172c36cf5 | 110 | (void)printf("Starting the IoTHub client sample MQTT...\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 111 | |
Azure.IoT Build | 2:65c172c36cf5 | 112 | if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol)) == NULL) |
Azure.IoT Build | 2:65c172c36cf5 | 113 | { |
Azure.IoT Build | 2:65c172c36cf5 | 114 | (void)printf("ERROR: iotHubClientHandle is NULL!\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 115 | } |
Azure.IoT Build | 2:65c172c36cf5 | 116 | else |
Azure.IoT Build | 2:65c172c36cf5 | 117 | { |
Azure.IoT Build | 2:65c172c36cf5 | 118 | bool traceOn = true; |
Azure.IoT Build | 2:65c172c36cf5 | 119 | IoTHubClient_LL_SetOption(iotHubClientHandle, "logtrace", &traceOn); |
Azure.IoT Build | 2:65c172c36cf5 | 120 | |
Azure.IoT Build | 2:65c172c36cf5 | 121 | #ifdef MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 2:65c172c36cf5 | 122 | // For mbed add the certificate information |
Azure.IoT Build | 2:65c172c36cf5 | 123 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 2:65c172c36cf5 | 124 | { |
Azure.IoT Build | 2:65c172c36cf5 | 125 | printf("failure to set option \"TrustedCerts\"\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 126 | } |
Azure.IoT Build | 2:65c172c36cf5 | 127 | #endif // MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 2:65c172c36cf5 | 128 | |
Azure.IoT Build | 2:65c172c36cf5 | 129 | /* Setting Message call back, so we can receive Commands. */ |
Azure.IoT Build | 2:65c172c36cf5 | 130 | if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 2:65c172c36cf5 | 131 | { |
Azure.IoT Build | 2:65c172c36cf5 | 132 | (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 133 | } |
Azure.IoT Build | 2:65c172c36cf5 | 134 | else |
Azure.IoT Build | 2:65c172c36cf5 | 135 | { |
Azure.IoT Build | 2:65c172c36cf5 | 136 | (void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 137 | |
Azure.IoT Build | 2:65c172c36cf5 | 138 | /* Now that we are ready to receive commands, let's send some messages */ |
Azure.IoT Build | 2:65c172c36cf5 | 139 | for (size_t i = 0; i < MESSAGE_COUNT; i++) |
Azure.IoT Build | 2:65c172c36cf5 | 140 | { |
Azure.IoT Build | 2:65c172c36cf5 | 141 | sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f}", avgWindSpeed + (rand() % 4 + 2)); |
Azure.IoT Build | 2:65c172c36cf5 | 142 | if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL) |
Azure.IoT Build | 2:65c172c36cf5 | 143 | { |
Azure.IoT Build | 2:65c172c36cf5 | 144 | (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 145 | } |
Azure.IoT Build | 2:65c172c36cf5 | 146 | else |
Azure.IoT Build | 2:65c172c36cf5 | 147 | { |
Azure.IoT Build | 2:65c172c36cf5 | 148 | messages[i].messageTrackingId = i; |
AzureIoTClient | 6:02304619a9fa | 149 | MAP_HANDLE propMap = IoTHubMessage_Properties(messages[i].messageHandle); |
AzureIoTClient | 6:02304619a9fa | 150 | sprintf_s(propText, sizeof(propText), "PropMsg_%d", i); |
AzureIoTClient | 6:02304619a9fa | 151 | if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK) |
AzureIoTClient | 6:02304619a9fa | 152 | { |
AzureIoTClient | 6:02304619a9fa | 153 | (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n"); |
AzureIoTClient | 6:02304619a9fa | 154 | } |
Azure.IoT Build | 2:65c172c36cf5 | 155 | |
Azure.IoT Build | 2:65c172c36cf5 | 156 | if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 2:65c172c36cf5 | 157 | { |
Azure.IoT Build | 2:65c172c36cf5 | 158 | (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 159 | } |
Azure.IoT Build | 2:65c172c36cf5 | 160 | else |
Azure.IoT Build | 2:65c172c36cf5 | 161 | { |
Azure.IoT Build | 2:65c172c36cf5 | 162 | (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", (int)i); |
Azure.IoT Build | 2:65c172c36cf5 | 163 | } |
Azure.IoT Build | 2:65c172c36cf5 | 164 | |
Azure.IoT Build | 2:65c172c36cf5 | 165 | } |
Azure.IoT Build | 2:65c172c36cf5 | 166 | } |
Azure.IoT Build | 2:65c172c36cf5 | 167 | } |
Azure.IoT Build | 2:65c172c36cf5 | 168 | |
Azure.IoT Build | 2:65c172c36cf5 | 169 | /* Wait for Commands. */ |
Azure.IoT Build | 2:65c172c36cf5 | 170 | while (continueRunning) |
Azure.IoT Build | 2:65c172c36cf5 | 171 | { |
Azure.IoT Build | 2:65c172c36cf5 | 172 | IoTHubClient_LL_DoWork(iotHubClientHandle); |
Azure.IoT Build | 2:65c172c36cf5 | 173 | ThreadAPI_Sleep(1); |
Azure.IoT Build | 2:65c172c36cf5 | 174 | } |
Azure.IoT Build | 2:65c172c36cf5 | 175 | IoTHubClient_LL_Destroy(iotHubClientHandle); |
Azure.IoT Build | 2:65c172c36cf5 | 176 | } |
Azure.IoT Build | 2:65c172c36cf5 | 177 | platform_deinit(); |
Azure.IoT Build | 2:65c172c36cf5 | 178 | } |
Azure.IoT Build | 2:65c172c36cf5 | 179 | } |