Some familiarity with Microsoft C# programming language constructs is desirable.

Introduction

This tutorial is a retrace of the excellent example provided by Dominic Betts at the Microsoft Azure site as noted in the reference section above. All credit is due to Dominic for sharing his work in a very informative manner.

The introductory work will emulate the steps published by Dominic using a C# client. A latter article will examine the Python and Node.js interfaces for the equivalent exercises.

It will take a few minutes for the hub to be created. Azure will post a notification when the deployment of the hub is successful.

On the portal page, click All Resources and then click the Name of the newly deployed IoT hub:

Figure 3 All Resources

Make a note of the fully qualified Hostname as highlighted in the preceding figure under the Essentials group of the blade for the named hub.

On the blade for the named hub, under General, drill down as follows:

Shared access policies è iothubowner

Figure 4 Connection String

On the iobhubowner pane, in Connection string – primary key, copy the string to the clipboard using the icon button adjacent to the right of the field. Paste the Connection string to a document for later use.

The Hostname and Connection string are the two key parameters used by IoT devices to exchange data.

Create a device identity

In order for a device to communicate with the IoT hub, it needs to identify itself uniquely. Azure IoT uses the term device identity for this purpose. There is yet another article by Dominic Betts that explains the background information in greater detail.

A basic Windows console app using the Microsoft Azure IoT Service SDK synthesizes the device identity for use in communications with the Azure IoT hub.

Build Windows Console App

The following steps build a basic Windows Console app:

Launch Visual Studio 2015

Figure 5 Launch Visual Studio

On the Start Page, drill down to Project:

File è New è Project…

Figure 6 Start Page

On the New Project window:

o Drill down to Console Application:

Installed è Classic Desktop è Console Application

o Enter an appropriate Name for the app

o Click the OK button

Figure 7 New Project

Figure 8 CreateDeviceIdentity Initial Panes

After the wizard completes its operations, the shell for the main program appears on the left-center pane and the Solution Explorer on the right pane as shown below:

Figure 9 Manage Nuget Packages

In the Solution Explorer pane, right-click the CreateDeviceIdentity project and on the context menu click Manage Nuget Packages:

In the Nuget Package Manager window, enter microsoft.azure.devices in the text box on the upper left and click the Browse button.

Select Microsoft.Azure.Devices in the results pane and click the Install button on the right pane

Figure 10 Install Nuget Package

In the Review Changes window, click the OK button

Figure 11 Review Changes

In the License Acceptance window, click the I Accept button

Figure 12 I Accept

Figure 13 New References

In the Solution Explorer pane, for the CreateDeviceIdentity project, the References node should have new entries for the Azure libraries

using Microsoft.Azure.Devices; using Microsoft.Azure.Devices.Common.Exceptions;

Figure 14 References to Azure SDK libraries

In the source code file, Program.cs, insert the references to the newly added libraries with C# language using statements as shown below:

1. static RegistryManager registryManager;

static string connectionString = “<your iot hub connection string>”;

Figure 15 Program variables

In the source code file, Program.cs, insert the following statements to define the variables, registryManager and connectionString, for use in registering the device identity:

The connection string value should be the corresponding string for your newly created Azure IoT Hub as shown in Figure 4 Connection String.

The app requires a nominal set of instructions to create the device identity using a method, AddDeviceAsync, for the class that constitutes the app, Program:

As explained in literature, async methods are now de rigueur for most operations especially when these methods communicate external to client environment. Also, for illustration purposes, a set of instructions for exception handling, used for simple illustration here, represent good practice towards the stepwise refinement of the solution for more robust operations.

The variable, deviceId, should be any identifier unique to the device. This example uses the host name of the client machine as the device identifier.

The last step for the code is to insert the instructions to connect to the Azure service, retrieve

The console terminal window will display the device identity key. Save this key for use in subsequent message exchanges between the device (client machine) and the specific IoT hub that was created previously for this exercise.

Create a simulated client

For the limited purposes of this exercise, using a simulated client to send messages to the IoT hub in the cloud is more expedient than configuring any IoT device. Opinions will differ on this rationalization. Examples of practical IoT devices (Raspberry Pi, Intel Edison, Freescale/NXP/Qualcomm et al.) are planned for later exercises.

Build the app

In Solution Explorer, right-click the current solution name and drill-down the context menus to add a new project:

In the Program class, add the instructions to send the messages from the client to the IoT Hub:

Close the solution to save all the files that were modified.

The Create method for DeviceClient uses the AMQP protocol which is popular for IoT devices. There are other protocols (e.g. HTTP) for data exchanges with the IoT Hub. The corresponding packages for these protocols are available through NuGet.

For real-world scenarios, more robust messaging may be necessary. The Microsoft Patterns and Practices team provides examples as well as guidelines on Transient Fault Handling to retry when transmission errors arise.

Build the applications

This example build a solution file with three projects:

Create device identity

Send messages to hub

Read Messages at hub

The first project is run only once to obtain the identity key. The other two projects can be run concurrently to demonstrate the transmission from the client and the processing at the hub.

Startup Projects

The two projects that must run concurrently in the current example are:

ReadMessages

SimulatedClient

Figure 39 Current Solution

Visual Studio 2015 permits both projects to start-up concurrently.

In the Solution Explorer pane, right-click the solution name and from the context menu select

Figure 40 Set StartUp Projects

Set StartUp Projects:

In the Solution Property Pages window:

o In the left pane, select Startup Project

o In the middle pane, select Multiple startup projects

o For the project ReadMessages, select the Action as Start

o For the project SimulatedClient, select the Action as Start

o Click the OK button

Run the projects

On the menu bar, click Debug and on the cascade menu click Start Debugging. Alternatively, press the F5 key:

Figure 41 Start Debugging

Two console terminal windows will launch as shown below with transactions logged based on the interval specified in the client app: