Raspberry Pi(Python)のテレメトリーをAzure IoT Hubから取得する


アーキテクチャ

事前準備

MacOS

Azure CLI インストール

brew update && brew install python3
brew link --overwrite python3
brew install azure-cli

Azure CLIログインとAzure CLIのIoT拡張インストール

az login
az extension add --name azure-cli-iot-ext

Azure IoT Hub 作成

# AzureサブスクリプションID取得
subscriptionID=$(az account show --query id)

# ロケーション設定(Japan East)
location=japaneast

# リソースグループ設定
resourceGroup={ResourceGroupName}

# リソースグループ作成
az group create --name $resourceGroup --location $location

# IoT Hub名設定
iotHubName={AzureIoTHubName}

# IoT Hub作成 (F1無料)
az iot hub create --name $iotHubName --resource-group $resourceGroup --sku F1 --location $location

https://portal.azure.com/#create/Microsoft.IotHub

デバイス登録

az iot hub device-identity create --hub-name {AzureIoTHubName} --device-id {DeviceName}

デバイス接続情報取得

az iot hub device-identity show-connection-string --hub-name {AzureIoTHubName} --device-id {DeviceName} --output table

サービス接続情報取得

az iot hub show-connection-string --policy-name service --name {AzureIoTHubName} --output table

テレメトリー送信(定期と呼び出し)

Raspberry Pi

Pythonパッケージインストール

pip3 install azure-iot-device

サンプルコードのリポジトリクローン

mkdir -p ~/Source/Repos && cd ~/Source/Repos
git clone https://github.com/Azure-Samples/azure-iot-samples-python/
cd ./azure-iot-samples-python/iot-hub/Quickstarts/simulated-device-2

デバイス接続情報更新

vi SimulatedDevice.py

実行

python3 SimulatedDevice.py

テレメトリー確認

Azureポータル

Azure Cloud Shell

az iot hub monitor-events --hub-name {AzureIoTHubName} --device-id {DeviceName}

テレメトリー取得

MacOS

Pythonパッケージインストール

pip3 install azure-iot-hub

サンプルコードのリポジトリクローン

mkdir -p ~/Source/Repos && cd ~/Source/Repos
git clone https://github.com/Azure-Samples/azure-iot-samples-python/
cd ./azure-iot-samples-python/iot-hub/Quickstarts/back-end-application

サービス接続情報更新

vi BackEndApplication.py

実行

python3 BackEndApplication.py

https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-control-device-python