title | description | author | ms.author | ms.date | ms.topic | ms.service | services | manager | ms.devlang | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|
Provision a simulated TPM device to Azure IoT Hub using Python | Microsoft Docs |
Azure Quickstart - Create and provision a simulated TPM device using Java device SDK for IoT Hub Device Provisioning Service.This quickstart uses individual enrollments. |
wesmc7777 |
wesmc |
05/21/2018 |
quickstart |
iot-dps |
iot-dps |
timlt |
python |
mvc |
Create and provision a simulated TPM device using Python device SDK for IoT Hub Device Provisioning Service
[!INCLUDE iot-dps-selector-quick-create-simulated-device-tpm]
These steps show how to create a simulated device on your development machine running Windows OS, run the Windows TPM simulator as the Hardware Security Module (HSM) of the device, and use the Python code sample to connect this simulated device with the Device Provisioning Service and your IoT hub.
If you're unfamiliar with the process of autoprovisioning, be sure to also review Auto-provisioning concepts. Also make sure you've completed the steps in Set up IoT Hub Device Provisioning Service with the Azure portal before continuing.
The Azure IoT Device Provisioning Service supports two types of enrollments:
- Enrollment groups: Used to enroll multiple related devices.
- Individual Enrollments: Used to enroll a single device.
This article will demonstrate individual enrollments.
[!INCLUDE IoT Device Provisioning Service basic]
-
Make sure you have either Visual Studio 2015 or Visual Studio 2017 installed on your machine. You must have 'Desktop development with C++' workload enabled for your Visual Studio installation.
-
Download and install the CMake build system.
-
Make sure
git
is installed on your machine and is added to the environment variables accessible to the command window. See Software Freedom Conservancy's Git client tools for the latest version ofgit
tools to install, which includes the Git Bash, the command-line app that you can use to interact with your local Git repository. -
Open a command prompt or Git Bash. Clone the GitHub repo for device simulation code sample:
git clone https://github.com/Azure/azure-iot-sdk-python.git --recursive
-
Create a folder in your local copy of this GitHub repo for CMake build process.
cd azure-iot-sdk-python/c mkdir cmake cd cmake
-
The code sample uses a Windows TPM simulator. Run the following command to enable the SAS token authentication. It also generates a Visual Studio solution for the simulated device.
cmake -Duse_prov_client:BOOL=ON -Duse_tpm_simulator:BOOL=ON ..
-
In a separate command prompt, navigate to the TPM simulator folder and run the TPM simulator. Click Allow Access. It listens over a socket on ports 2321 and 2322. Do not close this command window; you will need to keep this simulator running until the end of this Quickstart guide.
.\azure-iot-sdk-python\c\provisioning_client\deps\utpm\tools\tpm_simulator\Simulator.exe
-
Open the solution generated in the cmake folder named
azure_iot_sdks.sln
, and build it in Visual Studio. -
Right-click the tpm_device_provision project and select Set as Startup Project. Run the solution. The output window displays the Endorsement Key and the Registration ID needed for device enrollment. Note down these values.
-
Sign in to the Azure portal, click on the All resources button on the left-hand menu and open your Device Provisioning service.
-
On the Device Provisioning Service summary blade, select Manage enrollments. Select Individual Enrollments tab and click the Add individual enrollment button at the top.
-
Under the Add Enrollment, enter the following information:
- Select TPM as the identity attestation Mechanism.
- Enter the Registration ID and Endorsement key for your TPM device.
- Select an IoT hub linked with your provisioning service.
- Enter a unique device ID. Make sure to avoid sensitive data while naming your device.
- Update the Initial device twin state with the desired initial configuration for the device.
- Once complete, click the Save button.
On successful enrollment, the Registration ID of your device will appear in the list under the Individual Enrollments tab.
-
Download and install Python 2.x or 3.x. Make sure to use the 32-bit or 64-bit installation as required by your setup. When prompted during the installation, make sure to add Python to your platform-specific environment variables.
- If you are using Windows OS, then Visual C++ redistributable package to allow the use of native DLLs from Python.
-
Follow these instructions to build the Python packages.
[!NOTE] > If running the
build_client.cmd
make sure to use the--use-tpm-simulator
flag.[!NOTE] > If using
pip
make sure to also install theazure-iot-provisioning-device-client
package. Note that the released PIP packages are using the real TPM, not the simulator. To use the simulator you need to compile from the source using the--use-tpm-simulator
flag. -
Navigate to the samples folder.
cd azure-iot-sdk-python/provisioning_device_client/samples
-
Using your Python IDE, edit the python script named provisioning_device_client_sample.py. Modify the GLOBAL_PROV_URI and ID_SCOPE variables to the values noted previously. Also make sure SECURITY_DEVICE_TYPE is set to
ProvisioningSecurityDeviceType.TPM
GLOBAL_PROV_URI = "{globalServiceEndpoint}" ID_SCOPE = "{idScope}" SECURITY_DEVICE_TYPE = ProvisioningSecurityDeviceType.TPM PROTOCOL = ProvisioningTransportProvider.HTTP
-
Run the sample.
python provisioning_device_client_sample.py
-
Notice the messages that simulate the device booting and connecting to the Device Provisioning Service to get your IoT hub information.
-
On successful provisioning of your simulated device to the IoT hub linked with your provisioning service, the device ID appears on the hub's Device Explorer blade.
If you changed the initial device twin state from the default value in the enrollment entry for your device, it can pull the desired twin state from the hub and act accordingly. For more information, see Understand and use device twins in IoT Hub
If you plan to continue working on and exploring the device client sample, do not clean up the resources created in this Quickstart. If you do not plan to continue, use the following steps to delete all resources created by this Quickstart.
- Close the device client sample output window on your machine.
- Close the TPM simulator window on your machine.
- From the left-hand menu in the Azure portal, click All resources and then select your Device Provisioning service. Open the Manage Enrollments blade for your service, and then click the Individual Enrollments tab. Select the REGISTRATION ID of the device you enrolled in this Quickstart, and click the Delete button at the top.
- From the left-hand menu in the Azure portal, click All resources and then select your IoT hub. Open the IoT Devices blade for your hub, select the DEVICE ID of the device you registered in this Quickstart, and then click Delete button at the top.
In this Quickstart, you’ve created a TPM simulated device on your machine and provisioned it to your IoT hub using the IoT Hub Device Provisioning Service. To learn how to enroll your TPM device programmatically, continue to the Quickstart for programmatic enrollment of a TPM device.
[!div class="nextstepaction"] Azure Quickstart - Enroll TPM device to Azure IoT Hub Device Provisioning Service