Page 20 - MSDN Magazine, June 2017
P. 20
bit.ly/2nEePNi. In this case, no Azure App Service is created because the solution portal runs on a local machine. Such an approach can be particularly useful for development and debugging or when you want to modify a preconfigured solution.
Once the provisioning is finished, you can launch the solution, and its portal will appear in the default browser (refer back to Figure 1). This portal has a few tabs. For the purpose of this arti- cle, I’ll limit my attention to just two of them: dashboard and devices. Dashboard displays the map with remote devices and the telemetry data they stream. The Devices tab shows a list of remote devices, including their status, capabilities and a description. By default, there are several emulated devices. Let me explain how to register the new, non-emulated hardware.
Registering a Device
To register a device you use the solution portal, where you press the Add a device hyperlink located in the bottom-left corner. You can then choose between a simulated or a custom device. Pick the sec- ond option and press the Add New button. Now you can define the Device ID. I set this value to RemoteCamera. Afterward, the ADD A CUSTOM DEVICE form displays device credentials (see Figure 9), which I’ll use later to connect my IoT device to the IoT Hub.
Device Metadata and Communicating with the Cloud
The device you add will appear in the devices list, and you can then send device metadata or device information. Device information com- prises a JSON object that describes the remote device. This object tells the cloud endpoint the capabilities of your device, and includes a hard- ware description and the list of remote commands accepted by your device. These commands can be sent by the end user to your device through the IoT solution portal. In the RemoteCamera app, device info is represented as the DeviceInfo class (in the AzureHelpers subfolder):
public class DeviceInfo {
public bool IsSimulatedDevice { get; set; }
public string Version { get; set; }
public string ObjectType { get; set; }
public DeviceProperties DeviceProperties { get; set; } public Command[] Commands { get; set; }
}
The first two properties of DeviceInfo specify whether a device is simulated and define the version of the DeviceInfo object. As you’ll see later, the third property, ObjectType, is set to a string constant, DeviceInfo. This string is used by the cloud, specifically by the Azure Stream Analytics job, to filter device info from telemetry data. Next, DeviceProperties (see the AzureHelpers subfolder) holds a collection of properties (like serial number, memory, platform, RAM) that describe your device. Finally, the Commands property is a collec- tion of remote commands recognized by your device. You define each command by specifying its name and a list of parameters, which are represented by the Command and CommandParameter classes, respectively (see AzureHelpers\\\\Command.cs).
The Devices tab shows a
list of remote devices, including their status, capabilities and a description.
To establish communication between your IoT device and the IoT Hub you use the Microsoft.Azure.Devices.Client NuGet pack- age. This package provides the DeviceClient class, which you use to send and receive messages to and from the cloud. You can create an instance of the DeviceClient using either the Create or Create- FromConnectionString static methods. Here, I use the first option (CloudHelper.cs in the AzureHelpers folder):
public async Task Initialize() {
if (!IsInitialized) {
deviceClient = DeviceClient.Create(
Configuration.Hostname, Configuration.AuthenticationKey());
await deviceClient.OpenAsync(); IsInitialized = true; BeginRemoteCommandHandling();
}
As you can see, the DeviceClient.Create method requires you to provide the hostname of the IoT Hub and device credentials (the identifier and the key). You obtain these values from the solution portal during device provisioning (refer back to Figure 9). In the RemoteCamera app, I store the hostname, device id and the key within the Configuration static class:
public static class Configuration {
public static string Hostname { get; } = "<iot-hub-name>.azure-devices.net"; public static string DeviceId { get; } = "RemoteCamera";
public static string DeviceKey { get; } = "<your_key>";
public static DeviceAuthenticationWithRegistrySymmetricKey AuthenticationKey() {
return new DeviceAuthenticationWithRegistrySymmetricKey(DeviceId, DeviceKey); }
}
Additionally, the Configuration class implements a static method AuthenticationKey, which wraps device credentials into an instance Internet of Things
Figure 9 Summary of Device Registration 16 msdn magazine
}