[英]AWS IoT and Raspberry Pi with paho-mqtt don't connect
我已經在我的樹莓派上安裝了最新版本的 raspbian,我在亞馬遜上開設了一個 AWS IoT 賬戶,然后在 IoT 網絡界面中我創建了一個東西,名稱為“RaspberryPi_2”並創建證書並將證書連接到事情,我遵循了本指南:
http://blog.getflint.io/blog/get-started-with-aws-iot-and-raspberry-pi
然后我在指南中創建了腳本,以連接和訂閱樹莓派,這是我的代碼:
#!/usr/bin/python3
#required libraries
import sys
import ssl
import paho.mqtt.client as mqtt
#called while client tries to establish connection with the server
def on_connect(mqttc, obj, flags, rc):
if rc==0:
print ("Subscriber Connection status code: "+str(rc)+" | Connection status: successful")
elif rc==1:
print ("Subscriber Connection status code: "+str(rc)+" | Connection status: Connection refused")
#called when a topic is successfully subscribed to
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos)+"data"+str(obj))
#called when a message is received by a topic
def on_message(mqttc, obj, msg):
print("Received message from topic: "+msg.topic+" | QoS: "+str(msg.qos)+" | Data Received: "+str(msg.payload))
#creating a client with client-id=mqtt-test
mqttc = mqtt.Client(client_id="mqtt-test")
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message
#Configure network encryption and authentication options. Enables SSL/TLS support.
#adding client-side certificates and enabling tlsv1.2 support as required by aws-iot service
mqttc.tls_set("/home/pi/aws_iot/things/raspberryPi_2/certs/aws-iot-rootCA.crt",
certfile="/home/pi/aws_iot/things/raspberryPi_2/certs/0ea2cd7eb6-certificate.pem.crt",
keyfile="/home/pi/aws_iot/things/raspberryPi_2/certs/0ea2cd7eb6-private.pem.key",
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None)
#connecting to aws-account-specific-iot-endpoint
mqttc.connect("A2GF7W5U5A46J1.iot.us-west-2.amazonaws.com", port=8883) #AWS IoT service hostname and portno
#the topic to publish to
mqttc.subscribe("$aws/things/RaspberryPi_2/shadow/update/#", qos=1) #The names of these topics start with $aws/things/thingName/shadow."
#automatically handles reconnecting
mqttc.loop_forever()
但是當我執行此命令時:
python3 mqtt_test.py
或此命令:
python mqtt_test.py
然后按回車,光標閃爍,不打印任何東西並停留在那里,有人可以幫助我嗎?
我也不明白 client-id 名稱是否應該與事物名稱和訂閱路徑的含義相同,例如在我發現的教程中:
mqttc.publish("temperature", tempreading, qos=1)
為什么沒有完整的路徑?
或這個:
$aws/things/RaspberryPi_2/shadow/update/delta
所以我可以把我想要的一切都放在路徑上?
謝謝
該代碼正在訂閱一個主題,但沒有人發布該主題。 因此,該代碼還有一個on_connect
函數,該函數將在連接成功后觸發。 是否正在打印消息"Subscriber Connection status code: ..."
? 如果是,來自on_subscribe
的消息也應該出現。 是嗎?
如果不是,則您在連接到 AWS 服務器之前遇到了問題。 在這種情況下,使用netstat
命令查看您的 Raspberry Pi 連接與否,並發布更多調試信息。
如果顯示了連接和訂閱消息並且之后沒有任何反應,這是正常的,因為您只訂閱了一個主題但沒有發布任何內容。
關於主題,將它們視為目錄結構。 主題層次結構沒有嚴格的規則。 “溫度”主題將是服務器上的temperature
主題,“溫度/客廳”將是temperature/livingroom
,您可以在同一服務器上訂閱一個、另一個或兩者。 你為你的東西選擇的路徑對你的應用程序是有意義的。 例如,一所房子可以表示為:
房子/廚房/環境/溫度屋/廚房/環境/濕度屋/廚房/燈/水槽燈屋/廚房/燈/主屋/主床/環境/溫度屋/主床/環境/濕度屋/主床/燈/閱讀燈左屋/masterbed/lamp/readinglampright house/masterbed/lamp/mainlamp house/masterbed/lamp/mirrorlamp
等等。
假設您在主卧室有一個恆溫器。 它只對temperature
感興趣,對humidity
不感興趣。 它也只對主卧室溫度感興趣。 該恆溫器應訂閱house/masterbed/env/temperature
。 與此相反,一個顯示房間內所有事物狀態的房間寬面板將訂閱house/masterbed/#
,意思是“house/masterbed 之后的一切”。 在此處閱讀有關通配符的更多信息
您訂閱的主題: $aws/things/RaspberryPi_2/shadow/update/#
表示“$aws/things/RaspberryPi_2/shadow/update/ 之后的所有內容”。 請注意,他是一個特殊的主題,它以$aws
開頭,特別是它以$
字符開頭。 在AWS 上下文中,這意味着:
任何以 $ 開頭的主題都被視為保留主題,不支持發布和訂閱,除非使用 Thing Shadows 服務。 有關更多信息,請參閱事物陰影。
所以你需要了解thing shadow
是什么thing shadow
。 這是一個 AWS 特定(並且非常實用)的概念。 請閱讀有關此主題的文檔。
最后,我建議您安裝一個本地代理(mosquitto 在 respbian 上可用)並在到達 AWS 之前嘗試使用它。 通過這種方式,您可以在沒有連接問題的情況下掌握 mqtt 概念。 稍后您將 AWS 加入其中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.