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

标签 python amazon-web-services raspberry-pi iot paho

我已经在我的树莓派上安装了最新版本的raspbian,并且我在亚马逊上开设了一个AWS IoT帐户,然后在IoT Web界面中我创建了一个东西,名称为“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 函数,该函数将在连接成功后触发。是否正在打印消息“订阅者连接状态代码:...”?如果是,来自 on_subscribe 的消息也应该出现。是吗?

如果不是,您在连接到 AWS 服务器之前就会遇到问题。使用 netstat 命令查看 Raspberry Pi 的连接位置,并在这种情况下发布更多调试信息。

如果显示连接和订阅消息,但之后没有任何反应,这是正常的,因为您只是订阅主题,但没有发布任何内容。

关于主题,将它们视为目录结构。主题层次没有严格的规则。 “温度”主题将是服务器上的温度主题,“温度/客厅”将是温度/客厅,您可以在服务器上订阅一个、另一个或两者。同一个服务器。您为事物选择的路径应对您的应用有意义。例如,一栋房子可能表示为:

房子/厨房/环境/温度 房子/厨房/环境/湿度 房子/厨房/灯/水槽灯 房子/厨房/灯/mainlap 房子/主床/环境/温度 房子/主床/环境/湿度 房子/主床/灯/阅读灯左 房子/主床/灯/阅读灯right 房子/主床/灯/主灯 房子/主床/灯/镜灯

等等。

假设您的主卧室有一个恒温器。它只对温度感兴趣,而不对湿度感兴趣。它也只对主卧室的温度感兴趣。该恒温器应订阅house/masterbed/env/Temperature。与此相反,显示房间中每件东西状态的房间宽面板将订阅house/masterbed/#,意思是“house/masterbed之后的所有东西”。了解有关通配符的更多信息 here

您订阅的主题:$aws/things/RaspberryPi_2/shadow/update/# 表示“$aws/things/RaspberryPi_2/shadow/update/之后的所有内容”。请注意,他是一个特殊主题,它以 $aws 开头,特别是,它以 $ 字符开头。在 AWS context这意味着:

Any topics beginning with $ are considered reserved and are not supported for publishing and subscribing except when working with the Thing Shadows service. For more information, see Thing Shadows.

所以你需要了解事物影子是什么。这是 AWS 特定的(并且非常实用)的概念。请阅读docs关于这个主题。

最后,我建议您安装一个本地代理(mosquitto 在 respbian 上可用)并在到达 AWS 之前尝试使用它。这样您就可以掌握 mqtt 概念,而不会出现连接问题。后来您将 AWS 加入其中。

关于python - AWS IoT 和带有 paho-mqtt 的 Raspberry Pi 无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37168822/

相关文章:

object - 如何判断对象是否是 AWS S3 上的文件夹

python代码在特定时间控制GPIO

python - Pylons 响应未返回 Content-Length header

python - 操作系统错误 : Could not find a suitable TLS CA certificate bundle

python - flake8 和 "old style class declaration"

mysql - AWS RDS - 错误 : Access denied for user 'geornalAdmin' @'%' to database 'mysql' When trying to migrate using sequelize-cli

amazon-web-services - 有没有办法将 S3 存储桶中的目录克隆到同一存储桶中的另一个路径?

Python内部函数/装饰器: When should I use parentheses when returning an inner function?

python - 通过 zbar 和 Raspicam 模块扫描二维码

python smbus/i2c 频率