Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: iothub_client EthernetInterface iothub_http_transport mbed-rtos mbed wolfSSL azure_c_shared_utility NTPClient
Diff: iothub_client_sample_http.c
- Revision:
- 42:289de245ba1d
- Parent:
- 41:7a930aa6dc01
- Child:
- 46:91bd03862871
diff -r 7a930aa6dc01 -r 289de245ba1d iothub_client_sample_http.c
--- a/iothub_client_sample_http.c Tue Jun 07 10:50:25 2016 -0700
+++ b/iothub_client_sample_http.c Fri Jun 17 17:04:01 2016 -0700
@@ -14,6 +14,7 @@
#else
#include "iothub_client_ll.h"
#include "iothub_message.h"
+#include "azure_c_shared_utility/threadapi.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "iothubtransporthttp.h"
#include "azure_c_shared_utility/platform.h"
@@ -29,6 +30,8 @@
static const char* connectionString = "[device connection string]";
static int callbackCounter;
+#define MESSAGE_COUNT 5
+static bool g_continueRunning;
DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES);
@@ -52,6 +55,10 @@
else
{
(void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size);
+ if (memcmp(buffer, "quit", size) == 0)
+ {
+ g_continueRunning = false;
+ }
}
// Retrieve properties from the message
@@ -102,6 +109,7 @@
EVENT_INSTANCE messages[MESSAGE_COUNT];
double avgWindSpeed = 10.0;
int receiveContext = 0;
+ g_continueRunning = true;
srand((unsigned int)time(NULL));
@@ -152,52 +160,57 @@
}
else
{
- int i;
-
(void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n");
/* Now that we are ready to receive commands, let's send some messages */
- for (i = 0; i < MESSAGE_COUNT; i++)
+ size_t iterator = 0;
+ do
{
- 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");
- }
- else
+ if (iterator < MESSAGE_COUNT)
{
- MAP_HANDLE propMap;
-
- messages[i].messageTrackingId = i;
-
- propMap = IoTHubMessage_Properties(messages[i].messageHandle);
- sprintf_s(propText, sizeof(propText), "PropMsg_%d", i);
- if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
+ sprintf_s(msgText, sizeof(msgText), "{\"deviceId\": \"myFirstDevice\",\"windSpeed\": %.2f}", avgWindSpeed + (rand() % 4 + 2));
+ if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
{
- (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
- }
-
- if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK)
- {
- (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
+ (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
}
else
{
- (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", i);
- }
+ MAP_HANDLE propMap;
+
+ messages[iterator].messageTrackingId = iterator;
- }
- }
- }
+ propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
+ sprintf_s(propText, sizeof(propText), "PropMsg_%d", iterator);
+ if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
+ {
+ (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
+ }
- /* Wait for Commands. */
- while (1)
- {
- IoTHubClient_LL_DoWork(iotHubClientHandle);
+ if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
+ {
+ (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
+ }
+ else
+ {
+ (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%zu] for transmission to IoT Hub.\r\n", iterator);
+ }
+
+ }
+ }
+ IoTHubClient_LL_DoWork(iotHubClientHandle);
+ ThreadAPI_Sleep(1);
+
+ iterator++;
+ } while (g_continueRunning);
}
-
IoTHubClient_LL_Destroy(iotHubClientHandle);
}
platform_deinit();
}
}
+
+int main(void)
+{
+ iothub_client_sample_http_run();
+ return 0;
+}
