本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用您的視窗或 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
-
查看您的電腦上是否已安裝 Git。在命令列中輸入此命令。
git --version
如果命令顯示 Git 版本,表示已安裝 Git,而且您可以繼續進行下一個步驟。
如果命令顯示錯誤,請開啟
https://git-scm.com/download
並為您的電腦安裝 Git。 -
查看您是否已安裝 Python。在命令列中輸入此命令。
python -V
注意
如果此命令提供錯誤:
Python was not found
,原因可能是您的作業系統呼叫 Python v3.x 可執行檔作為Python3
。在此情況下,將python
的所有執行個體取代為python3
,並繼續進行本教學課程的其餘部分。如果命令顯示 Python 版本,則表示已安裝 Python。本教學課程需要 Python v3.7 或更新版本。
-
如果已安裝 Python,則可以跳過本節的其餘步驟。如果未安裝,請繼續。
-
打開 https://www.py.org/下載/
並下載計算機的安裝程序。 -
如果下載未自動開始安裝,請執行下載的程式來安裝 Python。
-
驗證 Python 的安裝。
python -V
確認命令顯示 Python 版本。如果未顯示 Python 版本,請嘗試再次下載並安裝 Python。
安裝 AWS IoT Python SDK 的設備
若要安裝 AWS IoT 在您的計算機上SDK使用 Python 的設備
-
安裝的 V2 AWS IoT Python SDK 的設備。
python3 -m pip install awsiotsdk
-
克隆 AWS IoT Python 存儲庫SDK的設備放入到您的主目錄的 aws-iot-device-sdk-python-v2 目錄中。此程序指的是您要安裝的檔案的基本目錄
home
.的實際位置
home
目錄取決於您的操作系統。注意
如果您使用的是 Windows PowerShell 而不是cmd.exe,請使用以下命令。
echo $home
如需詳細資訊,請參閱 AWS IoT
用SDK於 Python GitHub 存儲庫的設備
準備執行範例應用程式
準備您的系統以執行範例應用程式
-
建立
certs
目錄。將下列項目複製至certs
目錄:私有金鑰、裝置憑證,以及您在 建立 AWS IoT resources 建立和註冊物件時所儲存的根憑證授權機構憑證檔案。目的地目錄中每個檔案的檔案名稱應與表格中的檔案名稱相符。下節命令假設您的金鑰和憑證檔案儲存在您的裝置上,如下表所示。
設定政策和執行範例應用程式
在本節中,您將設定原則並執行在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 |
|
所以此 your-iot-endpoint
值的格式為:
,例如endpoint_id
-ats.iot.region
.amazonaws.com.rproxy.goskope.coma3qj468EXAMPLE-ats.iot.us-west-2.amazonaws.com
。
執行指令碼之前,請確定您物件的政策許可範例指令碼連接、訂閱、發佈和接收。
尋找並檢閱物件資源的政策文件
-
在 中AWS IoT 主控
台的「物件」清單中,尋找代表您裝置的物件資源。 -
選擇代表您裝置物件資源的 Name (名稱) 連結,以開啟 Thing details (物件詳細資訊) 頁面。
-
在 Thing details (物件詳細資訊) 頁面中的 Certificates (憑證) 索引標籤上,選擇與物件資源相連的憑證。清單中應該只有一個憑證。如果有多個憑證,請選擇其檔案已安裝在您裝置上且將用於連線的憑證 AWS IoT Core.
於 Certificate (憑證) 詳細資訊頁面的 Policies (政策) 索引標籤中,選擇與憑證相連的政策。其中應只有一個政策。如果有多個政策,請針對每個政策重複下一個步驟,以確保至少有一個政策授予必要的存取權。
-
在 [原則概觀] 頁面中,尋找編輯JSON器,然後選擇 [編輯原則文件],視需要檢閱和編輯政策文件。
-
策略顯JSON示在下列範例中。在元
"Resource"
素中,
替換為 AWS 區域 以及 AWS 帳戶 在每個region:account
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-*" ] } ] }
範例指令碼:
-
連接至 AWS IoT Core 為您的帳戶。
-
訂閱訊息主題 test/topic,並顯示其收到關於該主題的訊息。
-
將 10 則訊息發佈至主題 test/topic。
-
顯示類似下列內容的輸出:
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訊息
-
檢閱 檢視MQTT訊息 AWS IoT MQTT用戶端。這可協助您學習如何使用中的MQTT測試用戶端 AWS IoT 控制台以查看MQTT消息通過消息代理程序時的消息。
-
在中開啟MQTT測試用戶端 AWS IoT 控制台。
-
在 Subscribe to a topic (訂閱主題) 中,訂閱主題 test/topic。
-
在命令行窗口中,再次運行示例應用程序,然後在MQTT客戶端中查看消息 AWS IoT 控制台。
有關更多信息MQTT以及如何 AWS IoT Core 支援通訊協定,請參閱MQTT。
在 Python 中執行共享訂閱範例
AWS IoT Core 支援 MQTT 3 和 MQTT 5 的共用訂閱。共享訂閱允許多個用戶端共享一個主題的訂閱,而且只有一個用戶端會使用隨機分佈接收發佈至該主題的訊息。若要使用共享訂閱,用戶端會訂閱共享訂閱的主題篩選條件:$share/{ShareName}/{TopicFilter}
。
設定政策並執行共享訂閱範例
-
若要執行共用訂閱範例,您必須依照 MQTT5 個共用訂閱
中的說明來設定物件的原則。 -
若要執行共享訂閱,請執行下列命令。
注意
執行範例 (例如
--group_identifier consumer
) 時,您可以根據需求選擇性地指定群組識別碼。如果您沒有指定一個,則python-sample
是預設群組識別碼。 -
此命令列的輸出如下所示:
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!
-
在中開啟MQTT測試用戶端 AWS IoT 控制台。在訂閱主題中,訂閱共享訂閱的主題,例如:
$share/consumer/test/topic
。執行範例 (例如--group_identifier consumer
) 時,您可以根據需求指定群組識別碼。如果您未指定群組識別碼,則預設值為python-sample
。如需詳細資訊,請參閱 MQTT5 個共用訂閱 Python 範例和共用訂閱 AWS IoT Core 開發人員指南。 在命令行窗口中,再次運行示例應用程序,並在MQTT測試客戶端中查看消息的分佈情況 AWS IoT 控制台和命令行。