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.

Committer:
AzureIoTClient
Date:
Thu Oct 22 18:33:57 2015 -0700
Revision:
17:3abbcd6aac9f
Child:
19:253a0a1ea1c5
v1.0.0-preview.4

Who changed what in which revision?

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