Remote Monitoring of IoT Devices Using Azure and HoloLens

Premier Developer

Premier

App Dev Manager, Richard Newell explores HoloLens as a remote monitoring tool for IoT devices using Azure.


In this post we will cover all the steps from the setting up of the backend infrastructure, setting up your IoT environment, and connecting it with Azure Services deploying it onto a device. A brief overview of the different aspects of this blog are as follows:

  • Setting up the backend infrastructure
  • Building up the Azure IoT solutions
  • Connecting a holographic app with Azure

By the end of the post, we will have covered the complete end-to-end development process that requires building connected devices using several Azure IOT services.

This is going to be an IoT solution, which receives data from connected devices, stores the data and makes it available for consumption by a holographic or mobile application. So, you will first learn to set up the backend infrastructure–where you will have a device–which connects with a Cloud Gateway. Data received by the Cloud Gateway is stored within persistent storage, and finally, this data will be made available to a Holographic or Mobile app through Web APIs.

In this solution architecture, we use an IoT Hub as a Cloud Gateway for event data ingress coming from the sensor.

Setting up the Infrastructure

First, we want to create an event hub, you will need a valid Azure subscription, navigate to https://ms.portal.azure.com

  1. From New, select Internet of Things and create a new IoT Hub by providing a name, resource, and other required details, such as pricing tier or location.
    HL1
  2. Once the IoT Hub is created and deployed, open the IoT Hub and navigate to Settings | Shared Access Polices and select ‘iothubowner’.
  3. Write down its connection string-primary key for further reference.
    hl2

Second, we want to create the Azure Cosmos DB

  1. From New, select Azure Cosmos DB service and create a new Cosmos DB by providing ID, resource group, location, and your subscription. You must make sure that you select SQL (DocumentDB) from the API Dropdown.
  2. Once “Azure Cosmos DB account” is created, add a collection by clicking on the “Add Collection” button and add a database with it.
  3. Write down the URI, Primary Key and the Primary connection string.
    hl3

Now that the IoT Hub and the Azure Cosmos DB are created we can go ahead and create the Stream Analytics Job which will take up the data input from the IoT Hub and store the data in the Azure Cosmos DB. Once you create the newly created job, do the following configurations:

  1. Select the Input tab and add a new input of type IoT Hub, connecting to the IoT Hub which you have just created above.
  2. Select the Output tab and add a new output of type SQL-DocumentDB, connecting with SQL-DocumentDB.
  3. Select the query tab and add a simple query to connect the input and output stream.

 

Install Visual Studio Code if you haven’t already done so. Look for Azure IoT Workbench in the extension marketplace and install it.
In this project we are using the IoT Kit with the AZ3166
hl4
MXCHIP IoT DevKit – Where to buy on Amazon: http://a.co/d/6a7xmny

    1. Download and install Arduino IDE. It provides the necessary toolchain for compiling and uploading Arduino code.
    2. Open File > Preference > Settings and add following lines to configure Arduino;

· Windows:

“arduino.path”: “C:\\Program Files (x86)\\Arduino”,

“arduino.additionalUrls”:”https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/master/

  1. Click F1 to open the command palette, type and select Arduino: Board Manager. Search for AZ3166 and install the latest version.
  2. Download and install USB driver from STMicro.
  3. Install the Arduino extension in Visual Studio Code.

In the new opened project window, click F1 to open the command palette, type and select IoT Workbench: Cloud, then select Azure Provision.

hl5

NOTE: Follow the prompts to select your subscription and the IOT hub you created earlier

  1. Click F1 to open the command palette, type and select IoT Workbench: Device, then select Config Device Settings > Select IoT Hub Device Connection String.
  2. On IoT DevKit, hold down button A, push and release the reset button, and then release button A. Your IoT DevKit enters configuration mode and saves the connection string.

Click F1 again, type and select IoT Workbench: Device, then select Device Upload.

hl6

That’s it you have successfully connected your device to Azure, if you are looking to use a different board please see the chart below I have also included simulator example code if you don’t have a physical device.

IoT deviceProgramming language
Raspberry PiNode.js, C
IoT DevkitArduino in VS Code
Adafruit Feather HUZZAH ESP8266Arduino

Developing the Web API

The Web API consumes the Azure Cosmos DB – SQL (DocumentDB) data received from sensors and exposes the REST API to be consumed by other applications. Here are the steps to follow to build the service. The solution consists of three projects;

https://github.com/sysrichn/HoloLensDemo.IoT.git

  1. HoloLensDemo.Model – consists of all entities related to building, floors and rooms
  2. HoloLensDemo.Simulator – consists of simulated simulator for building sensors, and pushes data to Azure EventHub on periodic interval
  3. HoloLensDemo.Web – Web API project, reads data from Document DB, and exposes as API

To make it work, make following configuration changes;

  1. Within HoloLensDemo.Simulator\Program.cs file, update following EventHub connection settings:
  2. Within HoloLensDemo.Web\Data\BuildingCreator.cs, update following settings related to DocumentDB:

In the next step, deploy the services in Azure. There are several ways to do that. The easiest and fastest way that you can do it is from Visual Studio itself. Right-click on the WebAPI project and select Publish.

hl7

Publish the Web API

It will launch the Application Publish wizard, from where you can select the Microsoft Azure App Service option and follow the wizard steps to complete the deployment process.

hl8

Web API Publish Wizard

Once your service is hosted and deployed on Azure, you can hit the endpoints with any of the Rest API clients (such as the Postman REST API Client) or consume the data in your Holographic or Mobile app.

Connecting a holographic app with Azure

First in your holographic application the data model will hold the data returned by the Rest Services you just created, hence it should be same structure. Using the data class in the example code I provided add that same data model to your assets in your project;

Example the building class

Now we will create an Azure Bridge, which fetches the data from the services and maps it with the Building Data Model, which we have just defined in the preceding section:

  1. Navigate to the Assets| Scripts folder.
  2. Add a new script by navigating to context menu | Create | C# Scripts and name it AzureBridge
  3. Open the script file in Visual Studio

As in the first step, we will make this class a Singleton to use only one instance of it. You can achieve this by just inheriting the class from the Singleton class, by passing the same class type as parameter:

hl9

Attach the script with the Root object of your holographic app, by just dragging the script into the Root Object, either in Object Hierarchy or in the Inspector window.

With this, our Azure Bridge is ready to connect and fetch the live data. If you run the application by placing a breakpoint inside the GetBuilding() method, you should be able to explore the data retrieved from the services.

hl10

The objective of this blog was to build an enterprise scenario with HoloLens with integrated IoT, there are several enhancements that you can do to extend this solution.

Premier Developer
Premier Developer

Premier Support for Developers

Follow Premier   

0 comments

    Leave a comment