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.
simplesample_amqp.c@64:a1b8a60c0a5b, 2017-05-08 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Mon May 08 10:53:01 2017 -0700
- Revision:
- 64:a1b8a60c0a5b
- Parent:
- 56:68f0d4c20dea
- Child:
- 74:b684f612f838
1.1.14
Who changed what in which revision?
User | Revision | Line number | New 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 | 64:a1b8a60c0a5b | 35 | WITH_DATA(float, Temperature), |
AzureIoTClient | 64:a1b8a60c0a5b | 36 | WITH_DATA(float, Humidity), |
AzureIoTClient | 16:b2c6de619743 | 37 | WITH_ACTION(TurnFanOn), |
AzureIoTClient | 16:b2c6de619743 | 38 | WITH_ACTION(TurnFanOff), |
AzureIoTClient | 16:b2c6de619743 | 39 | WITH_ACTION(SetAirResistance, int, Position) |
AzureIoTClient | 16:b2c6de619743 | 40 | ); |
AzureIoTClient | 16:b2c6de619743 | 41 | |
AzureIoTClient | 16:b2c6de619743 | 42 | END_NAMESPACE(WeatherStation); |
AzureIoTClient | 16:b2c6de619743 | 43 | |
AzureIoTClient | 64:a1b8a60c0a5b | 44 | static char propText[1024]; |
AzureIoTClient | 16:b2c6de619743 | 45 | |
AzureIoTClient | 16:b2c6de619743 | 46 | EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device) |
AzureIoTClient | 16:b2c6de619743 | 47 | { |
AzureIoTClient | 16:b2c6de619743 | 48 | (void)device; |
AzureIoTClient | 16:b2c6de619743 | 49 | (void)printf("Turning fan on.\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 TurnFanOff(ContosoAnemometer* device) |
AzureIoTClient | 16:b2c6de619743 | 54 | { |
AzureIoTClient | 16:b2c6de619743 | 55 | (void)device; |
AzureIoTClient | 16:b2c6de619743 | 56 | (void)printf("Turning fan off.\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 57 | return EXECUTE_COMMAND_SUCCESS; |
AzureIoTClient | 16:b2c6de619743 | 58 | } |
AzureIoTClient | 16:b2c6de619743 | 59 | |
AzureIoTClient | 16:b2c6de619743 | 60 | EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position) |
AzureIoTClient | 16:b2c6de619743 | 61 | { |
AzureIoTClient | 16:b2c6de619743 | 62 | (void)device; |
AzureIoTClient | 16:b2c6de619743 | 63 | (void)printf("Setting Air Resistance Position to %d.\r\n", Position); |
AzureIoTClient | 16:b2c6de619743 | 64 | return EXECUTE_COMMAND_SUCCESS; |
AzureIoTClient | 16:b2c6de619743 | 65 | } |
AzureIoTClient | 16:b2c6de619743 | 66 | |
AzureIoTClient | 16:b2c6de619743 | 67 | void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
AzureIoTClient | 16:b2c6de619743 | 68 | { |
AzureIoTClient | 43:f179aa2ce96f | 69 | unsigned int messageTrackingId = (unsigned int)(uintptr_t)userContextCallback; |
AzureIoTClient | 16:b2c6de619743 | 70 | |
AzureIoTClient | 43:f179aa2ce96f | 71 | (void)printf("Message Id: %u Received.\r\n", messageTrackingId); |
AzureIoTClient | 16:b2c6de619743 | 72 | |
AzureIoTClient | 16:b2c6de619743 | 73 | (void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); |
AzureIoTClient | 16:b2c6de619743 | 74 | } |
AzureIoTClient | 16:b2c6de619743 | 75 | |
AzureIoTClient | 16:b2c6de619743 | 76 | |
AzureIoTClient | 64:a1b8a60c0a5b | 77 | static void sendMessage(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size, ContosoAnemometer *myWeather) |
AzureIoTClient | 16:b2c6de619743 | 78 | { |
AzureIoTClient | 16:b2c6de619743 | 79 | static unsigned int messageTrackingId; |
AzureIoTClient | 16:b2c6de619743 | 80 | IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size); |
AzureIoTClient | 16:b2c6de619743 | 81 | if (messageHandle == NULL) |
AzureIoTClient | 16:b2c6de619743 | 82 | { |
AzureIoTClient | 16:b2c6de619743 | 83 | printf("unable to create a new IoTHubMessage\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 84 | } |
AzureIoTClient | 16:b2c6de619743 | 85 | else |
AzureIoTClient | 16:b2c6de619743 | 86 | { |
AzureIoTClient | 64:a1b8a60c0a5b | 87 | MAP_HANDLE propMap = IoTHubMessage_Properties(messageHandle); |
AzureIoTClient | 64:a1b8a60c0a5b | 88 | (void)sprintf_s(propText, sizeof(propText), myWeather->Temperature > 28 ? "true" : "false"); |
AzureIoTClient | 64:a1b8a60c0a5b | 89 | if (Map_AddOrUpdate(propMap, "temperatureAlert", propText) != MAP_OK) |
AzureIoTClient | 64:a1b8a60c0a5b | 90 | { |
AzureIoTClient | 64:a1b8a60c0a5b | 91 | (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n"); |
AzureIoTClient | 64:a1b8a60c0a5b | 92 | } |
AzureIoTClient | 64:a1b8a60c0a5b | 93 | |
AzureIoTClient | 16:b2c6de619743 | 94 | if (IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 16:b2c6de619743 | 95 | { |
AzureIoTClient | 16:b2c6de619743 | 96 | printf("failed to hand over the message to IoTHubClient"); |
AzureIoTClient | 16:b2c6de619743 | 97 | } |
AzureIoTClient | 16:b2c6de619743 | 98 | else |
AzureIoTClient | 16:b2c6de619743 | 99 | { |
AzureIoTClient | 16:b2c6de619743 | 100 | printf("IoTHubClient accepted the message for delivery\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 101 | } |
AzureIoTClient | 16:b2c6de619743 | 102 | |
AzureIoTClient | 16:b2c6de619743 | 103 | IoTHubMessage_Destroy(messageHandle); |
AzureIoTClient | 16:b2c6de619743 | 104 | } |
AzureIoTClient | 16:b2c6de619743 | 105 | free((void*)buffer); |
AzureIoTClient | 16:b2c6de619743 | 106 | messageTrackingId++; |
AzureIoTClient | 16:b2c6de619743 | 107 | } |
AzureIoTClient | 16:b2c6de619743 | 108 | |
AzureIoTClient | 16:b2c6de619743 | 109 | static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
AzureIoTClient | 16:b2c6de619743 | 110 | { |
AzureIoTClient | 16:b2c6de619743 | 111 | IOTHUBMESSAGE_DISPOSITION_RESULT result; |
AzureIoTClient | 16:b2c6de619743 | 112 | const unsigned char* buffer; |
AzureIoTClient | 16:b2c6de619743 | 113 | size_t size; |
AzureIoTClient | 16:b2c6de619743 | 114 | if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 16:b2c6de619743 | 115 | { |
AzureIoTClient | 16:b2c6de619743 | 116 | printf("unable to IoTHubMessage_GetByteArray\r\n"); |
AzureIoTClient | 56:68f0d4c20dea | 117 | result = IOTHUBMESSAGE_ABANDONED; |
AzureIoTClient | 16:b2c6de619743 | 118 | } |
AzureIoTClient | 16:b2c6de619743 | 119 | else |
AzureIoTClient | 16:b2c6de619743 | 120 | { |
AzureIoTClient | 16:b2c6de619743 | 121 | /*buffer is not zero terminated*/ |
AzureIoTClient | 16:b2c6de619743 | 122 | char* temp = malloc(size + 1); |
AzureIoTClient | 16:b2c6de619743 | 123 | if (temp == NULL) |
AzureIoTClient | 16:b2c6de619743 | 124 | { |
AzureIoTClient | 16:b2c6de619743 | 125 | printf("failed to malloc\r\n"); |
AzureIoTClient | 56:68f0d4c20dea | 126 | result = IOTHUBMESSAGE_ABANDONED; |
AzureIoTClient | 16:b2c6de619743 | 127 | } |
AzureIoTClient | 16:b2c6de619743 | 128 | else |
AzureIoTClient | 16:b2c6de619743 | 129 | { |
AzureIoTClient | 56:68f0d4c20dea | 130 | (void)memcpy(temp, buffer, size); |
AzureIoTClient | 16:b2c6de619743 | 131 | temp[size] = '\0'; |
AzureIoTClient | 16:b2c6de619743 | 132 | EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp); |
AzureIoTClient | 16:b2c6de619743 | 133 | result = |
AzureIoTClient | 16:b2c6de619743 | 134 | (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED : |
AzureIoTClient | 16:b2c6de619743 | 135 | (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED : |
AzureIoTClient | 16:b2c6de619743 | 136 | IOTHUBMESSAGE_REJECTED; |
AzureIoTClient | 16:b2c6de619743 | 137 | free(temp); |
AzureIoTClient | 16:b2c6de619743 | 138 | } |
AzureIoTClient | 16:b2c6de619743 | 139 | } |
AzureIoTClient | 16:b2c6de619743 | 140 | return result; |
AzureIoTClient | 16:b2c6de619743 | 141 | } |
AzureIoTClient | 16:b2c6de619743 | 142 | |
AzureIoTClient | 16:b2c6de619743 | 143 | void simplesample_amqp_run(void) |
AzureIoTClient | 16:b2c6de619743 | 144 | { |
Azure.IoT Build | 29:12a5fd69f49e | 145 | if (platform_init() != 0) |
AzureIoTClient | 16:b2c6de619743 | 146 | { |
Azure.IoT Build | 29:12a5fd69f49e | 147 | printf("Failed to initialize the platform.\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 148 | } |
AzureIoTClient | 16:b2c6de619743 | 149 | else |
AzureIoTClient | 16:b2c6de619743 | 150 | { |
Azure.IoT Build | 29:12a5fd69f49e | 151 | if (serializer_init(NULL) != SERIALIZER_OK) |
AzureIoTClient | 16:b2c6de619743 | 152 | { |
Azure.IoT Build | 29:12a5fd69f49e | 153 | (void)printf("Failed on serializer_init\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 154 | } |
AzureIoTClient | 16:b2c6de619743 | 155 | else |
AzureIoTClient | 16:b2c6de619743 | 156 | { |
Azure.IoT Build | 29:12a5fd69f49e | 157 | /* Setup IoTHub client configuration */ |
Azure.IoT Build | 29:12a5fd69f49e | 158 | IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol); |
Azure.IoT Build | 29:12a5fd69f49e | 159 | srand((unsigned int)time(NULL)); |
Azure.IoT Build | 29:12a5fd69f49e | 160 | int avgWindSpeed = 10; |
AzureIoTClient | 64:a1b8a60c0a5b | 161 | float minTemperature = 20.0; |
AzureIoTClient | 64:a1b8a60c0a5b | 162 | float minHumidity = 60.0; |
Azure.IoT Build | 29:12a5fd69f49e | 163 | |
AzureIoTClient | 43:f179aa2ce96f | 164 | // Turn on Log |
AzureIoTClient | 43:f179aa2ce96f | 165 | bool trace = true; |
AzureIoTClient | 43:f179aa2ce96f | 166 | (void)IoTHubClient_SetOption(iotHubClientHandle, "logtrace", &trace); |
AzureIoTClient | 43:f179aa2ce96f | 167 | |
Azure.IoT Build | 29:12a5fd69f49e | 168 | if (iotHubClientHandle == NULL) |
Azure.IoT Build | 26:9662f727e974 | 169 | { |
Azure.IoT Build | 29:12a5fd69f49e | 170 | (void)printf("Failed on IoTHubClient_Create\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 171 | } |
AzureIoTClient | 16:b2c6de619743 | 172 | else |
AzureIoTClient | 16:b2c6de619743 | 173 | { |
Azure.IoT Build | 29:12a5fd69f49e | 174 | #ifdef MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 29:12a5fd69f49e | 175 | // For mbed add the certificate information |
Azure.IoT Build | 29:12a5fd69f49e | 176 | if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
Azure.IoT Build | 29:12a5fd69f49e | 177 | { |
Azure.IoT Build | 29:12a5fd69f49e | 178 | (void)printf("failure to set option \"TrustedCerts\"\r\n"); |
Azure.IoT Build | 29:12a5fd69f49e | 179 | } |
Azure.IoT Build | 29:12a5fd69f49e | 180 | #endif // MBED_BUILD_TIMESTAMP |
Azure.IoT Build | 26:9662f727e974 | 181 | |
Azure.IoT Build | 29:12a5fd69f49e | 182 | ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer); |
Azure.IoT Build | 29:12a5fd69f49e | 183 | if (myWeather == NULL) |
Azure.IoT Build | 25:9cb9b0c15218 | 184 | { |
Azure.IoT Build | 29:12a5fd69f49e | 185 | (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 186 | } |
AzureIoTClient | 16:b2c6de619743 | 187 | else |
AzureIoTClient | 16:b2c6de619743 | 188 | { |
Azure.IoT Build | 29:12a5fd69f49e | 189 | unsigned char* destination; |
Azure.IoT Build | 29:12a5fd69f49e | 190 | size_t destinationSize; |
AzureIoTClient | 16:b2c6de619743 | 191 | |
Azure.IoT Build | 29:12a5fd69f49e | 192 | if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK) |
AzureIoTClient | 16:b2c6de619743 | 193 | { |
Azure.IoT Build | 29:12a5fd69f49e | 194 | printf("unable to IoTHubClient_SetMessageCallback\r\n"); |
AzureIoTClient | 16:b2c6de619743 | 195 | } |
AzureIoTClient | 16:b2c6de619743 | 196 | else |
AzureIoTClient | 16:b2c6de619743 | 197 | { |
Azure.IoT Build | 29:12a5fd69f49e | 198 | myWeather->DeviceId = "myFirstDevice"; |
Azure.IoT Build | 29:12a5fd69f49e | 199 | myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2); |
AzureIoTClient | 64:a1b8a60c0a5b | 200 | myWeather->Temperature = minTemperature + (rand() % 10); |
AzureIoTClient | 64:a1b8a60c0a5b | 201 | myWeather->Humidity = minHumidity + (rand() % 20); |
AzureIoTClient | 16:b2c6de619743 | 202 | |
AzureIoTClient | 64:a1b8a60c0a5b | 203 | if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed, myWeather->Temperature, myWeather->Humidity) != CODEFIRST_OK) |
Azure.IoT Build | 29:12a5fd69f49e | 204 | { |
Azure.IoT Build | 29:12a5fd69f49e | 205 | (void)printf("Failed to serialize\r\n"); |
Azure.IoT Build | 29:12a5fd69f49e | 206 | } |
Azure.IoT Build | 29:12a5fd69f49e | 207 | else |
Azure.IoT Build | 29:12a5fd69f49e | 208 | { |
AzureIoTClient | 64:a1b8a60c0a5b | 209 | sendMessage(iotHubClientHandle, destination, destinationSize, myWeather); |
Azure.IoT Build | 29:12a5fd69f49e | 210 | } |
Azure.IoT Build | 29:12a5fd69f49e | 211 | |
Azure.IoT Build | 29:12a5fd69f49e | 212 | /* wait for commands */ |
Azure.IoT Build | 29:12a5fd69f49e | 213 | (void)getchar(); |
Azure.IoT Build | 29:12a5fd69f49e | 214 | } |
Azure.IoT Build | 29:12a5fd69f49e | 215 | DESTROY_MODEL_INSTANCE(myWeather); |
AzureIoTClient | 16:b2c6de619743 | 216 | } |
Azure.IoT Build | 29:12a5fd69f49e | 217 | IoTHubClient_Destroy(iotHubClientHandle); |
AzureIoTClient | 16:b2c6de619743 | 218 | } |
Azure.IoT Build | 29:12a5fd69f49e | 219 | serializer_deinit(); |
AzureIoTClient | 16:b2c6de619743 | 220 | } |
Azure.IoT Build | 29:12a5fd69f49e | 221 | platform_deinit(); |
AzureIoTClient | 16:b2c6de619743 | 222 | } |
AzureIoTClient | 16:b2c6de619743 | 223 | } |
AzureIoTClient | 16:b2c6de619743 | 224 |