練習 - 將模擬咖啡機連線至應用程式

已完成

若要在連線實體裝置之前驗證您的案例,請使用實作裝置模型中所定義行為的裝置模擬器。

回想一下,在已啟用 IoT 的咖啡機範例中,您會將咖啡機模擬器連線到 IoT Central,以監視遙測資料,例如水溫和濕度、觀察機器的狀態、設定最佳溫度、接收保固狀態,以及傳送命令。

但在此,您會使用 C# 應用程式來模擬裝置,並將其連線至 Azure IoT Central 應用程式。

Illustration that shows a Coffee machine.

將咖啡機新增至 IoT Central 中

首先,註冊裝置:

  1. 在 IoT Central 應用程式中,瀏覽至 [裝置],然後選取 [+ 新增]。
  2. 在 [建立新裝置表單] 上,將 [裝置識別碼] 變更為 ccm-001,並將 [裝置名稱] 變更為「已連線咖啡機 - 001」。 請確定 [裝置範本] 為 [未指派],且 [模擬此裝置] 為 [否],然後選取 [建立]。

已連線咖啡機 - 001 裝置現在會顯示在裝置清單中,且其狀態將會是 [已註冊],而範本為 [未指派]。 在上一個單元中,我們已查看 C# 應用程式程式碼,並確認佈建裝置時會傳送模型識別碼。 這可讓 IoT Central 在裝置第一次連線時指派正確的裝置範本。

取得連線資訊

若要執行 C# 應用程式,您需要下列設定值:

  1. 選取裝置清單中的 [已連線咖啡機 - 001] 來檢視裝置詳細資料。

  2. 選取 [連線] 以開啟 [裝置連線群組] 面板。

  3. 將此頁面維持在開啟狀態,或記下識別碼範圍裝置識別碼,以及主索引鍵的值。

    Screenshot that shows device connection information.

使用 Azure Cloud Shell來設定 C# 應用程式所使用的環境變數。

export ID_SCOPE=<The ID scope you made a note of previously>
export DEVICE_ID=<The Device ID you made a note of previously>
export DEVICE_KEY=<The Primary key you made a note of previously>

列印環境變數,並確認其已正確定義。

printenv ID_SCOPE DEVICE_ID DEVICE_KEY

建立 C# 應用程式

下列步驟說明如何建立用戶端應用程式,以實作咖啡機裝置模擬器。

提示

在本練習中,會在 Azure Cloud Shell 中建立 C# 應用程式,如此您便不需要在本機電腦上安裝任何項目。

  1. 在 Azure Cloud Shell 中執行下列命令,以建立 coffee-maker 資料夾並瀏覽到該資料夾:

    mkdir ~/coffee-maker
    cd ~/coffee-maker
    
  2. 在 Cloud Shell 中執行下列命令,以下載 coffee-maker 資料夾中的來源檔案:

    wget https://raw.githubusercontent.com/Azure-Samples/iot-central-docs-samples/main/monitor-manage-device-with-iotcentral/CoffeeMaker-csharp.csproj
    wget https://raw.githubusercontent.com/Azure-Samples/iot-central-docs-samples/main/monitor-manage-device-with-iotcentral/CoffeeMaker.cs
    wget https://raw.githubusercontent.com/Azure-Samples/iot-central-docs-samples/main/monitor-manage-device-with-iotcentral/Parameters.cs
    wget https://raw.githubusercontent.com/Azure-Samples/iot-central-docs-samples/main/monitor-manage-device-with-iotcentral/Program.cs
    
  3. 在 Cloud Shell 中執行下列命令,以啟動應用程式:

    dotnet run
    
  4. 確認應用程式在 Cloud Shell 視窗中啟動,並顯示「裝置成功連線到 Azure IoT Central」訊息以及「遙測傳送:」訊息。

恭喜! 您的應用程式已啟動並執行,而且正在與 IoT Central 通訊!

檢查您的工作

現在,您已使用 Azure IoT Central 應用程式,並將咖啡機連線至 Azure IoT Central。 您很快就可開始監視和管理遠端咖啡機。 在這裡,您會花點時間使用稍早定義的已連線咖啡機範本來驗證設定和連線。 您會在屬性表單上更新最佳溫度、執行命令來更新機器的狀態,並檢視已連線咖啡機的遙測。

更新屬性來將應用程式與咖啡機同步

使用您在裝置範本中定義的屬性表單,將設定資料從應用程式傳送到咖啡機。

  1. 巡覽至 Azure IoT Central 應用程式的 [裝置] 頁面。

  2. 從裝置清單中選取 ccm-001,以檢視裝置詳細資料。

  3. 選取 [屬性] 來檢視表單。

  4. 輸入 95 作為最佳溫度。 然後選取儲存

變更屬性後,屬性會在 UI 中標記為 [擱置中:現在],直到咖啡機承認設定變更為止。 然後,屬性即會標記為 [已接受:現在]

注意

成功的設定更新會指出資料流程,並驗證連線。 遙測i度量會回應最佳溫度的更新。

更新雲端屬性

使用您在裝置範本中定義的屬性表單,以更新咖啡機的設定資料。

  1. 巡覽至 Azure IoT Central 應用程式的 [裝置] 頁面。

  2. 從裝置清單中選取 ccm-001,以檢視裝置詳細資料。

  3. 選取 [屬性] 來檢視表單。

  4. 輸入 99 作為 [咖啡機溫度上限 (°C)],並輸入 92 作為 [咖啡機溫度下限 (°C)]。 然後選取儲存

雲端屬性不會與裝置同步,這些屬性只會存在於 IoT Central 應用程式內。

在咖啡機上執行命令

巡覽至裝置的 [命令] 頁面以進行下列練習。 若要驗證命令設定,您可從 IoT Central 對咖啡機執行遠端命令。 若命令成功,則咖啡機會傳回確認訊息。

  1. 選取 [執行] 來開始遠端沖泡。

    若滿足以下三個條件,咖啡機即會啟動:

    • 偵測到咖啡杯
    • 非維修中
    • 尚未沖泡

    注意

    當成功開始沖泡後,[遙測] 頁面上指出的機器狀態會變更為 [正在沖泡]

    在模擬咖啡機的主控台記錄中尋找確認訊息。

    Set up the device client.
    Device successfully connected to Azure IoT Central
    - Set handler for "SetMaintenanceMode" command.
    - Set handler for "StartBrewing" command.
    - Set handler to receive "OptimalTemperature" updates.
    - Update "DeviceWarrantyExpired" reported property on the initial startup.
     * Property: Update - { "DeviceWarrantyExpired": True } is Completed.
    Telemetry send: Temperature: 97ºC Humidity: 25.4% Cup Detected: Y Brewing: N Maintenance Mode: N
    Telemetry send: Temperature: 94.3ºC Humidity: 22.4% Cup Detected: Y Brewing: N Maintenance Mode: N
     * Property: Received - { "OptimalTemperature": 95°C }.
     * Property: Update - {"OptimalTemperature ": 95°C } is InProgress.
    Telemetry send: Temperature: 96.2ºC Humidity: 53.5% Cup Detected: N Brewing: N Maintenance Mode: N
     * Property: Update - {"OptimalTemperature ": 95°C } is Completed.
    Telemetry send: Temperature: 95.9ºC Humidity: 35.3% Cup Detected: N Brewing: N Maintenance Mode: N
    Telemetry send: Temperature: 96.4ºC Humidity: 75.5% Cup Detected: N Brewing: N Maintenance Mode: N
     * Start brewing command received
    Telemetry send: Temperature: 94.2ºC Humidity: 86.3% Cup Detected: Y Brewing: Y Maintenance Mode: N
    Telemetry send: Temperature: 94.8ºC Humidity: 85.4% Cup Detected: Y Brewing: Y Maintenance Mode: N
    Telemetry send: Temperature: 96.7ºC Humidity: 93.7% Cup Detected: Y Brewing: Y Maintenance Mode: N
    
  2. 在 [命令] 頁面上選擇 [執行] 以設定維護模式。 如果咖啡機「尚未」處於維護模式,則其將設為「維護」。

    在咖啡機器的主控台記錄中尋找確認訊息。

    注意

    如同在實際情況下,技術人員會先將機器離線並執行必要的維修,再將它切換回線上狀態。咖啡機在您重新啟動用戶端程式碼之前,將持續處於維護模式。

    Set up the device client.
    Device successfully connected to Azure IoT Central
    - Set handler for "SetMaintenanceMode" command.
    - Set handler for "StartBrewing" command.
    - Set handler to receive "OptimalTemperature" updates.
    - Update "DeviceWarrantyExpired" reported property on the initial startup.
     * Property: Update - { "DeviceWarrantyExpired": False } is Completed.
    Telemetry send: Temperature: 96.8ºC Humidity: 39.2% Cup Detected: Y Brewing: N Maintenance Mode: N
    Telemetry send: Temperature: 96.8ºC Humidity: 57.7% Cup Detected: Y Brewing: N Maintenance Mode: N
    Telemetry send: Temperature: 97.2ºC Humidity: 45.2% Cup Detected: Y Brewing: N Maintenance Mode: N
     * Maintenance command received
    Telemetry send: Temperature: 96ºC Humidity: 54.7% Cup Detected: Y Brewing: N Maintenance Mode: Y
    Telemetry send: Temperature: 95.6ºC Humidity: 22.1% Cup Detected: Y Brewing: N Maintenance Mode: Y
    Telemetry send: Temperature: 95.3ºC Humidity: 92% Cup Detected: Y Brewing: N Maintenance Mode: Y
     * Start brewing command received
     - Warning: The device is in maintenance mode.
    Telemetry send: Temperature: 94.5ºC Humidity: 85.5% Cup Detected: Y Brewing: N Maintenance Mode: Y
    Telemetry send: Temperature: 96.9ºC Humidity: 62.7% Cup Detected: Y Brewing: N Maintenance Mode: Y
    Telemetry send: Temperature: 97.6ºC Humidity: 47.3% Cup Detected: Y Brewing: N Maintenance Mode: Y
    

檢視咖啡機遙測

巡覽至咖啡機的 [遙測] 頁面。 您會在建立裝置範本時定義此頁面:

Screenshot that shows the Telemetry page for the connected coffee machine device template.

重要

建議您執行 C# 應用程式的時間不要超過 60 分鐘,以防止應用程式傳送給您不想要的通知/電子郵件。 在不進行此課程模組時停止應用程式,也可避免耗盡每日的訊息配額。 您可以新增旗標來定義應用程式的執行時間:dotnet run -n [TimeInSeconds]