Skip to content

Instantly share code, notes, and snippets.

@hauserkristof
Created June 15, 2021 17:01
Show Gist options
  • Save hauserkristof/5477ee4c3828c0bb33eeb74cd09f5bc2 to your computer and use it in GitHub Desktop.
Save hauserkristof/5477ee4c3828c0bb33eeb74cd09f5bc2 to your computer and use it in GitHub Desktop.
Send D2C message #IoTHub
(void)printf("Sending 1 messages to IoTHub every %d seconds for %d messages (Send any message to stop)\r\n", TIME_BETWEEN_MESSAGES, MESSAGES_TO_SEND);
do
{
if (iothub_info.connected != 0)
{
// Send a message every TIME_BETWEEN_MESSAGES seconds
(void)tickcounter_get_current_ms(tick_counter_handle, &current_tick);
if ((current_tick - last_send_time) / 1000 > TIME_BETWEEN_MESSAGES)
{
static char msgText[1024];
sprintf_s(msgText, sizeof(msgText), "{ \"message_index\" : \"%zu\" }", msg_count++);
IOTHUB_MESSAGE_HANDLE msg_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText));
if (msg_handle == NULL)
{
(void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
}
else
{
if (IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, msg_handle, NULL, NULL) != IOTHUB_CLIENT_OK)
{
(void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
}
else
{
(void)tickcounter_get_current_ms(tick_counter_handle, &last_send_time);
(void)printf("IoTHubClient_LL_SendEventAsync accepted message [%zu] for transmission to IoT Hub.\r\n", msg_count);
}
IoTHubMessage_Destroy(msg_handle);
}
}
}
IoTHubDeviceClient_LL_DoWork(device_ll_handle);
ThreadAPI_Sleep(1);
} while (iothub_info.stop_running == 0 && msg_count < MESSAGES_TO_SEND);
size_t index = 0;
for (index = 0; index < 10; index++)
{
IoTHubDeviceClient_LL_DoWork(device_ll_handle);
ThreadAPI_Sleep(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment