A simple IoTHub sample using HTTP as transport
Dependencies: EthernetInterface NTPClient iothub_client iothub_http_transport mbed-rtos mbed wolfSSL serializer azure_c_shared_utility
This sample showcases the usage of Azure IoT client libraries with the HTTP transport for sending/receiving raw messages from an IoT Hub.
Diff: simplesample_http.c
- Revision:
- 19:253a0a1ea1c5
- Parent:
- 17:3abbcd6aac9f
- Child:
- 20:188fd25e65fc
--- a/simplesample_http.c Fri Oct 23 01:39:18 2015 +0000 +++ b/simplesample_http.c Thu Dec 17 18:24:13 2015 -0800 @@ -11,15 +11,21 @@ Simply changing the using the convenience layer (functions not having _LL) and removing calls to _DoWork will yield the same results. */ +#ifdef ARDUINO +#include "AzureIoT.h" +#else #include "serializer.h" #include "iothub_client_ll.h" #include "iothubtransporthttp.h" #include "threadapi.h" +#endif #ifdef MBED_BUILD_TIMESTAMP #include "certs.h" #endif // MBED_BUILD_TIMESTAMP +/*String containing Hostname, Device Id & Device Key in the format: */ +/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */ static const char* connectionString = "[device connection string]"; // Define the Model @@ -113,9 +119,11 @@ } else { + EXECUTE_COMMAND_RESULT executeCommandResult; + memcpy(temp, buffer, size); temp[size] = '\0'; - EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp); + executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp); result = (executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED : (executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED : @@ -135,8 +143,9 @@ else { IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol); + double avgWindSpeed = 10.0; + srand((unsigned int)time(NULL)); - double avgWindSpeed = 10.0; if (iotHubClientHandle == NULL) { @@ -144,7 +153,14 @@ } else { - unsigned int minimumPollingTime = 9; /*because it can poll "after 9 seconds" polls will happen effectively at ~10 seconds*/ + // Because it can poll "after 9 seconds" polls will happen + // effectively at ~10 seconds. + // Note that for scalabilty, the default value of minimumPollingTime + // is 25 minutes. For more information, see: + // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging + unsigned int minimumPollingTime = 9; + ContosoAnemometer* myWeather; + if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK) { printf("failure to set option \"MinimumPollingTime\"\r\n"); @@ -158,7 +174,7 @@ } #endif // MBED_BUILD_TIMESTAMP - ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer); + myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer); if (myWeather == NULL) { (void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");