PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/articles/iot-hub/iot-hub-device-management-get-started.md

https://gitlab.com/yeah568/azure-content
Markdown | 198 lines | 122 code | 76 blank | 0 comment | 0 complexity | 378b87f387a193c111754b8107f96d4b MD5 | raw file
  1. <properties
  2. pageTitle="IoT Hub device management get started | Microsoft Azure"
  3. description="Azure IoT Hub for device management with C# getting started tutorial. Use Azure IoT Hub and C# with the Microsoft Azure IoT SDKs to implement device management."
  4. services="iot-hub"
  5. documentationCenter=".net"
  6. authors="juanjperez"
  7. manager="timlt"
  8. editor=""/>
  9. <tags
  10. ms.service="iot-hub"
  11. ms.devlang="dotnet"
  12. ms.topic="hero-article"
  13. ms.tgt_pltfrm="na"
  14. ms.workload="na"
  15. ms.date="04/29/2016"
  16. ms.author="juanpere"/>
  17. # Get started with Azure IoT Hub device management using C# (preview)
  18. [AZURE.INCLUDE [iot-hub-device-management-get-started-selector](../../includes/iot-hub-device-management-get-started-selector.md)]
  19. ## Introduction
  20. To get started with Azure IoT Hub device management, you must create an Azure IoT Hub, provision devices in the IoT Hub, start multiple simulated devices, and view these devices in the device management sample UI. This tutorial walks you through these steps.
  21. > [AZURE.NOTE] You need to create a new IoT Hub to enable device management capabilities even if you have an existing IoT Hub because existing IoT Hubs do not have device management capabilities yet. Once device management is generally available, all existing IoT Hubs will be upgraded to get device management capabilities.
  22. ## Prerequisites
  23. This tutorial assumes you are using a Windows development machine.
  24. You need the following installed to complete the steps:
  25. - Microsoft Visual Studio 2015
  26. - Git
  27. - CMake (version 2.8 or later). Install CMake from <https://cmake.org/download/>. For a Windows PC, please choose the Windows Installer (.msi) option. Make sure to check the box to add CMake to the current user PATH variable.
  28. - Node.js 6.1.0 or greater. Install Node.js for your platform from <https://nodejs.org/>.
  29. - An active Azure subscription. If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see [Azure Free Trial][lnk-free-trial].
  30. ## Create a device management enabled IoT Hub
  31. You need to create a device management enabled IoT Hub for your simulated devices to connect to. The following steps show you how to complete this task using the Azure portal.
  32. 1. Sign in to the [Azure portal].
  33. 2. In the Jumpbar, click **New**, then click **Internet of Things**, and then click **Azure IoT Hub**.
  34. ![][img-new-hub]
  35. 3. In the **IoT Hub** blade, choose the configuration for your IoT Hub.
  36. ![][img-configure-hub]
  37. - In the **Name** box, enter a name for your IoT Hub. If the **Name** is valid and available, a green check mark appears in the **Name** box.
  38. - Select a **Pricing and scale tier**. This tutorial does not require a specific tier.
  39. - In **Resource group**, create a new resource group, or select an existing one. For more information, see [Using resource groups to manage your Azure resources].
  40. - Check the box to **Enable Device Management**.
  41. - In **Location**, select the location to host your IoT Hub. IoT Hub device management is only available in East US, North Europe, and East Asia during public preview. In the future, it will be available in all regions.
  42. > [AZURE.NOTE] If you don't check the box to **Enable Device Management** the samples won't work.
  43. 4. When you have chosen your IoT Hub configuration options, click **Create**. It can take a few minutes for Azure to create your IoT Hub. To check the status, you can monitor the progress on the **Startboard** or in the **Notifications** panel.
  44. ![][img-monitor]
  45. 5. When the IoT Hub has been created successfully, open the blade of the new IoT Hub, make a note of the **Hostname**, and then click the **Keys** icon.
  46. ![][img-keys]
  47. 6. Click the **iothubowner** policy, then copy and make note of the connection string in the **iothubowner** blade. Copy it to a location you can access later because you will need it to complete the rest of this tutorial.
  48. > [AZURE.NOTE] In production scenarios, make sure to refrain from using the **iothubowner** credentials.
  49. ![][img-connection]
  50. You have now created a device management enabled IoT Hub. You will need the connection string to complete the rest of this tutorial.
  51. ## Build the samples and provision devices in your IoT Hub
  52. In this section, you will run a script that builds the simulated device and the samples and provisions a set of new device identities in the device registry of your IoT Hub. A device cannot connect to IoT Hub unless it has an entry in the device registry.
  53. To build the samples and provision devices in you IoT Hub, follow the steps below:
  54. 1. Open the **Developer Command Prompt for VS2015**.
  55. 2. Clone the github repository. **Make sure to clone in a directory that does not have any spaces.**
  56. ```
  57. git clone --recursive --branch dmpreview https://github.com/Azure/azure-iot-sdks.git
  58. ```
  59. 3. From the root folder where you cloned the **azure-iot-sdks** repository, navigate to the **\\azure-iot-sdks\\csharp\\service\\samples** folder and run, replacing the placeholder value with your connection string from the previous section:
  60. ```
  61. setup.bat <IoT Hub Connection String>
  62. ```
  63. This script does the following:
  64. 1. Runs **cmake** to create a Visual Studio 2015 solution for the simulated device. This project file is **azure-iot-sdks\\csharp\\service\\samples\\cmake\\iotdm\_client\\samples\\iotdm\_simple\_sample\\iotdm\_simple\_sample.vcxproj**. Note that the source files are in the folder ***azure-iot-sdks\\c\\iotdm\_client\\samples\\iotdm\_simple\_sample**.
  65. 2. Builds the simulated device project **iotdm\_simple\_sample.vcxproj**.
  66. 3. Builds the device management samples **azure-iot-sdks\\csharp\\service\\samples\\GetStartedWithIoTDM\\GetStartedWithIoTDM.sln**.
  67. 4. Runs **GenerateDevices.exe** to provision device identities in your IoT Hub. The devices are described in **sampledevices.json** (located in the **azure-iot-sdks\\node\\service\\samples** folder) and after the devices are provisioned, the credentials are stored in the **devicecreds.txt** file (located in the **azure-iot-sdks\\csharp\\service\\samples\\bin** folder).
  68. ## Start your simulated devices
  69. Now that the devices have been added to the device registry, you can start simulated managed devices. One simulated device is started for each device identity provisioned in the Azure IoT Hub.
  70. Using the developer command prompt, in the **\\azure-iot-sdks\\csharp\\service\\samples\\bin** folder, run:
  71. ```
  72. simulate.bat
  73. ```
  74. This script runs one instance of **iotdm\_simple\_sample.exe** for each device listed in the **devicecreds.txt** file. The simulated device will continue to run until you close the command window.
  75. The **iotdm\_simple\_sample** sample application is built using the Azure IoT Hub device management client library for C, which enables the creation of IoT devices that can be managed by Azure IoT Hub. Device makers can use this library to report device properties and implement the execute actions required by device jobs. This library is a component delivered as part of the open source Azure IoT Hub SDKs.
  76. When you run **simulate.bat**, you see a stream of data in the output window. This output shows the incoming and outgoing traffic as well as **printf** statements in the application specific callback functions. This allows you to see incoming and outgoing traffic along with how the sample application is handling the decoded packets. When the device connects to the IoT Hub, the service automatically starts to observe resources on the device. The IoT Hub DM client library then invokes the device callbacks to retrieve the latest values from the device.
  77. Below is output from the **iotdm\_simple\_sample** sample application. At the top you see a successful **REGISTERED** message, showing the device with Id **Device11-7ce4a850** connecting to IoT Hub.
  78. > [AZURE.NOTE] To have less verbose output, build and run the retail configuration.
  79. ![][img-output]
  80. Make sure to leave all the simulated devices running as you complete the following sections.
  81. ## Run the device management sample UI
  82. Now that you have provisioned an IoT Hub and have several simulated devices running and registered for management, you can deploy the device management sample UI. The device management sample UI provides you with a working example of how to utilize the device management APIs to build an interactive UI experience. For more information about the device management sample UI, including [known issues](https://github.com/Azure/azure-iot-device-management#knownissues), see the [Azure IoT device management UI][lnk-dm-github] GitHub repository.
  83. To retrieve, build, and run the device management sample UI, follow the steps below:
  84. 1. Open a **Command Prompt**.
  85. 2. Confirm that youve installed Node.js 6.1.0 or greater according the prerequisites section by typing `node --version`.
  86. 3. Clone the Azure IoT device management UI GitHub repository by running the following command:
  87. ```
  88. git clone https://github.com/Azure/azure-iot-device-management.git
  89. ```
  90. 4. In the root folder of your cloned copy of the Azure IoT device management UI repository, run the following command to retrieve the dependent packages:
  91. ```
  92. npm install
  93. ```
  94. 5. When the npm install command has completed, run the following command to build the code:
  95. ```
  96. npm run build
  97. ```
  98. 6. Use a text editor to open the user-config.json file in root of the cloned folder. Replace the text "&lt;YOUR CONNECTION STRING HERE&gt;" with your IoT Hub connection string from the previous section and save the file.
  99. 7. In the command prompt, run the following command to start the device management UX app:
  100. ```
  101. npm run start
  102. ```
  103. 8. When the command prompt has reported "Services have started", open a web browser (Edge/IE 11+/Safari/Chrome are currently supported) and navigate to the device management app at the following URL to view your simulated devices: <http://127.0.0.1:3003>.
  104. ![][img-dm-ui]
  105. Leave the simulated devices and the device management app running as you proceed to the next device management tutorial.
  106. ## Next steps
  107. To continue getting started with IoT Hub, see [Getting started with the Gateway SDK][lnk-gateway-SDK].
  108. To learn more about the Azure IoT Hub device management features, see the [Explore Azure IoT Hub device management using the sample UI][lnk-sample-ui] tutorial.
  109. <!-- images and links -->
  110. [img-new-hub]: media/iot-hub-device-management-get-started/image1.png
  111. [img-configure-hub]: media/iot-hub-device-management-get-started/image2.png
  112. [img-monitor]: media/iot-hub-device-management-get-started/image3.png
  113. [img-keys]: media/iot-hub-device-management-get-started/image4.png
  114. [img-connection]: media/iot-hub-device-management-get-started/image5.png
  115. [img-output]: media/iot-hub-device-management-get-started/image6.png
  116. [img-dm-ui]: media/iot-hub-device-management-get-started/dmui.png
  117. [lnk-free-trial]: http://azure.microsoft.com/pricing/free-trial/
  118. [Azure portal]: https://portal.azure.com/
  119. [Using resource groups to manage your Azure resources]: ../azure-portal/resource-group-portal.md
  120. [lnk-dm-github]: https://github.com/Azure/azure-iot-device-management
  121. [lnk-sample-ui]: iot-hub-device-management-ui-sample.md
  122. [lnk-gateway-SDK]: iot-hub-linux-gateway-sdk-get-started.md