我从 Raspberry Pi 运行一个简单的发布消息。我的设备似乎已连接到 AWS IoT,但未发布消息且未调用on_connect
和on_publish
方法。
任何人都可以帮助找出原因吗?
#!/usr/bin/python2.7
import paho.mqtt.client as mqtt
import ssl
import json,time
def on_connect(client, userdata, flags, rc):
print ("Subscriber Connection status code: "+str(rc))
def on_publish(client, userdata, mid):
print(client, userdata, mid)
#Connect to AWS IoT
print("starting aws client")
awsclient = mqtt.Client(client_id="raspberry",protocol=mqtt.MQTTv311)
awsclient.on_connect = on_connect
awsclient.on_publish = on_publish
awsclient.tls_set("./root-CA.pem",certfile="./575c6bc5b3- certificate.pem.crt",keyfile="./575c6bc5b3- private.pem.key",tls_version=ssl.PROTOCOL_SSLv23,ciphers=None)
awsclient.tls_insecure_set(True)
awsclient.connect("a1e2evxrc4wz76.iot.us-east-1.amazonaws.com", 8883, 60)
awsclient.loop_start()
rc=0
while rc == 0:
data={}
data['temp']='64'
data['humid']='65'
print(data)
payload = json.dumps(data)
print("Payload: " + payload)
awsclient.publish("Rasp/data", payload, qos=1)
time.sleep(10)
print('rc: ' +str(rc))`