IoTHub raw messaging sample using HTTP
Dependencies: iothub_client EthernetInterface iothub_http_transport mbed-rtos mbed wolfSSL azure_c_shared_utility NTPClient
This sample showcases the usage of Azure IoT client libraries with the HTTP transport for sending/receiving raw messages from an IoT Hub.
Diff: iothub_client_sample_http.c
- Revision:
- 10:e42076d83f79
- Parent:
- 0:0e4a5a208d47
--- a/iothub_client_sample_http.c Thu Sep 17 00:21:39 2015 -0700 +++ b/iothub_client_sample_http.c Tue Sep 22 20:37:29 2015 -0700 @@ -20,7 +20,6 @@ #endif // MBED_BUILD_TIMESTAMP static const char* connectionString = "[device connection string]"; - static int callbackCounter; DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES); @@ -31,22 +30,22 @@ 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) { printf("unable to retrieve the message data\r\n"); } else { - (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; @@ -90,12 +89,15 @@ 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 HTTP...\r\n"); - if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, IoTHubTransportHttp_ProvideTransportInterface)) == NULL) + if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol)) == NULL) { (void)printf("ERROR: iotHubClientHandle is NULL!\r\n"); } @@ -121,19 +123,19 @@ } #endif // MBED_BUILD_TIMESTAMP - /* Setting Notification call back, so we can receive Commands. */ - if (IoTHubClient_LL_SetNotificationCallback(iotHubClientHandle, ReceiveNotificationCallback, &receiveContext) != IOTHUB_CLIENT_OK) + /* Setting Message call back, so we can receive Commands. */ + if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) { - (void)printf("ERROR: IoTHubClient_LL_SetNotificationCallback..........FAILED!\r\n"); + (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n"); } else { - (void)printf("IoTHubClient_LL_SetNotificationCallback...successful.\r\n"); + (void)printf("IoTHubClient_LL_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++) { - sprintf_s(msgText, sizeof(msgText), "Message_%d_From_IoTHubClient_LL_Over_HTTP", 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");