BUSINESS INSIGHTS

Dec 08, 2016

Azure IoT Hub and Streaming Analytics Services

Larry Baker Posted by Larry Baker

The industry has reached an inflection point where cheap hardware, ubiquitous connections, and powerful cloud platforms allow us to consume data from devices all around us. The Internet of Things (IoT) represents a compelling opportunity for organizations to gain operational insight and perform predictive analytics in real-time and at a grain previously unimagined. These capabilities allow businesses to improve efficiency, reliability, and agility.

AZURE IOT HUB AND STREAMING ANALYTICS SERVICES

From predicting and preventing product failures to rerouting flights to improve fuel efficiency, creative application of IoT capabilities represent a tremendous opportunity to gain a competitive advantage. Microsoft’s Azure IoT Hub is a hyperscale cloud solution that is easy to provision, use, and manage while providing functionality across the spectrum of IoT solution needs, from security to device management.

Microsoft offers several ways to get started with its platform. Azure IoT Suite offers two demo scenarios to demonstrate the power of the Azure IoT hub. The code for both the predictive maintenance and the remote monitoring scenarios are available on GitHub for you to customize. Raspberry Pi boards are an inexpensive and useful tool to explore the features and functionality of Azure IoT Hub with a physical device. Microsoft, in collaboration with Adafruit, sells an IoT Pack for Raspberry Pi 3 that is a wonderful way to jumpstart your development efforts or to get started with an IoT proof of concept. Microsoft also published a handy getting started guide that shows how to set up a development environment and how to set up the Raspberry Pi board. The getting started guide is also useful if you already have a Raspberry Pi board and simply want to experiment with Windows IoT Core and Azure IoT Hub.

Microsoft publishes Azure IoT Hub SDKs for C, .NET, Java, Node.js, and Python 2.7. The variety of popular languages gives you options to leverage your developer’s existing skills.  Windows IoT Core is targeted at .NET development, and Microsoft published a samples library that has code examples for everything from controlling a single LED to building a voice-controlled coffee maker.

One simple and fun starter project is to monitor the temperature and humidity in your environment using a DHT 11 or 12 sensor and to display a real-time graph of the data in PowerBI.  You can find the wiring diagrams for both sensors here.

IoT.jpg

Once you have the Raspberry Pi board configured and the sensor wired correctly, you can begin programming the device to stream data to the Azure IoT hub.  You can download and install the Windows IoT Core Project Templates for visual studio and use the Background Application template as a simple starter project.  In order to get started quickly, I recommend Daniel Porrey’s Universal Windows Library for the DHT11/22 sensors, which will allow you to focus on the Azure IoT Hub capabilities and device to cloud communication rather than low-level device programming. Add the library to your solution and add a reference to it in your background application project.  Newtonsoft’s json library is also an invaluable tool to easily serialize and de-serialize messages to and from the cloud.

The IoT Connector Client sample provides examples of how to connect to, send messages to, and receive messages from Azure IoT Hub.  Note that the sample assumes you are using the Trusted Platform Module (TPM) to secure connection information.  You can learn more about TPM and how to provision devices here.  The Raspberry Pi 3 does not contain a hardware TPM device, but rather uses a software emulated device. Your code should look something like this:

public void Run(IBackgroundTaskInstance taskInstance)
{
    deferral = taskInstance.GetDeferral();

    TpmDevice device = new TpmDevice(0);
    string hubUri = device.GetHostName();
    string deviceId = device.GetDeviceId();
    string sasToken = device.GetSASToken();

    _sendDeviceClient = DeviceClient.Create(hubUri, AuthenticationMethodFactory.CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp);

    InitGPIO();

    readingTimer = ThreadPoolTimer.CreatePeriodicTimer(ReadingTimerTick, TimeSpan.FromSeconds(1));
}

Once you are connected, simply put the code to read the DHT sensor, serialize the data, and send the message into the Background Application’s main timed loop.  Your code may look something like this:

private async void ReadingTimerTick(ThreadPoolTimer timer)
{
    DhtReading reading = new DhtReading();

    reading = await dht.GetReadingAsync().AsTask();

    if (reading.IsValid)
    {
        float currentTemperature = Convert.ToSingle(reading.Temperature);
        float currentHumidity = Convert.ToSingle(reading.Humidity);

        temperature = currentTemperature * 9 / 5 + 32;
        humidity = currentHumidity;

        var telemetryDataPoint = new
        {
            time = DateTime.Now.ToString(),
            deviceId = deviceId,
            location = "Office",
            currentHumidity = humidity,
            currentTemperature = temperature
        };

        var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
        var message = new Message(Encoding.ASCII.GetBytes(messageString));

        await _sendDeviceClient.SendEventAsync(message);
    }
}

Build and deploy the project to your Raspberry Pi.  You can use Device Explorer to confirm that your application is sending data to the Azure IoT hub.

IoT2.png

The final step is to use Azure Streaming Analytics Service to stream your data into PowerBI.  Configuring the stream to PowerBI is straightforward.  Simply choose Azure IoT Hub as a source and PowerBI as a destination.  A very simple example query is shown below.

IoT3.png

Microsoft’s how-to document on building a real-time analytics dashboard contains detailed instructions on how to configure Streaming Analytics Service and PowerBI to visualize the temperature and humidity data in real time.

IoT4.png

Azure IoT Hub is a unique offering that simplifies many of the difficult tasks involved with a large-scale IoT project’s implementation.  This article outlined a few possibilities to explore the features of the platform and to inexpensively begin experimenting with your own solution.

To learn more about how BlueGranite could help you build and manage your Azure IoT Hub, contact us today!

Creating Streaming Solutions Using Azure Webinar
Larry Baker

About The Author

Larry Baker

Larry is a Senior Solution Consultant at BlueGranite who specializes in data warehousing. He has extensive experience delivering successful business intelligence projects on the Microsoft Business Intelligence technical stack. His experience includes dimensional design, ETL, OLAP, and reporting solutions. Larry is also a former real time embedded systems programmer and test engineer for a medical device manufacturer.

Latest Posts

Creating Streaming Solutions Using Azure Webinar