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:
-
-
- Azure IoT
- Azure EventHub–sending IoT device events to EventHub
- Azure ServiceBus Queue–reading query results from Stream Analytics
- Azure Stream Analytics–reading events from EventHub, running query and saving results to ServiceBus
- 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.
-
Log into Azure Portal
Click on + New button
In the Search, type Event Hubs
Click on the Event Hubs and click on Create button
Provide a name, resource group, pricing tier and Create button as shown
Once this Event Hubs is deployed, navigate to this newly created Event Hubs
Now get the Connection String for this Event Hub
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
Now lets write a console app to push events to this EventHub
In Visual Studio, create a console app
Add Azure ServiceBus and Azure ServiceBus.EventProcessorHost NuGet packages
Add following code (Complete code is available here )
static void SendEvents(int count) { var eventHubName = "wabaceventhub"; var connectionString = "Endpoint=sb://wabaceventhubs.servicebus.windows.net/;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))); Console.Write("."); System.Threading.Thread.Sleep(1000); } Console.WriteLine("done"); } static void CheckIfEventHubGotEvents() { var eventHubName = "wabaceventhub"; var eventHubConnectionString = "Endpoint=sb://wabaceventhubs.servicebus.windows.net/;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); }; eventProcessorHost.RegisterEventProcessorAsync(options).Wait(); Console.WriteLine("Receiving. Press enter key to stop worker."); Console.ReadLine(); eventProcessorHost.UnregisterEventProcessorAsync().Wait(); }
Run this code to push 10 events to EventHub and read from the event hub
Next we are going to create Azure ServiceBus Queue, click here to continue