简体   繁体   English

带有 paho-mqtt 的 AWS IoT 和 Raspberry Pi 无法连接

[英]AWS IoT and Raspberry Pi with paho-mqtt don't connect

I have installed the last version of raspbian on my raspberry pi, and I have opened an account AWS IoT on Amazon, then in the IoT web interface I have created a thing, with "RaspberryPi_2" name and create certificate and connect the certificate to the thing, I have followed this guide:我已经在我的树莓派上安装了最新版本的 raspbian,我在亚马逊上开设了一个 AWS IoT 账户,然后在 IoT 网络界面中我创建了一个东西,名称为“RaspberryPi_2”并创建证书并将证书连接到事情,我遵循了本指南:

http://blog.getflint.io/blog/get-started-with-aws-iot-and-raspberry-pi http://blog.getflint.io/blog/get-started-with-aws-iot-and-raspberry-pi

I have then created the script in the guide, to connect and subscribed the raspberry pi, this is my code:然后我在指南中创建了脚本,以连接和订阅树莓派,这是我的代码:

#!/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()

but when I do this command:但是当我执行此命令时:

python3 mqtt_test.py

or this command:或此命令:

python mqtt_test.py

and press enter, and the cursor flash and doesn't print anything and stay stuck there, someone can help me?然后按回车,光标闪烁,不打印任何东西并停留在那里,有人可以帮助我吗?

I haven't also understand if the client-id name should be the same of the things name, and the meaning of the subscribe path, for example in a tutorial I have found this:我也不明白 client-id 名称是否应该与事物名称和订阅路径的含义相同,例如在我发现的教程中:

mqttc.publish("temperature", tempreading, qos=1)

why there isn't the complete path?为什么没有完整的路径?

or this:或这个:

$aws/things/RaspberryPi_2/shadow/update/delta

so I can put everything I want in the path?所以我可以把我想要的一切都放在路径上?

thanks谢谢

The code is subscribing to a topic but there is no one publishing to it.该代码正在订阅一个主题,但没有人发布该主题。 So, the code also has a on_connect function that would be triggered after a success connection.因此,该代码还有一个on_connect函数,该函数将在连接成功后触发。 Is the message "Subscriber Connection status code: ..." being printed?是否正在打印消息"Subscriber Connection status code: ..." If it is, the message from on_subscribe should also appear.如果是,来自on_subscribe的消息也应该出现。 Is it?是吗?

If it is not you have a problem before connect to the AWS server.如果不是,则您在连接到 AWS 服务器之前遇到了问题。 Use netstat command to see where your Raspberry Pi is connected or not and post more debug information in this case.在这种情况下,使用netstat命令查看您的 Raspberry Pi 连接与否,并发布更多调试信息。

If the connect and subscribe messages are shown and nothing happens after it, this is normal because you are only subscribing to a topic but not publishing anything.如果显示了连接和订阅消息并且之后没有任何反应,这是正常的,因为您只订阅了一个主题但没有发布任何内容。

Regarding topics, think of them as a directory structure.关于主题,将它们视为目录结构。 There is no strict rule for topic hierarchy.主题层次结构没有严格的规则。 A "temperature" topic would be temperature topic on the server and a "temperature/livingroom" would be temperature/livingroom , you can subscribe to one, another or both on the same server. “温度”主题将是服务器上的temperature主题,“温度/客厅”将是temperature/livingroom ,您可以在同一服务器上订阅一个、另一个或两者。 The path you choose for your things shall be meaningful to your application.你为你的东西选择的路径对你的应用程序是有意义的。 A house, for instance, might be represented as:例如,一所房子可以表示为:

house/kitchen/env/temperature house/kitchen/env/humidity house/kitchen/lamp/sinklamp house/kitchen/lamp/mainlap house/masterbed/env/temperature house/masterbed/env/humidity house/masterbed/lamp/readinglampleft house/masterbed/lamp/readinglampright house/masterbed/lamp/mainlamp house/masterbed/lamp/mirrorlamp房子/厨房/环境/温度屋/厨房/环境/湿度屋/厨房/灯/水槽灯屋/厨房/灯/主屋/主床/环境/温度屋/主床/环境/湿度屋/主床/灯/阅读灯左屋/masterbed/lamp/readinglampright house/masterbed/lamp/mainlamp house/masterbed/lamp/mirrorlamp

And so on.等等。

Let´s say you have a thermostat at master bedroom.假设您在主卧室有一个恒温器。 It is interested only in temperature but not humidity .它只对temperature感兴趣,对humidity不感兴趣。 It is also interested only in master bedroom temperature.它也只对主卧室温度感兴趣。 This thermostat shall subscribe to house/masterbed/env/temperature .该恒温器应订阅house/masterbed/env/temperature Opposite to this, a room wide panel that shows state of every thing in the room, would subscribe to house/masterbed/# , meaning "everything after house/masterbed".与此相反,一个显示房间内所有事物状态的房间宽面板将订阅house/masterbed/# ,意思是“house/masterbed 之后的一切”。 Read more on wildcards here此处阅读有关通配符的更多信息

The topic you subscribed for: $aws/things/RaspberryPi_2/shadow/update/# means, "every thing after $aws/things/RaspberryPi_2/shadow/update/".您订阅的主题: $aws/things/RaspberryPi_2/shadow/update/#表示“$aws/things/RaspberryPi_2/shadow/update/ 之后的所有内容”。 Notice that his is a special topic, it starts with $aws , specially, it starts with $ character.请注意,他是一个特殊的主题,它以$aws开头,特别是它以$字符开头。 In the AWS context this means:AWS 上下文中,这意味着:

Any topics beginning with $ are considered reserved and are not supported for publishing and subscribing except when working with the Thing Shadows service.任何以 $ 开头的主题都被视为保留主题,不支持发布和订阅,除非使用 Thing Shadows 服务。 For more information, see Thing Shadows.有关更多信息,请参阅事物阴影。

So you need to understand what thing shadow is.所以你需要了解thing shadow是什么thing shadow This is a AWS specific (and very util) concept.这是一个 AWS 特定(并且非常实用)的概念。 Please read docs on this subject.请阅读有关此主题的文档

Finally, I would you suggest you install a local broker (mosquitto is available on respbian) and try with it before got to AWS.最后,我建议您安装一个本地代理(mosquitto 在 respbian 上可用)并在到达 AWS 之前尝试使用它。 This way you can master mqtt concept without connectivity issues.通过这种方式,您可以在没有连接问题的情况下掌握 mqtt 概念。 Later you put AWS to the mix.稍后您将 AWS 加入其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM