使用AWS作为MQTT B将Raspberry Pi#1链接到Rapsberry Pi#2

2024-05-23 17:42:44 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个Raspberry Pi#1,它将通过一个主题sensors/Button向AWS发布MQTT消息。按下按钮时会触发,如下所示。在

# Import SDK packages
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
from time import sleep
from gpiozero import Button
from signal import pause

button = Button(13, pull_up=False)

def callMQTT():
    print("button is pressed.Sending to MQTT")
    mqtt_message = "{\"message\":\"button_pressed\"}"
    print(mqtt_message)
    my_rpi.publish("sensors/Button", mqtt_message, 1)
    print("Message Published!")
    sleep(5)


host="host.amazonaws.com"
rootCAPath = "rootca.pem"
certificatePath = "certificate.pem.crt"
privateKeyPath = "private.pem.key"

try:
    my_rpi = AWSIoTMQTTClient("basicPubSub")
    my_rpi.configureEndpoint(host,8883)
    my_rpi.configureCredentials(rootCAPath, privateKeyPath, certificatePath)

    my_rpi.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
    my_rpi.configureDrainingFrequency(2)  # Draining: 2 Hz

    # Connect and subscribe to AWS IoT
    my_rpi.connect()
    print("Connection Succesful")
except:
    print("Unexpected error:", sys.exc_info()[0])

button.when_pressed = callMQTT
pause()

在Raspberry Pi#2上,它将尝试使用与Raspbery Pi#1相同的主机、相同的东西、相同的密钥和相同的证书从AWS订阅MQTT。如果收到信息,它将鸣响蜂鸣器并点亮LED,如下所示。在

^{pr2}$

然而,这是不可能的。当两个程序同时运行时,Rasberry Pi#2总是超时。由于某些原因,它一次只允许一个连接。 当我运行Raspberry Pi#1代码时,我尝试直接通过AWS订阅主题。它在AWS上显示消息。另外,如果我尝试直接在AWS上发布消息并只运行Raspberry Pi#2代码,那么它也可以工作,但当两者都运行代码时就不行了。我在Raspberry Pi#2上得到的错误是:

No handlers could be found for logger "AWSIoTPythonSDK.core.protocol.mqttCore" Traceback (most recent call last): File "Doorbell_Indoor.py", line 72, in my_rpi.subscribe("sensors/Button", 1, customCallback) File "/usr/local/lib/python2.7/dist-packages/AWSIoTPythonSDK/MQTTLib.py", line 491, in subscribe return self._mqttCore.subscribe(topic, QoS, callback) File "/usr/local/lib/python2.7/dist-packages/AWSIoTPythonSDK/core/protocol/mqttCore.py", line 416, in subscribe raise subscribeTimeoutException() AWSIoTPythonSDK.exception.AWSIoTExceptions.subscribeTimeoutException

有人知道怎么解决这个问题吗?提前谢谢!在


Tags: fromimportawsmessagemypibuttonsubscribe
2条回答

我在Python-SDK文档中没有看到它的文档,但是在Java-SDK文档中它说:

clientId - the client ID uniquely identify a MQTT connection. Two clients with the same client ID are not allowed to be connected concurrently to a same endpoint.

尝试对每个连接使用唯一的clientId值,而不是"basicPubSub"。在

来自http://docs.aws.amazon.com/iot/latest/developerguide/protocols.html

The message broker uses the client ID to identify each client. The client ID is passed in from the client to the message broker as part of the MQTT payload. Two clients with the same client ID are not allowed to be connected concurrently to the message broker. When a client connects to the message broker using a client ID that another client is using, a CONNACK message will be sent to both clients >and the currently connected client will be disconnected.

必须为每个客户端传入一个唯一的字符串到AWSIoTMQTTClient()或一个空字符串。如果您为客户端id传递一个空字符串,AWS IoT网关将在连接时为您分配一个随机的客户端id。在

将AWSIoTMQTTClient调用从

my_rpi = AWSIoTMQTTClient("basicPubSub")

^{pr2}$

在连接时,每个rpi都将被分配一个唯一的随机客户机id。AWS不建议在规模上这样做,但对于测试来说这是可以的。您还将失去在策略中使用clientid的能力。在

相关问题 更多 >