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.

Committer:
AzureIoTClient
Date:
Mon May 08 10:52:00 2017 -0700
Revision:
68:a8c0f61d8478
Parent:
62:2320ad565fc3
Child:
76:e9a30712b6d7
1.1.14

Who changed what in which revision?

UserRevisionLine numberNew 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 62:2320ad565fc3 53 const char* messageId;
AzureIoTClient 62:2320ad565fc3 54 const char* correlationId;
AzureIoTClient 20:52f8298d7256 55
AzureIoTClient 62:2320ad565fc3 56 // Message properties
AzureIoTClient 62:2320ad565fc3 57 if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
AzureIoTClient 62:2320ad565fc3 58 {
AzureIoTClient 62:2320ad565fc3 59 messageId = "<null>";
AzureIoTClient 62:2320ad565fc3 60 }
AzureIoTClient 62:2320ad565fc3 61
AzureIoTClient 62:2320ad565fc3 62 if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL)
AzureIoTClient 62:2320ad565fc3 63 {
AzureIoTClient 62:2320ad565fc3 64 correlationId = "<null>";
AzureIoTClient 62:2320ad565fc3 65 }
AzureIoTClient 62:2320ad565fc3 66
AzureIoTClient 62:2320ad565fc3 67 // Message content
AzureIoTClient 18:e3e29fc771c9 68 if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
AzureIoTClient 18:e3e29fc771c9 69 {
AzureIoTClient 18:e3e29fc771c9 70 printf("unable to retrieve the message data\r\n");
AzureIoTClient 18:e3e29fc771c9 71 }
AzureIoTClient 18:e3e29fc771c9 72 else
AzureIoTClient 18:e3e29fc771c9 73 {
AzureIoTClient 62:2320ad565fc3 74 (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);
AzureIoTClient 54:54dc461c75da 75 if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
AzureIoTClient 42:289de245ba1d 76 {
AzureIoTClient 42:289de245ba1d 77 g_continueRunning = false;
AzureIoTClient 42:289de245ba1d 78 }
AzureIoTClient 18:e3e29fc771c9 79 }
AzureIoTClient 18:e3e29fc771c9 80
AzureIoTClient 18:e3e29fc771c9 81 // Retrieve properties from the message
AzureIoTClient 20:52f8298d7256 82 mapProperties = IoTHubMessage_Properties(message);
AzureIoTClient 18:e3e29fc771c9 83 if (mapProperties != NULL)
AzureIoTClient 18:e3e29fc771c9 84 {
AzureIoTClient 18:e3e29fc771c9 85 const char*const* keys;
AzureIoTClient 18:e3e29fc771c9 86 const char*const* values;
AzureIoTClient 18:e3e29fc771c9 87 size_t propertyCount = 0;
AzureIoTClient 18:e3e29fc771c9 88 if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
AzureIoTClient 18:e3e29fc771c9 89 {
AzureIoTClient 18:e3e29fc771c9 90 if (propertyCount > 0)
AzureIoTClient 18:e3e29fc771c9 91 {
AzureIoTClient 41:7a930aa6dc01 92 size_t index;
AzureIoTClient 20:52f8298d7256 93
AzureIoTClient 62:2320ad565fc3 94 printf(" Message Properties:\r\n");
AzureIoTClient 20:52f8298d7256 95 for (index = 0; index < propertyCount; index++)
AzureIoTClient 18:e3e29fc771c9 96 {
AzureIoTClient 18:e3e29fc771c9 97 printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
AzureIoTClient 18:e3e29fc771c9 98 }
AzureIoTClient 18:e3e29fc771c9 99 printf("\r\n");
AzureIoTClient 18:e3e29fc771c9 100 }
AzureIoTClient 18:e3e29fc771c9 101 }
AzureIoTClient 18:e3e29fc771c9 102 }
AzureIoTClient 18:e3e29fc771c9 103
AzureIoTClient 18:e3e29fc771c9 104 /* Some device specific action code goes here... */
AzureIoTClient 18:e3e29fc771c9 105 (*counter)++;
AzureIoTClient 18:e3e29fc771c9 106 return IOTHUBMESSAGE_ACCEPTED;
AzureIoTClient 18:e3e29fc771c9 107 }
AzureIoTClient 18:e3e29fc771c9 108
AzureIoTClient 18:e3e29fc771c9 109 static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 18:e3e29fc771c9 110 {
AzureIoTClient 18:e3e29fc771c9 111 EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
AzureIoTClient 49:2aa1b773af3c 112
AzureIoTClient 49:2aa1b773af3c 113 (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 114
AzureIoTClient 18:e3e29fc771c9 115 /* Some device specific action code goes here... */
AzureIoTClient 18:e3e29fc771c9 116 callbackCounter++;
AzureIoTClient 18:e3e29fc771c9 117 IoTHubMessage_Destroy(eventInstance->messageHandle);
AzureIoTClient 18:e3e29fc771c9 118 }
AzureIoTClient 18:e3e29fc771c9 119
AzureIoTClient 18:e3e29fc771c9 120 void iothub_client_sample_http_run(void)
AzureIoTClient 18:e3e29fc771c9 121 {
AzureIoTClient 18:e3e29fc771c9 122 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
AzureIoTClient 18:e3e29fc771c9 123
AzureIoTClient 18:e3e29fc771c9 124 EVENT_INSTANCE messages[MESSAGE_COUNT];
AzureIoTClient 20:52f8298d7256 125 double avgWindSpeed = 10.0;
AzureIoTClient 68:a8c0f61d8478 126 double minTemperature = 20.0;
AzureIoTClient 68:a8c0f61d8478 127 double minHumidity = 60.0;
AzureIoTClient 20:52f8298d7256 128 int receiveContext = 0;
Azure.IoT Build 46:91bd03862871 129
AzureIoTClient 42:289de245ba1d 130 g_continueRunning = true;
AzureIoTClient 18:e3e29fc771c9 131
AzureIoTClient 18:e3e29fc771c9 132 srand((unsigned int)time(NULL));
AzureIoTClient 18:e3e29fc771c9 133
AzureIoTClient 18:e3e29fc771c9 134 callbackCounter = 0;
AzureIoTClient 18:e3e29fc771c9 135
Azure.IoT Build 33:f8eb46ca453d 136 if (platform_init() != 0)
AzureIoTClient 18:e3e29fc771c9 137 {
Azure.IoT Build 33:f8eb46ca453d 138 printf("Failed to initialize the platform.\r\n");
AzureIoTClient 18:e3e29fc771c9 139 }
AzureIoTClient 18:e3e29fc771c9 140 else
AzureIoTClient 18:e3e29fc771c9 141 {
Azure.IoT Build 33:f8eb46ca453d 142 (void)printf("Starting the IoTHub client sample HTTP...\r\n");
AzureIoTClient 18:e3e29fc771c9 143
Azure.IoT Build 33:f8eb46ca453d 144 if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol)) == NULL)
AzureIoTClient 18:e3e29fc771c9 145 {
Azure.IoT Build 33:f8eb46ca453d 146 (void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
AzureIoTClient 18:e3e29fc771c9 147 }
AzureIoTClient 18:e3e29fc771c9 148 else
AzureIoTClient 18:e3e29fc771c9 149 {
Azure.IoT Build 33:f8eb46ca453d 150 unsigned int timeout = 241000;
Azure.IoT Build 33:f8eb46ca453d 151 // Because it can poll "after 9 seconds" polls will happen effectively // at ~10 seconds.
Azure.IoT Build 33:f8eb46ca453d 152 // Note that for scalabilty, the default value of minimumPollingTime
Azure.IoT Build 33:f8eb46ca453d 153 // is 25 minutes. For more information, see:
Azure.IoT Build 33:f8eb46ca453d 154 // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
Azure.IoT Build 33:f8eb46ca453d 155 unsigned int minimumPollingTime = 9;
Azure.IoT Build 33:f8eb46ca453d 156 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "timeout", &timeout) != IOTHUB_CLIENT_OK)
Azure.IoT Build 33:f8eb46ca453d 157 {
Azure.IoT Build 33:f8eb46ca453d 158 printf("failure to set option \"timeout\"\r\n");
Azure.IoT Build 33:f8eb46ca453d 159 }
AzureIoTClient 20:52f8298d7256 160
Azure.IoT Build 33:f8eb46ca453d 161 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
AzureIoTClient 18:e3e29fc771c9 162 {
Azure.IoT Build 33:f8eb46ca453d 163 printf("failure to set option \"MinimumPollingTime\"\r\n");
Azure.IoT Build 33:f8eb46ca453d 164 }
Azure.IoT Build 33:f8eb46ca453d 165
Azure.IoT Build 33:f8eb46ca453d 166 #ifdef MBED_BUILD_TIMESTAMP
Azure.IoT Build 33:f8eb46ca453d 167 // For mbed add the certificate information
Azure.IoT Build 33:f8eb46ca453d 168 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
Azure.IoT Build 33:f8eb46ca453d 169 {
Azure.IoT Build 33:f8eb46ca453d 170 printf("failure to set option \"TrustedCerts\"\r\n");
Azure.IoT Build 33:f8eb46ca453d 171 }
Azure.IoT Build 33:f8eb46ca453d 172 #endif // MBED_BUILD_TIMESTAMP
Azure.IoT Build 33:f8eb46ca453d 173
Azure.IoT Build 33:f8eb46ca453d 174 /* Setting Message call back, so we can receive Commands. */
Azure.IoT Build 33:f8eb46ca453d 175 if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
Azure.IoT Build 33:f8eb46ca453d 176 {
Azure.IoT Build 33:f8eb46ca453d 177 (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
Azure.IoT Build 33:f8eb46ca453d 178 }
Azure.IoT Build 33:f8eb46ca453d 179 else
Azure.IoT Build 33:f8eb46ca453d 180 {
Azure.IoT Build 33:f8eb46ca453d 181 (void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n");
Azure.IoT Build 33:f8eb46ca453d 182
Azure.IoT Build 33:f8eb46ca453d 183 /* Now that we are ready to receive commands, let's send some messages */
AzureIoTClient 42:289de245ba1d 184 size_t iterator = 0;
AzureIoTClient 68:a8c0f61d8478 185 double temperature = 0;
AzureIoTClient 68:a8c0f61d8478 186 double humidity = 0;
AzureIoTClient 42:289de245ba1d 187 do
AzureIoTClient 18:e3e29fc771c9 188 {
AzureIoTClient 42:289de245ba1d 189 if (iterator < MESSAGE_COUNT)
AzureIoTClient 18:e3e29fc771c9 190 {
AzureIoTClient 68:a8c0f61d8478 191 temperature = minTemperature + (rand() % 10);
AzureIoTClient 68:a8c0f61d8478 192 humidity = minHumidity + (rand() % 20);
AzureIoTClient 68:a8c0f61d8478 193 sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f,\"temperature\":%.2f,\"humidity\":%.2f}", avgWindSpeed + (rand() % 4 + 2), temperature, humidity);
AzureIoTClient 42:289de245ba1d 194 if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
Azure.IoT Build 33:f8eb46ca453d 195 {
AzureIoTClient 42:289de245ba1d 196 (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
Azure.IoT Build 33:f8eb46ca453d 197 }
Azure.IoT Build 33:f8eb46ca453d 198 else
Azure.IoT Build 33:f8eb46ca453d 199 {
AzureIoTClient 42:289de245ba1d 200 MAP_HANDLE propMap;
AzureIoTClient 42:289de245ba1d 201
AzureIoTClient 42:289de245ba1d 202 messages[iterator].messageTrackingId = iterator;
Azure.IoT Build 33:f8eb46ca453d 203
AzureIoTClient 42:289de245ba1d 204 propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
AzureIoTClient 68:a8c0f61d8478 205 (void)sprintf_s(propText, sizeof(propText), temperature > 28 ? "true" : "false");
AzureIoTClient 68:a8c0f61d8478 206 if (Map_AddOrUpdate(propMap, "temperatureAlert", propText) != MAP_OK)
AzureIoTClient 42:289de245ba1d 207 {
AzureIoTClient 42:289de245ba1d 208 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
AzureIoTClient 42:289de245ba1d 209 }
AzureIoTClient 18:e3e29fc771c9 210
AzureIoTClient 42:289de245ba1d 211 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
AzureIoTClient 42:289de245ba1d 212 {
AzureIoTClient 42:289de245ba1d 213 (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
AzureIoTClient 42:289de245ba1d 214 }
AzureIoTClient 42:289de245ba1d 215 else
AzureIoTClient 42:289de245ba1d 216 {
AzureIoTClient 49:2aa1b773af3c 217 (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%zu] for transmission to IoT Hub.\r\n", iterator);
AzureIoTClient 49:2aa1b773af3c 218
AzureIoTClient 42:289de245ba1d 219 }
AzureIoTClient 42:289de245ba1d 220
AzureIoTClient 42:289de245ba1d 221 }
AzureIoTClient 42:289de245ba1d 222 }
AzureIoTClient 42:289de245ba1d 223 IoTHubClient_LL_DoWork(iotHubClientHandle);
AzureIoTClient 42:289de245ba1d 224 ThreadAPI_Sleep(1);
AzureIoTClient 42:289de245ba1d 225
AzureIoTClient 42:289de245ba1d 226 iterator++;
AzureIoTClient 42:289de245ba1d 227 } while (g_continueRunning);
Azure.IoT Build 46:91bd03862871 228
AzureIoTClient 48:07c7ca66634a 229 (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 230 for (size_t index = 0; index < DOWORK_LOOP_NUM; index++)
Azure.IoT Build 46:91bd03862871 231 {
Azure.IoT Build 46:91bd03862871 232 IoTHubClient_LL_DoWork(iotHubClientHandle);
Azure.IoT Build 46:91bd03862871 233 ThreadAPI_Sleep(1);
Azure.IoT Build 46:91bd03862871 234 }
Azure.IoT Build 33:f8eb46ca453d 235 }
Azure.IoT Build 33:f8eb46ca453d 236 IoTHubClient_LL_Destroy(iotHubClientHandle);
AzureIoTClient 18:e3e29fc771c9 237 }
Azure.IoT Build 33:f8eb46ca453d 238 platform_deinit();
AzureIoTClient 18:e3e29fc771c9 239 }
AzureIoTClient 18:e3e29fc771c9 240 }
AzureIoTClient 42:289de245ba1d 241
AzureIoTClient 42:289de245ba1d 242 int main(void)
AzureIoTClient 42:289de245ba1d 243 {
AzureIoTClient 48:07c7ca66634a 244 iothub_client_sample_http_run();
AzureIoTClient 48:07c7ca66634a 245 return 0;
AzureIoTClient 42:289de245ba1d 246 }