Bardaan A
Published © GPL3+

003 - How to Stream Device Data to the Azure IoT Hub

This is a tutorial on how to collect and stream your device data to the Azure IoT Hub. We'll also show how to monitor IoT Hub messages.

IntermediateFull instructions provided12 hours5,214
003 - How to Stream Device Data to the Azure IoT Hub

Things used in this project

Story

Read more

Code

MonitorIoTHub

C#
This program when run after following all the necessary steps as explained in this tutorial will read the EventHub messages and display them on the Console Window. The messages read by this program is in fact an acknowledgement of Payload received by the IoT Hub.
////The MIT License(MIT)
////Copyright(c) 2016 BardaanA

////Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

////The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

////THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// This namespace contains the class EventHubClient which is what we're going to use to
// receive the messages from IoT Hub
using Microsoft.ServiceBus.Messaging;
using System.Threading;


namespace MonitorIoTHub
{
    class Program
    {
        // This is the Connection String that you copy from youe IoT Hub Shared Access Policies
        static string connectionString = "Don't forget to replace this with you own connection string";
        // This tells the EventHubClient to listen to both events and messages.
        static string iotHubD2cEndpoint = "messages/events";
        // EventHubClient, this is what gives us access to the EventHub
        static EventHubClient eventHubClient;
        static string[] d2cPartitions;  // This string array was created just to hold the partition values
        // EventHubReceiver, this is what handles the Receiving job
        static EventHubReceiver eventHubReceiver;
        // EventData, this is where Received Data is stored for further action.
        static EventData eventData;
        static string data;  // This was created to temporarily store the Event data.

        static void Main(string[] args)
        {
            try
            {
                // Create an instance of EventHubClient
                eventHubClient = EventHubClient.CreateFromConnectionString(connectionString,iotHubD2cEndpoint);
            }
            catch(Exception ex)
            {
                Console.WriteLine("EventHub client creation fail!  {0}",ex.Message);  // Why EventHub why??
            }

            if(eventHubClient != null)
            {
                // string[] of partitions
                d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;

                foreach(string partition in d2cPartitions)
                {
                    // So this is where the real job is handled to the method
                    ReceiveMessage(partition).Wait();
                }
            }            
        }

        // Method designed to poll the EventHub and actually gather EventHub Data
        private static async Task ReceiveMessage(string partitionS)
        {
            // Creata an instance of EventHubReceiver
            eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partitionS,DateTime.UtcNow);
            while(true)
            {
                // So this is where all the magic happens...
                eventData = await eventHubReceiver.ReceiveAsync();
                if(eventData != null)
                {
                    data = Encoding.UTF8.GetString(eventData.GetBytes());
                    Console.WriteLine("Partition: {0} :: Data: {1}",partitionS,data);  // Finally, Ugh!
                }
            }
        }
    }
}

Credits

Bardaan A

Bardaan A

8 projects • 16 followers
Just another Software and Technology enthusiast.
Thanks to Microsoft.

Comments