![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IoTHub raw messaging client sample using AMQP
Dependencies: iothub_client EthernetInterface NTPClient iothub_amqp_transport azure_c_shared_utility mbed-rtos mbed azure_uamqp_c wolfSSL
This sample showcases the usage of Azure IoT client libraries with the AMQP transport for sending/receiving raw messages from an IoT Hub.
Diff: iothub_client_sample_amqp.c
- Revision:
- 8:5db3366b6f57
- Parent:
- 4:24cbfabe1b9d
--- a/iothub_client_sample_amqp.c Sat Sep 19 03:49:34 2015 +0000 +++ b/iothub_client_sample_amqp.c Tue Sep 22 20:37:03 2015 -0700 @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. #include <stdio.h> +#include <stdlib.h> #include "iothub_client.h" #include "iothub_message.h" @@ -13,7 +14,6 @@ #include "certs.h" #endif // MBED_BUILD_TIMESTAMP - static const char* connectionString = "[device connection string]"; static int callbackCounter; @@ -25,18 +25,18 @@ int messageTrackingId; // For tracking the messages within the user callback. } EVENT_INSTANCE; -static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveNotificationCallback(IOTHUB_MESSAGE_HANDLE notificationMessage, void* userContextCallback) +static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { int* counter = (int*)userContextCallback; const char* buffer; size_t size; - if (IoTHubMessage_GetByteArray(notificationMessage, (const unsigned char**)&buffer, &size) == IOTHUB_MESSAGE_OK) + if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) == IOTHUB_MESSAGE_OK) { - (void)printf("Received Notification [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size); + (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size); } // Retrieve properties from the message - MAP_HANDLE mapProperties = IoTHubMessage_Properties(notificationMessage); + MAP_HANDLE mapProperties = IoTHubMessage_Properties(message); if (mapProperties != NULL) { const char*const* keys; @@ -80,38 +80,41 @@ EVENT_INSTANCE messages[MESSAGE_COUNT]; + srand((unsigned int)time(NULL)); + double avgWindSpeed = 10.0; + callbackCounter = 0; int receiveContext = 0; (void)printf("Starting the IoTHub client sample AMQP...\r\n"); - if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, IoTHubTransportAmqp_ProvideTransportInterface)) == NULL) + if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol)) == NULL) { (void)printf("ERROR: iotHubClientHandle is NULL!\r\n"); } else { #ifdef MBED_BUILD_TIMESTAMP - // For mbed add the certificate information - if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) - { - printf("failure to set option \"TrustedCerts\"\r\n"); - } + // For mbed add the certificate information + if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) + { + printf("failure to set option \"TrustedCerts\"\r\n"); + } #endif // MBED_BUILD_TIMESTAMP - /* Setting Notification call back, so we can receive Commands. */ - if (IoTHubClient_SetNotificationCallback(iotHubClientHandle, ReceiveNotificationCallback, &receiveContext) != IOTHUB_CLIENT_OK) + /* Setting Message call back, so we can receive Commands. */ + if (IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) { - (void)printf("ERROR: IoTHubClient_SetNotificationCallback..........FAILED!\r\n"); + (void)printf("ERROR: IoTHubClient_SetMessageCallback..........FAILED!\r\n"); } else { - (void)printf("IoTHubClient_SetNotificationCallback...successful.\r\n"); + (void)printf("IoTHubClient_SetMessageCallback...successful.\r\n"); /* Now that we are ready to receive commands, let's send some messages */ - for (int i = 0; i < MESSAGE_COUNT; i++) + for (size_t i = 0; i < MESSAGE_COUNT; i++) { - sprintf_s(msgText, sizeof(msgText), "Message_%d_From_IoTHubClient_Over_AMQP", i); + sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f}", avgWindSpeed+(rand()%4+2) ); if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL) { (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); @@ -135,7 +138,6 @@ { (void)printf("IoTHubClient_SendEventAsync accepted data for transmission to IoT Hub.\r\n"); } - } }