Bardaan A
Published © GPL3+

002 - How To Setup Devices And Register Into Azure IoT Hub

In this tutorial we'll walk you through the process of installing Windows IoT into Raspberry Pi 2B/3B and registering it into Azure IoT Hub.

IntermediateFull instructions provided8 hours5,293
002 - How To Setup Devices And Register Into Azure IoT Hub

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
FezHat
×1
u-blox Andoer NEO-6M GPS Module Serial
×1
Western Digital SanDisk Extreme 16GB microSD card
×2

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015
Microsoft Azure
Microsoft Azure
Windows 10 IoT Core
Microsoft Windows 10 IoT Core

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Raspberry Pi 3B and u-Blox GPS module Schematics

This is a schematics of the wiring between Raspberry Pi 3B and GPS module via Serial UART.

Code

AzureIoTRegistryManagerApp

C#
This is the code for Accessing and Managing Azure IoT Hub Registry by making use of RegistryManager class.
////The MIT License(MIT)
////Copyright(c) 2016 BardaanA

////Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

////The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

////THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Threading.Tasks;

// These are the Microsoft's recommended libraries to access
// and make changes to Azure IoT Hub's Registry
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Common.Exceptions;

// This namespace which also dictates the name of the Assembly can be anything you desire.
namespace AzureIoTRegistryManagerApp
{
    class Program
    {
        // RegistryManager object which is going to do most of the work for our application
        static RegistryManager registryManager;
        // This here is the Connection String that you copied from the IoT Hub Shared Access Policies > iothubowner > Shared access keys; remember??
        static string connectionString = "Don't forget to replace this with your own connection string";
        // Keeps track of the registry access status
        static string registryAccessStatus = "";

        static void Main(string[] args)
        {
            try
            {
                // Let's try and create a Registry Manager using our connection string, shall we?
                registryManager = RegistryManager.CreateFromConnectionString(connectionString);
                registryAccessStatus = "Successfuly connected to the IoT Hub registry"; // Yay!
            }
            catch(Exception ex)
            {
                Console.WriteLine("Registry access failed!  {0}",ex.Message);  // Bummer!!
            }
            // Check if RegistryManager was created successfully
            if(registryManager != null)
            {
                Console.WriteLine("*****************************************************");
                Console.WriteLine("===== Welcome to the Azure IoT Registry Manager =====");
                Console.WriteLine();
                Console.WriteLine("++ {0} ++",registryAccessStatus);
                Console.WriteLine();
                int menuSelection = 0;
                while(menuSelection != 3)  // Loop to keep you going...
                {
                    Console.WriteLine("  1) Add device into registry");
                    Console.WriteLine("  2) Remove device from the registry");
                    Console.WriteLine("  3) Close this application");
                    Console.WriteLine("------------------------------------");
                    Console.Write("Enter your selection: ");
                    menuSelection = int.Parse(Console.ReadLine());
                    Console.WriteLine();
                    switch(menuSelection)
                    {
                        case 1:
                            Console.Write("Enter device name that you want to register: ");
                            string deviceName = Console.ReadLine();
                            Console.WriteLine();
                            if(deviceName.Length > 0 && !deviceName.Contains(" "))  // Weak validation :)
                            {
                                // Calling method that actually adds the device into the registry
                                AddDevice(deviceName).Wait();
                            }
                            else
                            {
                                Console.WriteLine("---");
                                Console.WriteLine("Enter valid name!");
                                Console.WriteLine("---");
                            }
                            break;
                        case 2:
                            Console.Write("Enter name of the device to be removed: ");
                            string deviceRemoveName = Console.ReadLine();
                            Console.WriteLine();
                            if(deviceRemoveName.Length > 0 && !deviceRemoveName.Contains(" "))  // Weak validation :(
                            {
                                // Calling method that actually removes the device from the registry
                                RemoveDevice(deviceRemoveName).Wait();
                            }
                            else
                            {
                                Console.WriteLine("---");
                                Console.WriteLine("Enter valid name!");
                                Console.WriteLine("---");
                            }
                            break;
                        case 3:
                            // Breaks out of the loop
                            break;
                        default:
                            Console.WriteLine("---");
                            Console.WriteLine("Choose valid entry!");
                            Console.WriteLine("---");
                            break;
                    }
                }
                // Closes the RegistryManager access right before exiting the application
                registryManager.CloseAsync().Wait();
            }
        }

        // Method used to add device into the Registry, takes in a string as a parameter
        private static async Task AddDevice(string deviceId)
        {
            // A Device object
            Device device;
            try
            {
                // Lets try and create a Device into the Device Registry
                device = await registryManager.AddDeviceAsync(new Device(deviceId));
                if(device != null)
                {
                    Console.WriteLine("Device: {0} added successfully!",deviceId); // Hooray!
                }
            }
            catch(DeviceAlreadyExistsException)  // What?
            {
                Console.WriteLine("---");
                Console.WriteLine("This device has already been registered...");// When did I do that??
                Console.WriteLine("---");
                device = await registryManager.GetDeviceAsync(deviceId);
            }
            Console.WriteLine();
            Console.WriteLine("Generated device key: {0}",device.Authentication.SymmetricKey.PrimaryKey);  // Now you're talking!
            Console.WriteLine();
        }

        // Method used to remove a device from the Device Registry, takes a string as a parameter
        private static async Task RemoveDevice(string deviceId)
        {
            try
            {
                // Lets try and get rid of the Device from our registry, using the device id.
                await registryManager.RemoveDeviceAsync(deviceId);
                Console.WriteLine("Device: {0} removed successfully!",deviceId);  // Yup!
            }
            catch(DeviceNotFoundException)
            {
                Console.WriteLine("---");
                Console.WriteLine("This device has not been registered into this registry!");  // Are you sure??
                Console.WriteLine("---");
            }
        }
    }
}

Credits

Bardaan A

Bardaan A

8 projects • 16 followers
Just another Software and Technology enthusiast.
Thanks to Microsoft.

Comments