演習 - IoT デバイスに Azure Functions として Azure AI サービスをデプロイする

完了

Azure Cognitive Service を作成する

Azure portal で、[リソースの作成] をクリックし、[音声] を検索します。

音声サービスを作成します。

フォームに入力して音声サービスを作成します。

名前:名前を入力します

サブスクリプション:ご自分の使用可能なサブスクリプションを選択します

場所:ご自分の場所を選択します

価格レベル: S0 を選択します

リソース グループ:リソース グループを選択します

An illustration is showing how to create cognitive speech service.

先ほど作成した音声サービスに移動し、キーのセクションをクリックして、[キー 1] をコピーしてメモします。 これは、IoT DevKit のアクセスで使用します。

An illustration is showing speech API-keys.

Visual Studio Code 向け拡張機能をインストールする

Visual Studio Code に、いくつかの拡張機能をインストールする必要があります。

  • Visual Studio Code を起動して、マーケットプレースで Arduino の拡張機能を検索し、それをインストールします。 この拡張機能は、Arduino プラットフォームでの開発エクスペリエンスを強化します。

  • マーケットプレースで Azure IoT Tools の拡張機能を検索し、それをインストールします。

    Note

    Azure IoT Tools の拡張パックには、さまざまな IoT devkit デバイスの開発とデバッグに使用できる Azure IoT Device Workbench が含まれています。 Azure IoT Tools の拡張パックにも含まれている Azure IoT Hub 拡張機能は、Azure IoT Hubs の管理と対話に使用します。

  • 拡張機能マーケットプレースから C# を検索し、それをインストールします。

Visual Studio Code で Arduino 設定を構成する

Visual Studio Code で [ファイル] > [基本設定] > [設定] の順にクリックします。 次いで [...] をクリックし、settings.json を開きます。

お使いのプラットフォームに合わせて次の行を追加し、Arduino を構成します。

  • Windows:

    "arduino.path": "C:\\Program Files (x86)\\Arduino",
    "arduino.additionalUrls": "https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/master/package_azureboard_index.json"
    
  • macOS:

    "arduino.path": "/Applications",
    "arduino.additionalUrls": "https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/master/package_azureboard_index.json"
    
  • Ubuntu:

    下の {username} プレースホルダーを自分のユーザー名に変更します。

    "arduino.path": "/home/{username}/Downloads/arduino-1.8.8",
    "arduino.additionalUrls": "https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/master/package_azur
    

サンプル プロジェクトを開く

まず、自分の Azure アカウントでサインインを開始します。 F1 をクリックしてコマンド パレットを開き、入力し、Azure:サインイン

F1 をクリックしてコマンド パレットを開き、入力し、[Azure IoT Device Workbench:Open Examples...] を選択します。次に、基板として [IoT DevKit] を選択します。

[IoT Workbench Examples] ページで [DevKit Translator] を見つけ、[Open Sample] をクリックします。 次に、既定のパスを選択してサンプル コードをダウンロードします。

An illustration is showing how to open sample project in Visual Studio Code.

基板を選択する

F1 をクリックしてコマンド パレットを開き、入力し、[Arduino:Board Manager] を選択します。 AZ3166 を検索してインストールします。

An illustration is showing how to select device board using arduino extension.

シリアル ポートのインストール:ST-Link ドライバー

  • Windows:STMicroelectronics Web サイトから USB ドライバーをダウンロードし、インストールします。

  • macOS:macOS にはドライバーが必要ありません。

  • Ubuntu:ターミナルでコマンドを実行し、サインアウトしてサインインし、グループ変更を適用します。

    # Copy the default rules. This grants permission to the group 'plugdev'
    sudo cp ~/.arduino15/packages/AZ3166/tools/openocd/0.10.0/linux/contrib/60-openocd.rules /etc/udev/rules.d/
    sudo udevadm control --reload-rules
    
    # Add yourself to the group 'plugdev'
    # Logout and log back in for the group to take effect
    sudo usermod -a -G plugdev $(whoami)
    

Note

ST-Link/V2 は、開発コンピューターと通信するために IoT DevKit で使用される USB インターフェイスです。 コンパイルしたデバイス コードを DevKit に送信するには、Windows に ST-Link/V2 をインストールする必要があります。 OS 固有の手順に従い、コンピューターがデバイスにアクセスすることを許可します。

Azure Function を作成し、Azure Services をプロビジョニングする

Visual Studio Code で F1 をクリックし、入力し、[Azure IoT Device Workbench:Provision Azure Services...] を選択します。

An illustration is showing how to provision Azure services.

手順に従い、Azure IoT Hub と Azure Functions のプロビジョニングを完了します。

An illustration is showing how to provision iot services.

作成した Azure IoT Hub デバイス名をメモします。

これで Azure IoT Hub プロビジョニングされ、その中でデバイスが作成されました。 また、後で IoT DevKit を構成するためにデバイス接続文字列が Visual Studio Code に保存されます。

An illustration is showing the confirmation of provisioning Azure services.

Functions\DevKitTranslatorFunction.cs を開き、メモしておいたデバイス名と Speech Service キーで次のコード行を更新します。

// Subscription Key of Speech Service
const string speechSubscriptionKey = "";

// Region of the speech service, see https://learn.microsoft.com/azure/cognitive-services/speech-service/regions for more details.
const string speechServiceRegion = "";

// Device ID
const string deviceName = "";

Azure 関数をデプロイする

F1 をクリックし、入力し、[Azure IoT Device Workbench:Deploy to Azure...] を選択します。Visual Studio Code から再デプロイの確認を求められたら、[Yes] をクリックします。

An illustration is showing how to deploy Azure function to the cloud.

デプロイが正常に完了したことを確認します。

An illustration is showing the confirmation of Azure function deployment.

Azure portal で、[関数アプリ] セクションに移動し、作成した Azure Function アプリを見つけます。 [devkit_translator] をクリックし、[関数の URL の取得] をクリックして URL をコピーします。

An illustration is showing how to get function url from the cloud.

URL を azure_config.h ファイルに貼り付けます。

An illustration is showing how to configure Azure function url in the sample project.

デバイス設定を構成する

右下のステータス バーで、選択されている基板として MXCHIP AZ3166 が表示されていることと STMicroelectronics のシリアル ポートが使用されていることを確認します。

An illustration is showing how to select serial port.

F1 をクリックして入力し、[Azure IoT Device Workbench: Configure Device Settings...] (Azure IoT Device Workbench: デバイス設定を構成する...) > [Config Device Connection String] (デバイス接続文字列の構成) の順に選択します。 IoT Hub デバイス接続文字列を選択し、DevKit に合わせてそれを構成します。

An illustration is showing how to configure connecting string.

DevKit で A ボタンを押しながらリセット ボタンを押して離し、A ボタンを離します。DevKit が構成モードに入り、接続文字列が保存されます。

正常に完了すると、通知が表示されます。

An illustration is showing the confirmation of configuration the connection string.

デバイス コードをアップロードする

F1 をもう一度クリックし、入力し、[Azure IoT Device Workbench:Upload Device Code] を選択します。 コードがコンパイルされ、DevKit にアップロードされます。

An illustration is showing how to upload device code to the iot device.

DevKit が再起動し、コードの実行が開始されます。

プロジェクトをテストする

アプリの初期化後、DevKit 画面の指示に従います。 既定のソース言語は中国語です。

別の言語の翻訳を選択するには:

  1. A ボタンを押して設定モードに入ります。
  2. B ボタンを押すと、サポートされているすべてのソース言語をスクロールして表示できます。
  3. A ボタンを押し、ソース言語の選択を確定します。
  4. B ボタンを押しながら話します。B ボタンを離すと翻訳が開始されます。
  5. 英語に翻訳されたテキストが画面に表示されます。

翻訳結果画面では次の操作が可能です。

  1. A ボタンと B ボタンを押し、スクロールしてソース言語を選択できます。
  2. B ボタンを押して発話できます。 音声を送信し、翻訳テキストを取得するには、B ボタンを離します。