Skip to content

Latest commit

 

History

History

Module2-IntroAzureIoT-NoDevice

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

NOTE: Unlike Module 3, this lab has not gone through QA. There are likely some bugs in it. Please referr to module 3 if you have any problems.



Introduction to Azure IoT (Simulated IoT Device)#


Overview

Azure IoT Hub is an Azure service that enables secure and reliable bi-directional communications between your application back end and millions of devices. It allows the application back end to receive telemetry at scale from your devices, route that data to a stream event processor, and also to send cloud-to-device commands to specific devices. The bi-drectional communication is enabled through use of the AMQP protocol.

In this module, you'll learn to send data to Microsoft Azure, using a PC-based application simulating data, and to implement great IoT solutions taking advantage of Microsoft Azure advanced analytic services such as Azure Stream Analytics.

Note: The Raspberry Pi is not used in this Module. Everything is done throug the PC application. Lab 3 is an equivalent lab which uses the Raspberry Pi.

Objectives

In this module, you'll see how to:

  • Configure Azure IoT Hub to send telemetry from your Windows 10 IoT core device
  • Use Azure Stream Analytics to process the data from your device
  • Create a dashboard in Power BI to analyze the data
  • Consume the IoT hub data from an Azure Web Site

Prerequisites

The following is required to complete this module:

Note: You can take advantage of the Visual Studio Dev Essentials subscription in order to get everything you need to build and deploy your app on any platform.

Setup

In order to run the exercises in this module, you'll need to set up your environment first.

  1. Open Windows Explorer and browse to the module's Source folder.
  2. Right-click Setup.cmd and select Run as administrator to launch the setup process that will configure your environment and install the Visual Studio code snippets for this module.
  3. If the User Account Control dialog box is shown, confirm the action to proceed.

Note: Make sure you've checked all the dependencies for this module before running the setup.

Using the Code Snippets

Throughout the module document, you'll be instructed to insert code blocks. For your convenience, most of this code is provided as Visual Studio Code Snippets, which you can access from within Visual Studio 2015 to avoid having to add it manually.

Note: Each exercise is accompanied by a starting solution located in the Begin folder of the exercise that allows you to follow each exercise independently of the others. Please be aware that the code snippets that are added during an exercise are missing from these starting solutions and may not work until you have completed the exercise. Inside the source code for an exercise, you'll also find an End folder containing a Visual Studio solution with the code that results from completing the steps in the corresponding exercise. You can use these solutions as guidance if you need additional help as you work through this module.


Exercises

This module includes the following exercises:

  1. Setting up the app to send data to Azure
  2. Using Stream Analytics to process the data
  3. Analyzing telemetry data using Power BI
  4. (Optional) Consuming the IoT Hub data from a Website

Estimated time to complete this module: 60 minutes

Note: When you first start Visual Studio, you must select one of the predefined settings collections. Each predefined collection is designed to match a particular development style and determines window layouts, editor behavior, IntelliSense code snippets, and dialog box options. The procedures in this module describe the actions necessary to accomplish a given task in Visual Studio when using the General Development Settings collection. If you choose a different settings collection for your development environment, there may be differences in the steps that you should take into account.

Exercise 1: Setting up the app to send data to Azure

In this exercise, you'll prepare an Universal application on the PC to send telemetry data (temperature and light level) to Azure IoT Hub.

Task 1 - Creating an IoT Hub

In this task, you'll create an IoT Hub for communicating with your device (the device will be simulated locally using the Universal app).

  1. Go the Azure portal, by navigating to http://portal.azure.com

  2. Create a new IoT Hub. To do this, click New in the jumpbar, then click Internet of Things, then click Azure IoT Hub.

  3. Configure the IoT hub with the desired information:

  • Enter a Name for the hub e.g. iot-workshop,

  • Select a Pricing and scale tier (F1 Free tier is enough),

  • Create a new resource group, or select and existing one. For more information, see Using resource groups to manage your Azure resources.

  • Select the Region West US for where the service will be located.

    New IoT Hub Settings

    New IoT Hub Settings

  1. It can take a few minutes for the IoT hub to be created. Once it's ready, open the blade of the new IoT hub, take note of the hostname. Select the key icon at the top to access the shared access policy settings:

    IoT hub shared access policies

    IoT hub shared access policies

  2. Select the Shared access policy called iothubowner, and take note of the Primary key and connection string in the right blade. You should copy these into a text file since you'll need them later.

    Get IoT Hub owner connection string

    Get IoT Hub owner connection string

Task 2 - Registering your device

You must register your simulated device in order to be able to send and receive information from the Azure IoT Hub. This is done by registering a Device Identity in the IoT Hub.

  1. Open the Device Explorer app (C:\Program Files (x86)\Microsoft\DeviceExplorer\DeviceExplorer.exe), and fill the IoT Hub Connection String field with the connection string of the IoT Hub you created in previous steps, and click Update.

    Configure Device Explorer

    Configure Device Explore

  2. Go to the Management tab and click Create. The Create Device popup will be displayed. Fill the Device ID field with a new Id for your device (mySimulatedDevice for example) and click Create:

    Creating a Device Identity

    Creating a Device Identity

  3. Once the device identity is created, it will be displayed on the grid. Right click the identity you just created, select Copy connection string for selected device and take note of the value copied to your clipboard, since it will be required to connect your device with the IoT Hub.

    Copying Device connection information

    Copying Device connection information

    Note: The device identities registration can be automated using the Azure IoT Hubs SDK. An example of how to do that can be found here.

Task 3 - Sending telemetry data to the Azure IoT hub

Now that the simulated device is configured, you'll see how to make an application that simulates the sensors, and then sends those values to an Azure IoT Hub.

  1. Open in Visual Studio the IoTWorkshop.sln solution located at Source\Ex1\Begin folder.

  2. In Solution Explorer, right-click the IoTWorkshop project, and then click Manage NuGet Packages.

  3. In the NuGet Package Manager window, search for Microsoft Azure Devices, click Install to install the Microsoft.Azure.Devices.Client package, and accept the terms of use.

    This downloads, installs, and adds a reference to the Microsoft Azure IoT Service SDK NuGet package.

  4. Add the following using statements at the top of the MainPage.xaml.cs file:

    (Code Snippet - AzureIoT - Using)

    using Microsoft.Azure.Devices.Client;
  5. Add the following field to the MainPage class, replace the placeholder value with the device connection string you've created in the previous task:

    (Code Snippet - AzureIoT - DeviceClientConn)

    private DeviceClient deviceClient = DeviceClient.CreateFromConnectionString("{device connection string}");
  6. Add the following method to the MainPage class to create and send messages to the IoT hub:

    (Code Snippet - AzureIoT - SendMessage)

    public async void SendMessage(string message)
    {
    	 // Send message to an IoT Hub using IoT Hub SDK
    	 try
    	 {
    		  var content = new Message(Encoding.UTF8.GetBytes(message));
    		  await deviceClient.SendEventAsync(content);
    
    		  Debug.WriteLine("Message Sent: {0}", message, null);
    	 }
    	 catch (Exception e)
    	 {
    		  Debug.WriteLine("Exception when sending message:" + e.Message);
    	 }
    }
  7. Add the following code to the Timer_Tick method to send a message with the temperature and another with the light level:

    (Code Snippet - AzureIoT - TempAndLightMessages)

    // send data to IoT Hub
    var jsonMessage = string.Format("{{ displayname:null, location:\"USA\", organization:\"Fabrikam\", guid: \"41c2e437-6c3d-48d0-8e12-81eab2aa5013\", timecreated: \"{0}\", measurename: \"Temperature\", unitofmeasure: \"C\", value:{1}}}",
    	 DateTime.UtcNow.ToString("o"),
    	 temp);
    
    this.SendMessage(jsonMessage);
    
    jsonMessage = string.Format("{{ displayname:null, location:\"USA\", organization:\"Fabrikam\", guid: \"41c2e437-6c3d-48d0-8e12-81eab2aa5013\", timecreated: \"{0}\", measurename: \"Light\", unitofmeasure: \"L\", value:{1}}}",
    	 DateTime.UtcNow.ToString("o"),
    	 light);
    
    this.SendMessage(jsonMessage);
  8. Press F5 to run and debug the application.

    The information being sent can be monitored using the Device Explorer application. Run the application and go to the Data tab and select the name of the device you want to monitor (mySimulatedDevice in your case), then click on Monitor

    Monitoring messages sent

    Monitoring messages sent

Exercise 2: Using Stream Analytics to process the data

You have seen how to use the Device Explorer to peek at the data being sent to the Azure IoT Hub. However, the Azure IoT suite offers many different ways to generate meaningful information from the data gathered by the devices.

In this exercise you will use Azure Stream Analytics to process the data and generate meaningful information.

#### Task 1 - Create a Service Bus Consumer Group #### In order to allow several consumer applications to read data from the IoT Hub independently, a **Consumer Group** must be configured for each one. If all of the consumer applications (the Device Explorer, Stream Analytics / Power BI, the Web site you'll configure in the next section) read the data from the default consumer group, only one application will retain the lease and the others will be disconnected.

In this task, you'll create a new Consumer Group that will be used by a Stream Analytics job.

  1. Open the Azure Portal (https://portal.azure.com/), and select the IoT Hub you created.

  2. From the settings blade, click Messaging.

  3. At the bottom of the Messaging blade, type the name of the new Consumer Group: "PowerBI"

    Create Consumer Group

    Create Consumer Group

  4. From the top menu, click Save.

Task 2 - Creating a Stream Analytics Job

Before the information can be consumed (for instance using Power BI), it must be processed by a Stream Analytics Job. To do so, an input for that job must be provided. As the simulated devices are sending information to an IoT Hub, we'll use these as the input for the job.

Note: You will create the Stream Analytics job using the classic Azure Portal since the Power BI sink for Stream Analytics output is not available in the new Azure Portal yet.

  1. Go to the classic Azure Portal (https://manage.windowsazure.com/), and click NEW on the bottom bar.

  2. Then select DATA SERVICES > STREAM ANALYTICS > QUICK CREATE and enter the required fields:

    • Job Name: Enter a job name.
    • Region: Select the region where you want to run the job. Consider placing the job and the event hub in the same region to ensure better performance and to ensure that you will not be paying to transfer data between regions.
    • Subscription: If you have more than one subscription associated to your account, select the one where you created the IoT hub.
    • Regional Monitoring Storage Account: Choose the Azure storage account that you would like to use to store monitoring data for all Stream Analytics jobs running within this region. You have the option to choose an existing storage account or to create a new one.

    Creating Stream Analytics job

    Creating Stream Analytics job

  3. The new job will be shown with a status of Created. Notice that the Start button is disabled. You must configure the job Input, Output, and Query before you can start the job.

Task 3 - Defining the Stream Analytics Job Input

In this task, you'll specify a job Input using the IoT Hub you previously created.

  1. Go to your Stream Analytics job, click on the INPUTS tab and then in the ADD AN INPUT button.

  2. In the Add an input to your job popup, select the Data Stream option and click Next. In the following step, select the option IoT Hub and click Next. Lastly, in the IoT Hub Settings screen, provide the following information:

    • Input Alias: TelemetryHUB
    • Subscription: Use IoT Hub from Current Subscription
    • Choose an IoT Hub: Use the one you used for the IoT Hub you created
    • IoT Hub Shared Access Policy Name: iothubowner
    • Shared access policy key: The primary key of the iothubowner access policy
    • IoT Hub Consumer Group: powerbi

    Creating Stream Analytics Input

    Creating Stream Analytics Input

  3. Click Next, and then Complete (leave the Serialization settings as they are).

#### Task 4 - Defining the Stream Analytics Job Output #### The output of the Stream Analytics job will be **Power BI**.
  1. In your Stream Analytics job click OUTPUTS tab, and click the ADD AN OUTPUT link.

  2. In the Add an output to your job popup, select the POWER BI option and the click the Next button.

  3. In the following screen you will setup the credentials of your Power BI account in order to allow the job to connect and send data to it. Click the Authorize Now link. You will be redirected to the Microsoft login page.

  4. Enter your Power BI account email and password and click Continue. If the authorization is successful, you will be redirected back to the Microsoft Power BI Settings screen.

  5. In this screen you will enter the following information:

    • Output alias: PowerBI
    • Dataset Name: Simulated
    • Table Name: Telemetry
    • Workspace: My Workspace
  6. Click Complete to create the output.

    Creating Stream Analytics Output

    Creating Stream Analytics Output

Task 5 - Defining the Stream Analytics Job Query

Now that the job's inputs and outputs are configured, the Stream Analytics Job needs to know how to transform the input data into the output data source. To do so, you'll create a new Query.

  1. Go to the Stream Analytics Job QUERY tab and replace the query with the following statement:

    SELECT
    	 iothub.iothub.connectiondeviceid displayname,
    	 location,
    	 guid,
    	 measurename,
    	 unitofmeasure,
    	 Max(timecreated) timecreated,
    	 Avg(value) AvgValue
    INTO
    	 [PowerBI]
    FROM
    	 [TelemetryHUB] TIMESTAMP by timecreated
    GROUP BY
    	 iothub.iothub.connectiondeviceid, location, guid, measurename, unitofmeasure,
    	 TumblingWindow(Second, 10)

    The query takes the data from the input (using the alias defined when the input was created TelemetryHUB) and inserts into the output (PowerBI, the alias of the output) after grouping it by using a 10 second window.

    Creating Stream Analytics Query

    Creating Stream Analytics Query

  2. Click on the SAVE button and YES in the confirmation dialog.

Task 6 - Starting the Stream Analytics Job

In this task, you'll run the Stream Analytics job and view the output in Visual Studio.

  1. Click the START button at the bottom bar.

  2. In the Start Output popup, select the JOB START TIME option. After clicking OK the job will be started. The job status will change to Starting; it can take several minutes to start.

    Once the job starts it creates the Power BI datasource associated with the given subscription.

Exercise 3: Analyzing the telemetry data using Power BI

Power BI is a cloud based data analysis, which can be used for reporting and data analysis from wide range of data source. Power BI is simple and user friendly enough that business analysts and power users can work with it and get benefits of it.

In this exercise, you'll create reports in Power BI to visualize the telemetry data processed by Stream Analytics and sent by the device through the Azure IoT hub.

Task 1 - Creating a Light By Time report

After some minutes of the job running, you'll see that the dataset that you configured as an output for the Job, is now displayed in the Power BI workspace Datasets section.

  1. Go to PowerBI.com and login with your work or school account. If the Stream Analytics job query outputs results, you'll see your dataset is already created:

    Power BI: New Datasource

    Power BI: New Datasource

    Note: The Power BI dataset will only be created if the job is running and if it is receiving data from the IoT Hub input, so check that the Universal App is running and sending data to Azure to ensure that the dataset be created.

  2. Once the datasource becomes available you can start creating reports. To create a new Report click on the Simulated datasource:

    Power BI: Report Designer

    Power BI: Report Designer

    The Report designer will open showing the list of available fields for the selected datasource and the different visualizations supported by the tool.

  3. To create the Average Light by time report, select the following fields:

    • avgvalue
    • timecreated

    As you can see the avgvalue field is automatically set to the Value field and the timecreated is inserted as an axis. Now change the chart type to a Line Chart:

    Selecting the Line Chart

    Selecting the Line Chart

  4. To create a filter to show only the Light sensor data, drag the measurename field to the Filters section, and then select the Light value:

    Select Report Filter Select Light sensor values

    Selecting the Report Filters

  5. Now the report is almost ready. Click Save and set Light by Time as the name for the report.

    Light by Time Report

    Light by Time Report

Task 2 - Creating a Power BI dashboard

In this task, you'll create a new Dashboard, and pin the Light by Time report to it.

  1. Click the plus sign (+) next to the Dashboards section to create a new dashboard.

  2. Set Simulated Telemetry as the Title and press Enter.

  3. Go back to your report, and click the pin icon to add the report to the recently created dashboard.

    Pin a Report to the Dashboard

    Pinning a Report to the Dashboard

Task 3 - Creating a Temperature report

In this task, you'll create a second chart with the information of the average Temperature.

  1. Click the Simulated datasource to create a new report.

  2. Select the avgvalue field

  3. Drag the measurename field to the filters section and select Temperature

  4. Now change the visualization to a gauge chart:

    Gauge visualization

    Gauge visualization

  5. Change the Value from Sum to Average

    Change Value to Average

    Change Value to Average

    Now the Report is ready:

    Gauge Report

    Gauge Report

  6. Save and then Pin it to the Dashboard.

  7. You can create a new Temperature by Time report by repeating the same instructions from Task 1, but selecting Temperature from the measurename field, and add it to the dashboard.

  8. Lastly, edit the reports name in the dashboard by clicking the pencil icon next to each report.

    Editing the Report Title

    Editing the Report Title

    After renaming both reports, you'll get a dashboard similar to the one in the following screenshot, which will be automatically refreshed as new data arrives.

    Final Power BI Dashboard

    Final Power BI Dashboard

Exercise 4: Consuming the IoT Hub data from a Website

In this exercise, you'll deploy a website to Azure, and then you'll enable WebSockets to allow communication with the IoT hub, and display live telemetry data using charts.

Task 1 - Create a Consumer Group for the Web site

Before you can consume data from the web site, you need to create Consumer Groups to avoid conflicts with the Stream Analytics job.

Note: In order to use the EventProcessorHost class, you must have an Azure Storage account to enable the EventProcessorHost to record checkpoint information. You can use an existing storage account, or follow the instructions in About Azure Storage to create a new one. Make a note of the storage account connection string.

  1. Create two different consumer groups (follow steps from Exercise 2 Task 1):
  • website
  • local (used when debugging)
  1. Take note of the Event Hub-compatible name and Event Hub-compatible endpoint values in the Messaging blade

    Adding website consumer groups

    Adding website consumer groups

  2. Go to the Source\Ex4\Begin folder, and open the Web Site project (IoTWorkshopWebSite.sln) in Visual Studio.

  3. Edit the Web.config file and add the corresponding values for the following keys:

    • Microsoft.ServiceBus.EventHubDevices: Event hub-compatible name you copied in the previous step.
    • Microsoft.ServiceBus.ConnectionStringDevices: Event hub-compatible connection string which is composed by the Event hub-compatible endpoint and the iothubowner Shared access policy Primary Key.
    • Microsoft.Storage.ConnectionString: Regional monitoring storage account configured in the Stream Analytics, in this case use the storage account name and storage account primary key to complete the endpoint.

Task 2 - Deploying to Azure Web Site

In this task, you'll deploy the website to an Azure Web Site.

  1. In Visual Studio, right-click the project name and select Publish.

  2. Select Microsoft Azure Web Apps.

    Selecting Publish Target

    Selecting Publish target

  3. Click New and use the following configuration.

    • Web App name: Pick something unique.
    • App Service plan: Select an App Service plan in the same region used for Stream Analytics or create a new one using that region.
    • Region: Pick same region as you used for Stream Analytics.
    • Database server: No database.
  4. Click Create. After some time the website will be created in Azure.

    Creating a new Web App on Microsoft Azure

    Creating a new Web App on Microsoft Azure

  5. Click Publish.

    Note: You might need to install the WebDeploy extension if you are having an error stating that the Web deployment task failed. You can find WebDeploy here.

Task 3 - Enabling WebSockets in the Azure Web Site

After you deploy the site, it's required that you enable Web sockets. To do this, perform the following steps:

  1. Browse to https://portal.azure.com and select your _Azure Web App.

  2. Click All settings.

  3. Click Applicattion settings

  4. Then set Web sockets to On and click Save.

    Enabling Web Sockets in your website

    Enabling Web Sockets in your website

  5. Navigate to your recently deployed Web Application. You will see something like in the following screenshot. There will be 2 real-time graphs representing the values read from the temperature and light sensors.

    Note: Take into account that the Universal app must be running and sending information to the IoT Hub in order to see the graphics.

    Web Site Consuming the IoT Hub Data

    Web Site Consuming the IoT Hub data

    Note: At the bottom of the page you should see "Connected". If you see "ERROR undefined" you likely didn't enabled WebSockets for the Azure Web Site.


Summary

By completing this module, you should have:

  • Learnt how to create an Azure IoT hub and configure a device app to connect to the hub.
  • Created a Windows Universal Platform app that sends telemetry messages to Azure IoT Hub.
  • Used Stream Analytics to process data in near-realtime and spool data to Blob Storage and Power BI.
  • Created a Power BI reports and dashboards with graphs to visualize the telemetry data.
  • Configured and deployed an Azure Web site that consumes and displays the live data from the Azure IoT hub.