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@30:0a147a179e7e, 2017-02-24 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Feb 24 14:02:44 2017 -0800
- Revision:
- 30:0a147a179e7e
- Parent:
- 27:7c908b91210a
- Child:
- 36:eb9d95ee6d63
1.1.8
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 | 14:0d0177062483 | 11 | #include "azure_c_shared_utility/platform.h" |
Azure.IoT Build | 2:65c172c36cf5 | 12 | #include "iothubtransportmqtt.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 | 14:0d0177062483 | 26 | static bool g_continueRunning; |
Azure.IoT Build | 2:65c172c36cf5 | 27 | #define MESSAGE_COUNT 5 |
Azure.IoT Build | 14:0d0177062483 | 28 | #define DOWORK_LOOP_NUM 3 |
Azure.IoT Build | 2:65c172c36cf5 | 29 | |
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; |
AzureIoTClient | 15:2f2b3dc3ecb4 | 34 | size_t 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 | static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
Azure.IoT Build | 2:65c172c36cf5 | 38 | { |
Azure.IoT Build | 2:65c172c36cf5 | 39 | int* counter = (int*)userContextCallback; |
Azure.IoT Build | 2:65c172c36cf5 | 40 | const char* buffer; |
Azure.IoT Build | 2:65c172c36cf5 | 41 | size_t size; |
AzureIoTClient | 30:0a147a179e7e | 42 | MAP_HANDLE mapProperties; |
AzureIoTClient | 30:0a147a179e7e | 43 | const char* messageId; |
AzureIoTClient | 30:0a147a179e7e | 44 | const char* correlationId; |
AzureIoTClient | 10:f77bb0044e11 | 45 | |
AzureIoTClient | 30:0a147a179e7e | 46 | // Message properties |
AzureIoTClient | 30:0a147a179e7e | 47 | if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL) |
AzureIoTClient | 30:0a147a179e7e | 48 | { |
AzureIoTClient | 30:0a147a179e7e | 49 | messageId = "<null>"; |
AzureIoTClient | 30:0a147a179e7e | 50 | } |
AzureIoTClient | 30:0a147a179e7e | 51 | |
AzureIoTClient | 30:0a147a179e7e | 52 | if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL) |
AzureIoTClient | 30:0a147a179e7e | 53 | { |
AzureIoTClient | 30:0a147a179e7e | 54 | correlationId = "<null>"; |
AzureIoTClient | 30:0a147a179e7e | 55 | } |
AzureIoTClient | 30:0a147a179e7e | 56 | |
AzureIoTClient | 30:0a147a179e7e | 57 | // Message content |
Azure.IoT Build | 2:65c172c36cf5 | 58 | if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK) |
Azure.IoT Build | 2:65c172c36cf5 | 59 | { |
Azure.IoT Build | 2:65c172c36cf5 | 60 | (void)printf("unable to retrieve the message data\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 61 | } |
Azure.IoT Build | 2:65c172c36cf5 | 62 | else |
Azure.IoT Build | 2:65c172c36cf5 | 63 | { |
AzureIoTClient | 30:0a147a179e7e | 64 | (void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size); |
Azure.IoT Build | 2:65c172c36cf5 | 65 | // If we receive the work 'quit' then we stop running |
AzureIoTClient | 30:0a147a179e7e | 66 | if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0) |
Azure.IoT Build | 2:65c172c36cf5 | 67 | { |
AzureIoTClient | 10:f77bb0044e11 | 68 | g_continueRunning = false; |
Azure.IoT Build | 2:65c172c36cf5 | 69 | } |
Azure.IoT Build | 2:65c172c36cf5 | 70 | } |
Azure.IoT Build | 2:65c172c36cf5 | 71 | |
Azure.IoT Build | 2:65c172c36cf5 | 72 | // Retrieve properties from the message |
AzureIoTClient | 30:0a147a179e7e | 73 | mapProperties = IoTHubMessage_Properties(message); |
Azure.IoT Build | 2:65c172c36cf5 | 74 | if (mapProperties != NULL) |
Azure.IoT Build | 2:65c172c36cf5 | 75 | { |
Azure.IoT Build | 2:65c172c36cf5 | 76 | const char*const* keys; |
Azure.IoT Build | 2:65c172c36cf5 | 77 | const char*const* values; |
Azure.IoT Build | 2:65c172c36cf5 | 78 | size_t propertyCount = 0; |
Azure.IoT Build | 2:65c172c36cf5 | 79 | if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK) |
Azure.IoT Build | 2:65c172c36cf5 | 80 | { |
Azure.IoT Build | 2:65c172c36cf5 | 81 | if (propertyCount > 0) |
Azure.IoT Build | 2:65c172c36cf5 | 82 | { |
AzureIoTClient | 30:0a147a179e7e | 83 | size_t index; |
AzureIoTClient | 30:0a147a179e7e | 84 | |
AzureIoTClient | 30:0a147a179e7e | 85 | printf(" Message Properties:\r\n"); |
AzureIoTClient | 27:7c908b91210a | 86 | for (index = 0; index < propertyCount; index++) |
Azure.IoT Build | 2:65c172c36cf5 | 87 | { |
Azure.IoT Build | 2:65c172c36cf5 | 88 | (void)printf("\tKey: %s Value: %s\r\n", keys[index], values[index]); |
Azure.IoT Build | 2:65c172c36cf5 | 89 | } |
Azure.IoT Build | 2:65c172c36cf5 | 90 | (void)printf("\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 91 | } |
Azure.IoT Build | 2:65c172c36cf5 | 92 | } |
Azure.IoT Build | 2:65c172c36cf5 | 93 | } |
Azure.IoT Build | 2:65c172c36cf5 | 94 | |
Azure.IoT Build | 2:65c172c36cf5 | 95 | /* Some device specific action code goes here... */ |
Azure.IoT Build | 2:65c172c36cf5 | 96 | (*counter)++; |
Azure.IoT Build | 2:65c172c36cf5 | 97 | return IOTHUBMESSAGE_ACCEPTED; |
Azure.IoT Build | 2:65c172c36cf5 | 98 | } |
Azure.IoT Build | 2:65c172c36cf5 | 99 | |
Azure.IoT Build | 2:65c172c36cf5 | 100 | static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
Azure.IoT Build | 2:65c172c36cf5 | 101 | { |
Azure.IoT Build | 2:65c172c36cf5 | 102 | EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback; |
AzureIoTClient | 15:2f2b3dc3ecb4 | 103 | (void)printf("Confirmation[%d] received for message tracking id = %zu with result = %s\r\n", callbackCounter, eventInstance->messageTrackingId, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); |
Azure.IoT Build | 2:65c172c36cf5 | 104 | /* Some device specific action code goes here... */ |
Azure.IoT Build | 2:65c172c36cf5 | 105 | callbackCounter++; |
Azure.IoT Build | 2:65c172c36cf5 | 106 | IoTHubMessage_Destroy(eventInstance->messageHandle); |
Azure.IoT Build | 2:65c172c36cf5 | 107 | } |
Azure.IoT Build | 2:65c172c36cf5 | 108 | |
Azure.IoT Build | 2:65c172c36cf5 | 109 | void iothub_client_sample_mqtt_run(void) |
Azure.IoT Build | 2:65c172c36cf5 | 110 | { |
Azure.IoT Build | 2:65c172c36cf5 | 111 | IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle; |
Azure.IoT Build | 2:65c172c36cf5 | 112 | |
Azure.IoT Build | 2:65c172c36cf5 | 113 | EVENT_INSTANCE messages[MESSAGE_COUNT]; |
Azure.IoT Build | 2:65c172c36cf5 | 114 | |
AzureIoTClient | 10:f77bb0044e11 | 115 | g_continueRunning = true; |
Azure.IoT Build | 2:65c172c36cf5 | 116 | srand((unsigned int)time(NULL)); |
Azure.IoT Build | 2:65c172c36cf5 | 117 | double avgWindSpeed = 10.0; |
Azure.IoT Build | 2:65c172c36cf5 | 118 | |
Azure.IoT Build | 2:65c172c36cf5 | 119 | callbackCounter = 0; |
Azure.IoT Build | 2:65c172c36cf5 | 120 | int receiveContext = 0; |
Azure.IoT Build | 2:65c172c36cf5 | 121 | |
Azure.IoT Build | 2:65c172c36cf5 | 122 | if (platform_init() != 0) |
Azure.IoT Build | 2:65c172c36cf5 | 123 | { |
Azure.IoT Build | 2:65c172c36cf5 | 124 | (void)printf("Failed to initialize the platform.\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 125 | } |
Azure.IoT Build | 2:65c172c36cf5 | 126 | else |
Azure.IoT Build | 2:65c172c36cf5 | 127 | { |
Azure.IoT Build | 2:65c172c36cf5 | 128 | if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol)) == NULL) |
Azure.IoT Build | 2:65c172c36cf5 | 129 | { |
Azure.IoT Build | 2:65c172c36cf5 | 130 | (void)printf("ERROR: iotHubClientHandle is NULL!\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 131 | } |
Azure.IoT Build | 2:65c172c36cf5 | 132 | else |
Azure.IoT Build | 2:65c172c36cf5 | 133 | { |
Azure.IoT Build | 2:65c172c36cf5 | 134 | bool traceOn = true; |
Azure.IoT Build | 2:65c172c36cf5 | 135 | IoTHubClient_LL_SetOption(iotHubClientHandle, "logtrace", &traceOn); |
Azure.IoT Build | 2:65c172c36cf5 | 136 | |
Azure.IoT Build | 2:65c172c36cf5 | 137 | #ifdef MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 2:65c172c36cf5 | 138 | // For mbed add the certificate information |
Azure.IoT Build | 2:65c172c36cf5 | 139 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 2:65c172c36cf5 | 140 | { |
Azure.IoT Build | 2:65c172c36cf5 | 141 | printf("failure to set option \"TrustedCerts\"\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 142 | } |
Azure.IoT Build | 2:65c172c36cf5 | 143 | #endif // MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 2:65c172c36cf5 | 144 | |
Azure.IoT Build | 2:65c172c36cf5 | 145 | /* Setting Message call back, so we can receive Commands. */ |
Azure.IoT Build | 2:65c172c36cf5 | 146 | if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 2:65c172c36cf5 | 147 | { |
Azure.IoT Build | 2:65c172c36cf5 | 148 | (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 149 | } |
Azure.IoT Build | 2:65c172c36cf5 | 150 | else |
Azure.IoT Build | 2:65c172c36cf5 | 151 | { |
Azure.IoT Build | 2:65c172c36cf5 | 152 | (void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 153 | |
Azure.IoT Build | 2:65c172c36cf5 | 154 | /* Now that we are ready to receive commands, let's send some messages */ |
AzureIoTClient | 10:f77bb0044e11 | 155 | size_t iterator = 0; |
AzureIoTClient | 10:f77bb0044e11 | 156 | do |
Azure.IoT Build | 2:65c172c36cf5 | 157 | { |
AzureIoTClient | 10:f77bb0044e11 | 158 | if (iterator < MESSAGE_COUNT) |
Azure.IoT Build | 2:65c172c36cf5 | 159 | { |
AzureIoTClient | 10:f77bb0044e11 | 160 | sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f}", avgWindSpeed + (rand() % 4 + 2)); |
AzureIoTClient | 10:f77bb0044e11 | 161 | if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL) |
AzureIoTClient | 6:02304619a9fa | 162 | { |
AzureIoTClient | 10:f77bb0044e11 | 163 | (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); |
Azure.IoT Build | 2:65c172c36cf5 | 164 | } |
Azure.IoT Build | 2:65c172c36cf5 | 165 | else |
Azure.IoT Build | 2:65c172c36cf5 | 166 | { |
AzureIoTClient | 10:f77bb0044e11 | 167 | messages[iterator].messageTrackingId = iterator; |
AzureIoTClient | 10:f77bb0044e11 | 168 | MAP_HANDLE propMap = IoTHubMessage_Properties(messages[iterator].messageHandle); |
AzureIoTClient | 15:2f2b3dc3ecb4 | 169 | (void)sprintf_s(propText, sizeof(propText), "PropMsg_%zu", iterator); |
AzureIoTClient | 10:f77bb0044e11 | 170 | if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK) |
AzureIoTClient | 10:f77bb0044e11 | 171 | { |
AzureIoTClient | 10:f77bb0044e11 | 172 | (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n"); |
AzureIoTClient | 10:f77bb0044e11 | 173 | } |
AzureIoTClient | 10:f77bb0044e11 | 174 | |
AzureIoTClient | 10:f77bb0044e11 | 175 | if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 10:f77bb0044e11 | 176 | { |
AzureIoTClient | 10:f77bb0044e11 | 177 | (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n"); |
AzureIoTClient | 10:f77bb0044e11 | 178 | } |
AzureIoTClient | 10:f77bb0044e11 | 179 | else |
AzureIoTClient | 10:f77bb0044e11 | 180 | { |
AzureIoTClient | 10:f77bb0044e11 | 181 | (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", (int)iterator); |
AzureIoTClient | 10:f77bb0044e11 | 182 | } |
Azure.IoT Build | 2:65c172c36cf5 | 183 | } |
Azure.IoT Build | 2:65c172c36cf5 | 184 | } |
AzureIoTClient | 10:f77bb0044e11 | 185 | IoTHubClient_LL_DoWork(iotHubClientHandle); |
AzureIoTClient | 10:f77bb0044e11 | 186 | ThreadAPI_Sleep(1); |
Azure.IoT Build | 2:65c172c36cf5 | 187 | |
AzureIoTClient | 10:f77bb0044e11 | 188 | iterator++; |
AzureIoTClient | 10:f77bb0044e11 | 189 | } while (g_continueRunning); |
Azure.IoT Build | 14:0d0177062483 | 190 | |
Azure.IoT Build | 14:0d0177062483 | 191 | (void)printf("iothub_client_sample_mqtt has gotten quit message, call DoWork %d more time to complete final sending...\r\n", DOWORK_LOOP_NUM); |
AzureIoTClient | 27:7c908b91210a | 192 | size_t index = 0; |
AzureIoTClient | 27:7c908b91210a | 193 | for (index = 0; index < DOWORK_LOOP_NUM; index++) |
Azure.IoT Build | 14:0d0177062483 | 194 | { |
Azure.IoT Build | 14:0d0177062483 | 195 | IoTHubClient_LL_DoWork(iotHubClientHandle); |
Azure.IoT Build | 14:0d0177062483 | 196 | ThreadAPI_Sleep(1); |
Azure.IoT Build | 14:0d0177062483 | 197 | } |
Azure.IoT Build | 2:65c172c36cf5 | 198 | } |
Azure.IoT Build | 2:65c172c36cf5 | 199 | IoTHubClient_LL_Destroy(iotHubClientHandle); |
Azure.IoT Build | 2:65c172c36cf5 | 200 | } |
Azure.IoT Build | 2:65c172c36cf5 | 201 | platform_deinit(); |
Azure.IoT Build | 2:65c172c36cf5 | 202 | } |
Azure.IoT Build | 2:65c172c36cf5 | 203 | } |
AzureIoTClient | 10:f77bb0044e11 | 204 | |
AzureIoTClient | 10:f77bb0044e11 | 205 | int main(void) |
AzureIoTClient | 10:f77bb0044e11 | 206 | { |
AzureIoTClient | 10:f77bb0044e11 | 207 | iothub_client_sample_mqtt_run(); |
AzureIoTClient | 10:f77bb0044e11 | 208 | return 0; |
AzureIoTClient | 10:f77bb0044e11 | 209 | } |