IoT on Microsoft Azure

What is Internet of Things?

Adesh Shah
Four Minute Blogs
5 min readMay 5, 2017

--

At its core, IoT is simple: it’s about connecting devices over the internet, letting them talk to us, applications, and each other.

IoT is more than smart homes and connected appliances, however. It scales up to include smart cities — think of connected traffic signals that monitor utility use, or smart bins that signal when they need to be emptied — and industry, with connected sensors for everything from tracking parts to monitoring crops.

Microsoft is building Azure into an Internet of things (IoT) platform to serve companies with connected devices.

Letting the cloud manage thousands of devices for you is certainly appealing to companies which don’t want the complexity and cost of scaling such an operation in-house, but how does it work?

The Azure IoT Suite: connect, analyse, integrate

IoT Hub is the key component of any IoT solution. It primarily serves as a cloud gateway that connects all the smart meters with the Cloud and establishes communication between them. It can scale to connect millions of meters and can process huge volumes of data. It supports multiple protocols such as HTTP, AMPQ, MQTT to enable control and command capabilities. It is also responsible for per-device authentication, thus playing a major role in security aspects.

Transform your business with Microsoft IoT

  • Build Things — IoT beings with your things. Build with your things, from adding sensors to creating smart devices, to start your IoT solution.
  • Control your things — Deploy IoT solutions that control, monitor, and manage your things, allowing you to capture real-time data.
  • Analyze data — Take the data you collect and apply advanced analytics to uncover new business insights.
  • Act on insights — Transform insights into action through powerful applications creating new revenue and business opportunities.

How industries are using the Internet of Things

Let’s Azure IoT hub by example

Azure IoT Hub is a fully managed service that enables reliable and secure bi-directional communications between millions of Internet of Things (IoT) devices and a solution back end. One of the biggest challenges that IoT projects face is how to reliably and securely connect devices to the solution back end. To address this challenge, IoT Hub:

  • Offers reliable device-to-cloud and cloud-to-device hyper-scale messaging.
  • Enables secure communications using per-device security credentials and access control.
  • Includes device libraries for the most popular languages and platforms.

This tutorial shows you how to:

  • Use the Azure portal to create an IoT hub.
  • Create a device identity in your IoT hub.

To complete this tutorial, you need the following:

  • Visual Studio 2015 or Visual Studio 2017.
  • An active Azure account. (If you don’t have an account, you can create a free account in just a couple of minutes.)

Create an IoT hub

Create an IoT hub for your simulated device app to connect to.

Sign in to the Azure Portal. Go to New -> Internet of Things -> IoT Hub

In IoT hub choose configuration for your IoT hub.

In the Shared access policies blade, click the iothubowner policy, and then copy and make note of the IoT Hub connection string in the iothubowner blade. For more information, see Access control in the “IoT Hub developer guide.”

Create a device identity

In Visual Studio, add a Visual C# Windows Classic Desktop project to a new solution by using the Console App (.NET Framework) project template. Make sure the .NET Framework version is 4.5.1 or later. Name the project CreateDeviceIdentity.

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

In the NuGet Package Manager window, select Browse, search for microsoft.azure.devices, select Install to install the Microsoft.Azure.Devices package, and accept the terms of use. This procedure downloads, installs, and adds a reference to the Azure IoT service SDK NuGet package and its dependencies.

Add the following using statements at the top of the Program.cs file:

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

Add the following fields to the Program class. Replace the placeholder value with the IoT Hub connection string for the hub that you created in the previous section.

static RegistryManager registryManager;  
static string connectionString = "{iot hub connection string}";

Add the following method to the Program class:

private static async Task AddDeviceAsync() 
{
string deviceId = "myFirstDevice";
Device device;
try
{
device = await registryManager.AddDeviceAsync(new Device(deviceId)); }
catch (DeviceAlreadyExistsException)
{
device = await registryManager.GetDeviceAsync(deviceId);
}
Console.WriteLine("Generated device key: {0}", device.Authentication.SymmetricKey.PrimaryKey);
}

This method creates a device identity with ID myFirstDevice. (If that device ID already exists in the identity registry, the code simply retrieves the existing device information.) The app then displays the primary key for that identity. You use this key in the simulated device app to connect to your IoT hub.

Finally, add the following lines to the Main method:

registryManager = RegistryManager.CreateFromConnectionString(connectionString);
AddDeviceAsync().Wait();
Console.ReadLine();

Run this application, and make a note of the device key.

In this next article I will be discussing how you can ReadDeviceToCloudMessages, which displays the telemetry sent by your simulated device app.

References:

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-getstarted

Stay tuned,

Adesh Shah

--

--

Adesh Shah
Four Minute Blogs

Writing to express, not to impress. Principal Investigator | Machine Learning Fanatic | AI Dev | Optimistic Futurist