IoTHub raw messaging sample using HTTP
Dependencies: iothub_client EthernetInterface iothub_http_transport mbed-rtos mbed wolfSSL azure_c_shared_utility NTPClient
This sample showcases the usage of Azure IoT client libraries with the HTTP transport for sending/receiving raw messages from an IoT Hub.
iothub_client_sample_http.c@33:f8eb46ca453d, 2016-03-11 (annotated)
- Committer:
- Azure.IoT Build
- Date:
- Fri Mar 11 17:03:09 2016 -0800
- Revision:
- 33:f8eb46ca453d
- Parent:
- 20:52f8298d7256
- Child:
- 38:eb426458564b
1.0.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 18:e3e29fc771c9 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 18:e3e29fc771c9 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 18:e3e29fc771c9 | 3 | |
AzureIoTClient | 18:e3e29fc771c9 | 4 | #include <stdio.h> |
AzureIoTClient | 18:e3e29fc771c9 | 5 | #include <stdlib.h> |
AzureIoTClient | 18:e3e29fc771c9 | 6 | |
AzureIoTClient | 18:e3e29fc771c9 | 7 | /* This sample uses the _LL APIs of iothub_client for example purposes. |
AzureIoTClient | 18:e3e29fc771c9 | 8 | That does not mean that HTTP only works with the _LL APIs. |
AzureIoTClient | 18:e3e29fc771c9 | 9 | Simply changing the using the convenience layer (functions not having _LL) |
AzureIoTClient | 18:e3e29fc771c9 | 10 | and removing calls to _DoWork will yield the same results. */ |
AzureIoTClient | 18:e3e29fc771c9 | 11 | |
AzureIoTClient | 20:52f8298d7256 | 12 | #ifdef ARDUINO |
AzureIoTClient | 20:52f8298d7256 | 13 | #include "AzureIoT.h" |
AzureIoTClient | 20:52f8298d7256 | 14 | #else |
AzureIoTClient | 18:e3e29fc771c9 | 15 | #include "iothub_client_ll.h" |
AzureIoTClient | 18:e3e29fc771c9 | 16 | #include "iothub_message.h" |
AzureIoTClient | 18:e3e29fc771c9 | 17 | #include "crt_abstractions.h" |
AzureIoTClient | 18:e3e29fc771c9 | 18 | #include "iothubtransporthttp.h" |
Azure.IoT Build | 33:f8eb46ca453d | 19 | #include "platform.h" |
AzureIoTClient | 20:52f8298d7256 | 20 | #endif |
AzureIoTClient | 18:e3e29fc771c9 | 21 | |
AzureIoTClient | 18:e3e29fc771c9 | 22 | #ifdef MBED_BUILD_TIMESTAMP |
AzureIoTClient | 18:e3e29fc771c9 | 23 | #include "certs.h" |
AzureIoTClient | 18:e3e29fc771c9 | 24 | #endif // MBED_BUILD_TIMESTAMP |
AzureIoTClient | 18:e3e29fc771c9 | 25 | |
AzureIoTClient | 20:52f8298d7256 | 26 | /*String containing Hostname, Device Id & Device Key in the format: */ |
AzureIoTClient | 20:52f8298d7256 | 27 | /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */ |
AzureIoTClient | 18:e3e29fc771c9 | 28 | static const char* connectionString = "[device connection string]"; |
AzureIoTClient | 18:e3e29fc771c9 | 29 | static int callbackCounter; |
AzureIoTClient | 18:e3e29fc771c9 | 30 | |
AzureIoTClient | 18:e3e29fc771c9 | 31 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES); |
AzureIoTClient | 18:e3e29fc771c9 | 32 | |
AzureIoTClient | 18:e3e29fc771c9 | 33 | typedef struct EVENT_INSTANCE_TAG |
AzureIoTClient | 18:e3e29fc771c9 | 34 | { |
AzureIoTClient | 18:e3e29fc771c9 | 35 | IOTHUB_MESSAGE_HANDLE messageHandle; |
AzureIoTClient | 18:e3e29fc771c9 | 36 | int messageTrackingId; // For tracking the messages within the user callback. |
AzureIoTClient | 18:e3e29fc771c9 | 37 | } EVENT_INSTANCE; |
AzureIoTClient | 18:e3e29fc771c9 | 38 | |
AzureIoTClient | 18:e3e29fc771c9 | 39 | static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
AzureIoTClient | 18:e3e29fc771c9 | 40 | { |
AzureIoTClient | 18:e3e29fc771c9 | 41 | int* counter = (int*)userContextCallback; |
AzureIoTClient | 18:e3e29fc771c9 | 42 | const char* buffer; |
AzureIoTClient | 18:e3e29fc771c9 | 43 | size_t size; |
AzureIoTClient | 20:52f8298d7256 | 44 | MAP_HANDLE mapProperties; |
AzureIoTClient | 20:52f8298d7256 | 45 | |
AzureIoTClient | 18:e3e29fc771c9 | 46 | if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 47 | { |
AzureIoTClient | 18:e3e29fc771c9 | 48 | printf("unable to retrieve the message data\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 49 | } |
AzureIoTClient | 18:e3e29fc771c9 | 50 | else |
AzureIoTClient | 18:e3e29fc771c9 | 51 | { |
AzureIoTClient | 18:e3e29fc771c9 | 52 | (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size); |
AzureIoTClient | 18:e3e29fc771c9 | 53 | } |
AzureIoTClient | 18:e3e29fc771c9 | 54 | |
AzureIoTClient | 18:e3e29fc771c9 | 55 | // Retrieve properties from the message |
AzureIoTClient | 20:52f8298d7256 | 56 | mapProperties = IoTHubMessage_Properties(message); |
AzureIoTClient | 18:e3e29fc771c9 | 57 | if (mapProperties != NULL) |
AzureIoTClient | 18:e3e29fc771c9 | 58 | { |
AzureIoTClient | 18:e3e29fc771c9 | 59 | const char*const* keys; |
AzureIoTClient | 18:e3e29fc771c9 | 60 | const char*const* values; |
AzureIoTClient | 18:e3e29fc771c9 | 61 | size_t propertyCount = 0; |
AzureIoTClient | 18:e3e29fc771c9 | 62 | if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 63 | { |
AzureIoTClient | 18:e3e29fc771c9 | 64 | if (propertyCount > 0) |
AzureIoTClient | 18:e3e29fc771c9 | 65 | { |
AzureIoTClient | 20:52f8298d7256 | 66 | size_t index; |
AzureIoTClient | 20:52f8298d7256 | 67 | |
AzureIoTClient | 18:e3e29fc771c9 | 68 | printf("Message Properties:\r\n"); |
AzureIoTClient | 20:52f8298d7256 | 69 | for (index = 0; index < propertyCount; index++) |
AzureIoTClient | 18:e3e29fc771c9 | 70 | { |
AzureIoTClient | 18:e3e29fc771c9 | 71 | printf("\tKey: %s Value: %s\r\n", keys[index], values[index]); |
AzureIoTClient | 18:e3e29fc771c9 | 72 | } |
AzureIoTClient | 18:e3e29fc771c9 | 73 | printf("\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 74 | } |
AzureIoTClient | 18:e3e29fc771c9 | 75 | } |
AzureIoTClient | 18:e3e29fc771c9 | 76 | } |
AzureIoTClient | 18:e3e29fc771c9 | 77 | |
AzureIoTClient | 18:e3e29fc771c9 | 78 | /* Some device specific action code goes here... */ |
AzureIoTClient | 18:e3e29fc771c9 | 79 | (*counter)++; |
AzureIoTClient | 18:e3e29fc771c9 | 80 | return IOTHUBMESSAGE_ACCEPTED; |
AzureIoTClient | 18:e3e29fc771c9 | 81 | } |
AzureIoTClient | 18:e3e29fc771c9 | 82 | |
AzureIoTClient | 18:e3e29fc771c9 | 83 | static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
AzureIoTClient | 18:e3e29fc771c9 | 84 | { |
AzureIoTClient | 18:e3e29fc771c9 | 85 | EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback; |
AzureIoTClient | 18:e3e29fc771c9 | 86 | (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)); |
AzureIoTClient | 18:e3e29fc771c9 | 87 | /* Some device specific action code goes here... */ |
AzureIoTClient | 18:e3e29fc771c9 | 88 | callbackCounter++; |
AzureIoTClient | 18:e3e29fc771c9 | 89 | IoTHubMessage_Destroy(eventInstance->messageHandle); |
AzureIoTClient | 18:e3e29fc771c9 | 90 | } |
AzureIoTClient | 18:e3e29fc771c9 | 91 | |
AzureIoTClient | 18:e3e29fc771c9 | 92 | static char msgText[1024]; |
AzureIoTClient | 18:e3e29fc771c9 | 93 | static char propText[1024]; |
AzureIoTClient | 18:e3e29fc771c9 | 94 | #define MESSAGE_COUNT 5 |
AzureIoTClient | 18:e3e29fc771c9 | 95 | |
AzureIoTClient | 18:e3e29fc771c9 | 96 | void iothub_client_sample_http_run(void) |
AzureIoTClient | 18:e3e29fc771c9 | 97 | { |
AzureIoTClient | 18:e3e29fc771c9 | 98 | IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle; |
AzureIoTClient | 18:e3e29fc771c9 | 99 | |
AzureIoTClient | 18:e3e29fc771c9 | 100 | EVENT_INSTANCE messages[MESSAGE_COUNT]; |
AzureIoTClient | 20:52f8298d7256 | 101 | double avgWindSpeed = 10.0; |
AzureIoTClient | 20:52f8298d7256 | 102 | int receiveContext = 0; |
AzureIoTClient | 18:e3e29fc771c9 | 103 | |
AzureIoTClient | 18:e3e29fc771c9 | 104 | srand((unsigned int)time(NULL)); |
AzureIoTClient | 18:e3e29fc771c9 | 105 | |
AzureIoTClient | 18:e3e29fc771c9 | 106 | callbackCounter = 0; |
AzureIoTClient | 18:e3e29fc771c9 | 107 | |
Azure.IoT Build | 33:f8eb46ca453d | 108 | if (platform_init() != 0) |
AzureIoTClient | 18:e3e29fc771c9 | 109 | { |
Azure.IoT Build | 33:f8eb46ca453d | 110 | printf("Failed to initialize the platform.\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 111 | } |
AzureIoTClient | 18:e3e29fc771c9 | 112 | else |
AzureIoTClient | 18:e3e29fc771c9 | 113 | { |
Azure.IoT Build | 33:f8eb46ca453d | 114 | (void)printf("Starting the IoTHub client sample HTTP...\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 115 | |
Azure.IoT Build | 33:f8eb46ca453d | 116 | if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol)) == NULL) |
AzureIoTClient | 18:e3e29fc771c9 | 117 | { |
Azure.IoT Build | 33:f8eb46ca453d | 118 | (void)printf("ERROR: iotHubClientHandle is NULL!\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 119 | } |
AzureIoTClient | 18:e3e29fc771c9 | 120 | else |
AzureIoTClient | 18:e3e29fc771c9 | 121 | { |
Azure.IoT Build | 33:f8eb46ca453d | 122 | unsigned int timeout = 241000; |
Azure.IoT Build | 33:f8eb46ca453d | 123 | // Because it can poll "after 9 seconds" polls will happen effectively // at ~10 seconds. |
Azure.IoT Build | 33:f8eb46ca453d | 124 | // Note that for scalabilty, the default value of minimumPollingTime |
Azure.IoT Build | 33:f8eb46ca453d | 125 | // is 25 minutes. For more information, see: |
Azure.IoT Build | 33:f8eb46ca453d | 126 | // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging |
Azure.IoT Build | 33:f8eb46ca453d | 127 | unsigned int minimumPollingTime = 9; |
Azure.IoT Build | 33:f8eb46ca453d | 128 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "timeout", &timeout) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 33:f8eb46ca453d | 129 | { |
Azure.IoT Build | 33:f8eb46ca453d | 130 | printf("failure to set option \"timeout\"\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 131 | } |
AzureIoTClient | 20:52f8298d7256 | 132 | |
Azure.IoT Build | 33:f8eb46ca453d | 133 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 134 | { |
Azure.IoT Build | 33:f8eb46ca453d | 135 | printf("failure to set option \"MinimumPollingTime\"\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 136 | } |
Azure.IoT Build | 33:f8eb46ca453d | 137 | |
Azure.IoT Build | 33:f8eb46ca453d | 138 | #ifdef MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 33:f8eb46ca453d | 139 | // For mbed add the certificate information |
Azure.IoT Build | 33:f8eb46ca453d | 140 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 33:f8eb46ca453d | 141 | { |
Azure.IoT Build | 33:f8eb46ca453d | 142 | printf("failure to set option \"TrustedCerts\"\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 143 | } |
Azure.IoT Build | 33:f8eb46ca453d | 144 | #endif // MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 33:f8eb46ca453d | 145 | |
Azure.IoT Build | 33:f8eb46ca453d | 146 | /* Setting Message call back, so we can receive Commands. */ |
Azure.IoT Build | 33:f8eb46ca453d | 147 | if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 33:f8eb46ca453d | 148 | { |
Azure.IoT Build | 33:f8eb46ca453d | 149 | (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 150 | } |
Azure.IoT Build | 33:f8eb46ca453d | 151 | else |
Azure.IoT Build | 33:f8eb46ca453d | 152 | { |
Azure.IoT Build | 33:f8eb46ca453d | 153 | int i; |
Azure.IoT Build | 33:f8eb46ca453d | 154 | |
Azure.IoT Build | 33:f8eb46ca453d | 155 | (void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 156 | |
Azure.IoT Build | 33:f8eb46ca453d | 157 | /* Now that we are ready to receive commands, let's send some messages */ |
Azure.IoT Build | 33:f8eb46ca453d | 158 | for (i = 0; i < MESSAGE_COUNT; i++) |
AzureIoTClient | 18:e3e29fc771c9 | 159 | { |
Azure.IoT Build | 33:f8eb46ca453d | 160 | sprintf_s(msgText, sizeof(msgText), "{\"deviceId\": \"myFirstDevice\",\"windSpeed\": %.2f}", avgWindSpeed + (rand() % 4 + 2)); |
Azure.IoT Build | 33:f8eb46ca453d | 161 | if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL) |
AzureIoTClient | 18:e3e29fc771c9 | 162 | { |
Azure.IoT Build | 33:f8eb46ca453d | 163 | (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 164 | } |
AzureIoTClient | 18:e3e29fc771c9 | 165 | else |
AzureIoTClient | 18:e3e29fc771c9 | 166 | { |
Azure.IoT Build | 33:f8eb46ca453d | 167 | MAP_HANDLE propMap; |
Azure.IoT Build | 33:f8eb46ca453d | 168 | |
Azure.IoT Build | 33:f8eb46ca453d | 169 | messages[i].messageTrackingId = i; |
Azure.IoT Build | 33:f8eb46ca453d | 170 | |
Azure.IoT Build | 33:f8eb46ca453d | 171 | propMap = IoTHubMessage_Properties(messages[i].messageHandle); |
Azure.IoT Build | 33:f8eb46ca453d | 172 | sprintf_s(propText, sizeof(propText), "PropMsg_%d", i); |
Azure.IoT Build | 33:f8eb46ca453d | 173 | if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK) |
Azure.IoT Build | 33:f8eb46ca453d | 174 | { |
Azure.IoT Build | 33:f8eb46ca453d | 175 | (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 176 | } |
Azure.IoT Build | 33:f8eb46ca453d | 177 | |
Azure.IoT Build | 33:f8eb46ca453d | 178 | if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 33:f8eb46ca453d | 179 | { |
Azure.IoT Build | 33:f8eb46ca453d | 180 | (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 181 | } |
Azure.IoT Build | 33:f8eb46ca453d | 182 | else |
Azure.IoT Build | 33:f8eb46ca453d | 183 | { |
Azure.IoT Build | 33:f8eb46ca453d | 184 | (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", i); |
Azure.IoT Build | 33:f8eb46ca453d | 185 | } |
Azure.IoT Build | 33:f8eb46ca453d | 186 | |
AzureIoTClient | 18:e3e29fc771c9 | 187 | } |
AzureIoTClient | 18:e3e29fc771c9 | 188 | } |
AzureIoTClient | 18:e3e29fc771c9 | 189 | } |
AzureIoTClient | 18:e3e29fc771c9 | 190 | |
Azure.IoT Build | 33:f8eb46ca453d | 191 | /* Wait for Commands. */ |
Azure.IoT Build | 33:f8eb46ca453d | 192 | while (1) |
Azure.IoT Build | 33:f8eb46ca453d | 193 | { |
Azure.IoT Build | 33:f8eb46ca453d | 194 | IoTHubClient_LL_DoWork(iotHubClientHandle); |
Azure.IoT Build | 33:f8eb46ca453d | 195 | } |
Azure.IoT Build | 33:f8eb46ca453d | 196 | |
Azure.IoT Build | 33:f8eb46ca453d | 197 | IoTHubClient_LL_Destroy(iotHubClientHandle); |
AzureIoTClient | 18:e3e29fc771c9 | 198 | } |
Azure.IoT Build | 33:f8eb46ca453d | 199 | platform_deinit(); |
AzureIoTClient | 18:e3e29fc771c9 | 200 | } |
AzureIoTClient | 18:e3e29fc771c9 | 201 | } |