Azure EventHub–sending IoT device events to EventHub

This blog is part of IoT Series, where I am trying to build few IoT devices that push events to Azure EventHub.  From the EventHub, Azure Stream Analytics will execute my query to calculate average values for each individual device and publish these average values to Azure ServiceBus. From Azure ServiceBus, I am going to read the average values in Azure Functions Apps and save them into Azure Redis Cache. My Azure Website will poll this Redis Cache and displays the average values.

Here are list of blog posts in this series:

      1. Azure IoT
      2. Azure EventHub–sending IoT device events to EventHub
      3. Azure ServiceBus Queue–reading query results from Stream Analytics
      4. Azure Stream Analytics–reading events from EventHub, running query and saving results to ServiceBus
      5. Azure Function Apps – reading events from ServiceBus and writing to Redis Cache

In this blog, I am going to show how to configure Azure EventHub and use console app to send events.

    1. Log into Azure Portal

    2. Click on + New button

    3. In the Search, type Event Hubs

    4. Click on the Event Hubs and click on Create button

    5. Provide a name, resource group, pricing tier and Create button as shown

    6. Once this Event Hubs is deployed, navigate to this newly created Event Hubs

    7. Click on the Add Event Hub and type a name as shown below

    8. Now get the Connection String for this Event Hub

    9. Click on the Shared access policies in the Event Hubs blade (and not in the individual Event Hub blade) as shown below and copy the connection string

    10. Now lets write a console app to push events to this EventHub

    11. In Visual Studio, create a console app

    12. Add Azure ServiceBus and  Azure ServiceBus.EventProcessorHost NuGet packages

    13. Add following code (Complete code is available here )

       static void SendEvents(int count)
                  var eventHubName = "wabaceventhub";
                  var connectionString = "Endpoint=sb://;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=";
                  var eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, eventHubName);
                  Console.Write($"Sending {count} messages");
                  MyInput e = new MyInput();
                  Random r = new Random();
                  for (int i = 0; i < count; i++)
                      e.DeviceName = $"Console App - {i}";
                      e.DateTime = DateTime.Now.ToString("o");
                      e.temperature = r.Next(0, 100);
                      e.speed = r.Next(200, 300);
                      var msg = Newtonsoft.Json.JsonConvert.SerializeObject(e);
                      eventHubClient.Send(new EventData(Encoding.UTF8.GetBytes(msg)));
              static void CheckIfEventHubGotEvents()
                  var eventHubName = "wabaceventhub";
                  var eventHubConnectionString = "Endpoint=sb://;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=";
                  var storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=wabac;AccountKey=";
                  string eventProcessorHostName = Guid.NewGuid().ToString();
                  EventProcessorHost eventProcessorHost = new EventProcessorHost(eventProcessorHostName, eventHubName, EventHubConsumerGroup.DefaultGroupName, eventHubConnectionString, storageConnectionString);
                  var options = new EventProcessorOptions();
                  options.ExceptionReceived += (sender, e) => { Console.WriteLine(e.Exception); };
                  Console.WriteLine("Receiving. Press enter key to stop worker.");
    14. Run this code to push 10 events to EventHub and read from the event hub

    15. Next we are going to create Azure ServiceBus Queue, click here to continue