使用您的視窗或 Linux 個人電腦或 Mac 作為 AWS IoT 裝置 - AWS IoT Core

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用您的視窗或 Linux 個人電腦或 Mac 作為 AWS IoT 裝置

在本教學課程中,您將配置個人電腦以搭配使用 AWS IoT。 這些說明支援視窗和 Linux PCs 和 Mac。若要完成此目的,您需要在電腦上安裝一些軟體。如果您不想要在電腦上安裝軟體,則可以嘗試 使用 Amazon 創建虛擬設備 EC2,它會在虛擬機器上安裝所有軟體。

設定您的個人電腦

若要完成本教學課程,您需要一部 Windows 或 Linux PC,或是一部可連接到網際網路的 Mac。

繼續下一個步驟之前,請確定您可以在電腦上開啟命令列視窗。在 Windows PC 上使用 cmd.exe。在 Linux PC 或 Mac 上,使用 Terminal

安裝 Git、Python 和 AWS IoT Python SDK 的設備

在本節中,您將安裝 Python 和 AWS IoT SDK適用於 Python 的設備在您的計算機上。

安裝最新版本的 Git 和 Python

在您的電腦上下載並安裝 Git 和 Python
  1. 查看您的電腦上是否已安裝 Git。在命令列中輸入此命令。

    git --version

    如果命令顯示 Git 版本,表示已安裝 Git,而且您可以繼續進行下一個步驟。

    如果命令顯示錯誤,請開啟 https://git-scm.com/download 並為您的電腦安裝 Git。

  2. 查看您是否已安裝 Python。在命令列中輸入此命令。

    python -V
    注意

    如果此命令提供錯誤:Python was not found,原因可能是您的作業系統呼叫 Python v3.x 可執行檔作為 Python3。在此情況下,將 python 的所有執行個體取代為 python3,並繼續進行本教學課程的其餘部分。

    如果命令顯示 Python 版本,則表示已安裝 Python。本教學課程需要 Python v3.7 或更新版本。

  3. 如果已安裝 Python,則可以跳過本節的其餘步驟。如果未安裝,請繼續。

  4. 打開 https://www.py.org/下載/ 並下載計算機的安裝程序。

  5. 如果下載未自動開始安裝,請執行下載的程式來安裝 Python。

  6. 驗證 Python 的安裝。

    python -V

    確認命令顯示 Python 版本。如果未顯示 Python 版本,請嘗試再次下載並安裝 Python。

安裝 AWS IoT Python SDK 的設備

若要安裝 AWS IoT 在您的計算機上SDK使用 Python 的設備
  1. 安裝的 V2 AWS IoT Python SDK 的設備。

    python3 -m pip install awsiotsdk
  2. 克隆 AWS IoT Python 存儲庫SDK的設備放入到您的主目錄的 aws-iot-device-sdk-python-v2 目錄中。此程序指的是您要安裝的檔案的基本目錄 home.

    的實際位置 home 目錄取決於您的操作系統。

    Linux/macOS

    在 macOS 系統和 Linux 中,home 目錄是~

    cd ~ git clone https://github.com/aws/aws-iot-device-sdk-python-v2.git
    Windows

    在視窗中,您可以找到 home 通過在cmd窗口中運行此命令的目錄路徑。

    echo %USERPROFILE% cd %USERPROFILE% git clone https://github.com/aws/aws-iot-device-sdk-python-v2.git
    注意

    如果您使用的是 Windows PowerShell 而不是cmd.exe,請使用以下命令。

    echo $home

如需詳細資訊,請參閱 AWS IoT 用SDK於 Python GitHub 存儲庫的設備

準備執行範例應用程式

準備您的系統以執行範例應用程式
  • 建立 certs 目錄。將下列項目複製至 certs 目錄:私有金鑰、裝置憑證,以及您在 建立 AWS IoT resources 建立和註冊物件時所儲存的根憑證授權機構憑證檔案。目的地目錄中每個檔案的檔案名稱應與表格中的檔案名稱相符。

    下節命令假設您的金鑰和憑證檔案儲存在您的裝置上,如下表所示。

    Linux/macOS

    執行此命令來建立 certs 子目錄,您會在執行範例應用程式時使用這個子目錄。

    mkdir ~/certs

    在新的子目錄中,將檔案複製到下表所示的目的地檔案路徑。

    憑證檔案名稱

    檔案

    檔案路徑

    私有金鑰

    ~/certs/private.pem.key

    裝置憑證

    ~/certs/device.pem.crt

    根 CA 憑證

    ~/certs/Amazon-root-CA-1.pem

    執行此命令來列出 certs 目錄中的檔案,並將其與表格中列出的檔案進行比較。

    ls -l ~/certs
    Windows

    執行此命令來建立 certs 子目錄,您會在執行範例應用程式時使用這個子目錄。

    mkdir %USERPROFILE%\certs

    在新的子目錄中,將檔案複製到下表所示的目的地檔案路徑。

    憑證檔案名稱

    檔案

    檔案路徑

    私有金鑰

    %USERPROFILE%\certs\private.pem.key

    裝置憑證

    %USERPROFILE%\certs\device.pem.crt

    根 CA 憑證

    %USERPROFILE%\certs\Amazon-root-CA-1.pem

    執行此命令來列出 certs 目錄中的檔案,並將其與表格中列出的檔案進行比較。

    dir %USERPROFILE%\certs

設定政策和執行範例應用程式

在本節中,您將設定原則並執行在aws-iot-device-sdk-python-v2/samples目錄中找到的pubsub.py範例指令碼 適用於 Python 的 AWS IoT Device SDK。 此指令碼顯示您的裝置如何使用程式MQTT庫來發佈和訂閱MQTT訊息。

pubsub.py 範例應用程式會訂閱主題 test/topic、將 10 則訊息發佈至該主題,以及在從訊息代理程式接收訊息時顯示這些訊息。

若要執行 pubsub.py 範例指令碼,您需要以下資訊:

應用程式參數值

參數

可在哪裡找到值

your-iot-endpoint
  1. 中AWS IoT 主控台的左側功能表中,選擇 [設定]

  2. Settings (設定) 頁面上,您的端點會顯示在 Device data endpoint (裝置資料端點) 區段中。

所以此 your-iot-endpoint 值的格式為:endpoint_id-ats.iot.region.amazonaws.com,例如a3qj468EXAMPLE-ats.iot.us-west-2.amazonaws.com

執行指令碼之前,請確定您物件的政策許可範例指令碼連接、訂閱、發佈和接收。

尋找並檢閱物件資源的政策文件
  1. 中AWS IoT 主控台的「件」清單中,尋找代表您裝置的物件資源。

  2. 選擇代表您裝置物件資源的 Name (名稱) 連結,以開啟 Thing details (物件詳細資訊) 頁面。

  3. Thing details (物件詳細資訊) 頁面中的 Certificates (憑證) 索引標籤上,選擇與物件資源相連的憑證。清單中應該只有一個憑證。如果有多個憑證,請選擇其檔案已安裝在您裝置上且將用於連線的憑證 AWS IoT Core.

    Certificate (憑證) 詳細資訊頁面的 Policies (政策) 索引標籤中,選擇與憑證相連的政策。其中應只有一個政策。如果有多個政策,請針對每個政策重複下一個步驟,以確保至少有一個政策授予必要的存取權。

  4. 在 [原則概觀] 頁面中,尋找編輯JSON器,然後選擇 [編輯原則文件],視需要檢閱和編輯政策文件。

  5. 策略顯JSON示在下列範例中。在元"Resource"素中,region:account替換為 AWS 區域 以及 AWS 帳戶 在每個Resource值中。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "iot:Publish", "iot:Receive" ], "Resource": [ "arn:aws:iot:region:account:topic/test/topic" ] }, { "Effect": "Allow", "Action": [ "iot:Subscribe" ], "Resource": [ "arn:aws:iot:region:account:topicfilter/test/topic" ] }, { "Effect": "Allow", "Action": [ "iot:Connect" ], "Resource": [ "arn:aws:iot:region:account:client/test-*" ] } ] }
Linux/macOS
在 Linux/macOS 上執行範本指令碼
  1. 在指令行視窗中,導覽至使用這些指令SDK建立的~/aws-iot-device-sdk-python-v2/samples/node/pub_sub目錄。

    cd ~/aws-iot-device-sdk-python-v2/samples
  2. 在命令行窗口中,替換 your-iot-endpoint 如所示並運行此命令。

    python3 pubsub.py --endpoint your-iot-endpoint --ca_file ~/certs/Amazon-root-CA-1.pem --cert ~/certs/device.pem.crt --key ~/certs/private.pem.key
Windows
在 Windows PC 上執行範例應用程式
  1. 在命令列視窗中,導覽至SDK建立的%USERPROFILE%\aws-iot-device-sdk-python-v2\samples目錄,然後使用這些指令安裝範例應用程式。

    cd %USERPROFILE%\aws-iot-device-sdk-python-v2\samples
  2. 在命令行窗口中,替換 your-iot-endpoint 如所示並運行此命令。

    python3 pubsub.py --endpoint your-iot-endpoint --ca_file %USERPROFILE%\certs\Amazon-root-CA-1.pem --cert %USERPROFILE%\certs\device.pem.crt --key %USERPROFILE%\certs\private.pem.key

範例指令碼:

  1. 連接至 AWS IoT Core 為您的帳戶。

  2. 訂閱訊息主題 test/topic,並顯示其收到關於該主題的訊息。

  3. 將 10 則訊息發佈至主題 test/topic

  4. 顯示類似下列內容的輸出:

Connected! Subscribing to topic 'test/topic'... Subscribed with QoS.AT_LEAST_ONCE Sending 10 message(s) Publishing message to topic 'test/topic': Hello World! [1] Received message from topic 'test/topic': b'"Hello World! [1]"' Publishing message to topic 'test/topic': Hello World! [2] Received message from topic 'test/topic': b'"Hello World! [2]"' Publishing message to topic 'test/topic': Hello World! [3] Received message from topic 'test/topic': b'"Hello World! [3]"' Publishing message to topic 'test/topic': Hello World! [4] Received message from topic 'test/topic': b'"Hello World! [4]"' Publishing message to topic 'test/topic': Hello World! [5] Received message from topic 'test/topic': b'"Hello World! [5]"' Publishing message to topic 'test/topic': Hello World! [6] Received message from topic 'test/topic': b'"Hello World! [6]"' Publishing message to topic 'test/topic': Hello World! [7] Received message from topic 'test/topic': b'"Hello World! [7]"' Publishing message to topic 'test/topic': Hello World! [8] Received message from topic 'test/topic': b'"Hello World! [8]"' Publishing message to topic 'test/topic': Hello World! [9] Received message from topic 'test/topic': b'"Hello World! [9]"' Publishing message to topic 'test/topic': Hello World! [10] Received message from topic 'test/topic': b'"Hello World! [10]"' 10 message(s) received. Disconnecting... Disconnected!

若在執行範例應用程式時發生問題,請檢閱 疑難排解範例應用程式的問題

您也可以將 --verbosity Debug 參數新增至命令列,以便範例應用程式顯示有關其正在做什麼的詳細訊息。該資訊可能會協助您更正問題。

檢視來自範例應用程式的訊息 AWS IoT 主控台

您可以查看示例應用程序的消息,當它們通過消息代理程序時,通過使用中的MQTT測試客戶端 AWS IoT 控制台

檢視範例應用程式所發佈的MQTT訊息
  1. 檢閱 檢視MQTT訊息 AWS IoT MQTT用戶端。這可協助您學習如何使用中的MQTT測試用戶端 AWS IoT 控制台以查看MQTT消息通過消息代理程序時的消息。

  2. 在中開啟MQTT測試用戶端 AWS IoT 控制台

  3. Subscribe to a topic (訂閱主題) 中,訂閱主題 test/topic

  4. 在命令行窗口中,再次運行示例應用程序,然後在MQTT客戶端中查看消息 AWS IoT 控制台

    Linux/macOS
    cd ~/aws-iot-device-sdk-python-v2/samples python3 pubsub.py --topic test/topic --ca_file ~/certs/Amazon-root-CA-1.pem --cert ~/certs/device.pem.crt --key ~/certs/private.pem.key --endpoint your-iot-endpoint
    Windows
    cd %USERPROFILE%\aws-iot-device-sdk-python-v2\samples python3 pubsub.py --topic test/topic --ca_file %USERPROFILE%\certs\Amazon-root-CA-1.pem --cert %USERPROFILE%\certs\device.pem.crt --key %USERPROFILE%\certs\private.pem.key --endpoint your-iot-endpoint

有關更多信息MQTT以及如何 AWS IoT Core 支援通訊協定,請參閱MQTT

在 Python 中執行共享訂閱範例

AWS IoT Core 支援 MQTT 3 和 MQTT 5 的共用訂閱。共享訂閱允許多個用戶端共享一個主題的訂閱,而且只有一個用戶端會使用隨機分佈接收發佈至該主題的訊息。若要使用共享訂閱,用戶端會訂閱共享訂閱的主題篩選條件$share/{ShareName}/{TopicFilter}

設定政策並執行共享訂閱範例
  1. 若要執行共用訂閱範例,您必須依照 MQTT5 個共用訂閱中的說明來設定物件的原則。

  2. 若要執行共享訂閱,請執行下列命令。

    Linux/macOS
    在 Linux/macOS 上執行範本指令碼
    1. 在指令行視窗中,導覽至使用這些指令SDK建立的~/aws-iot-device-sdk-python-v2/samples目錄。

      cd ~/aws-iot-device-sdk-python-v2/samples
    2. 在命令行窗口中,替換 your-iot-endpoint 如所示並運行此命令。

      python3 mqtt5_shared_subscription.py --endpoint your-iot-endpoint --ca_file ~/certs/Amazon-root-CA-1.pem --cert ~/certs/device.pem.crt --key ~/certs/private.pem.key --group_identifier consumer
    Windows
    在 Windows PC 上執行範例應用程式
    1. 在命令列視窗中,導覽至SDK建立的%USERPROFILE%\aws-iot-device-sdk-python-v2\samples目錄,然後使用這些指令安裝範例應用程式。

      cd %USERPROFILE%\aws-iot-device-sdk-python-v2\samples
    2. 在命令行窗口中,替換 your-iot-endpoint 如所示並運行此命令。

      python3 mqtt5_shared_subscription.py --endpoint your-iot-endpoint --ca_file %USERPROFILE%\certs\Amazon-root-CA-1.pem --cert %USERPROFILE%\certs\device.pem.crt --key %USERPROFILE%\certs\private.pem.key --group_identifier consumer
    注意

    執行範例 (例如 --group_identifier consumer) 時,您可以根據需求選擇性地指定群組識別碼。如果您沒有指定一個,則 python-sample 是預設群組識別碼。

  3. 此命令列的輸出如下所示:

    Publisher]: Lifecycle Connection Success [Publisher]: Connected Subscriber One]: Lifecycle Connection Success [Subscriber One]: Connected Subscriber Two]: Lifecycle Connection Success [Subscriber Two]: Connected [Subscriber One]: Subscribed to topic 'test/topic' in shared subscription group 'consumer'. [Subscriber One]: Full subscribed topic is: '$share/consumer/test/topic' with SubAck code: [<SubackReasonCode.GRANTED_QOS_1: 1>] [Subscriber Two]: Subscribed to topic 'test/topic' in shared subscription group 'consumer'. [Subscriber Two]: Full subscribed topic is: '$share/consumer/test/topic' with SubAck code: [<SubackReasonCode.GRANTED_QOS_1: 1>] [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber Two] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [1]"' [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber One] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [2]"' [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber Two] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [3]"' [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber One] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [4]"' [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber Two] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [5]"' [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber One] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [6]"' [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber Two] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [7]"' [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber One] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [8]"' [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber Two] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [9]"' [Publisher]: Sent publish and got PubAck code: <PubackReasonCode.SUCCESS: 0> [Subscriber One] Received a publish Publish received message on topic: test/topic Message: b'"Hello World! [10]"' [Subscriber One]: Unsubscribed to topic 'test/topic' in shared subscription group 'consumer'. [Subscriber One]: Full unsubscribed topic is: '$share/consumer/test/topic' with UnsubAck code: [<UnsubackReasonCode.SUCCESS: 0>] [Subscriber Two]: Unsubscribed to topic 'test/topic' in shared subscription group 'consumer'. [Subscriber Two]: Full unsubscribed topic is: '$share/consumer/test/topic' with UnsubAck code [<UnsubackReasonCode.SUCCESS: 0>] Publisher]: Lifecycle Disconnected [Publisher]: Lifecycle Stopped [Publisher]: Fully stopped Subscriber One]: Lifecycle Disconnected [Subscriber One]: Lifecycle Stopped [Subscriber One]: Fully stopped Subscriber Two]: Lifecycle Disconnected [Subscriber Two]: Lifecycle Stopped [Subscriber Two]: Fully stopped Complete!
  4. 在中開啟MQTT測試用戶端 AWS IoT 控制台。在訂閱主題中,訂閱共享訂閱的主題,例如:$share/consumer/test/topic。執行範例 (例如 --group_identifier consumer) 時,您可以根據需求指定群組識別碼。如果您未指定群組識別碼,則預設值為 python-sample。如需詳細資訊,請參閱 MQTT5 個共用訂閱 Python 範例共用訂閱 AWS IoT Core 開發人員指南

    在命令行窗口中,再次運行示例應用程序,並在MQTT測試客戶端中查看消息的分佈情況 AWS IoT 控制台和命令行。

    共用訂閱輸出頁面。