A simple IoTHub sample using AMQP as transport

Dependencies:   EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed azure_c_shared_utility serializer wolfSSL azure_uamqp_c

This sample showcases the usage of Azure IoT client libraries with the AMQP transport for sending/receiving raw messages from an IoT Hub.

Committer:
Azure.IoT.Build
Date:
Wed Dec 14 16:03:19 2016 -0800
Revision:
52:595dd76f48d2
Parent:
51:289ece2f210e
Child:
56:68f0d4c20dea
1.1.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 16:b2c6de619743 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 16:b2c6de619743 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 16:b2c6de619743 3
AzureIoTClient 16:b2c6de619743 4 #include <stdlib.h>
AzureIoTClient 16:b2c6de619743 5
AzureIoTClient 16:b2c6de619743 6 #include <stdio.h>
AzureIoTClient 16:b2c6de619743 7 #include <stdint.h>
AzureIoTClient 16:b2c6de619743 8
Azure.IoT.Build 52:595dd76f48d2 9 #ifdef ARDUINO
Azure.IoT.Build 52:595dd76f48d2 10 #include "AzureIoTHub.h"
Azure.IoT.Build 52:595dd76f48d2 11 #else
AzureIoTClient 16:b2c6de619743 12 #include "serializer.h"
AzureIoTClient 35:862d7bc6755c 13 #include "azure_c_shared_utility/threadapi.h"
AzureIoTClient 35:862d7bc6755c 14 #include "azure_c_shared_utility/sastoken.h"
Azure.IoT Build 42:6bb5884a0648 15 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 16:b2c6de619743 16 #include "iothub_client.h"
AzureIoTClient 16:b2c6de619743 17 #include "iothubtransportamqp.h"
AzureIoTClient 16:b2c6de619743 18 #include "iothub_client_ll.h"
Azure.IoT.Build 52:595dd76f48d2 19 #endif
AzureIoTClient 16:b2c6de619743 20
AzureIoTClient 16:b2c6de619743 21 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 16:b2c6de619743 22 #include "certs.h"
AzureIoTClient 16:b2c6de619743 23 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 16:b2c6de619743 24
AzureIoTClient 18:a3ec1aab72dc 25 /*String containing Hostname, Device Id & Device Key in the format: */
AzureIoTClient 18:a3ec1aab72dc 26 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
AzureIoTClient 16:b2c6de619743 27 static const char* connectionString = "[device connection string]";
AzureIoTClient 16:b2c6de619743 28
AzureIoTClient 16:b2c6de619743 29 // Define the Model
AzureIoTClient 16:b2c6de619743 30 BEGIN_NAMESPACE(WeatherStation);
AzureIoTClient 16:b2c6de619743 31
AzureIoTClient 16:b2c6de619743 32 DECLARE_MODEL(ContosoAnemometer,
AzureIoTClient 16:b2c6de619743 33 WITH_DATA(ascii_char_ptr, DeviceId),
AzureIoTClient 19:eae21b79ceff 34 WITH_DATA(int, WindSpeed),
AzureIoTClient 16:b2c6de619743 35 WITH_ACTION(TurnFanOn),
AzureIoTClient 16:b2c6de619743 36 WITH_ACTION(TurnFanOff),
AzureIoTClient 16:b2c6de619743 37 WITH_ACTION(SetAirResistance, int, Position)
AzureIoTClient 16:b2c6de619743 38 );
AzureIoTClient 16:b2c6de619743 39
AzureIoTClient 16:b2c6de619743 40 END_NAMESPACE(WeatherStation);
AzureIoTClient 16:b2c6de619743 41
AzureIoTClient 16:b2c6de619743 42
AzureIoTClient 16:b2c6de619743 43 EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device)
AzureIoTClient 16:b2c6de619743 44 {
AzureIoTClient 16:b2c6de619743 45 (void)device;
AzureIoTClient 16:b2c6de619743 46 (void)printf("Turning fan on.\r\n");
AzureIoTClient 16:b2c6de619743 47 return EXECUTE_COMMAND_SUCCESS;
AzureIoTClient 16:b2c6de619743 48 }
AzureIoTClient 16:b2c6de619743 49
AzureIoTClient 16:b2c6de619743 50 EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device)
AzureIoTClient 16:b2c6de619743 51 {
AzureIoTClient 16:b2c6de619743 52 (void)device;
AzureIoTClient 16:b2c6de619743 53 (void)printf("Turning fan off.\r\n");
AzureIoTClient 16:b2c6de619743 54 return EXECUTE_COMMAND_SUCCESS;
AzureIoTClient 16:b2c6de619743 55 }
AzureIoTClient 16:b2c6de619743 56
AzureIoTClient 16:b2c6de619743 57 EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position)
AzureIoTClient 16:b2c6de619743 58 {
AzureIoTClient 16:b2c6de619743 59 (void)device;
AzureIoTClient 16:b2c6de619743 60 (void)printf("Setting Air Resistance Position to %d.\r\n", Position);
AzureIoTClient 16:b2c6de619743 61 return EXECUTE_COMMAND_SUCCESS;
AzureIoTClient 16:b2c6de619743 62 }
AzureIoTClient 16:b2c6de619743 63
AzureIoTClient 16:b2c6de619743 64 void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 16:b2c6de619743 65 {
AzureIoTClient 43:f179aa2ce96f 66 unsigned int messageTrackingId = (unsigned int)(uintptr_t)userContextCallback;
AzureIoTClient 16:b2c6de619743 67
AzureIoTClient 43:f179aa2ce96f 68 (void)printf("Message Id: %u Received.\r\n", messageTrackingId);
AzureIoTClient 16:b2c6de619743 69
AzureIoTClient 16:b2c6de619743 70 (void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
AzureIoTClient 16:b2c6de619743 71 }
AzureIoTClient 16:b2c6de619743 72
AzureIoTClient 16:b2c6de619743 73
AzureIoTClient 16:b2c6de619743 74 static void sendMessage(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
AzureIoTClient 16:b2c6de619743 75 {
AzureIoTClient 16:b2c6de619743 76 static unsigned int messageTrackingId;
AzureIoTClient 16:b2c6de619743 77 IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
AzureIoTClient 16:b2c6de619743 78 if (messageHandle == NULL)
AzureIoTClient 16:b2c6de619743 79 {
AzureIoTClient 16:b2c6de619743 80 printf("unable to create a new IoTHubMessage\r\n");
AzureIoTClient 16:b2c6de619743 81 }
AzureIoTClient 16:b2c6de619743 82 else
AzureIoTClient 16:b2c6de619743 83 {
AzureIoTClient 16:b2c6de619743 84 if (IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
AzureIoTClient 16:b2c6de619743 85 {
AzureIoTClient 16:b2c6de619743 86 printf("failed to hand over the message to IoTHubClient");
AzureIoTClient 16:b2c6de619743 87 }
AzureIoTClient 16:b2c6de619743 88 else
AzureIoTClient 16:b2c6de619743 89 {
AzureIoTClient 16:b2c6de619743 90 printf("IoTHubClient accepted the message for delivery\r\n");
AzureIoTClient 16:b2c6de619743 91 }
AzureIoTClient 16:b2c6de619743 92
AzureIoTClient 16:b2c6de619743 93 IoTHubMessage_Destroy(messageHandle);
AzureIoTClient 16:b2c6de619743 94 }
AzureIoTClient 16:b2c6de619743 95 free((void*)buffer);
AzureIoTClient 16:b2c6de619743 96 messageTrackingId++;
AzureIoTClient 16:b2c6de619743 97 }
AzureIoTClient 16:b2c6de619743 98
AzureIoTClient 16:b2c6de619743 99 static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
AzureIoTClient 16:b2c6de619743 100 {
AzureIoTClient 16:b2c6de619743 101 IOTHUBMESSAGE_DISPOSITION_RESULT result;
AzureIoTClient 16:b2c6de619743 102 const unsigned char* buffer;
AzureIoTClient 16:b2c6de619743 103 size_t size;
AzureIoTClient 16:b2c6de619743 104 if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
AzureIoTClient 16:b2c6de619743 105 {
AzureIoTClient 16:b2c6de619743 106 printf("unable to IoTHubMessage_GetByteArray\r\n");
AzureIoTClient 16:b2c6de619743 107 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 16:b2c6de619743 108 }
AzureIoTClient 16:b2c6de619743 109 else
AzureIoTClient 16:b2c6de619743 110 {
AzureIoTClient 16:b2c6de619743 111 /*buffer is not zero terminated*/
AzureIoTClient 16:b2c6de619743 112 char* temp = malloc(size + 1);
AzureIoTClient 16:b2c6de619743 113 if (temp == NULL)
AzureIoTClient 16:b2c6de619743 114 {
AzureIoTClient 16:b2c6de619743 115 printf("failed to malloc\r\n");
AzureIoTClient 16:b2c6de619743 116 result = EXECUTE_COMMAND_ERROR;
AzureIoTClient 16:b2c6de619743 117 }
AzureIoTClient 16:b2c6de619743 118 else
AzureIoTClient 16:b2c6de619743 119 {
AzureIoTClient 16:b2c6de619743 120 memcpy(temp, buffer, size);
AzureIoTClient 16:b2c6de619743 121 temp[size] = '\0';
AzureIoTClient 16:b2c6de619743 122 EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
AzureIoTClient 16:b2c6de619743 123 result =
AzureIoTClient 16:b2c6de619743 124 (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
AzureIoTClient 16:b2c6de619743 125 (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
AzureIoTClient 16:b2c6de619743 126 IOTHUBMESSAGE_REJECTED;
AzureIoTClient 16:b2c6de619743 127 free(temp);
AzureIoTClient 16:b2c6de619743 128 }
AzureIoTClient 16:b2c6de619743 129 }
AzureIoTClient 16:b2c6de619743 130 return result;
AzureIoTClient 16:b2c6de619743 131 }
AzureIoTClient 16:b2c6de619743 132
AzureIoTClient 16:b2c6de619743 133 void simplesample_amqp_run(void)
AzureIoTClient 16:b2c6de619743 134 {
Azure.IoT Build 29:12a5fd69f49e 135 if (platform_init() != 0)
AzureIoTClient 16:b2c6de619743 136 {
Azure.IoT Build 29:12a5fd69f49e 137 printf("Failed to initialize the platform.\r\n");
AzureIoTClient 16:b2c6de619743 138 }
AzureIoTClient 16:b2c6de619743 139 else
AzureIoTClient 16:b2c6de619743 140 {
Azure.IoT Build 29:12a5fd69f49e 141 if (serializer_init(NULL) != SERIALIZER_OK)
AzureIoTClient 16:b2c6de619743 142 {
Azure.IoT Build 29:12a5fd69f49e 143 (void)printf("Failed on serializer_init\r\n");
AzureIoTClient 16:b2c6de619743 144 }
AzureIoTClient 16:b2c6de619743 145 else
AzureIoTClient 16:b2c6de619743 146 {
Azure.IoT Build 29:12a5fd69f49e 147 /* Setup IoTHub client configuration */
Azure.IoT Build 29:12a5fd69f49e 148 IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol);
Azure.IoT Build 29:12a5fd69f49e 149 srand((unsigned int)time(NULL));
Azure.IoT Build 29:12a5fd69f49e 150 int avgWindSpeed = 10;
Azure.IoT Build 29:12a5fd69f49e 151
AzureIoTClient 43:f179aa2ce96f 152 // Turn on Log
AzureIoTClient 43:f179aa2ce96f 153 bool trace = true;
AzureIoTClient 43:f179aa2ce96f 154 (void)IoTHubClient_SetOption(iotHubClientHandle, "logtrace", &trace);
AzureIoTClient 43:f179aa2ce96f 155
Azure.IoT Build 29:12a5fd69f49e 156 if (iotHubClientHandle == NULL)
Azure.IoT Build 26:9662f727e974 157 {
Azure.IoT Build 29:12a5fd69f49e 158 (void)printf("Failed on IoTHubClient_Create\r\n");
AzureIoTClient 16:b2c6de619743 159 }
AzureIoTClient 16:b2c6de619743 160 else
AzureIoTClient 16:b2c6de619743 161 {
Azure.IoT Build 29:12a5fd69f49e 162 #ifdef MBED_BUILD_TIMESTAMP
Azure.IoT Build 29:12a5fd69f49e 163 // For mbed add the certificate information
Azure.IoT Build 29:12a5fd69f49e 164 if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
Azure.IoT Build 29:12a5fd69f49e 165 {
Azure.IoT Build 29:12a5fd69f49e 166 (void)printf("failure to set option \"TrustedCerts\"\r\n");
Azure.IoT Build 29:12a5fd69f49e 167 }
Azure.IoT Build 29:12a5fd69f49e 168 #endif // MBED_BUILD_TIMESTAMP
Azure.IoT Build 26:9662f727e974 169
Azure.IoT Build 29:12a5fd69f49e 170 ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
Azure.IoT Build 29:12a5fd69f49e 171 if (myWeather == NULL)
Azure.IoT Build 25:9cb9b0c15218 172 {
Azure.IoT Build 29:12a5fd69f49e 173 (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
AzureIoTClient 16:b2c6de619743 174 }
AzureIoTClient 16:b2c6de619743 175 else
AzureIoTClient 16:b2c6de619743 176 {
Azure.IoT Build 29:12a5fd69f49e 177 unsigned char* destination;
Azure.IoT Build 29:12a5fd69f49e 178 size_t destinationSize;
AzureIoTClient 16:b2c6de619743 179
Azure.IoT Build 29:12a5fd69f49e 180 if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
AzureIoTClient 16:b2c6de619743 181 {
Azure.IoT Build 29:12a5fd69f49e 182 printf("unable to IoTHubClient_SetMessageCallback\r\n");
AzureIoTClient 16:b2c6de619743 183 }
AzureIoTClient 16:b2c6de619743 184 else
AzureIoTClient 16:b2c6de619743 185 {
Azure.IoT Build 29:12a5fd69f49e 186 myWeather->DeviceId = "myFirstDevice";
Azure.IoT Build 29:12a5fd69f49e 187 myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
AzureIoTClient 16:b2c6de619743 188
AzureIoTClient 51:289ece2f210e 189 if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != CODEFIRST_OK)
Azure.IoT Build 29:12a5fd69f49e 190 {
Azure.IoT Build 29:12a5fd69f49e 191 (void)printf("Failed to serialize\r\n");
Azure.IoT Build 29:12a5fd69f49e 192 }
Azure.IoT Build 29:12a5fd69f49e 193 else
Azure.IoT Build 29:12a5fd69f49e 194 {
Azure.IoT Build 29:12a5fd69f49e 195 sendMessage(iotHubClientHandle, destination, destinationSize);
Azure.IoT Build 29:12a5fd69f49e 196 }
Azure.IoT Build 29:12a5fd69f49e 197
Azure.IoT Build 29:12a5fd69f49e 198 /* wait for commands */
Azure.IoT Build 29:12a5fd69f49e 199 (void)getchar();
Azure.IoT Build 29:12a5fd69f49e 200 }
Azure.IoT Build 29:12a5fd69f49e 201 DESTROY_MODEL_INSTANCE(myWeather);
AzureIoTClient 16:b2c6de619743 202 }
Azure.IoT Build 29:12a5fd69f49e 203 IoTHubClient_Destroy(iotHubClientHandle);
AzureIoTClient 16:b2c6de619743 204 }
Azure.IoT Build 29:12a5fd69f49e 205 serializer_deinit();
AzureIoTClient 16:b2c6de619743 206 }
Azure.IoT Build 29:12a5fd69f49e 207 platform_deinit();
AzureIoTClient 16:b2c6de619743 208 }
AzureIoTClient 16:b2c6de619743 209 }
AzureIoTClient 16:b2c6de619743 210