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@54:54dc461c75da, 2016-10-20 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Thu Oct 20 17:08:51 2016 -0700
- Revision:
- 54:54dc461c75da
- Parent:
- 49:2aa1b773af3c
- Child:
- 62:2320ad565fc3
1.0.10
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 | 41:7a930aa6dc01 | 8 | That does not mean that HTTP only works with the _LL APIs. |
AzureIoTClient | 41:7a930aa6dc01 | 9 | Simply changing the using the convenience layer (functions not having _LL) |
AzureIoTClient | 41:7a930aa6dc01 | 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 |
Azure.IoT Build | 46:91bd03862871 | 15 | #include "azure_c_shared_utility/threadapi.h" |
Azure.IoT Build | 46:91bd03862871 | 16 | #include "azure_c_shared_utility/crt_abstractions.h" |
Azure.IoT Build | 46:91bd03862871 | 17 | #include "azure_c_shared_utility/platform.h" |
AzureIoTClient | 18:e3e29fc771c9 | 18 | #include "iothub_client_ll.h" |
AzureIoTClient | 18:e3e29fc771c9 | 19 | #include "iothub_message.h" |
AzureIoTClient | 18:e3e29fc771c9 | 20 | #include "iothubtransporthttp.h" |
AzureIoTClient | 20:52f8298d7256 | 21 | #endif |
AzureIoTClient | 18:e3e29fc771c9 | 22 | |
AzureIoTClient | 18:e3e29fc771c9 | 23 | #ifdef MBED_BUILD_TIMESTAMP |
AzureIoTClient | 18:e3e29fc771c9 | 24 | #include "certs.h" |
AzureIoTClient | 18:e3e29fc771c9 | 25 | #endif // MBED_BUILD_TIMESTAMP |
AzureIoTClient | 18:e3e29fc771c9 | 26 | |
AzureIoTClient | 39:405f9c637ba4 | 27 | /*String containing Hostname, Device Id & Device Key in the format: */ |
AzureIoTClient | 39:405f9c637ba4 | 28 | /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */ |
AzureIoTClient | 39:405f9c637ba4 | 29 | /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */ |
AzureIoTClient | 18:e3e29fc771c9 | 30 | static const char* connectionString = "[device connection string]"; |
AzureIoTClient | 39:405f9c637ba4 | 31 | |
AzureIoTClient | 48:07c7ca66634a | 32 | |
AzureIoTClient | 18:e3e29fc771c9 | 33 | static int callbackCounter; |
AzureIoTClient | 42:289de245ba1d | 34 | static bool g_continueRunning; |
Azure.IoT Build | 46:91bd03862871 | 35 | static char msgText[1024]; |
Azure.IoT Build | 46:91bd03862871 | 36 | static char propText[1024]; |
Azure.IoT Build | 46:91bd03862871 | 37 | #define MESSAGE_COUNT 5 |
Azure.IoT Build | 46:91bd03862871 | 38 | #define DOWORK_LOOP_NUM 3 |
AzureIoTClient | 18:e3e29fc771c9 | 39 | |
AzureIoTClient | 18:e3e29fc771c9 | 40 | |
AzureIoTClient | 18:e3e29fc771c9 | 41 | typedef struct EVENT_INSTANCE_TAG |
AzureIoTClient | 18:e3e29fc771c9 | 42 | { |
AzureIoTClient | 18:e3e29fc771c9 | 43 | IOTHUB_MESSAGE_HANDLE messageHandle; |
AzureIoTClient | 47:bc18bf0e4a6a | 44 | size_t messageTrackingId; // For tracking the messages within the user callback. |
AzureIoTClient | 18:e3e29fc771c9 | 45 | } EVENT_INSTANCE; |
AzureIoTClient | 18:e3e29fc771c9 | 46 | |
AzureIoTClient | 18:e3e29fc771c9 | 47 | static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
AzureIoTClient | 18:e3e29fc771c9 | 48 | { |
AzureIoTClient | 18:e3e29fc771c9 | 49 | int* counter = (int*)userContextCallback; |
AzureIoTClient | 18:e3e29fc771c9 | 50 | const char* buffer; |
AzureIoTClient | 18:e3e29fc771c9 | 51 | size_t size; |
AzureIoTClient | 41:7a930aa6dc01 | 52 | MAP_HANDLE mapProperties; |
AzureIoTClient | 20:52f8298d7256 | 53 | |
AzureIoTClient | 18:e3e29fc771c9 | 54 | if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 55 | { |
AzureIoTClient | 18:e3e29fc771c9 | 56 | printf("unable to retrieve the message data\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 57 | } |
AzureIoTClient | 18:e3e29fc771c9 | 58 | else |
AzureIoTClient | 18:e3e29fc771c9 | 59 | { |
AzureIoTClient | 18:e3e29fc771c9 | 60 | (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size); |
AzureIoTClient | 54:54dc461c75da | 61 | if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0) |
AzureIoTClient | 42:289de245ba1d | 62 | { |
AzureIoTClient | 42:289de245ba1d | 63 | g_continueRunning = false; |
AzureIoTClient | 42:289de245ba1d | 64 | } |
AzureIoTClient | 18:e3e29fc771c9 | 65 | } |
AzureIoTClient | 18:e3e29fc771c9 | 66 | |
AzureIoTClient | 18:e3e29fc771c9 | 67 | // Retrieve properties from the message |
AzureIoTClient | 20:52f8298d7256 | 68 | mapProperties = IoTHubMessage_Properties(message); |
AzureIoTClient | 18:e3e29fc771c9 | 69 | if (mapProperties != NULL) |
AzureIoTClient | 18:e3e29fc771c9 | 70 | { |
AzureIoTClient | 18:e3e29fc771c9 | 71 | const char*const* keys; |
AzureIoTClient | 18:e3e29fc771c9 | 72 | const char*const* values; |
AzureIoTClient | 18:e3e29fc771c9 | 73 | size_t propertyCount = 0; |
AzureIoTClient | 18:e3e29fc771c9 | 74 | if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 75 | { |
AzureIoTClient | 18:e3e29fc771c9 | 76 | if (propertyCount > 0) |
AzureIoTClient | 18:e3e29fc771c9 | 77 | { |
AzureIoTClient | 41:7a930aa6dc01 | 78 | size_t index; |
AzureIoTClient | 20:52f8298d7256 | 79 | |
AzureIoTClient | 18:e3e29fc771c9 | 80 | printf("Message Properties:\r\n"); |
AzureIoTClient | 20:52f8298d7256 | 81 | for (index = 0; index < propertyCount; index++) |
AzureIoTClient | 18:e3e29fc771c9 | 82 | { |
AzureIoTClient | 18:e3e29fc771c9 | 83 | printf("\tKey: %s Value: %s\r\n", keys[index], values[index]); |
AzureIoTClient | 18:e3e29fc771c9 | 84 | } |
AzureIoTClient | 18:e3e29fc771c9 | 85 | printf("\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 86 | } |
AzureIoTClient | 18:e3e29fc771c9 | 87 | } |
AzureIoTClient | 18:e3e29fc771c9 | 88 | } |
AzureIoTClient | 18:e3e29fc771c9 | 89 | |
AzureIoTClient | 18:e3e29fc771c9 | 90 | /* Some device specific action code goes here... */ |
AzureIoTClient | 18:e3e29fc771c9 | 91 | (*counter)++; |
AzureIoTClient | 18:e3e29fc771c9 | 92 | return IOTHUBMESSAGE_ACCEPTED; |
AzureIoTClient | 18:e3e29fc771c9 | 93 | } |
AzureIoTClient | 18:e3e29fc771c9 | 94 | |
AzureIoTClient | 18:e3e29fc771c9 | 95 | static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
AzureIoTClient | 18:e3e29fc771c9 | 96 | { |
AzureIoTClient | 18:e3e29fc771c9 | 97 | EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback; |
AzureIoTClient | 49:2aa1b773af3c | 98 | |
AzureIoTClient | 49:2aa1b773af3c | 99 | (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)); |
AzureIoTClient | 49:2aa1b773af3c | 100 | |
AzureIoTClient | 18:e3e29fc771c9 | 101 | /* Some device specific action code goes here... */ |
AzureIoTClient | 18:e3e29fc771c9 | 102 | callbackCounter++; |
AzureIoTClient | 18:e3e29fc771c9 | 103 | IoTHubMessage_Destroy(eventInstance->messageHandle); |
AzureIoTClient | 18:e3e29fc771c9 | 104 | } |
AzureIoTClient | 18:e3e29fc771c9 | 105 | |
AzureIoTClient | 18:e3e29fc771c9 | 106 | void iothub_client_sample_http_run(void) |
AzureIoTClient | 18:e3e29fc771c9 | 107 | { |
AzureIoTClient | 18:e3e29fc771c9 | 108 | IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle; |
AzureIoTClient | 18:e3e29fc771c9 | 109 | |
AzureIoTClient | 18:e3e29fc771c9 | 110 | EVENT_INSTANCE messages[MESSAGE_COUNT]; |
AzureIoTClient | 20:52f8298d7256 | 111 | double avgWindSpeed = 10.0; |
AzureIoTClient | 20:52f8298d7256 | 112 | int receiveContext = 0; |
Azure.IoT Build | 46:91bd03862871 | 113 | |
AzureIoTClient | 42:289de245ba1d | 114 | g_continueRunning = true; |
AzureIoTClient | 18:e3e29fc771c9 | 115 | |
AzureIoTClient | 18:e3e29fc771c9 | 116 | srand((unsigned int)time(NULL)); |
AzureIoTClient | 18:e3e29fc771c9 | 117 | |
AzureIoTClient | 18:e3e29fc771c9 | 118 | callbackCounter = 0; |
AzureIoTClient | 18:e3e29fc771c9 | 119 | |
Azure.IoT Build | 33:f8eb46ca453d | 120 | if (platform_init() != 0) |
AzureIoTClient | 18:e3e29fc771c9 | 121 | { |
Azure.IoT Build | 33:f8eb46ca453d | 122 | printf("Failed to initialize the platform.\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 123 | } |
AzureIoTClient | 18:e3e29fc771c9 | 124 | else |
AzureIoTClient | 18:e3e29fc771c9 | 125 | { |
Azure.IoT Build | 33:f8eb46ca453d | 126 | (void)printf("Starting the IoTHub client sample HTTP...\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 127 | |
Azure.IoT Build | 33:f8eb46ca453d | 128 | if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol)) == NULL) |
AzureIoTClient | 18:e3e29fc771c9 | 129 | { |
Azure.IoT Build | 33:f8eb46ca453d | 130 | (void)printf("ERROR: iotHubClientHandle is NULL!\r\n"); |
AzureIoTClient | 18:e3e29fc771c9 | 131 | } |
AzureIoTClient | 18:e3e29fc771c9 | 132 | else |
AzureIoTClient | 18:e3e29fc771c9 | 133 | { |
Azure.IoT Build | 33:f8eb46ca453d | 134 | unsigned int timeout = 241000; |
Azure.IoT Build | 33:f8eb46ca453d | 135 | // Because it can poll "after 9 seconds" polls will happen effectively // at ~10 seconds. |
Azure.IoT Build | 33:f8eb46ca453d | 136 | // Note that for scalabilty, the default value of minimumPollingTime |
Azure.IoT Build | 33:f8eb46ca453d | 137 | // is 25 minutes. For more information, see: |
Azure.IoT Build | 33:f8eb46ca453d | 138 | // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging |
Azure.IoT Build | 33:f8eb46ca453d | 139 | unsigned int minimumPollingTime = 9; |
Azure.IoT Build | 33:f8eb46ca453d | 140 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "timeout", &timeout) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 33:f8eb46ca453d | 141 | { |
Azure.IoT Build | 33:f8eb46ca453d | 142 | printf("failure to set option \"timeout\"\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 143 | } |
AzureIoTClient | 20:52f8298d7256 | 144 | |
Azure.IoT Build | 33:f8eb46ca453d | 145 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 18:e3e29fc771c9 | 146 | { |
Azure.IoT Build | 33:f8eb46ca453d | 147 | printf("failure to set option \"MinimumPollingTime\"\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 148 | } |
Azure.IoT Build | 33:f8eb46ca453d | 149 | |
Azure.IoT Build | 33:f8eb46ca453d | 150 | #ifdef MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 33:f8eb46ca453d | 151 | // For mbed add the certificate information |
Azure.IoT Build | 33:f8eb46ca453d | 152 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 33:f8eb46ca453d | 153 | { |
Azure.IoT Build | 33:f8eb46ca453d | 154 | printf("failure to set option \"TrustedCerts\"\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 155 | } |
Azure.IoT Build | 33:f8eb46ca453d | 156 | #endif // MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 33:f8eb46ca453d | 157 | |
Azure.IoT Build | 33:f8eb46ca453d | 158 | /* Setting Message call back, so we can receive Commands. */ |
Azure.IoT Build | 33:f8eb46ca453d | 159 | if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 33:f8eb46ca453d | 160 | { |
Azure.IoT Build | 33:f8eb46ca453d | 161 | (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 162 | } |
Azure.IoT Build | 33:f8eb46ca453d | 163 | else |
Azure.IoT Build | 33:f8eb46ca453d | 164 | { |
Azure.IoT Build | 33:f8eb46ca453d | 165 | (void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 166 | |
Azure.IoT Build | 33:f8eb46ca453d | 167 | /* Now that we are ready to receive commands, let's send some messages */ |
AzureIoTClient | 42:289de245ba1d | 168 | size_t iterator = 0; |
AzureIoTClient | 42:289de245ba1d | 169 | do |
AzureIoTClient | 18:e3e29fc771c9 | 170 | { |
AzureIoTClient | 42:289de245ba1d | 171 | if (iterator < MESSAGE_COUNT) |
AzureIoTClient | 18:e3e29fc771c9 | 172 | { |
AzureIoTClient | 42:289de245ba1d | 173 | sprintf_s(msgText, sizeof(msgText), "{\"deviceId\": \"myFirstDevice\",\"windSpeed\": %.2f}", avgWindSpeed + (rand() % 4 + 2)); |
AzureIoTClient | 42:289de245ba1d | 174 | if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL) |
Azure.IoT Build | 33:f8eb46ca453d | 175 | { |
AzureIoTClient | 42:289de245ba1d | 176 | (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); |
Azure.IoT Build | 33:f8eb46ca453d | 177 | } |
Azure.IoT Build | 33:f8eb46ca453d | 178 | else |
Azure.IoT Build | 33:f8eb46ca453d | 179 | { |
AzureIoTClient | 42:289de245ba1d | 180 | MAP_HANDLE propMap; |
AzureIoTClient | 42:289de245ba1d | 181 | |
AzureIoTClient | 42:289de245ba1d | 182 | messages[iterator].messageTrackingId = iterator; |
Azure.IoT Build | 33:f8eb46ca453d | 183 | |
AzureIoTClient | 42:289de245ba1d | 184 | propMap = IoTHubMessage_Properties(messages[iterator].messageHandle); |
AzureIoTClient | 47:bc18bf0e4a6a | 185 | (void)sprintf_s(propText, sizeof(propText), "PropMsg_%zu", iterator); |
AzureIoTClient | 49:2aa1b773af3c | 186 | if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK) |
AzureIoTClient | 42:289de245ba1d | 187 | { |
AzureIoTClient | 42:289de245ba1d | 188 | (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n"); |
AzureIoTClient | 42:289de245ba1d | 189 | } |
AzureIoTClient | 18:e3e29fc771c9 | 190 | |
AzureIoTClient | 42:289de245ba1d | 191 | if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 42:289de245ba1d | 192 | { |
AzureIoTClient | 42:289de245ba1d | 193 | (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n"); |
AzureIoTClient | 42:289de245ba1d | 194 | } |
AzureIoTClient | 42:289de245ba1d | 195 | else |
AzureIoTClient | 42:289de245ba1d | 196 | { |
AzureIoTClient | 49:2aa1b773af3c | 197 | (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%zu] for transmission to IoT Hub.\r\n", iterator); |
AzureIoTClient | 49:2aa1b773af3c | 198 | |
AzureIoTClient | 42:289de245ba1d | 199 | } |
AzureIoTClient | 42:289de245ba1d | 200 | |
AzureIoTClient | 42:289de245ba1d | 201 | } |
AzureIoTClient | 42:289de245ba1d | 202 | } |
AzureIoTClient | 42:289de245ba1d | 203 | IoTHubClient_LL_DoWork(iotHubClientHandle); |
AzureIoTClient | 42:289de245ba1d | 204 | ThreadAPI_Sleep(1); |
AzureIoTClient | 42:289de245ba1d | 205 | |
AzureIoTClient | 42:289de245ba1d | 206 | iterator++; |
AzureIoTClient | 42:289de245ba1d | 207 | } while (g_continueRunning); |
Azure.IoT Build | 46:91bd03862871 | 208 | |
AzureIoTClient | 48:07c7ca66634a | 209 | (void)printf("iothub_client_sample_http has gotten quit message, call DoWork %d more time to complete final sending...\r\n", DOWORK_LOOP_NUM); |
Azure.IoT Build | 46:91bd03862871 | 210 | for (size_t index = 0; index < DOWORK_LOOP_NUM; index++) |
Azure.IoT Build | 46:91bd03862871 | 211 | { |
Azure.IoT Build | 46:91bd03862871 | 212 | IoTHubClient_LL_DoWork(iotHubClientHandle); |
Azure.IoT Build | 46:91bd03862871 | 213 | ThreadAPI_Sleep(1); |
Azure.IoT Build | 46:91bd03862871 | 214 | } |
Azure.IoT Build | 33:f8eb46ca453d | 215 | } |
Azure.IoT Build | 33:f8eb46ca453d | 216 | IoTHubClient_LL_Destroy(iotHubClientHandle); |
AzureIoTClient | 18:e3e29fc771c9 | 217 | } |
Azure.IoT Build | 33:f8eb46ca453d | 218 | platform_deinit(); |
AzureIoTClient | 18:e3e29fc771c9 | 219 | } |
AzureIoTClient | 18:e3e29fc771c9 | 220 | } |
AzureIoTClient | 42:289de245ba1d | 221 | |
AzureIoTClient | 42:289de245ba1d | 222 | int main(void) |
AzureIoTClient | 42:289de245ba1d | 223 | { |
AzureIoTClient | 48:07c7ca66634a | 224 | iothub_client_sample_http_run(); |
AzureIoTClient | 48:07c7ca66634a | 225 | return 0; |
AzureIoTClient | 42:289de245ba1d | 226 | } |