Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

AzureのIoT系をつかう その6:実物RasPiをデバイスとして登録する

余っているRasPi3B+をつかって、Azure IoTに接続を接続してみます。

実際のところ

IoT Hub側の受け入れ用意

例のごとく、Azure Portalへ移動


リソースグループ名を「MyResourceGroup」IoT hub名を「RasPi3BPlus」領域を「Japan East」として新規作成します。

デプロイが完了したらIoT Hubのページに飛び、デバイスの画面を選択

バイスの追加で「RP3」として新規作成

バイスの画面で「プライマリ接続文字列」をコピーしておきます。

RasPi3B+の用意

RasPiはRaspberry Pi Imagerで64bit版を書き込みしました。
最近はWiFi接続もSSHも初期設定できるので便利ですね。

RasPi3B+にサンプルプログラムを用意する

nodeの環境を用意
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install -y nodejs npm

サンプルリポジトリを導入し、必要なライブラリ群を入れます。

$ git clone https://github.com/Azure-Samples/azure-iot-samples-node.git

$ cd azure-iot-samples-node/iot-hub/Tutorials/RaspberryPiApp
$ npm install

そのディレクトリのまま、config.jsonの一番上、simulateDataを(センサーがないので)trueとします。

{
 "simulatedData": true,
...
}

先ほどのプライマリ接続をいれて

$ YOURWORD="プライマリ接続文字列"
$ sudo node index.js '$YOURWORD'

接続するとデータ収集する旨がでてくるので、OKならy

Microsoft would like to collect data about how users use Azure IoT samples and some problems they encounter. Microsoft uses this information to improve our tooling experience. Participation is voluntary and when you choose to participate your device automatically sends information to Microsoft about how you use Azure IoT samples.

Select y to enable data collection :(y/n, default is y)

デフォの設定では2秒ごとにMQTTでパケットが送信されます。