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:
AzureIoTClient
Date:
Mon Jul 18 16:46:58 2016 -0700
Revision:
43:f179aa2ce96f
Parent:
42:6bb5884a0648
Child:
51:289ece2f210e
1.0.11

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