A simple IoTHub sample using HTTP as transport
Dependencies: EthernetInterface NTPClient iothub_client iothub_http_transport mbed-rtos mbed wolfSSL serializer azure_c_shared_utility
This sample showcases the usage of Azure IoT client libraries with the HTTP transport for sending/receiving raw messages from an IoT Hub.
simplesample_http.c@10:c884d9d28696, 2015-09-26 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Sat Sep 26 00:18:24 2015 -0700
- Revision:
- 10:c884d9d28696
- Parent:
- 8:a9db8d5c22ee
v1.0.0-preview.3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 0:a00ac564e057 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 0:a00ac564e057 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 0:a00ac564e057 | 3 | |
AzureIoTClient | 0:a00ac564e057 | 4 | #include <stdlib.h> |
AzureIoTClient | 0:a00ac564e057 | 5 | |
AzureIoTClient | 0:a00ac564e057 | 6 | #include <stdio.h> |
AzureIoTClient | 0:a00ac564e057 | 7 | #include <stdint.h> |
AzureIoTClient | 0:a00ac564e057 | 8 | |
AzureIoTClient | 0:a00ac564e057 | 9 | /* This sample uses the _LL APIs of iothub_client for example purposes. |
AzureIoTClient | 0:a00ac564e057 | 10 | That does not mean that HTTP only works with the _LL APIs. |
AzureIoTClient | 0:a00ac564e057 | 11 | Simply changing the using the convenience layer (functions not having _LL) |
AzureIoTClient | 0:a00ac564e057 | 12 | and removing calls to _DoWork will yield the same results. */ |
AzureIoTClient | 0:a00ac564e057 | 13 | |
AzureIoTClient | 0:a00ac564e057 | 14 | #include "serializer.h" |
AzureIoTClient | 0:a00ac564e057 | 15 | #include "iothub_client_ll.h" |
AzureIoTClient | 0:a00ac564e057 | 16 | #include "iothubtransporthttp.h" |
AzureIoTClient | 0:a00ac564e057 | 17 | #include "threadapi.h" |
AzureIoTClient | 0:a00ac564e057 | 18 | |
AzureIoTClient | 0:a00ac564e057 | 19 | #ifdef MBED_BUILD_TIMESTAMP |
AzureIoTClient | 0:a00ac564e057 | 20 | #include "certs.h" |
AzureIoTClient | 0:a00ac564e057 | 21 | #endif // MBED_BUILD_TIMESTAMP |
AzureIoTClient | 0:a00ac564e057 | 22 | |
AzureIoTClient | 0:a00ac564e057 | 23 | static const char* connectionString = "[device connection string]"; |
AzureIoTClient | 0:a00ac564e057 | 24 | |
AzureIoTClient | 0:a00ac564e057 | 25 | // Define the Model |
AzureIoTClient | 8:a9db8d5c22ee | 26 | BEGIN_NAMESPACE(WeatherStation); |
AzureIoTClient | 0:a00ac564e057 | 27 | |
AzureIoTClient | 8:a9db8d5c22ee | 28 | DECLARE_MODEL(ContosoAnemometer, |
AzureIoTClient | 8:a9db8d5c22ee | 29 | WITH_DATA(ascii_char_ptr, DeviceId), |
AzureIoTClient | 8:a9db8d5c22ee | 30 | WITH_DATA(double, WindSpeed), |
AzureIoTClient | 0:a00ac564e057 | 31 | WITH_ACTION(TurnFanOn), |
AzureIoTClient | 0:a00ac564e057 | 32 | WITH_ACTION(TurnFanOff), |
AzureIoTClient | 8:a9db8d5c22ee | 33 | WITH_ACTION(SetAirResistance, int, Position) |
AzureIoTClient | 0:a00ac564e057 | 34 | ); |
AzureIoTClient | 0:a00ac564e057 | 35 | |
AzureIoTClient | 8:a9db8d5c22ee | 36 | END_NAMESPACE(WeatherStation); |
AzureIoTClient | 0:a00ac564e057 | 37 | |
AzureIoTClient | 0:a00ac564e057 | 38 | DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES) |
AzureIoTClient | 0:a00ac564e057 | 39 | |
AzureIoTClient | 8:a9db8d5c22ee | 40 | EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device) |
AzureIoTClient | 0:a00ac564e057 | 41 | { |
AzureIoTClient | 0:a00ac564e057 | 42 | (void)device; |
AzureIoTClient | 0:a00ac564e057 | 43 | (void)printf("Turning fan on.\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 44 | return EXECUTE_COMMAND_SUCCESS; |
AzureIoTClient | 0:a00ac564e057 | 45 | } |
AzureIoTClient | 0:a00ac564e057 | 46 | |
AzureIoTClient | 8:a9db8d5c22ee | 47 | EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device) |
AzureIoTClient | 0:a00ac564e057 | 48 | { |
AzureIoTClient | 0:a00ac564e057 | 49 | (void)device; |
AzureIoTClient | 0:a00ac564e057 | 50 | (void)printf("Turning fan off.\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 51 | return EXECUTE_COMMAND_SUCCESS; |
AzureIoTClient | 0:a00ac564e057 | 52 | } |
AzureIoTClient | 0:a00ac564e057 | 53 | |
AzureIoTClient | 8:a9db8d5c22ee | 54 | EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position) |
AzureIoTClient | 0:a00ac564e057 | 55 | { |
AzureIoTClient | 0:a00ac564e057 | 56 | (void)device; |
AzureIoTClient | 8:a9db8d5c22ee | 57 | (void)printf("Setting Air Resistance Position to %d.\r\n", Position); |
AzureIoTClient | 0:a00ac564e057 | 58 | return EXECUTE_COMMAND_SUCCESS; |
AzureIoTClient | 0:a00ac564e057 | 59 | } |
AzureIoTClient | 0:a00ac564e057 | 60 | |
AzureIoTClient | 0:a00ac564e057 | 61 | void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
AzureIoTClient | 0:a00ac564e057 | 62 | { |
AzureIoTClient | 0:a00ac564e057 | 63 | int messageTrackingId = (intptr_t)userContextCallback; |
AzureIoTClient | 0:a00ac564e057 | 64 | |
AzureIoTClient | 0:a00ac564e057 | 65 | (void)printf("Message Id: %d Received.\r\n", messageTrackingId); |
AzureIoTClient | 0:a00ac564e057 | 66 | |
AzureIoTClient | 0:a00ac564e057 | 67 | (void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); |
AzureIoTClient | 0:a00ac564e057 | 68 | } |
AzureIoTClient | 0:a00ac564e057 | 69 | |
AzureIoTClient | 0:a00ac564e057 | 70 | static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size) |
AzureIoTClient | 0:a00ac564e057 | 71 | { |
AzureIoTClient | 0:a00ac564e057 | 72 | static unsigned int messageTrackingId; |
AzureIoTClient | 0:a00ac564e057 | 73 | IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size); |
AzureIoTClient | 0:a00ac564e057 | 74 | if (messageHandle == NULL) |
AzureIoTClient | 0:a00ac564e057 | 75 | { |
AzureIoTClient | 0:a00ac564e057 | 76 | printf("unable to create a new IoTHubMessage\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 77 | } |
AzureIoTClient | 0:a00ac564e057 | 78 | else |
AzureIoTClient | 0:a00ac564e057 | 79 | { |
AzureIoTClient | 0:a00ac564e057 | 80 | if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 0:a00ac564e057 | 81 | { |
AzureIoTClient | 0:a00ac564e057 | 82 | printf("failed to hand over the message to IoTHubClient"); |
AzureIoTClient | 0:a00ac564e057 | 83 | } |
AzureIoTClient | 0:a00ac564e057 | 84 | else |
AzureIoTClient | 0:a00ac564e057 | 85 | { |
AzureIoTClient | 0:a00ac564e057 | 86 | printf("IoTHubClient accepted the message for delivery\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 87 | } |
AzureIoTClient | 0:a00ac564e057 | 88 | IoTHubMessage_Destroy(messageHandle); |
AzureIoTClient | 0:a00ac564e057 | 89 | } |
AzureIoTClient | 0:a00ac564e057 | 90 | free((void*)buffer); |
AzureIoTClient | 0:a00ac564e057 | 91 | messageTrackingId++; |
AzureIoTClient | 0:a00ac564e057 | 92 | } |
AzureIoTClient | 0:a00ac564e057 | 93 | |
AzureIoTClient | 10:c884d9d28696 | 94 | /*this function "links" IoTHub to the serialization library*/ |
AzureIoTClient | 8:a9db8d5c22ee | 95 | static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
AzureIoTClient | 0:a00ac564e057 | 96 | { |
AzureIoTClient | 0:a00ac564e057 | 97 | IOTHUBMESSAGE_DISPOSITION_RESULT result; |
AzureIoTClient | 0:a00ac564e057 | 98 | const unsigned char* buffer; |
AzureIoTClient | 0:a00ac564e057 | 99 | size_t size; |
AzureIoTClient | 8:a9db8d5c22ee | 100 | if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 0:a00ac564e057 | 101 | { |
AzureIoTClient | 0:a00ac564e057 | 102 | printf("unable to IoTHubMessage_GetByteArray\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 103 | result = EXECUTE_COMMAND_ERROR; |
AzureIoTClient | 0:a00ac564e057 | 104 | } |
AzureIoTClient | 0:a00ac564e057 | 105 | else |
AzureIoTClient | 0:a00ac564e057 | 106 | { |
AzureIoTClient | 0:a00ac564e057 | 107 | /*buffer is not zero terminated*/ |
AzureIoTClient | 0:a00ac564e057 | 108 | char* temp = malloc(size + 1); |
AzureIoTClient | 0:a00ac564e057 | 109 | if (temp == NULL) |
AzureIoTClient | 0:a00ac564e057 | 110 | { |
AzureIoTClient | 0:a00ac564e057 | 111 | printf("failed to malloc\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 112 | result = EXECUTE_COMMAND_ERROR; |
AzureIoTClient | 0:a00ac564e057 | 113 | } |
AzureIoTClient | 0:a00ac564e057 | 114 | else |
AzureIoTClient | 0:a00ac564e057 | 115 | { |
AzureIoTClient | 0:a00ac564e057 | 116 | memcpy(temp, buffer, size); |
AzureIoTClient | 0:a00ac564e057 | 117 | temp[size] = '\0'; |
AzureIoTClient | 0:a00ac564e057 | 118 | EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp); |
AzureIoTClient | 0:a00ac564e057 | 119 | result = |
AzureIoTClient | 0:a00ac564e057 | 120 | (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED : |
AzureIoTClient | 0:a00ac564e057 | 121 | (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED : |
AzureIoTClient | 0:a00ac564e057 | 122 | IOTHUBMESSAGE_REJECTED; |
AzureIoTClient | 0:a00ac564e057 | 123 | free(temp); |
AzureIoTClient | 0:a00ac564e057 | 124 | } |
AzureIoTClient | 0:a00ac564e057 | 125 | } |
AzureIoTClient | 0:a00ac564e057 | 126 | return result; |
AzureIoTClient | 0:a00ac564e057 | 127 | } |
AzureIoTClient | 0:a00ac564e057 | 128 | |
AzureIoTClient | 0:a00ac564e057 | 129 | void simplesample_http_run(void) |
AzureIoTClient | 0:a00ac564e057 | 130 | { |
AzureIoTClient | 0:a00ac564e057 | 131 | if (serializer_init(NULL) != SERIALIZER_OK) |
AzureIoTClient | 0:a00ac564e057 | 132 | { |
AzureIoTClient | 0:a00ac564e057 | 133 | (void)printf("Failed on serializer_init\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 134 | } |
AzureIoTClient | 0:a00ac564e057 | 135 | else |
AzureIoTClient | 0:a00ac564e057 | 136 | { |
AzureIoTClient | 8:a9db8d5c22ee | 137 | IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol); |
AzureIoTClient | 8:a9db8d5c22ee | 138 | srand((unsigned int)time(NULL)); |
AzureIoTClient | 8:a9db8d5c22ee | 139 | double avgWindSpeed = 10.0; |
AzureIoTClient | 0:a00ac564e057 | 140 | |
AzureIoTClient | 0:a00ac564e057 | 141 | if (iotHubClientHandle == NULL) |
AzureIoTClient | 0:a00ac564e057 | 142 | { |
AzureIoTClient | 0:a00ac564e057 | 143 | (void)printf("Failed on IoTHubClient_LL_Create\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 144 | } |
AzureIoTClient | 0:a00ac564e057 | 145 | else |
AzureIoTClient | 0:a00ac564e057 | 146 | { |
AzureIoTClient | 0:a00ac564e057 | 147 | unsigned int minimumPollingTime = 9; /*because it can poll "after 9 seconds" polls will happen effectively at ~10 seconds*/ |
AzureIoTClient | 0:a00ac564e057 | 148 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 0:a00ac564e057 | 149 | { |
AzureIoTClient | 0:a00ac564e057 | 150 | printf("failure to set option \"MinimumPollingTime\"\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 151 | } |
AzureIoTClient | 0:a00ac564e057 | 152 | |
AzureIoTClient | 0:a00ac564e057 | 153 | #ifdef MBED_BUILD_TIMESTAMP |
AzureIoTClient | 0:a00ac564e057 | 154 | // For mbed add the certificate information |
AzureIoTClient | 0:a00ac564e057 | 155 | if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 0:a00ac564e057 | 156 | { |
AzureIoTClient | 8:a9db8d5c22ee | 157 | (void)printf("failure to set option \"TrustedCerts\"\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 158 | } |
AzureIoTClient | 0:a00ac564e057 | 159 | #endif // MBED_BUILD_TIMESTAMP |
AzureIoTClient | 0:a00ac564e057 | 160 | |
AzureIoTClient | 8:a9db8d5c22ee | 161 | ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer); |
AzureIoTClient | 8:a9db8d5c22ee | 162 | if (myWeather == NULL) |
AzureIoTClient | 0:a00ac564e057 | 163 | { |
AzureIoTClient | 0:a00ac564e057 | 164 | (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 165 | } |
AzureIoTClient | 0:a00ac564e057 | 166 | else |
AzureIoTClient | 0:a00ac564e057 | 167 | { |
AzureIoTClient | 8:a9db8d5c22ee | 168 | if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 0:a00ac564e057 | 169 | { |
AzureIoTClient | 8:a9db8d5c22ee | 170 | printf("unable to IoTHubClient_SetMessageCallback\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 171 | } |
AzureIoTClient | 0:a00ac564e057 | 172 | else |
AzureIoTClient | 0:a00ac564e057 | 173 | { |
AzureIoTClient | 8:a9db8d5c22ee | 174 | myWeather->DeviceId = "myFirstDevice"; |
AzureIoTClient | 8:a9db8d5c22ee | 175 | myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2); |
AzureIoTClient | 0:a00ac564e057 | 176 | { |
AzureIoTClient | 0:a00ac564e057 | 177 | unsigned char* destination; |
AzureIoTClient | 0:a00ac564e057 | 178 | size_t destinationSize; |
AzureIoTClient | 8:a9db8d5c22ee | 179 | if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != IOT_AGENT_OK) |
AzureIoTClient | 0:a00ac564e057 | 180 | { |
AzureIoTClient | 0:a00ac564e057 | 181 | (void)printf("Failed to serialize\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 182 | } |
AzureIoTClient | 0:a00ac564e057 | 183 | else |
AzureIoTClient | 0:a00ac564e057 | 184 | { |
AzureIoTClient | 0:a00ac564e057 | 185 | IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize); |
AzureIoTClient | 0:a00ac564e057 | 186 | if (messageHandle == NULL) |
AzureIoTClient | 0:a00ac564e057 | 187 | { |
AzureIoTClient | 0:a00ac564e057 | 188 | printf("unable to create a new IoTHubMessage\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 189 | } |
AzureIoTClient | 0:a00ac564e057 | 190 | else |
AzureIoTClient | 0:a00ac564e057 | 191 | { |
AzureIoTClient | 0:a00ac564e057 | 192 | if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 0:a00ac564e057 | 193 | { |
AzureIoTClient | 0:a00ac564e057 | 194 | printf("failed to hand over the message to IoTHubClient"); |
AzureIoTClient | 0:a00ac564e057 | 195 | } |
AzureIoTClient | 0:a00ac564e057 | 196 | else |
AzureIoTClient | 0:a00ac564e057 | 197 | { |
AzureIoTClient | 0:a00ac564e057 | 198 | printf("IoTHubClient accepted the message for delivery\r\n"); |
AzureIoTClient | 0:a00ac564e057 | 199 | } |
AzureIoTClient | 0:a00ac564e057 | 200 | |
AzureIoTClient | 0:a00ac564e057 | 201 | IoTHubMessage_Destroy(messageHandle); |
AzureIoTClient | 0:a00ac564e057 | 202 | } |
AzureIoTClient | 0:a00ac564e057 | 203 | free(destination); |
AzureIoTClient | 0:a00ac564e057 | 204 | } |
AzureIoTClient | 0:a00ac564e057 | 205 | } |
AzureIoTClient | 0:a00ac564e057 | 206 | |
AzureIoTClient | 0:a00ac564e057 | 207 | /* wait for commands */ |
AzureIoTClient | 0:a00ac564e057 | 208 | while (1) |
AzureIoTClient | 0:a00ac564e057 | 209 | { |
AzureIoTClient | 0:a00ac564e057 | 210 | IoTHubClient_LL_DoWork(iotHubClientHandle); |
AzureIoTClient | 8:a9db8d5c22ee | 211 | ThreadAPI_Sleep(100); |
AzureIoTClient | 0:a00ac564e057 | 212 | } |
AzureIoTClient | 0:a00ac564e057 | 213 | } |
AzureIoTClient | 0:a00ac564e057 | 214 | |
AzureIoTClient | 8:a9db8d5c22ee | 215 | DESTROY_MODEL_INSTANCE(myWeather); |
AzureIoTClient | 0:a00ac564e057 | 216 | } |
AzureIoTClient | 0:a00ac564e057 | 217 | IoTHubClient_LL_Destroy(iotHubClientHandle); |
AzureIoTClient | 0:a00ac564e057 | 218 | } |
AzureIoTClient | 0:a00ac564e057 | 219 | serializer_deinit(); |
AzureIoTClient | 0:a00ac564e057 | 220 | } |
AzureIoTClient | 0:a00ac564e057 | 221 | } |