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.
Diff: simplesample_amqp.c
- Revision:
- 25:9cb9b0c15218
- Parent:
- 19:eae21b79ceff
- Child:
- 26:9662f727e974
diff -r a025f06802ec -r 9cb9b0c15218 simplesample_amqp.c --- a/simplesample_amqp.c Fri Jan 29 23:50:22 2016 +0000 +++ b/simplesample_amqp.c Thu Feb 04 11:33:25 2016 -0800 @@ -12,6 +12,7 @@ #include "iothub_client.h" #include "iothubtransportamqp.h" #include "iothub_client_ll.h" +#include "platform.h" #ifdef MBED_BUILD_TIMESTAMP #include "certs.h" @@ -128,67 +129,75 @@ void simplesample_amqp_run(void) { - if (serializer_init(NULL) != SERIALIZER_OK) + if (platform_init() != 0) { (void)printf("Failed on serializer_init\r\n"); } else { - /* Setup IoTHub client configuration */ - IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol); - srand((unsigned int)time(NULL)); - int avgWindSpeed = 10; - - if (iotHubClientHandle == NULL) + if (serializer_init(NULL) != SERIALIZER_OK) { - (void)printf("Failed on IoTHubClient_Create\r\n"); + (void)printf("Failed on serializer_init\r\n"); } else { -#ifdef MBED_BUILD_TIMESTAMP - // For mbed add the certificate information - if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) + /* Setup IoTHub client configuration */ + IOTHUB_CLIENT_HANDLE iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol); + srand((unsigned int)time(NULL)); + int avgWindSpeed = 10; + + if (iotHubClientHandle == NULL) { - (void)printf("failure to set option \"TrustedCerts\"\r\n"); - } -#endif // MBED_BUILD_TIMESTAMP - - ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer); - if (myWeather == NULL) - { - (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n"); + (void)printf("Failed on IoTHubClient_Create\r\n"); } else { - unsigned char* destination; - size_t destinationSize; +#ifdef MBED_BUILD_TIMESTAMP + // For mbed add the certificate information + if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) + { + (void)printf("failure to set option \"TrustedCerts\"\r\n"); + } +#endif // MBED_BUILD_TIMESTAMP - if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK) + ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer); + if (myWeather == NULL) { - printf("unable to IoTHubClient_SetMessageCallback\r\n"); + (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n"); } else { - myWeather->DeviceId = "myFirstDevice"; - myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2); + unsigned char* destination; + size_t destinationSize; - if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != IOT_AGENT_OK) + if (IoTHubClient_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK) { - (void)printf("Failed to serialize\r\n"); + printf("unable to IoTHubClient_SetMessageCallback\r\n"); } else { - sendMessage(iotHubClientHandle, destination, destinationSize); - } + myWeather->DeviceId = "myFirstDevice"; + myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2); - /* wait for commands */ - (void)getchar(); + if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != IOT_AGENT_OK) + { + (void)printf("Failed to serialize\r\n"); + } + else + { + sendMessage(iotHubClientHandle, destination, destinationSize); + } + + /* wait for commands */ + (void)getchar(); + } + DESTROY_MODEL_INSTANCE(myWeather); } - DESTROY_MODEL_INSTANCE(myWeather); + IoTHubClient_Destroy(iotHubClientHandle); } - IoTHubClient_Destroy(iotHubClientHandle); + serializer_deinit(); } - serializer_deinit(); + platform_deinit(); } }