Python 用 Azure IoT Hub ライブラリ

管理 API

pip install azure-mgmt-iothub

管理クライアントを作成する

管理クライアントのインスタンスは、以下のコードで作成します。

サブスクリプションの一覧から取得できる をsubscription_id指定する必要があります。

Python SDK を使用した Azure Active Directory の認証処理と Credentials インスタンスの作成について詳しくは、「Resource Management Authentication (リソース管理の認証)」を参照してください。

from azure.mgmt.iothub import IotHubClient
from azure.common.credentials import UserPassCredentials

# Replace this with your subscription id
subscription_id = '33333333-3333-3333-3333-333333333333'

# See above for details on creating different types of AAD credentials
credentials = UserPassCredentials(
    'user@domain.com',  # Your user
    'my_password',      # Your password
)

iothub_client = IotHubClient(
    credentials,
    subscription_id
)

IoTHub の作成

async_iot_hub = iothub_client.iot_hub_resource.begin_create_or_update(
    'MyResourceGroup',
    'MyIoTHubAccount',
    {
        'location': 'westus',
        'subscriptionid': subscription_id,
        'resourcegroup': 'MyResourceGroup',
        'sku': {
            'name': 'S1',
            'capacity': 2
        },
        'properties': {
            'enable_file_upload_notifications': False,
            'operations_monitoring_properties': {
            'events': {
                "C2DCommands": "Error",
                "DeviceTelemetry": "Error",
                "DeviceIdentityOperations": "Error",
                "Connections": "Information"
            }
            },
            "features": "None",
        }
    }
)
iothub = async_iot_hub.result() # Blocking wait for creation