Click here to Skip to main content
15,921,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have raspberry pi 3 device running windows 10 iot core and visual studio 2015 community,
I want to send the data from my device to azure event hub, I spent couples of day on google I got some console application where I can send and receive the data on azure event hub, same thin I tried with UWP but its not working for me,
await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));

on this line Keep on waiting only, not going forward.

I referred this link
https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-send
and
http://stackoverflow.com/questions/36061131/uwp-app-for-recievering-messages-from-azure-iot-hub/38195572#38195572
Can any one have sample that will help me to send/receive data to/from Azure Event Hub.

thanks
sushil

What I have tried:

C#
private static EventHubClient eventHubClient;
        private const string EhConnectionString = "Endpoint=sb://-----.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=-------";

private const string EhEntityPath = "------";

private async Task MainAsync()
        {
            try
            {
                var connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString)
                {
                    EntityPath = EhEntityPath
                };

                eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
                await SendMessagesToEventHub();
                await eventHubClient.CloseAsync();
            }
            catch (Exception ex)
            {
                textBlock.Text = ex.Message;
            }
        }

private async Task SendMessagesToEventHub()
        {
            int avgSpeed = 10; // m/s
            Random rand = new Random();
            int seq = 0;
            while (true)
            {
                int currentSpeed = avgSpeed + rand.Next() * 4 - 2;
                var data = new
                {
                    guid = "guid_1",
                    organization = "Elpis_IT_2",
                    displayname = "Telemetry_2",
                    location = "Elpis_Lab_2",
                    measurename = "mn_2",
                    unitofmeasure = "n",
                    timecreated = DateTime.Now.ToString() + "__" + (++seq),
                    value = rand.Next()
                };
                var message = JsonConvert.SerializeObject(data); 
                try
                {
                    await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
                    textBlock1.Text = $"Sending message: {message}";
                    await Task.Delay(10);
                    //number--;
                }
                catch (Exception ex)
                {
                    textBlock2.Text = ex.Message;
                }
            }
        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            MainAsync();
        }
Posted
Updated 12-Feb-17 19:38pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900