Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
Azure Iot Hubに、数十台以上の規模でデバイス登録を行い、Iot Hubに接続するためのキー情報を得たいようなシナリオがあります。この場合、Azure Portal のデバイス エクスプローラーで一つ一つ手作業でこれを行うのは大変です。
そのような時、以下のサンプルをそのまま動作いただければ、一度に複数台「デバイス登録」と「Iot Hubに接続するためのキー情報を得る」ことができます。
How to read events from an IoT Hub with the Service Bus Explorer
https://code.msdn.microsoft.com/How-to-read-events-from-an-1641eb1b
Create a new device to the device identity registry using the Microsoft.Azure.Devices.RegistryManager.AddDeviceAsync(Microsoft.Azure.Devices.Device)"> AddDeviceAsync method of the RegistryManager class.
動作手順の例は以下の通りです。
<動作手順>
1. 上記ページの「Download」の右にある「C# (4.8MB)」をクリックし、How to read events from an IoT Hub with the Service Bus Explorer.zip をダウンロードし、解凍します。
2. Visual Studio 2015 で、C# フォルダの DeviceEmulator.sln を開きます。
3. ソリューションエクスプローラーで、ソリューションをビルドします。
4. C#\DeviceEmulator\bin\Debug\ フォルダに DeviceEmulator.exe が生成されますので、これを起動します。
5. Azure Portal で IoT Hub のリソースを選択し、[共有アクセス ポリシー] で、[iothubowner] ポリシーをクリックし、[iothubowner] ウィンドウで IoT Hub 接続文字列を確認します。
6. DeviceEmulator.exe の ConnectionString に、5. で確認した接続文字列を入力します。
7. Device Count に、作成したい個数を入力します。
8. Create をクリックすると、Device が作成され、キーも取得できます。
なお、上記に該当する実装は、MainForm.cs のInitializeDevicesAsync() です。
以下のfor ループでDevice Count の個数分、AddDeviceAsync メソッドでデバイスの登録を行っていることを確認できます。
for (var i = 1; i <= txtDeviceCount.IntegerValue; i++)
// try to create the device in the device identity registry device = await registryManager.AddDeviceAsync(new Device(deviceId)); |
また、プライマリキーもその次の行で取得できます。
WriteToLog($"Device [{deviceId}] successfully created in the device identity registry with key[{device.Authentication.SymmetricKey.PrimaryKey}]"); |
上記の実装を各言語で実施するにあたり、AddDeviceAsync() に相当する処理が、他の言語ではどうなっているのか確認するために、以下のドキュメントの「デバイスID の作成」の項目がご参考になるかと思います。
Azure IoT Hub とシミュレートされたデバイス入門チュートリアル
/ja-jp/azure/iot-hub/iot-hub-get-started-simulated
.NET
/ja-jp/azure/iot-hub/iot-hub-csharp-csharp-getstarted#create-a-device-identity
Java
/ja-jp/azure/iot-hub/iot-hub-java-java-getstarted#create-a-device-identity
Node.js
/ja-jp/azure/iot-hub/iot-hub-node-node-getstarted#create-a-device-identity
Python
/ja-jp/azure/iot-hub/iot-hub-python-getstarted#create-a-device-identity
REST API の場合は以下をご覧ください。
REST API
/en-us/rest/api/iothub/DeviceApi/PutDevice
以上の内容がお役に立ちましたら幸いです。
Azure IoT 開発サポートチーム 津田