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:
- 29:12a5fd69f49e
- Parent:
- 28:0b867098c0c8
- Child:
- 35:862d7bc6755c
diff -r 0b867098c0c8 -r 12a5fd69f49e simplesample_amqp.c --- a/simplesample_amqp.c Thu Feb 04 17:40:49 2016 -0800 +++ b/simplesample_amqp.c Fri Mar 11 17:03:54 2016 -0800 @@ -129,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"); + printf("Failed to initialize the platform.\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(); } }