我正在使用AWSIoTPythonSDK paho-mqtt库并且在连接AWS时遇到问题。请帮忙。谢谢!
#coding: utf-8
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
from datetime import datetime
from time import sleep
from gpiozero import Buzzer, LED
bz = Buzzer(22)
led = LED(18)
# Custom MQTT message callback
def custom_callback (client,userdata,message):
print("Received a new message: ")
print(message.payload)
if message.payload == 'button_pressed':
print("Doorbell pressed")
bz.on()
led.blink()
sleep(1)
bz.off()
led.off()
print("Done")
host="xxxxxxx.iot.ap-southeast-1.amazonaws.com"
rootCAPath="rootca.pem"
certificatePath="certificate.pem.crt"
privateKeyPath="private.pem.key"
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
my_rpi.configureConnectDisconnectTimeout(10)# 10 sec
my_rpi.configureMQTTOperationTimeout(5)# 5 sec
# Connect and subscribe to AWS IoT
my_rpi.connect()
my_rpi.subscribe("sensors/button",1,custom_callback)
sleep(2)
这是我运行上述代码时收到的错误。我已经检查过我已经安装了必要的库。所有AWS证书和密钥都存储在与上述python代码相同的文件夹中。
Traceback (most recent call last):
File "Doorbell_Indoor.py", line 38, in <module>
my_rpi.connect()
File "/usr/local/lib/python2.7/dist-packages/AWSIoTPythonSDK/MQTTLib.py", line 403, in connect
return self._mqttCore.connect(keepAliveIntervalSecond)
File "/usr/local/lib/python2.7/dist-packages/AWSIoTPythonSDK/core/protocol/mqttCore.py", line 290, in connect
self._pahoClient.connect(self._host, self._port, keepAliveInterval) # Throw exception...
File "/usr/local/lib/python2.7/dist-packages/AWSIoTPythonSDK/core/protocol/paho/client.py", line 655, in connect
return self.reconnect()
File "/usr/local/lib/python2.7/dist-packages/AWSIoTPythonSDK/core/protocol/paho/client.py", line 777, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known