Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: iothub_client EthernetInterface iothub_http_transport mbed-rtos mbed wolfSSL azure_c_shared_utility NTPClient
iothub_client_sample_http.c@20:52f8298d7256, 2015-12-17 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Thu Dec 17 18:23:58 2015 -0800
- Revision:
- 20:52f8298d7256
- Parent:
- 18:e3e29fc771c9
- Child:
- 33:f8eb46ca453d
v1.0.0-preview.4
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" |
AzureIoTClient | 20:52f8298d7256 | 19 | #endif |
AzureIoTClient | 18:e3e29fc771c9 | 20 | |
AzureIoTClient | 18:e3e29fc771c9 | 21 | #ifdef MBED_BUILD_TIMESTAMP |
AzureIoTClient | 18:e3e29fc771c9 | 22 | #include "certs.h" |
AzureIoTClient | 18:e3e29fc771c9 | 23 | #endif // MBED_BUILD_TIMESTAMP |
AzureIoTClient | 18:e3e29fc771c9 | 24 | |
AzureIoTClient | 20:52f8298d7256 | 25 | /*String containing Hostname, Device Id & Device Key in the format: */ |
AzureIoTClient | 20:52f8298d7256 | 26 | /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */ |
AzureIoTClient | 18:e3e29fc771c9 | 27 | static const char* connectionString = "[device connection string]"; |
AzureIoTClient | 18:e3e29fc771c9 | 28 | static int callbackCounter; |
AzureIoTClient | 18:e3e29fc771c9 | 29 | |
AzureIoTClient | 18:e3e29fc771c9 | 30 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES); |
AzureIoTClient | 18:e3e29fc771c9 | 31 | |
AzureIoTClient | 18:e3e29fc771c9 | 32 | typedef struct EVENT_INSTANCE_TAG |
AzureIoTClient | 18:e3e29fc771c9 | 33 | { |
AzureIoTClient | 18:e3e29fc771c9 | 34 | IOTHUB_MESSAGE_HANDLE messageHandle; |
AzureIoTClient | 18:e3e29fc771c9 | 35 | int messageTrackingId; // For tracking the messages within the user callback. |
AzureIoTClient | 18:e3e29fc771c9 | 36 | } EVENT_INSTANCE; |
AzureIoTClient | 18:e3e29fc771c9 | 37 | |
AzureIoTClient | 18:e3e29fc771c9 | 38 | static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
AzureIoTClient | 18:e3e29fc771c9 | 39 | { |
AzureIoTClient | 18:e3e29fc771c9 | 40 | int* counter = (int*)userContextCallback; |
AzureIoTClient | 18:e3e29fc771c9 | 41 | const char* buffer; |
AzureIoTClient | 18:e3e29fc771c9 | 42 | size_t size; |
AzureIoTClient | 20:52f8298d7256 | 43 | MAP_HANDLE mapProperties; |
AzureIoTClient | 20:52f8298d7256 | 44 | |
AzureIoTClient | 18:e3e29fc771c9 | 45 | if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 46 | { |
AzureIoTClient | 18:e3e29fc771c9 | 47 | printf("unable to retrieve the message data\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 48 | } |
AzureIoTClient | 18:e3e29fc771c9 | 49 | else |
AzureIoTClient | 18:e3e29fc771c9 | 50 | { |
AzureIoTClient | 18:e3e29fc771c9 | 51 | (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size); |
AzureIoTClient | 18:e3e29fc771c9 | 52 | } |
AzureIoTClient | 18:e3e29fc771c9 | 53 | |
AzureIoTClient | 18:e3e29fc771c9 | 54 | // Retrieve properties from the message |
AzureIoTClient | 20:52f8298d7256 | 55 | mapProperties = IoTHubMessage_Properties(message); |
AzureIoTClient | 18:e3e29fc771c9 | 56 | if (mapProperties != NULL) |
AzureIoTClient | 18:e3e29fc771c9 | 57 | { |
AzureIoTClient | 18:e3e29fc771c9 | 58 | const char*const* keys; |
AzureIoTClient | 18:e3e29fc771c9 | 59 | const char*const* values; |
AzureIoTClient | 18:e3e29fc771c9 | 60 | size_t propertyCount = 0; |
AzureIoTClient | 18:e3e29fc771c9 | 61 | if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 62 | { |
AzureIoTClient | 18:e3e29fc771c9 | 63 | if (propertyCount > 0) |
AzureIoTClient | 18:e3e29fc771c9 | 64 | { |
AzureIoTClient | 20:52f8298d7256 | 65 | size_t index; |
AzureIoTClient | 20:52f8298d7256 | 66 | |
AzureIoTClient | 18:e3e29fc771c9 | 67 | printf("Message Properties:\r\n"); |
AzureIoTClient | 20:52f8298d7256 | 68 | for (index = 0; index < propertyCount; index++) |
AzureIoTClient | 18:e3e29fc771c9 | 69 | { |
AzureIoTClient | 18:e3e29fc771c9 | 70 | printf("\tKey: %s Value: %s\r\n", keys[index], values[index]); |
AzureIoTClient | 18:e3e29fc771c9 | 71 | } |
AzureIoTClient | 18:e3e29fc771c9 | 72 | printf("\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 73 | } |
AzureIoTClient | 18:e3e29fc771c9 | 74 | } |
AzureIoTClient | 18:e3e29fc771c9 | 75 | } |
AzureIoTClient | 18:e3e29fc771c9 | 76 | |
AzureIoTClient | 18:e3e29fc771c9 | 77 | /* Some device specific action code goes here... */ |
AzureIoTClient | 18:e3e29fc771c9 | 78 | (*counter)++; |
AzureIoTClient | 18:e3e29fc771c9 | 79 | return IOTHUBMESSAGE_ACCEPTED; |
AzureIoTClient | 18:e3e29fc771c9 | 80 | } |
AzureIoTClient | 18:e3e29fc771c9 | 81 | |
AzureIoTClient | 18:e3e29fc771c9 | 82 | static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
AzureIoTClient | 18:e3e29fc771c9 | 83 | { |
AzureIoTClient | 18:e3e29fc771c9 | 84 | EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback; |
AzureIoTClient | 18:e3e29fc771c9 | 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)); |
AzureIoTClient | 18:e3e29fc771c9 | 86 | /* Some device specific action code goes here... */ |
AzureIoTClient | 18:e3e29fc771c9 | 87 | callbackCounter++; |
AzureIoTClient | 18:e3e29fc771c9 | 88 | IoTHubMessage_Destroy(eventInstance->messageHandle); |
AzureIoTClient | 18:e3e29fc771c9 | 89 | } |
AzureIoTClient | 18:e3e29fc771c9 | 90 | |
AzureIoTClient | 18:e3e29fc771c9 | 91 | static char msgText[1024]; |
AzureIoTClient | 18:e3e29fc771c9 | 92 | static char propText[1024]; |
AzureIoTClient | 18:e3e29fc771c9 | 93 | #define MESSAGE_COUNT 5 |
AzureIoTClient | 18:e3e29fc771c9 | 94 | |
AzureIoTClient | 18:e3e29fc771c9 | 95 | void iothub_client_sample_http_run(void) |
AzureIoTClient | 18:e3e29fc771c9 | 96 | { |
AzureIoTClient | 18:e3e29fc771c9 | 97 | IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle; |
AzureIoTClient | 18:e3e29fc771c9 | 98 | |
AzureIoTClient | 18:e3e29fc771c9 | 99 | EVENT_INSTANCE messages[MESSAGE_COUNT]; |
AzureIoTClient | 20:52f8298d7256 | 100 | double avgWindSpeed = 10.0; |
AzureIoTClient | 20:52f8298d7256 | 101 | int receiveContext = 0; |
AzureIoTClient | 18:e3e29fc771c9 | 102 | |
AzureIoTClient | 18:e3e29fc771c9 | 103 | srand((unsigned int)time(NULL)); |
AzureIoTClient | 18:e3e29fc771c9 | 104 | |
AzureIoTClient | 18:e3e29fc771c9 | 105 | callbackCounter = 0; |
AzureIoTClient | 18:e3e29fc771c9 | 106 | |
AzureIoTClient | 18:e3e29fc771c9 | 107 | (void)printf("Starting the IoTHub client sample HTTP...\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 108 | |
AzureIoTClient | 18:e3e29fc771c9 | 109 | if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol)) == NULL) |
AzureIoTClient | 18:e3e29fc771c9 | 110 | { |
AzureIoTClient | 18:e3e29fc771c9 | 111 | (void)printf("ERROR: iotHubClientHandle is NULL!\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 112 | } |
AzureIoTClient | 18:e3e29fc771c9 | 113 | else |
AzureIoTClient | 18:e3e29fc771c9 | 114 | { |
AzureIoTClient | 18:e3e29fc771c9 | 115 | unsigned int timeout = 241000; |
AzureIoTClient | 20:52f8298d7256 | 116 | // Because it can poll "after 9 seconds" polls will happen effectively // at ~10 seconds. |
AzureIoTClient | 20:52f8298d7256 | 117 | // Note that for scalabilty, the default value of minimumPollingTime |
AzureIoTClient | 20:52f8298d7256 | 118 | // is 25 minutes. For more information, see: |
AzureIoTClient | 20:52f8298d7256 | 119 | // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging |
AzureIoTClient | 20:52f8298d7256 | 120 | unsigned int minimumPollingTime = 9; |
AzureIoTClient | 18:e3e29fc771c9 | 121 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "timeout", &timeout) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 122 | { |
AzureIoTClient | 18:e3e29fc771c9 | 123 | printf("failure to set option \"timeout\"\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 124 | } |
AzureIoTClient | 18:e3e29fc771c9 | 125 | |
AzureIoTClient | 18:e3e29fc771c9 | 126 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 127 | { |
AzureIoTClient | 18:e3e29fc771c9 | 128 | printf("failure to set option \"MinimumPollingTime\"\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 129 | } |
AzureIoTClient | 18:e3e29fc771c9 | 130 | |
AzureIoTClient | 18:e3e29fc771c9 | 131 | #ifdef MBED_BUILD_TIMESTAMP |
AzureIoTClient | 18:e3e29fc771c9 | 132 | // For mbed add the certificate information |
AzureIoTClient | 18:e3e29fc771c9 | 133 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 134 | { |
AzureIoTClient | 18:e3e29fc771c9 | 135 | printf("failure to set option \"TrustedCerts\"\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 136 | } |
AzureIoTClient | 18:e3e29fc771c9 | 137 | #endif // MBED_BUILD_TIMESTAMP |
AzureIoTClient | 18:e3e29fc771c9 | 138 | |
AzureIoTClient | 18:e3e29fc771c9 | 139 | /* Setting Message call back, so we can receive Commands. */ |
AzureIoTClient | 18:e3e29fc771c9 | 140 | if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 141 | { |
AzureIoTClient | 18:e3e29fc771c9 | 142 | (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 143 | } |
AzureIoTClient | 18:e3e29fc771c9 | 144 | else |
AzureIoTClient | 18:e3e29fc771c9 | 145 | { |
AzureIoTClient | 20:52f8298d7256 | 146 | int i; |
AzureIoTClient | 20:52f8298d7256 | 147 | |
AzureIoTClient | 18:e3e29fc771c9 | 148 | (void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 149 | |
AzureIoTClient | 18:e3e29fc771c9 | 150 | /* Now that we are ready to receive commands, let's send some messages */ |
AzureIoTClient | 20:52f8298d7256 | 151 | for (i = 0; i < MESSAGE_COUNT; i++) |
AzureIoTClient | 18:e3e29fc771c9 | 152 | { |
AzureIoTClient | 18:e3e29fc771c9 | 153 | sprintf_s(msgText, sizeof(msgText), "{\"deviceId\": \"myFirstDevice\",\"windSpeed\": %.2f}", avgWindSpeed + (rand() % 4 + 2)); |
AzureIoTClient | 18:e3e29fc771c9 | 154 | if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL) |
AzureIoTClient | 18:e3e29fc771c9 | 155 | { |
AzureIoTClient | 18:e3e29fc771c9 | 156 | (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 157 | } |
AzureIoTClient | 18:e3e29fc771c9 | 158 | else |
AzureIoTClient | 18:e3e29fc771c9 | 159 | { |
AzureIoTClient | 20:52f8298d7256 | 160 | MAP_HANDLE propMap; |
AzureIoTClient | 20:52f8298d7256 | 161 | |
AzureIoTClient | 18:e3e29fc771c9 | 162 | messages[i].messageTrackingId = i; |
AzureIoTClient | 18:e3e29fc771c9 | 163 | |
AzureIoTClient | 20:52f8298d7256 | 164 | propMap = IoTHubMessage_Properties(messages[i].messageHandle); |
AzureIoTClient | 18:e3e29fc771c9 | 165 | sprintf_s(propText, sizeof(propText), "PropMsg_%d", i); |
AzureIoTClient | 18:e3e29fc771c9 | 166 | if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 167 | { |
AzureIoTClient | 18:e3e29fc771c9 | 168 | (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 169 | } |
AzureIoTClient | 18:e3e29fc771c9 | 170 | |
AzureIoTClient | 18:e3e29fc771c9 | 171 | if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 172 | { |
AzureIoTClient | 18:e3e29fc771c9 | 173 | (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 174 | } |
AzureIoTClient | 18:e3e29fc771c9 | 175 | else |
AzureIoTClient | 18:e3e29fc771c9 | 176 | { |
AzureIoTClient | 18:e3e29fc771c9 | 177 | (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", i); |
AzureIoTClient | 18:e3e29fc771c9 | 178 | } |
AzureIoTClient | 18:e3e29fc771c9 | 179 | |
AzureIoTClient | 18:e3e29fc771c9 | 180 | } |
AzureIoTClient | 18:e3e29fc771c9 | 181 | } |
AzureIoTClient | 18:e3e29fc771c9 | 182 | } |
AzureIoTClient | 18:e3e29fc771c9 | 183 | |
AzureIoTClient | 18:e3e29fc771c9 | 184 | /* Wait for Commands. */ |
AzureIoTClient | 18:e3e29fc771c9 | 185 | while (1) |
AzureIoTClient | 18:e3e29fc771c9 | 186 | { |
AzureIoTClient | 18:e3e29fc771c9 | 187 | IoTHubClient_LL_DoWork(iotHubClientHandle); |
AzureIoTClient | 18:e3e29fc771c9 | 188 | } |
AzureIoTClient | 18:e3e29fc771c9 | 189 | |
AzureIoTClient | 18:e3e29fc771c9 | 190 | IoTHubClient_LL_Destroy(iotHubClientHandle); |
AzureIoTClient | 18:e3e29fc771c9 | 191 | } |
AzureIoTClient | 18:e3e29fc771c9 | 192 | } |